Line data Source code
1 : import type { InjectionKey, Ref, WritableComputedRef } from 'vue'
2 : import { computed, inject, provide, ref } from 'vue'
3 :
4 24 : const openPopoverKey: InjectionKey<Ref<string | null>> =
5 : Symbol('load3dOpenPopover')
6 :
7 : /**
8 : * Keeps Load3D menubar popovers mutually exclusive. `@pointerdown.stop` on the
9 : * Load3D container blocks reka-ui's document-level outside-pointerdown
10 : * dismissal, so sibling popovers cannot close each other on their own — on
11 : * Safari not even via the focus-outside fallback, because clicking a button
12 : * there does not focus it.
13 : *
14 : * Call once per component; the returned factory yields a writable `open`
15 : * binding per popover id. The open-popover id is shared with ancestor scopes
16 : * via provide/inject, so exclusivity spans the whole menubar.
17 : */
18 83 : export function usePopoverExclusivity() {
19 83 : const openPopover = inject(openPopoverKey, null) ?? ref<string | null>(null)
20 83 : provide(openPopoverKey, openPopover)
21 83 : return function exclusivePopover(id: string): WritableComputedRef<boolean> {
22 111 : return computed({
23 : get: () => openPopover.value === id,
24 : set: (open) => {
25 19 : if (open) openPopover.value = id
26 9 : else if (openPopover.value === id) openPopover.value = null
27 : }
28 : })
29 : }
30 : }
|