Skip to content

Commit

Permalink
fix(VMenu): unregister on unmount
Browse files Browse the repository at this point in the history
fixes #17094
  • Loading branch information
KaelWD authored and blalan05 committed Nov 9, 2024
1 parent a62a425 commit 2f3163c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const VAutocomplete = genericComponent<new <
const menu = computed({
get: () => _menu.value,
set: v => {
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren) return
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren.size) return
_menu.value = v
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vuetify/src/components/VCombobox/VCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const VCombobox = genericComponent<new <
const menu = computed({
get: () => _menu.value,
set: v => {
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren) return
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren.size) return
_menu.value = v
},
})
Expand Down
24 changes: 19 additions & 5 deletions packages/vuetify/src/components/VMenu/VMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ import { useProxiedModel } from '@/composables/proxiedModel'
import { useScopeId } from '@/composables/scopeId'

// Utilities
import { computed, inject, mergeProps, nextTick, provide, ref, shallowRef, watch } from 'vue'
import {
computed,
inject,
mergeProps,
nextTick,
onBeforeUnmount,
onDeactivated,
provide,
ref,
shallowRef,
watch,
} from 'vue'
import { VMenuSymbol } from './shared'
import {
focusableChildren,
Expand Down Expand Up @@ -66,17 +77,17 @@ export const VMenu = genericComponent<OverlaySlots>()({
const overlay = ref<VOverlay>()

const parent = inject(VMenuSymbol, null)
const openChildren = shallowRef(0)
const openChildren = shallowRef(new Set<number>())
provide(VMenuSymbol, {
register () {
++openChildren.value
openChildren.value.add(uid)
},
unregister () {
--openChildren.value
openChildren.value.delete(uid)
},
closeParents (e) {
setTimeout(() => {
if (!openChildren.value &&
if (!openChildren.value.size &&
!props.persistent &&
(e == null || (overlay.value?.contentEl && !isClickInsideElement(e, overlay.value.contentEl)))
) {
Expand All @@ -87,6 +98,9 @@ export const VMenu = genericComponent<OverlaySlots>()({
},
})

onBeforeUnmount(() => parent?.unregister())
onDeactivated(() => isActive.value = false)

async function onFocusIn (e: FocusEvent) {
const before = e.relatedTarget as HTMLElement | null
const after = e.target as HTMLElement | null
Expand Down
2 changes: 1 addition & 1 deletion packages/vuetify/src/components/VSelect/VSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const VSelect = genericComponent<new <
const menu = computed({
get: () => _menu.value,
set: v => {
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren) return
if (_menu.value && !v && vMenuRef.value?.ΨopenChildren.size) return
_menu.value = v
},
})
Expand Down

0 comments on commit 2f3163c

Please sign in to comment.