{"version":3,"file":"menu-root.cjs","sources":["../../../components/menu/menu-root.tsx"],"sourcesContent":["'use client';\n\nimport { Menu as MenuPrimitive } from '@base-ui/react';\nimport {\n  createContext,\n  useCallback,\n  useContext,\n  useRef,\n  useState\n} from 'react';\n\ninterface CommonProps {\n  autocomplete?: boolean;\n  autocompleteMode?: 'auto' | 'manual';\n  inputValue?: string;\n  inputRef?: React.RefObject<HTMLInputElement | null>;\n  contentRef?: React.RefObject<HTMLDivElement | null>;\n  isInitialRender?: React.RefObject<boolean>;\n  open?: boolean;\n}\n\ninterface MenuContextValue extends CommonProps {\n  parent?: CommonProps;\n  onInputValueChange?: (value: string) => void;\n}\n\ninterface UseMenuContextReturn extends MenuContextValue {\n  shouldFilter?: boolean;\n  parent?: CommonProps & {\n    shouldFilter?: boolean;\n  };\n}\n\n/**\n Root context to manage the Menu control\n @remarks Only for internal usage.\n */\nexport const MenuContext = createContext<MenuContextValue | undefined>(\n  undefined\n);\n\nexport const useMenuContext = (): UseMenuContextReturn => {\n  const context = useContext(MenuContext);\n  if (!context) return {};\n\n  const shouldFilter = !!(\n    context?.autocomplete &&\n    context?.autocompleteMode === 'auto' &&\n    context?.inputValue?.length\n  );\n\n  const shouldFilterParent = !!(\n    context?.parent?.autocomplete &&\n    context?.parent?.autocompleteMode === 'auto' &&\n    context?.parent?.inputValue?.length\n  );\n\n  return {\n    ...context,\n    shouldFilter,\n    parent: context?.parent && {\n      ...context.parent,\n      shouldFilter: shouldFilterParent\n    }\n  };\n};\n\nexport interface MenuRootBaseProps {\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: MenuPrimitive.Root.Props['onOpenChange'];\n  modal?: boolean;\n  loopFocus?: boolean;\n}\n\nexport interface NormalMenuRootProps extends MenuPrimitive.Root.Props {\n  autocomplete?: false;\n  autocompleteMode?: never;\n  inputValue?: never;\n  onInputValueChange?: never;\n  defaultInputValue?: never;\n}\n\nexport interface AutocompleteMenuRootProps\n  extends MenuPrimitive.Root.Props,\n    CommonProps {\n  autocomplete: true;\n  onInputValueChange?: (value: string) => void;\n  defaultInputValue?: string;\n}\n\nexport type MenuRootProps = NormalMenuRootProps | AutocompleteMenuRootProps;\n\nexport const MenuRoot = ({\n  autocomplete,\n  autocompleteMode = 'auto',\n  inputValue: providedInputValue,\n  onInputValueChange,\n  defaultInputValue = '',\n  open: providedOpen,\n  onOpenChange,\n  defaultOpen = false,\n  ...props\n}: MenuRootProps) => {\n  const [internalInputValue, setInternalInputValue] =\n    useState(defaultInputValue);\n\n  const [internalOpen, setInternalOpen] = useState(defaultOpen);\n  const open = providedOpen ?? internalOpen;\n  const inputRef = useRef<HTMLInputElement>(null);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const isInitialRender = useRef(true);\n\n  const inputValue = providedInputValue ?? internalInputValue;\n\n  const setValue = useCallback(\n    (value: string) => {\n      setInternalInputValue(value);\n      onInputValueChange?.(value);\n    },\n    [onInputValueChange]\n  );\n\n  const handleOpenChange: MenuPrimitive.Root.Props['onOpenChange'] =\n    useCallback(\n      (value: boolean, eventDetails: MenuPrimitive.Root.ChangeEventDetails) => {\n        if (!value && autocomplete) {\n          setValue('');\n          isInitialRender.current = true;\n        }\n        setInternalOpen(value);\n        onOpenChange?.(value, eventDetails);\n      },\n      [onOpenChange, setValue, autocomplete]\n    );\n\n  return (\n    <MenuContext\n      value={{\n        autocomplete,\n        inputRef,\n        autocompleteMode,\n        inputValue,\n        onInputValueChange: setValue,\n        open: open,\n        isInitialRender,\n        contentRef\n      }}\n    >\n      <MenuPrimitive.Root\n        open={open}\n        onOpenChange={handleOpenChange}\n        loopFocus={false}\n        modal\n        {...props}\n      />\n    </MenuContext>\n  );\n};\nMenuRoot.displayName = 'Menu';\n\nexport interface NormalMenuSubMenuProps\n  extends MenuPrimitive.SubmenuRoot.Props {\n  autocomplete?: false;\n  autocompleteMode?: never;\n  inputValue?: never;\n  onInputValueChange?: never;\n  defaultInputValue?: never;\n}\n\nexport interface AutocompleteMenuSubMenuProps\n  extends MenuPrimitive.SubmenuRoot.Props {\n  autocomplete: true;\n  autocompleteMode?: 'auto' | 'manual';\n  inputValue?: string;\n  onInputValueChange?: (value: string) => void;\n  defaultInputValue?: string;\n}\n\nexport type MenuSubMenuProps =\n  | NormalMenuSubMenuProps\n  | AutocompleteMenuSubMenuProps;\n\nexport const MenuSubMenu = ({\n  autocomplete,\n  autocompleteMode = 'auto',\n  inputValue: providedInputValue,\n  onInputValueChange,\n  defaultInputValue = '',\n  open: providedOpen,\n  onOpenChange,\n  defaultOpen = false,\n  ...props\n}: MenuSubMenuProps) => {\n  const [internalInputValue, setInternalInputValue] =\n    useState(defaultInputValue);\n  const [internalOpen, setInternalOpen] = useState(defaultOpen);\n  const open = providedOpen ?? internalOpen;\n  const parentContext = useMenuContext();\n  const inputRef = useRef<HTMLInputElement>(null);\n  const isInitialRender = useRef(true);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const inputValue = providedInputValue ?? internalInputValue;\n\n  const setValue = useCallback(\n    (value: string) => {\n      setInternalInputValue(value);\n      onInputValueChange?.(value);\n    },\n    [onInputValueChange]\n  );\n\n  const handleOpenChange: MenuPrimitive.Root.Props['onOpenChange'] =\n    useCallback(\n      (value: boolean, eventDetails: MenuPrimitive.Root.ChangeEventDetails) => {\n        if (!value && autocomplete) {\n          setValue('');\n          isInitialRender.current = true;\n        }\n        setInternalOpen(value);\n        onOpenChange?.(value, eventDetails);\n      },\n      [onOpenChange, setValue, autocomplete]\n    );\n\n  return (\n    <MenuContext\n      value={{\n        autocomplete,\n        inputRef,\n        parent: parentContext,\n        autocompleteMode,\n        inputValue,\n        onInputValueChange: setValue,\n        open: open,\n        isInitialRender,\n        contentRef\n      }}\n    >\n      <MenuPrimitive.SubmenuRoot\n        loopFocus={false}\n        open={open}\n        onOpenChange={handleOpenChange}\n        {...props}\n      />\n    </MenuContext>\n  );\n};\nMenuSubMenu.displayName = 'Menu.Submenu';\n"],"names":[],"mappings":";;;;;;;AAiCA;;;AAGG;;AAKI;AACL;AACA;AAAc;AAEd;;AAGE;;AAKA;AACA;;AAIA;;AAEA;;AAEE;AACD;;AAEL;AA4Ba;;;AAeX;AACA;AACA;AACA;AAEA;AAEA;;AAGI;AACF;;AAOI;;AAEE;;;AAGF;;AAKN;;;;;AAOM;AACA;;;;AAcR;AACA;AAwBa;;;AAcX;AACA;AACA;AACA;AACA;AACA;AAEA;;AAGI;AACF;;AAOI;;AAEE;;;AAGF;;AAKN;;;AAKM;;;AAGA;AACA;;;;AAaR;AACA;;;;;"}