{"version":3,"file":"MenuSub.mjs","names":["useId"],"sources":["../../../../src/components/Menu/MenuSub/MenuSub.tsx"],"sourcesContent":["import { use, useCallback, useEffect, useRef } from 'react';\nimport {\n  safePolygon,\n  useFloating,\n  useHover,\n  useInteractions,\n  type SafePolygonOptions,\n} from '@floating-ui/react';\nimport { useId, useUncontrolled } from '@mantine/hooks';\nimport { ExtendComponent, Factory, useDirection, useProps } from '../../../core';\nimport {\n  FloatingAxesOffsets,\n  FloatingPosition,\n  getFloatingPosition,\n} from '../../../utils/Floating';\nimport { __PopoverProps, Popover } from '../../Popover';\nimport { TransitionOverride } from '../../Transition';\nimport { useMenuContext } from '../Menu.context';\nimport { MenuSubDropdown } from '../MenuSubDropdown/MenuSubDropdown';\nimport { MenuSubItem } from '../MenuSubItem/MenuSubItem';\nimport { MenuSubTarget } from '../MenuSubTarget/MenuSubTarget';\nimport { SubMenuContext } from './MenuSub.context';\n\nexport type MenuSubFactory = Factory<{\n  props: MenuSubProps;\n}>;\n\nexport interface MenuSubProps extends __PopoverProps {\n  children: React.ReactNode;\n\n  /** Controlled opened state */\n  opened?: boolean;\n\n  /** Called with current state when dropdown opens or closes */\n  onChange?: (opened: boolean) => void;\n\n  /** Open delay in ms, applicable when hover trigger is used */\n  openDelay?: number;\n\n  /** Close delay in ms, applicable when hover trigger is used */\n  closeDelay?: number;\n\n  /** Dropdown position relative to the target element @default 'right-start' */\n  position?: FloatingPosition;\n\n  /** Offset of the dropdown element @default 0 */\n  offset?: number | FloatingAxesOffsets;\n\n  /** Props passed down to the `Transition` component that used to animate dropdown presence, use to configure duration and animation type @default { duration: 0 } */\n  transitionProps?: TransitionOverride;\n\n  /** Determines whether submenu stays open while the cursor moves toward its dropdown. Pass an object to configure safe polygon behavior. @default true */\n  safeAreaPolygon?: boolean | SafePolygonOptions;\n}\n\nconst defaultProps = {\n  offset: 0,\n  position: 'right-start',\n  safeAreaPolygon: true,\n  transitionProps: { duration: 0 },\n  openDelay: 0,\n  middlewares: {\n    shift: {\n      // Enable crossAxis shift to keep submenu dropdown within viewport bounds when positioned horizontally\n      crossAxis: true,\n    },\n  },\n} satisfies Partial<MenuSubProps>;\n\nexport function MenuSub(_props: MenuSubProps) {\n  const {\n    children,\n    closeDelay,\n    openDelay,\n    position,\n    safeAreaPolygon,\n    opened: openedProp,\n    onChange,\n    ...others\n  } = useProps('MenuSub', defaultProps, _props);\n  const id = useId();\n  const [opened, setOpened] = useUncontrolled({\n    value: openedProp,\n    finalValue: false,\n    onChange,\n  });\n  const parentSubCtx = use(SubMenuContext);\n  const menuCtx = useMenuContext();\n  const { dir } = useDirection();\n  const resolvedPosition = getFloatingPosition(dir, position);\n\n  const levelRegister = parentSubCtx?.registerOpenSub ?? menuCtx.registerOpenSub;\n\n  const activeChildCloseRef = useRef<(() => void) | null>(null);\n  const registerOpenSub = useCallback((closeFn: () => void) => {\n    const prev = activeChildCloseRef.current;\n    if (prev && prev !== closeFn) {\n      prev();\n    }\n    activeChildCloseRef.current = closeFn;\n    return () => {\n      if (activeChildCloseRef.current === closeFn) {\n        activeChildCloseRef.current = null;\n      }\n    };\n  }, []);\n\n  const setOpenedRef = useRef(setOpened);\n  setOpenedRef.current = setOpened;\n\n  const handleOpen = useCallback(() => setOpenedRef.current(true), []);\n  const handleClose = useCallback(() => setOpenedRef.current(false), []);\n\n  useEffect(() => {\n    if (!opened) {\n      return undefined;\n    }\n\n    return levelRegister(handleClose);\n  }, [opened, levelRegister, handleClose]);\n\n  const { context, refs } = useFloating({\n    placement: resolvedPosition,\n    open: opened,\n    onOpenChange: (nextOpen) => {\n      if (nextOpen) {\n        handleOpen();\n      } else {\n        handleClose();\n      }\n    },\n  });\n\n  const { getReferenceProps, getFloatingProps } = useInteractions([\n    useHover(context, {\n      handleClose: safeAreaPolygon\n        ? safePolygon(typeof safeAreaPolygon === 'object' ? safeAreaPolygon : undefined)\n        : undefined,\n      delay: { open: openDelay, close: closeDelay },\n    }),\n  ]);\n\n  const focusFirstItem = () =>\n    window.setTimeout(() => {\n      document\n        .getElementById(`${id}-dropdown`)\n        ?.querySelectorAll<HTMLButtonElement>('[data-menu-item]:not([data-disabled])')[0]\n        ?.focus();\n    }, 16);\n\n  const focusParentItem = () =>\n    window.setTimeout(() => {\n      document.getElementById(`${id}-target`)?.focus();\n    }, 16);\n\n  return (\n    <SubMenuContext\n      value={{\n        opened,\n        close: handleClose,\n        open: handleOpen,\n        focusFirstItem,\n        focusParentItem,\n        parentContext: parentSubCtx,\n        setReference: refs.setReference,\n        setFloating: refs.setFloating,\n        getReferenceProps,\n        getFloatingProps,\n        registerOpenSub,\n      }}\n    >\n      <Popover\n        opened={opened}\n        onChange={(nextOpened) => (nextOpened ? handleOpen() : handleClose())}\n        withinPortal={false}\n        withArrow={false}\n        id={id}\n        position={position}\n        {...others}\n      >\n        {children}\n      </Popover>\n    </SubMenuContext>\n  );\n}\n\nMenuSub.extend = (input: ExtendComponent<MenuSubFactory>) => input;\nMenuSub.displayName = '@mantine/core/MenuSub';\nMenuSub.Target = MenuSubTarget;\nMenuSub.Dropdown = MenuSubDropdown;\nMenuSub.Item = MenuSubItem;\n"],"mappings":";;;;;;;;;;;;;;;AAuDA,MAAM,eAAe;CACnB,QAAQ;CACR,UAAU;CACV,iBAAiB;CACjB,iBAAiB,EAAE,UAAU,EAAE;CAC/B,WAAW;CACX,aAAa,EACX,OAAO,EAEL,WAAW,KACb,EACF;AACF;AAEA,SAAgB,QAAQ,QAAsB;CAC5C,MAAM,EACJ,UACA,YACA,WACA,UACA,iBACA,QAAQ,YACR,UACA,GAAG,WACD,SAAS,WAAW,cAAc,MAAM;CAC5C,MAAM,KAAKA,QAAM;CACjB,MAAM,CAAC,QAAQ,aAAa,gBAAgB;EAC1C,OAAO;EACP,YAAY;EACZ;CACF,CAAC;CACD,MAAM,eAAe,IAAI,cAAc;CACvC,MAAM,UAAU,eAAe;CAC/B,MAAM,EAAE,QAAQ,aAAa;CAC7B,MAAM,mBAAmB,oBAAoB,KAAK,QAAQ;CAE1D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