// One Export button with a menu of formats: options = [[value, name, description], ...]. // Arrow keys move, Enter picks, Esc or a click outside closes. import { useEffect, useRef, useState } from "react"; import { Icon } from "../lib/icons.jsx"; export function ExportMenu({ label, options, onPick, disabled }) { const [open, setOpen] = useState(false); const [active, setActive] = useState(0); const wrap = useRef(null), button = useRef(null), menu = useRef(null); useEffect(() => { if (!open) return undefined; const away = (e) => { if (!wrap.current?.contains(e.target)) setOpen(false); }; document.addEventListener("pointerdown", away); menu.current?.focus(); return () => document.removeEventListener("pointerdown", away); }, [open]); const pick = (value) => { setOpen(false); button.current?.focus(); onPick(value); }; const onKey = (e) => { if (e.key === "ArrowDown") { e.preventDefault(); setActive((a) => (a + 1) % options.length); } else if (e.key === "ArrowUp") { e.preventDefault(); setActive((a) => (a - 1 + options.length) % options.length); } else if (e.key === "Enter" || e.key === " ") { e.preventDefault(); pick(options[active][0]); } else if (e.key === "Escape" || e.key === "Tab") { setOpen(false); button.current?.focus(); } }; return (