|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { MenubarContentEmits, MenubarContentProps } from './MenubarContent.ts' |
| 3 | +import { MenuContent } from '../menu/index.ts' |
| 4 | +import { wrapArray } from '../utils/array.ts' |
| 5 | +import { composeEventHandlers } from '../utils/vue.ts' |
| 6 | +import { useMenubarMenuContext } from './MenubarMenu.ts' |
| 7 | +import { useCollection, useMenubarContext } from './MenubarRoot.ts' |
| 8 | +
|
| 9 | +defineOptions({ |
| 10 | + name: 'MenubarContent', |
| 11 | +}) |
| 12 | +
|
| 13 | +withDefaults(defineProps<MenubarContentProps>(), { |
| 14 | + align: 'start', |
| 15 | +}) |
| 16 | +const emit = defineEmits<MenubarContentEmits>() |
| 17 | +
|
| 18 | +const context = useMenubarContext('MenubarContent') |
| 19 | +const menuContext = useMenubarMenuContext('MenubarContent') |
| 20 | +const getItems = useCollection() |
| 21 | +let hasInteractedOutsideRef = false |
| 22 | +
|
| 23 | +const onCloseAutoFocus = composeEventHandlers((event) => { |
| 24 | + emit('closeAutoFocus', event) |
| 25 | +}, (event) => { |
| 26 | + const menubarOpen = Boolean(context.value.value) |
| 27 | + if (!menubarOpen && !hasInteractedOutsideRef) { |
| 28 | + menuContext.triggerRef.current?.focus() |
| 29 | + } |
| 30 | +
|
| 31 | + hasInteractedOutsideRef = false |
| 32 | + // Always prevent auto focus because we either focus manually or want user agent focus |
| 33 | + event.preventDefault() |
| 34 | +}) |
| 35 | +
|
| 36 | +const onFocusOutside = composeEventHandlers<MenubarContentEmits['focusOutside'][0]>((event) => { |
| 37 | + emit('focusOutside', event) |
| 38 | +}, (event) => { |
| 39 | + const target = event.target as HTMLElement |
| 40 | + const isMenubarTrigger = getItems().some(item => item?.contains(target)) |
| 41 | + if (isMenubarTrigger) |
| 42 | + event.preventDefault() |
| 43 | +}) |
| 44 | +
|
| 45 | +const onInteractOutside = composeEventHandlers<MenubarContentEmits['interactOutside'][0]>((event) => { |
| 46 | + emit('interactOutside', event) |
| 47 | +}, () => { |
| 48 | + hasInteractedOutsideRef = true |
| 49 | +}) |
| 50 | +
|
| 51 | +function onEntryFocus(event: Event) { |
| 52 | + if (!menuContext.wasKeyboardTriggerOpenRef.current) |
| 53 | + event.preventDefault() |
| 54 | +} |
| 55 | +
|
| 56 | +const onKeydown = composeEventHandlers<KeyboardEvent>( |
| 57 | + (event) => { |
| 58 | + emit('keydown', event) |
| 59 | + }, |
| 60 | + (event) => { |
| 61 | + if (['ArrowRight', 'ArrowLeft'].includes(event.key)) { |
| 62 | + const target = event.target as HTMLElement |
| 63 | + const targetIsSubTrigger = target.hasAttribute('data-radix-menubar-subtrigger') |
| 64 | + const isKeyDownInsideSubMenu |
| 65 | + = target.closest('[data-radix-menubar-content]') !== event.currentTarget |
| 66 | +
|
| 67 | + const prevMenuKey = context.dir.value === 'rtl' ? 'ArrowRight' : 'ArrowLeft' |
| 68 | + const isPrevKey = prevMenuKey === event.key |
| 69 | + const isNextKey = !isPrevKey |
| 70 | +
|
| 71 | + // Prevent navigation when we're opening a submenu |
| 72 | + if (isNextKey && targetIsSubTrigger) |
| 73 | + return |
| 74 | + // or we're inside a submenu and are moving backwards to close it |
| 75 | + if (isKeyDownInsideSubMenu && isPrevKey) |
| 76 | + return |
| 77 | +
|
| 78 | + let candidateValues: string[] = [] |
| 79 | +
|
| 80 | + for (const item of getItems()) { |
| 81 | + if (item.$$rcid.$menubar.disabled) |
| 82 | + continue |
| 83 | + candidateValues.push(item.$$rcid.$menubar.value) |
| 84 | + } |
| 85 | +
|
| 86 | + if (isPrevKey) |
| 87 | + candidateValues.reverse() |
| 88 | +
|
| 89 | + const currentIndex = candidateValues.indexOf(menuContext.value) |
| 90 | +
|
| 91 | + candidateValues = context.loop() |
| 92 | + ? wrapArray(candidateValues, currentIndex + 1) |
| 93 | + : candidateValues.slice(currentIndex + 1) |
| 94 | +
|
| 95 | + const [nextValue] = candidateValues |
| 96 | + if (nextValue) |
| 97 | + context.onMenuOpen(nextValue) |
| 98 | + } |
| 99 | + }, |
| 100 | + { checkForDefaultPrevented: false }, |
| 101 | +) |
| 102 | +
|
| 103 | +const style = { |
| 104 | + // re-namespace exposed content custom properties |
| 105 | + '--radix-menubar-content-transform-origin': 'var(--radix-popper-transform-origin)', |
| 106 | + '--radix-menubar-content-available-width': 'var(--radix-popper-available-width)', |
| 107 | + '--radix-menubar-content-available-height': 'var(--radix-popper-available-height)', |
| 108 | + '--radix-menubar-trigger-width': 'var(--radix-popper-anchor-width)', |
| 109 | + '--radix-menubar-trigger-height': 'var(--radix-popper-anchor-height)', |
| 110 | +} |
| 111 | +</script> |
| 112 | + |
| 113 | +<template> |
| 114 | + <MenuContent |
| 115 | + :id="menuContext.contentId" |
| 116 | + :aria-labelledby="menuContext.triggerId" |
| 117 | + data-radix-menubar-content="" |
| 118 | + :align="align" |
| 119 | + :style="style" |
| 120 | + @close-auto-focus="onCloseAutoFocus" |
| 121 | + @focus-outside="onFocusOutside" |
| 122 | + @interact-outside="onInteractOutside" |
| 123 | + @entry-focus="onEntryFocus" |
| 124 | + @keydown="onKeydown" |
| 125 | + > |
| 126 | + <slot /> |
| 127 | + </MenuContent> |
| 128 | +</template> |
0 commit comments