import { useEffect, useRef, useState } from "react"; export interface OverflowItem { label: string; onSelect: () => void; danger?: boolean; disabled?: boolean; } interface Props { items: OverflowItem[]; label?: string; align?: "left" | "right"; } /** The `⋯` menu that keeps destructive actions out of the main button row. */ export default function OverflowMenu({ items, label = "More actions", align = "right", }: Props) { const [open, setOpen] = useState(false); const rootRef = useRef(null); useEffect(() => { if (!open) return; const onDocClick = (e: MouseEvent) => { if (!rootRef.current?.contains(e.target as Node)) setOpen(false); }; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); }; document.addEventListener("mousedown", onDocClick); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("mousedown", onDocClick); document.removeEventListener("keydown", onKey); }; }, [open]); return (
{open && (
{items.map((item) => ( ))}
)}
); }