import { createContext, forwardRef, useCallback, useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type HTMLAttributes, type ReactNode } from "react"; import { useReducedMotion } from "motion/react"; import { cn } from "@/lib/utils"; interface SidebarPanelProps extends HTMLAttributes { header?: ReactNode; width: number; isClosing?: boolean; open?: boolean; floating?: boolean; contentClassName?: string; } export const SidebarPanelActionsContext = createContext(null); /** Retain the same surface through exit, including an interrupted close. */ export const SidebarPanel = forwardRef(function SidebarPanel({ open = true, isClosing, ...props }, ref) { const [present, setPresent] = useState(open); const reducedMotion = useReducedMotion(); useEffect(() => { if (open) { setPresent(true); return; } const timer = window.setTimeout(() => setPresent(false), reducedMotion ? 0 : 300); return () => window.clearTimeout(timer); }, [open, reducedMotion]); return open || present ? : null; }); /** Shared nonmodal side slot. Contents keep their final width while the slot opens. */ const SidebarPanelContent = forwardRef(function SidebarPanelContent({ width, isClosing = false, floating = false, header, contentClassName, className, style, children, ...props }, forwardedRef) { const panel = useRef(null); const [entered, setEntered] = useState(false); const [actionsHost, setActionsHost] = useState(null); const setPanel = useCallback((node: HTMLElement | null) => { panel.current = node; if (typeof forwardedRef === "function") forwardedRef(node); else if (forwardedRef) forwardedRef.current = node; }, [forwardedRef]); useLayoutEffect(() => { // Commit the outgoing slot width before transitioning to the replacement width. panel.current?.getBoundingClientRect(); let frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => setEntered(true)); }); return () => cancelAnimationFrame(frame); }, []); return ; });