import { Check, CaretDown } from "@phosphor-icons/react"; import { useEffect, useRef, useState } from "react"; export type BottomControlSelectOption = { value: TValue; label: string; }; export type BottomControlSelectMenuProps = { value: TValue; options: readonly BottomControlSelectOption[]; "aria-label": string; onValueChange: (value: TValue) => void; }; export function BottomControlSelectMenu({ value, options, onValueChange, ...props }: BottomControlSelectMenuProps) { const rootRef = useRef(null); const [open, setOpen] = useState(false); const selectedOption = options.find((option) => option.value === value) ?? options[0]; useEffect(() => { if (!open) return; const handlePointerDown = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false); }; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); }; window.addEventListener("pointerdown", handlePointerDown); window.addEventListener("keydown", handleKeyDown); return () => { window.removeEventListener("pointerdown", handlePointerDown); window.removeEventListener("keydown", handleKeyDown); }; }, [open]); return (
{open ? (
{options.map((option) => { const selected = option.value === value; return ( ); })}
) : null}
); }