import { useState, useRef, useEffect } from "react"; type Option = { value: string; label: string; }; type SelectProps = { options: Option[]; placeholder?: string; value: string; onChange: (value: string) => void; dropDirection?: "up" | "down" | "auto"; className?: string; }; export default function Select({ options, placeholder = "Select option...", value, onChange, dropDirection = "down", className }: SelectProps) { const [open, setOpen] = useState(false); const ref = useRef(null); const [direction, setDirection] = useState<"up" | "down">("down"); const buttonRef = useRef(null); useEffect(() => { if (open && dropDirection === "auto" && buttonRef.current) { requestAnimationFrame(() => { const rect = buttonRef.current!.getBoundingClientRect(); const dropdownHeight = 200; const spaceBelow = window.innerHeight - rect.bottom; const spaceAbove = rect.top; if (spaceBelow < dropdownHeight && spaceAbove > dropdownHeight) { setDirection("up"); } else { setDirection("down"); } }); } }, [open, dropDirection]); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (ref.current && !ref.current.contains(event.target as Node)) { setOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const selectedOption = options.find(o => o.value === value); return (
{open && (
    {options.map((option) => (
  • { onChange(option.value); setOpen(false); }} className={`py-2 px-4 cursor-pointer hover:bg-gray-100 dark:hover:bg-neutral-800 dark:text-neutral-200 ${ option.value === value ? "bg-gray-100 dark:bg-neutral-800 font-medium" : "" }`} > {option.label}
  • ))}
)}
); }