import { use, useRef } from 'react'; import { useMergedRef } from '@mantine/hooks'; import { BoxProps, CompoundStylesApiProps, createEventHandler, ElementProps, factory, Factory, useProps, } from '../../../core'; import { Popover } from '../../Popover'; import { useMenuContext } from '../Menu.context'; import { SubMenuContext } from '../MenuSub/MenuSub.context'; import { useMenuTypeAhead } from '../use-menu-type-ahead'; import classes from '../Menu.module.css'; export type MenuSubDropdownStylesNames = 'dropdown'; export interface MenuSubDropdownProps extends BoxProps, CompoundStylesApiProps, ElementProps<'div'> {} export type MenuSubDropdownFactory = Factory<{ props: MenuSubDropdownProps; ref: HTMLDivElement; stylesNames: MenuSubDropdownStylesNames; compound: true; }>; export const MenuSubDropdown = factory((props) => { const { classNames, className, style, styles, vars, onMouseEnter, onMouseLeave, onPointerEnter, onPointerLeave, onKeyDown, children, ref, ...others } = useProps('MenuSubDropdown', null, props); const wrapperRef = useRef(null); const ctx = useMenuContext(); const subCtx = use(SubMenuContext); const typeAhead = useMenuTypeAhead({ enabled: !ctx.hasSearch, opened: subCtx?.opened ?? false, getDropdown: () => wrapperRef.current, }); const handleKeyDown = createEventHandler(onKeyDown, (event) => { typeAhead(event); if ( !event.ctrlKey && !event.metaKey && !event.altKey && event.key.length === 1 && event.key !== ' ' ) { event.stopPropagation(); } }); const floatingProps = subCtx?.getFloatingProps({ onMouseEnter, onMouseLeave, onPointerEnter, onPointerLeave, }); return ( {children} ); }); MenuSubDropdown.classes = classes; MenuSubDropdown.displayName = '@mantine/core/MenuSubDropdown';