'use client' /** * A menubar is a row of menus. * * What it adds over a row of dropdowns is one behaviour: once a menu is open, * POINTING at a sibling opens it, without a click. Everything else a menubar * does — open state, click-away, keyboard navigation, portalling — is what * `DropdownMenu` does, so the rest of this file is that menu under the names a * menubar uses. */ import { XStack } from '@hanzo/gui' import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from '@hanzo/ui' import { createContext, useContext, useState, type ComponentProps, type ReactNode, } from 'react' /** * Which menu is open, for the whole bar. * * It lives here rather than in each menu because the hover-to-switch behaviour * is the one thing a menu cannot know on its own: it has to see that a SIBLING * is open to know that pointing at it should open this one. */ const BarContext = createContext<{ open: string | null setOpen: (id: string | null) => void } | null>(null) export function Menubar({ children, ...rest }: ComponentProps) { const [open, setOpen] = useState(null) return ( {children} ) } let seq = 0 /** * One menu in the bar. * * Controlled by the bar when it is inside one, and self-controlled when it is * not — so a `MenubarMenu` lifted out of a `Menubar` still opens, rather than * silently doing nothing. */ export function MenubarMenu({ children }: { readonly children?: ReactNode }) { const bar = useContext(BarContext) const [id] = useState(() => `menubar-${(seq += 1)}`) const [own, setOwn] = useState(false) const open = bar ? bar.open === id : own const setOpen = (next: boolean) => (bar ? bar.setOpen(next ? id : null) : setOwn(next)) return ( {children} ) } const MenuIdContext = createContext<{ id: string open: boolean setOpen: (v: boolean) => void } | null>(null) /** * The label you click — and, once any menu in the bar is open, the label you * only have to point at. That is the single thing a menubar adds. */ export function MenubarTrigger({ children, ...rest }: ComponentProps) { const bar = useContext(BarContext) const menu = useContext(MenuIdContext) return ( { // Only while the bar already has something open. Otherwise pointing // across a menubar on the way somewhere else would open every menu it // passed. if (bar?.open && menu && bar.open !== menu.id) bar.setOpen(menu.id) }} {...rest} > {children} ) } // The rest of a menubar IS a dropdown menu, under the names a menubar uses. export { DropdownMenuCheckboxItem as MenubarCheckboxItem, DropdownMenuContent as MenubarContent, DropdownMenuGroup as MenubarGroup, DropdownMenuItem as MenubarItem, DropdownMenuLabel as MenubarLabel, DropdownMenuPortal as MenubarPortal, DropdownMenuRadioGroup as MenubarRadioGroup, DropdownMenuRadioItem as MenubarRadioItem, DropdownMenuSeparator as MenubarSeparator, DropdownMenuShortcut as MenubarShortcut, DropdownMenuSub as MenubarSub, DropdownMenuSubContent as MenubarSubContent, DropdownMenuSubTrigger as MenubarSubTrigger, }