import { useEffect, useId, useRef, useState, type KeyboardEventHandler, type ReactNode } from "react"; import { cn } from "@/lib/utils"; /** Keep the content mounted so both directions animate and form drafts survive closing. */ export function DisclosureContent({ open, children, id, className, onKeyDown, onExitComplete, }: { open: boolean; children: ReactNode; id?: string; className?: string; onKeyDown?: KeyboardEventHandler; onExitComplete?: () => void; }) { const node = useRef(null); useEffect(() => { if (open || !onExitComplete) return; // Read the actual CSS transitions, including a reversal in progress. With // reduced motion (or no transition), there is nothing to wait for. const animations = node.current?.getAnimations?.() ?? []; if (!animations.length) { onExitComplete(); return; } let cancelled = false; void Promise.allSettled(animations.map((animation) => animation.finished)).then(() => { if (!cancelled) onExitComplete(); }); return () => { cancelled = true; }; }, [open, onExitComplete]); return (
{children}
); } export function Disclosure({ summary, children, className, summaryClassName, contentClassName, }: { summary: ReactNode; children: ReactNode; className?: string; summaryClassName?: string; contentClassName?: string; }) { const [open, setOpen] = useState(false); const id = useId(); return (
{children}
); }