import { Ellipsis } from "lucide-react"; import { useEffect, useId, useRef, useState, type ReactNode } from "react"; type Props = { label: string; children: (close: () => void) => ReactNode; footer?: ReactNode; }; /** * Overflow menu for secondary actions. Items stay ordinary buttons so they * keep their accessible names; the menu closes on Escape, outside click and * after an item is used. */ export function ActionsMenu({ label, children, footer }: Props) { const [open, setOpen] = useState(false); const rootRef = useRef(null); const triggerRef = useRef(null); const panelId = useId(); useEffect(() => { if (!open) return; const onPointer = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false); }; const onKey = (event: KeyboardEvent) => { if (event.key === "Escape") { setOpen(false); triggerRef.current?.focus(); } }; document.addEventListener("pointerdown", onPointer); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("pointerdown", onPointer); document.removeEventListener("keydown", onKey); }; }, [open]); const close = () => setOpen(false); return (
{open && (
{children(close)} {footer &&
{footer}
}
)}
); }