import { useEffect, useRef, useState } from "react"; export interface DropdownOption { value: string; label: string; } export function ThemedDropdown({ value, options, onChange, ariaLabel, compact = false, align = "right", }: { value: string; options: DropdownOption[]; onChange: (value: string) => void; ariaLabel: string; compact?: boolean; align?: "left" | "right"; }) { const [open, setOpen] = useState(false); const ref = useRef(null); const current = options.find((option) => option.value === value) ?? options[0]; useEffect(() => { const handlePointerDown = (event: PointerEvent) => { if (!ref.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); }; }, []); return (
{open ? (
{options.map((option) => { const selected = option.value === value; return ( ); })}
) : null}
); }