import { CSSProperties, FunctionComponent, KeyboardEventHandler, ReactNode, useCallback, useEffect, useRef, } from 'react' import { KeyboardKey } from '@/Services/IOService' import { useListKeyboardNavigation } from '@/Hooks/useListKeyboardNavigation' type MenuProps = { className?: string style?: CSSProperties | undefined a11yLabel: string children: ReactNode closeMenu?: () => void isOpen: boolean initialFocus?: number } const Menu: FunctionComponent = ({ children, className = '', style, a11yLabel, closeMenu, isOpen, initialFocus, }: MenuProps) => { const menuElementRef = useRef(null) const handleKeyDown: KeyboardEventHandler = useCallback( (event) => { if (event.key === KeyboardKey.Escape) { closeMenu?.() return } }, [closeMenu], ) useListKeyboardNavigation(menuElementRef, initialFocus) useEffect(() => { if (isOpen) { setTimeout(() => { menuElementRef.current?.focus() }) } }, [isOpen]) return ( {children} ) } export default Menu