"use client"; import { Check, ChevronDown } from "lucide-react"; import { AnimatePresence, animate, type MotionValue, motion, useMotionValue, useMotionValueEvent, useReducedMotion, useTransform, type Variants, } from "motion/react"; import { createContext, type ReactNode, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, } from "react"; import { MENU_ITEM_CLASS, MenuItemHighlight } from "@/components/motion/menu/menu-item-highlight"; import { EASE_OUT } from "@/lib/ease"; import { type DismissReason, useDismiss } from "@/lib/hooks/use-dismiss"; import { type ListboxRow, useListbox } from "@/lib/hooks/use-listbox"; import { useSound } from "@/lib/sound"; import { closeSound } from "@/lib/sounds/close"; import { openSound } from "@/lib/sounds/open"; import { selectSound } from "@/lib/sounds/select"; import { cn } from "@/lib/utils"; // One progress value drives the surface, gap, corners and chevron. This short // spring suits the full reveal distance; each reader clamps its small internal // overshoot, so all visible endpoints settle together. Rows share a short // reveal so long lists have no late entries. const UNFOLD_SPRING = { type: "spring", visualDuration: 0.2, bounce: 0.12, } as const; // Exits are faster than entrances: the panel folds back on a short ease. const UNFOLD_EXIT = { duration: 0.15, ease: EASE_OUT } as const; // Opened or closed from the keyboard: no motion at all. const INSTANT = { duration: 0 } as const; // The trigger's label rolls when the value changes: the old one rises out, // faster than the new one rises in, with a 2px blur that hides the overlap. const LABEL_IN = { duration: 0.18, ease: EASE_OUT } as const; const LABEL_OUT = { duration: 0.12, ease: EASE_OUT } as const; // The trigger is a pill (half its h-10 height). The panel is rounded-2xl, the // radius every floating list in the library uses. const TRIGGER_RADIUS = 20; const PANEL_RADIUS = 16; // How far the open panel sits from the trigger once it has pulled away. const GAP = 8; // The surface starts as a thin lip flush against the trigger's edge. const LIP = 8; const lerp = (a: number, b: number, t: number) => a + (b - a) * t; // `custom` is the instant flag: rows of a list opened from the keyboard are // simply there, like the panel around them. const ITEM_VARIANTS: Variants = { // A shared short reveal keeps late rows in long, scrolled lists ready with // the surface. Per-index staggering can delay visible rows by over a second. hidden: (instant = false) => ({ opacity: 0, y: -4, transition: instant ? INSTANT : { duration: 0.08, ease: EASE_OUT }, }), show: (instant = false) => ({ opacity: 1, y: 0, transition: instant ? INSTANT : { duration: 0.12, ease: EASE_OUT }, }), }; type Placement = "bottom" | "top"; interface SelectContextValue { value: string | undefined; open: boolean; /** `instant` skips the unfold: the keyboard asked for it. */ setOpen: (open: boolean, quiet?: boolean, instant?: boolean) => void; select: (value: string, instant?: boolean) => void; register: (value: string, label: string, disabled: boolean) => void; unregister: (value: string) => void; labelFor: (value: string | undefined) => string | undefined; rows: ListboxRow[]; /** Closes the panel; focus inside it returns to the trigger. */ close: (quiet?: boolean, instant?: boolean) => void; /** Whether the latest open or close skips its animation. */ instant: boolean; reduce: boolean; progress: MotionValue; triggerId: string; listId: string; disabled: boolean; placement: Placement; setPlacement: (p: Placement) => void; } const SelectContext = createContext(null); // The active option (resolved during render by the content), whether its // highlight glides (after the pointer) or jumps (after a key), and how a // pointer resting on an option makes it the active one. interface ActiveOption { activeId: string | undefined; glide: boolean; setActive: (id: string) => void; } const ActiveOptionContext = createContext({ activeId: undefined, glide: true, setActive: () => {}, }); function useSelectContext(component: string) { const ctx = useContext(SelectContext); if (!ctx) throw new Error(`${component} must be used within