"use client"; import * as React from "react"; import { createPortal } from "react-dom"; import { X } from "lucide-react"; import { cn } from "../../lib/utils"; import { motion, AnimatePresence } from "framer-motion"; interface SheetContextValue { open: boolean; setOpen: React.Dispatch>; } const SheetContext = React.createContext(undefined); interface SheetProps { children: React.ReactNode; defaultOpen?: boolean; open?: boolean; onOpenChange?: (open: boolean) => void; } const Sheet = ({ children, defaultOpen = false, open: controlledOpen, onOpenChange }: SheetProps) => { const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen); const isControlled = controlledOpen !== undefined; const open = isControlled ? controlledOpen : uncontrolledOpen; const setOpen = React.useCallback((value: React.SetStateAction) => { if (!isControlled) { setUncontrolledOpen(value); } if (onOpenChange) { const nextValue = typeof value === "function" ? value(open) : value; onOpenChange(nextValue); } }, [isControlled, onOpenChange, open]); return ( {children} ); }; interface SheetTriggerProps extends React.ButtonHTMLAttributes { asChild?: boolean; } const SheetTrigger = React.forwardRef( ({ children, asChild, ...props }, forwardedRef) => { const { setOpen } = React.useContext(SheetContext) || { setOpen: () => {} }; // Derive dependencies for the hook before the hook itself. // This logic can be conditional as it does not involve hooks. const child = asChild ? React.Children.only(children) : null; const childRef = child && React.isValidElement(child) ? (child as any).ref : undefined; // Call the hook unconditionally at the top level of the component. const mergedRef = React.useCallback( (node: HTMLElement | null) => { // Call the forwarded ref if (typeof forwardedRef === 'function') { forwardedRef(node as HTMLButtonElement | null); } else if (forwardedRef) { (forwardedRef as React.MutableRefObject).current = node as HTMLButtonElement | null; } // Call the child's original ref if (typeof childRef === 'function') { childRef(node); } else if (childRef) { (childRef as React.MutableRefObject).current = node; } }, [forwardedRef, childRef] ); if (asChild) { if (!React.isValidElement(child)) { console.error("SheetTrigger with `asChild` expects a single valid React element child."); return null; } // Use the memoized `mergedRef` inside the conditional block. return React.cloneElement(child, { ...child.props, ...props, // Pass down props like className, etc., to the child onClick: (e: React.MouseEvent) => { setOpen(true); if (child.props.onClick) child.props.onClick(e); }, ref: mergedRef, }); } return ( ); } ); SheetTrigger.displayName = "SheetTrigger"; const SheetClose = React.forwardRef< HTMLButtonElement, React.ButtonHTMLAttributes >(({ children, ...props }, ref) => { const { setOpen } = React.useContext(SheetContext) || { setOpen: () => { } }; return ( ); }); SheetClose.displayName = "SheetClose"; const SheetPortal = ({ children }: { children: React.ReactNode }) => { const { open } = React.useContext(SheetContext) || { open: false }; return open ? <>{children} : null; }; SheetPortal.displayName = "SheetPortal"; const SheetOverlay = React.forwardRef< HTMLDivElement, React.HTMLAttributes >(({ className, ...props }, ref) => { const { setOpen } = React.useContext(SheetContext) || { setOpen: () => { } }; const { onDrag: _, onDragEnd: __, onDragStart: ___, onDragExit: ____, onDragEnter: _____, onDragLeave: ______, onDragOver: _______, onDrop: ________, onAnimationStart: _________, ...restProps } = props; return ( setOpen(false)} {...restProps} /> ); }); SheetOverlay.displayName = "SheetOverlay"; interface SheetContentProps extends React.HTMLAttributes { side?: "top" | "right" | "bottom" | "left"; } const sideVariants = { top: { initial: { y: "-100%" }, animate: { y: "0%" }, exit: { y: "-100%" }, }, bottom: { initial: { y: "100%" }, animate: { y: "0%" }, exit: { y: "100%" }, }, left: { initial: { x: "-100%" }, animate: { x: "0%" }, exit: { x: "-100%" }, }, right: { initial: { x: "100%" }, animate: { x: "0%" }, exit: { x: "100%" }, }, }; const SheetContent = React.forwardRef( ({ side = "right", className, children, ...props }, ref) => { const { open, setOpen } = React.useContext(SheetContext) || { open: false, setOpen: () => { } }; const contentLocalRef = React.useRef(null); const combinedRef = React.useCallback((node: HTMLDivElement | null) => { contentLocalRef.current = node; if (typeof ref === 'function') { ref(node); } else if (ref) { (ref as React.MutableRefObject).current = node; } }, [ref]); React.useEffect(() => { if (!open) return; const handleMouseDown = (event: MouseEvent) => { if (contentLocalRef.current && !contentLocalRef.current.contains(event.target as Node)) { setOpen(false); } }; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { setOpen(false); } }; document.addEventListener('mousedown', handleMouseDown); document.addEventListener('keydown', handleKeyDown); return () => { document.removeEventListener('mousedown', handleMouseDown); document.removeEventListener('keydown', handleKeyDown); }; }, [open, setOpen]); const { onDrag: _, onDragEnd: __, onDragStart: ___, onDragExit: ____, onDragEnter: _____, onDragLeave: ______, onDragOver: _______, onDrop: ________, onAnimationStart: _________, ...restProps } = props; return createPortal( {open && ( {children} Close )} , document.body ); } ); SheetContent.displayName = "SheetContent"; const SheetHeader = ({ className, ...props }: React.HTMLAttributes) => (
); SheetHeader.displayName = "SheetHeader"; const SheetFooter = ({ className, ...props }: React.HTMLAttributes) => (
); SheetFooter.displayName = "SheetFooter"; const SheetTitle = React.forwardRef< HTMLHeadingElement, React.HTMLAttributes >(({ className, ...props }, ref) => (

)); SheetTitle.displayName = "SheetTitle"; const SheetDescription = React.forwardRef< HTMLParagraphElement, React.HTMLAttributes >(({ className, ...props }, ref) => (

)); SheetDescription.displayName = "SheetDescription"; export { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetOverlay, SheetPortal, SheetTitle, SheetTrigger, };