From 0db35e48e226c6f254e9c39076484de03e8df415 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Mon, 30 Nov 2026 11:44:21 +0100 Subject: [PATCH 1/4] feat(components): add `unmountOnHide` prop to Popover, DropdownMenu and ContextMenu --- .../content/docs/2.components/context-menu.md | 37 +++++++ .../docs/2.components/dropdown-menu.md | 35 ++++++ docs/content/docs/2.components/popover.md | 31 ++++++ src/runtime/components/ContextMenu.vue | 7 +- src/runtime/components/DropdownMenu.vue | 7 +- src/runtime/components/Popover.vue | 7 +- test/components/ContextMenu.spec.ts | 26 ++++- test/components/DropdownMenu.spec.ts | 26 ++++- test/components/Popover.spec.ts | 25 +++++ .../DropdownMenu-vue.spec.ts.snap | 99 +++++++++++++++++ .../__snapshots__/DropdownMenu.spec.ts.snap | 101 ++++++++++++++++++ .../__snapshots__/Popover-vue.spec.ts.snap | 18 ++++ .../__snapshots__/Popover.spec.ts.snap | 18 ++++ 13 files changed, 426 insertions(+), 11 deletions(-) diff --git a/docs/content/docs/2.components/context-menu.md b/docs/content/docs/2.components/context-menu.md index 2183fe06ac..87f2ed1659 100644 --- a/docs/content/docs/2.components/context-menu.md +++ b/docs/content/docs/2.components/context-menu.md @@ -298,6 +298,43 @@ slots: :div{class="flex items-center justify-center rounded-md border border-dashed border-accented text-sm aspect-video w-72"}[Right click here] :: +### Unmount :badge{label="Soon" class="align-text-top"} + +Use the `unmount-on-hide` prop to prevent the ContextMenu's content and its sub menus from being unmounted when it is closed. Defaults to `true`. + +::component-code +--- +prettier: true +collapse: true +ignore: + - items + - ui.content +external: + - items +externalTypes: + - ContextMenuItem[] +props: + unmountOnHide: false + items: + - label: System + icon: i-lucide-monitor + - label: Light + icon: i-lucide-sun + - label: Dark + icon: i-lucide-moon + ui: + content: 'w-48' +slots: + default: | + +
+ Right click here +
+--- + +:div{class="flex items-center justify-center rounded-md border border-dashed border-accented text-sm aspect-video w-72"}[Right click here] +:: + ## Examples ### With checkbox items diff --git a/docs/content/docs/2.components/dropdown-menu.md b/docs/content/docs/2.components/dropdown-menu.md index c084bb37a0..60a5e2650f 100644 --- a/docs/content/docs/2.components/dropdown-menu.md +++ b/docs/content/docs/2.components/dropdown-menu.md @@ -501,6 +501,41 @@ slots: :u-button{label="Open" icon="i-lucide-menu" color="neutral" variant="outline"} :: +### Unmount :badge{label="Soon" class="align-text-top"} + +Use the `unmount-on-hide` prop to prevent the DropdownMenu's content and its sub menus from being unmounted when it is closed. Defaults to `true`. + +::component-code +--- +prettier: true +collapse: true +ignore: + - items + - ui.content +external: + - items +externalTypes: + - DropdownMenuItem[] +props: + unmountOnHide: false + items: + - label: Profile + icon: i-lucide-user + - label: Billing + icon: i-lucide-credit-card + - label: Settings + icon: i-lucide-cog + ui: + content: 'w-48' +slots: + default: | + + +--- + +:u-button{label="Open" icon="i-lucide-menu" color="neutral" variant="outline"} +:: + ## Examples ### With checkbox items diff --git a/docs/content/docs/2.components/popover.md b/docs/content/docs/2.components/popover.md index a31d326086..c2f17efcf3 100644 --- a/docs/content/docs/2.components/popover.md +++ b/docs/content/docs/2.components/popover.md @@ -214,6 +214,37 @@ name: 'popover-dismissible-example' --- :: +### Unmount :badge{label="Soon" class="align-text-top"} + +Use the `unmount-on-hide` prop to prevent the Popover's content from being unmounted when it is closed. Defaults to `true`. + +::component-code +--- +prettier: true +ignore: + - title +props: + unmountOnHide: false +slots: + default: | + + + + content: | + + +--- + +:u-button{label="Open" color="neutral" variant="subtle"} + +#content +:placeholder{class="size-48 m-4 inline-flex"} +:: + +::note +This prop only applies to the `click` mode. The `hover` mode uses the Reka UI `HoverCard` which has no such option. +:: + ## Examples ### Control open state diff --git a/src/runtime/components/ContextMenu.vue b/src/runtime/components/ContextMenu.vue index 6bc6d0dad5..7af985e179 100644 --- a/src/runtime/components/ContextMenu.vue +++ b/src/runtime/components/ContextMenu.vue @@ -43,7 +43,7 @@ export interface ContextMenuItem extends Omit = ArrayOrNested> extends Omit { +export interface ContextMenuProps = ArrayOrNested> extends Omit { /** * @defaultValue 'md' */ @@ -127,7 +127,8 @@ const _props = withDefaults(defineProps>(), { modal: true, externalIcon: true, labelKey: 'label', - descriptionKey: 'description' + descriptionKey: 'description', + unmountOnHide: true }) const emits = defineEmits() const slots = defineSlots>() @@ -136,7 +137,7 @@ const props = useComponentProps>('contextMenu', _props) const appConfig = useAppConfig() as ContextMenu['AppConfig'] -const rootProps = useForwardProps(props, ['open', 'pressOpenDelay', 'modal'], emits) +const rootProps = useForwardProps(props, ['open', 'pressOpenDelay', 'modal', 'unmountOnHide'], emits) const contentProps = toRef(() => props.content) const getProxySlots = () => omit(slots, ['default']) diff --git a/src/runtime/components/DropdownMenu.vue b/src/runtime/components/DropdownMenu.vue index a0ac499588..8a9eb0d636 100644 --- a/src/runtime/components/DropdownMenu.vue +++ b/src/runtime/components/DropdownMenu.vue @@ -47,7 +47,7 @@ export interface DropdownMenuItem extends Omit = ArrayOrNested> extends Omit { +export interface DropdownMenuProps = ArrayOrNested> extends Omit { /** * @defaultValue 'md' */ @@ -161,7 +161,8 @@ const _props = withDefaults(defineProps>(), { labelKey: 'label', descriptionKey: 'description', filter: false, - ignoreFilter: false + ignoreFilter: false, + unmountOnHide: true }) const emits = defineEmits() const slots = defineSlots>() @@ -172,7 +173,7 @@ const props = useComponentProps>('dropdownMenu', _props) const appConfig = useAppConfig() as DropdownMenu['AppConfig'] -const rootProps = useForwardProps(props, ['defaultOpen', 'open', 'modal'], emits) +const rootProps = useForwardProps(props, ['defaultOpen', 'open', 'modal', 'unmountOnHide'], emits) const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8 }) as DropdownMenuContentProps) const arrowProps = toRef(() => defu(props.arrow, { rounded: true }) as DropdownMenuArrowProps) const getProxySlots = () => omit(slots, ['default']) diff --git a/src/runtime/components/Popover.vue b/src/runtime/components/Popover.vue index 4519f84951..002a4fb2de 100644 --- a/src/runtime/components/Popover.vue +++ b/src/runtime/components/Popover.vue @@ -9,7 +9,7 @@ import type { ComponentConfig } from '../types/tv' type Popover = ComponentConfig type PopoverMode = 'click' | 'hover' -export interface PopoverProps extends Omit, Pick { +export interface PopoverProps extends PopoverRootProps, Pick { /** * The display mode of the popover. * @defaultValue 'click' @@ -81,7 +81,8 @@ const _props = withDefaults(defineProps>(), { mode: 'click' as never, openDelay: 0, closeDelay: 0, - dismissible: true + dismissible: true, + unmountOnHide: true }) const emits = defineEmits() const slots = defineSlots>() @@ -90,7 +91,7 @@ const props = useComponentProps>('popover', _props) const appConfig = useAppConfig() as Popover['AppConfig'] -const rootProps = useForwardProps(props, props.mode === 'hover' ? ['defaultOpen', 'open', 'openDelay', 'closeDelay', 'enableTouch'] : ['defaultOpen', 'open', 'modal'], emits) +const rootProps = useForwardProps(props, props.mode === 'hover' ? ['defaultOpen', 'open', 'openDelay', 'closeDelay', 'enableTouch'] : ['defaultOpen', 'open', 'modal', 'unmountOnHide'], emits) const portalProps = usePortal(toRef(() => props.portal)) const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8 }) as PopoverContentProps) const contentEvents = computed(() => { diff --git a/test/components/ContextMenu.spec.ts b/test/components/ContextMenu.spec.ts index e78ebac4f6..72dad39c89 100644 --- a/test/components/ContextMenu.spec.ts +++ b/test/components/ContextMenu.spec.ts @@ -1,5 +1,5 @@ import { h, defineComponent } from 'vue' -import { describe, it, expect, test, expectTypeOf } from 'vitest' +import { describe, it, expect, test, expectTypeOf, vi } from 'vitest' import { axe } from 'vitest-axe' import { mountSuspended } from '@nuxt/test-utils/runtime' import type { AppConfig } from '@nuxt/schema' @@ -157,6 +157,30 @@ describe('ContextMenu', () => { expect(wrapper.findComponent(ContextMenuRoot).props('pressOpenDelay')).toBe(100) }) + it.each([ + [true, false], + [false, true] + ])('with unmountOnHide %s keeps the content mounted after closing: %s', async (unmountOnHide, kept) => { + const wrapper = await mountSuspended(ContextMenu, { + props: { ...props, open: true, unmountOnHide }, + slots: { default: () => h('span', 'Right Click') } + }) + + expect(wrapper.find('[data-slot="content"]').attributes('data-state')).toBe('open') + + await wrapper.setProps({ open: false }) + + await vi.waitFor(() => { + const content = wrapper.find('[data-slot="content"]') + expect(content.exists()).toBe(kept) + if (kept) { + expect(content.attributes('data-state')).toBe('closed') + // `v-show` hides the popper wrapper around the content + expect((content.element.parentElement as HTMLElement).style.display).toBe('none') + } + }) + }) + it('passes accessibility tests', async () => { const wrapper = await mountSuspended(ContextMenuWrapper, { props diff --git a/test/components/DropdownMenu.spec.ts b/test/components/DropdownMenu.spec.ts index f358cd24b5..c0e4ed9134 100644 --- a/test/components/DropdownMenu.spec.ts +++ b/test/components/DropdownMenu.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, test } from 'vitest' +import { describe, it, expect, test, vi } from 'vitest' import { axe } from 'vitest-axe' import { mountSuspended } from '@nuxt/test-utils/runtime' import { renderEach } from '../component-render' @@ -126,6 +126,7 @@ describe('DropdownMenu', () => { ['with descriptionKey', { props: { ...props, descriptionKey: 'description' } }], ['with disabled', { props: { ...props, disabled: true } }], ['with arrow', { props: { ...props, arrow: true } }], + ['with unmountOnHide', { props: { ...props, open: false, unmountOnHide: false } }], ...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size } }]), ['with externalIcon', { props: { ...props, externalIcon: 'i-lucide-external-link' } }], ['without externalIcon', { props: { ...props, externalIcon: false } }], @@ -143,6 +144,29 @@ describe('DropdownMenu', () => { ['with custom slot', { props, slots: { custom: () => 'Custom slot' } }] ]) + it.each([ + [true, false], + [false, true] + ])('with unmountOnHide %s keeps the content mounted after closing: %s', async (unmountOnHide, kept) => { + const wrapper = await mountSuspended(DropdownMenu, { + props: { ...props, unmountOnHide } + }) + + expect(wrapper.find('[data-slot="content"]').attributes('data-state')).toBe('open') + + await wrapper.setProps({ open: false }) + + await vi.waitFor(() => { + const content = wrapper.find('[data-slot="content"]') + expect(content.exists()).toBe(kept) + if (kept) { + expect(content.attributes('data-state')).toBe('closed') + // `v-show` hides the popper wrapper around the content + expect((content.element.parentElement as HTMLElement).style.display).toBe('none') + } + }) + }) + it('passes accessibility tests', async () => { const wrapper = await mountSuspended(DropdownMenu, { props diff --git a/test/components/Popover.spec.ts b/test/components/Popover.spec.ts index 2c6c42fa87..a8f4763308 100644 --- a/test/components/Popover.spec.ts +++ b/test/components/Popover.spec.ts @@ -20,6 +20,7 @@ describe('Popover', () => { ['with class', { props: { ...props, class: 'shadow-xl' } }], ['with ui', { props: { ...props, ui: { content: 'shadow-xl' } } }], ['with reference', { props: { ...props, reference }, slots: { content: () => 'Content slot' } }], + ['with unmountOnHide', { props: { ...props, open: false, unmountOnHide: false }, slots: { content: () => 'Content slot' } }], // Slots ['with default slot', { props, slots: { default: () => 'Default slot' } }], ['with content slot', { props, slots: { content: () => 'Content slot' } }], @@ -52,6 +53,30 @@ describe('Popover', () => { expect(wrapper.text()).toContain('Anchored content') }) + it.each([ + [true, false], + [false, true] + ])('with unmountOnHide %s keeps the content mounted after closing: %s', async (unmountOnHide, kept) => { + const wrapper = await mountSuspended(Popover, { + props: { ...props, unmountOnHide }, + slots: { content: () => 'Content slot' } + }) + + expect(wrapper.find('[data-slot="content"]').attributes('data-state')).toBe('open') + + await wrapper.setProps({ open: false }) + + await vi.waitFor(() => { + const content = wrapper.find('[data-slot="content"]') + expect(content.exists()).toBe(kept) + if (kept) { + expect(content.attributes('data-state')).toBe('closed') + // `v-show` hides the popper wrapper around the content + expect((content.element.parentElement as HTMLElement).style.display).toBe('none') + } + }) + }) + it.each([ ['Checkbox', Checkbox], ['Switch', Switch] diff --git a/test/components/__snapshots__/DropdownMenu-vue.spec.ts.snap b/test/components/__snapshots__/DropdownMenu-vue.spec.ts.snap index b90a02cb2a..d42c95177a 100644 --- a/test/components/__snapshots__/DropdownMenu-vue.spec.ts.snap +++ b/test/components/__snapshots__/DropdownMenu-vue.spec.ts.snap @@ -1328,6 +1328,105 @@ exports[`DropdownMenu > renders with ui correctly 1`] = ` +" +`; + +exports[`DropdownMenu > renders with unmountOnHide correctly 1`] = ` +" + + + + + +
+ +
+ + + + " `; diff --git a/test/components/__snapshots__/DropdownMenu.spec.ts.snap b/test/components/__snapshots__/DropdownMenu.spec.ts.snap index 892d25aee8..8a6428d9e6 100644 --- a/test/components/__snapshots__/DropdownMenu.spec.ts.snap +++ b/test/components/__snapshots__/DropdownMenu.spec.ts.snap @@ -1346,6 +1346,107 @@ exports[`DropdownMenu > renders with ui correctly 1`] = ` +" +`; + +exports[`DropdownMenu > renders with unmountOnHide correctly 1`] = ` +" + + + + + +
+ +
+ + + + " `; diff --git a/test/components/__snapshots__/Popover-vue.spec.ts.snap b/test/components/__snapshots__/Popover-vue.spec.ts.snap index 53b1b935a6..1ea7e214be 100644 --- a/test/components/__snapshots__/Popover-vue.spec.ts.snap +++ b/test/components/__snapshots__/Popover-vue.spec.ts.snap @@ -129,5 +129,23 @@ exports[`Popover > renders with ui correctly 1`] = ` +" +`; + +exports[`Popover > renders with unmountOnHide correctly 1`] = ` +" + + + + + +
+ +
+ + + " `; diff --git a/test/components/__snapshots__/Popover.spec.ts.snap b/test/components/__snapshots__/Popover.spec.ts.snap index 334c63aa2f..02d155ec93 100644 --- a/test/components/__snapshots__/Popover.spec.ts.snap +++ b/test/components/__snapshots__/Popover.spec.ts.snap @@ -129,5 +129,23 @@ exports[`Popover > renders with ui correctly 1`] = ` +" +`; + +exports[`Popover > renders with unmountOnHide correctly 1`] = ` +" + + + + + +
+ +
+ + + " `; From 70ada16eec80bf1b24f856293e1c5d5fb41134dd Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Mon, 30 Nov 2026 11:54:28 +0100 Subject: [PATCH 2/4] fix(DropdownMenu): refocus the filter and reset sub menu search on reopen when kept mounted --- .../components/DropdownMenuContent.vue | 28 ++++++- test/components/DropdownMenu.spec.ts | 81 +++++++++++++++++++ 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/runtime/components/DropdownMenuContent.vue b/src/runtime/components/DropdownMenuContent.vue index 836e83d180..aec114a0c9 100644 --- a/src/runtime/components/DropdownMenuContent.vue +++ b/src/runtime/components/DropdownMenuContent.vue @@ -58,9 +58,9 @@ type DropdownMenuContentSlots<