Skip to content

Commit

Permalink
fix(vue): do not throw when component type reference or floating rend…
Browse files Browse the repository at this point in the history
…ers nothing (#2697)
  • Loading branch information
lozinsky committed Jan 5, 2024
1 parent 45a3c09 commit 62a5242
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-trainers-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/vue": patch
---

fix: do not throw when component type reference or floating renders nothing
1 change: 1 addition & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
],
"dependencies": {
"@floating-ui/dom": "workspace:^",
"@floating-ui/utils": "workspace:^",
"vue-demi": ">=0.13.0"
},
"devDependencies": {
Expand Down
18 changes: 13 additions & 5 deletions packages/vue/src/utils/unwrapElement.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {getNodeName, isNode} from '@floating-ui/utils/dom';
import type {ComponentPublicInstance} from 'vue-demi';

import type {MaybeElement} from '../types';

export function unwrapElement<T>(element: MaybeElement<T>) {
return ((element as Exclude<MaybeElement<T>, T>)?.$el ?? element) as Exclude<
MaybeElement<T>,
ComponentPublicInstance
>;
function isComponentPublicInstance(target: unknown): target is ComponentPublicInstance {
return target != null && {}.hasOwnProperty.call(target, '$el');
}

export function unwrapElement<T>(target: MaybeElement<T>) {
if (isComponentPublicInstance(target)) {
const element = target.$el as Exclude<MaybeElement<T>, ComponentPublicInstance>;

return isNode(element) && getNodeName(element) === '#comment' ? null : element;
}

return target as Exclude<MaybeElement<T>, ComponentPublicInstance>;
}
94 changes: 94 additions & 0 deletions packages/vue/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,100 @@ describe('useFloating', () => {
expect(getByTestId('y').textContent).toBe('5');
});
});

test('does not throw when component type reference renders nothing', () => {
const FloatingReference = defineComponent({
name: 'FloatingReference',
render() {
return null;
},
});
const App = defineComponent({
name: 'App',
components: {FloatingReference},
setup() {
return setup({});
},
template: /* HTML */ `
<FloatingReference ref="reference" />
<div ref="floating" />
`,
});

render(App);
});

test('does not throw when component type floating renders nothing', () => {
const FloatingFloating = defineComponent({
name: 'FloatingFloating',
render() {
return null;
},
});
const App = defineComponent({
name: 'App',
components: {FloatingFloating},
setup() {
return setup({});
},
template: /* HTML */ `
<div ref="reference" />
<FloatingFloating ref="floating" />
`,
});

render(App);
});

test('does not throw when component type reference renders nothing and "$el" is null', () => {
const FloatingReference = defineComponent({
name: 'FloatingReference',
setup(props, {expose}) {
expose({$el: null});
},
render() {
return null;
},
});
const App = defineComponent({
name: 'App',
components: {FloatingReference},
setup() {
return setup({});
},
template: /* HTML */ `
<FloatingReference ref="reference" />
<div ref="floating" />
`,
});

render(App);
});

test('does not throw when component type floating renders nothing and "$el" is null', () => {
const FloatingFloating = defineComponent({
name: 'FloatingFloating',
setup(props, {expose}) {
expose({$el: null});
},
render() {
return null;
},
});
const App = defineComponent({
name: 'App',
components: {FloatingFloating},
setup() {
return setup({});
},
template: /* HTML */ `
<div ref="reference" />
<FloatingFloating ref="floating" />
`,
});

render(App);
});
});

describe('arrow', () => {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62a5242

Please sign in to comment.