import { animate, AnimatePresence, cubicBezier, motion } from "motion/react"; import { useEffect, useMemo, useRef, useState } from "react"; import { cn } from "@/utils/cn"; import { lockBody } from "../lockBody"; import PortalToBody from "../utils/portal-to-body"; export default function Combobox({ placeholder, options, value, onChange, className, }: { placeholder?: string; options: { label: string; value: string }[]; value: string; onChange: (value: string) => void; className?: string; }) { const selected = useMemo(() => { return options.find((option) => option.value === value); }, [options, value]); const [isOpen, setIsOpen] = useState(false); const [bounds, setBounds] = useState(null); const ref = useRef(null); useEffect(() => { lockBody("combobox", isOpen); }, [isOpen]); useEffect(() => { document.addEventListener("click", (e) => { if (ref.current && e.composedPath().includes(ref.current)) { return; } setIsOpen(false); }); }, []); return (
{isOpen && bounds && (
{ onChange(value); setIsOpen(false); }} />
)}
); } const Items = ({ options, onChange, }: { options: { label: string; value: string }[]; onChange: (value: string) => void; }) => { const backgroundRef = useRef(null); return (
{options.map((option) => ( ))}
); };