lightswind
Version:
A collection of beautifully crafted React Components, Blocks & Templates for Modern Developers. Create stunning web applications effortlessly by using our 160+ professional and animated react components.
171 lines (170 loc) • 9.39 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// @ts-nocheck
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";
const SheetContext = React.createContext(undefined);
const Sheet = ({ children, defaultOpen = false, open: controlledOpen, onOpenChange }) => {
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen);
const isControlled = controlledOpen !== undefined;
const open = isControlled ? controlledOpen : uncontrolledOpen;
const setOpen = React.useCallback((value) => {
if (!isControlled) {
setUncontrolledOpen(value);
}
if (onOpenChange) {
const nextValue = typeof value === "function" ? value(open) : value;
onOpenChange(nextValue);
}
}, [isControlled, onOpenChange, open]);
return (_jsx(SheetContext.Provider, { value: { open, setOpen }, children: children }));
};
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.ref : undefined;
// Call the hook unconditionally at the top level of the component.
const mergedRef = React.useCallback((node) => {
// Call the forwarded ref
if (typeof forwardedRef === 'function') {
forwardedRef(node);
}
else if (forwardedRef) {
forwardedRef.current = node;
}
// Call the child's original ref
if (typeof childRef === 'function') {
childRef(node);
}
else if (childRef) {
childRef.current = node;
}
}, [forwardedRef, childRef]);
if (asChild) {
if (!React.isValidElement(child)) {
console.error("SheetTrigger with `asChild` expects a single valid React element child.");
return null;
}
const element = child;
// Use the memoized `mergedRef` inside the conditional block.
return React.cloneElement(element, {
...element.props,
...props, // Pass down props like className, etc., to the child
onClick: (e) => {
setOpen(true);
if (element.props.onClick)
element.props.onClick(e);
},
ref: mergedRef,
});
}
return (_jsx("button", { ref: forwardedRef, type: "button", onClick: () => setOpen(true), ...props, children: children }));
});
SheetTrigger.displayName = "SheetTrigger";
const SheetClose = React.forwardRef(({ children, asChild = false, ...props }, ref) => {
const { setOpen } = React.useContext(SheetContext) || { setOpen: () => { } };
if (asChild && React.isValidElement(children)) {
return React.cloneElement(children, {
...props,
ref,
onClick: (e) => {
children.props.onClick?.(e);
setOpen(false);
},
});
}
return (_jsx("button", { ref: ref, type: "button", onClick: () => setOpen(false), ...props, children: children }));
});
SheetClose.displayName = "SheetClose";
const SheetPortal = ({ children }) => {
const { open } = React.useContext(SheetContext) || { open: false };
return open ? _jsx(_Fragment, { children: children }) : null;
};
SheetPortal.displayName = "SheetPortal";
const SheetOverlay = React.forwardRef(({ className, ...props }, ref) => {
const { setOpen } = React.useContext(SheetContext) || { setOpen: () => { } };
const { onDrag: _, onDragEnd: __, onDragStart: ___, onDragExit: ____, onDragEnter: _____, onDragLeave: ______, onDragOver: _______, onDrop: ________, onAnimationStart: _________, ...restProps } = props;
return (_jsx(motion.div, { ref: ref, initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.3, ease: "easeInOut" }, className: cn("fixed inset-0 z-50 bg-black/80", className), onClick: () => setOpen(false), ...restProps }));
});
SheetOverlay.displayName = "SheetOverlay";
const sideVariants = {
top: {
initial: { y: "-100%", opacity: 0.5, scale: 0.9 },
animate: { y: "0%", opacity: 1, scale: 1 },
exit: { y: "-100%", opacity: 0.5, scale: 0.9 },
},
bottom: {
initial: { y: "100%", opacity: 0.5, scale: 0.9 },
animate: { y: "0%", opacity: 1, scale: 1 },
exit: { y: "100%", opacity: 0.5, scale: 0.9 },
},
left: {
initial: { x: "-100%", opacity: 0.5, scale: 0.9 },
animate: { x: "0%", opacity: 1, scale: 1 },
exit: { x: "-100%", opacity: 0.5, scale: 0.9 },
},
right: {
initial: { x: "100%", opacity: 0.5, scale: 0.9 },
animate: { x: "0%", opacity: 1, scale: 1 },
exit: { x: "100%", opacity: 0.5, scale: 0.9 },
},
};
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) => {
contentLocalRef.current = node;
if (typeof ref === 'function') {
ref(node);
}
else if (ref) {
ref.current = node;
}
}, [ref]);
React.useEffect(() => {
if (!open)
return;
const handleMouseDown = (event) => {
if (contentLocalRef.current && !contentLocalRef.current.contains(event.target)) {
setOpen(false);
}
};
const handleKeyDown = (event) => {
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(_jsx(AnimatePresence, { children: open && (_jsxs(SheetPortal, { children: [_jsx(SheetOverlay, {}), _jsxs(motion.div, { ref: combinedRef, initial: sideVariants[side].initial, animate: sideVariants[side].animate, exit: sideVariants[side].exit, transition: {
type: "spring",
damping: 25,
stiffness: 300,
opacity: { duration: 0.2 }
}, style: {
transformOrigin: side === "top" ? "top center" :
side === "bottom" ? "bottom center" :
side === "left" ? "center left" :
"center right"
}, className: cn("fixed z-50 gap-4 bg-background p-6 shadow-lg pt-10", side === "top" && "inset-x-0 top-0 border-b border-gray-200 dark:border-gray-800/40", side === "bottom" && "inset-x-0 bottom-0 border-t border-gray-200 dark:border-gray-800/40 ", side === "left" && "inset-y-0 left-0 h-full w-3/4 border-r border-gray-200 dark:border-gray-800/40 sm:max-w-sm", side === "right" && "inset-y-0 right-0 h-full w-3/4 border-l border-gray-200 dark:border-gray-800/40 sm:max-w-sm", className), "data-lenis-prevent": true, ...restProps, children: [children, _jsxs(SheetClose, { className: "absolute right-4 top-11 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none", children: [_jsx(X, { className: "h-4 w-4" }), _jsx("span", { className: "sr-only", children: "Close" })] })] }, "sheet-content")] })) }), document.body);
});
SheetContent.displayName = "SheetContent";
const SheetHeader = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col space-y-2 text-center sm:text-left", className), ...props }));
SheetHeader.displayName = "SheetHeader";
const SheetFooter = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", className), ...props }));
SheetFooter.displayName = "SheetFooter";
const SheetTitle = React.forwardRef(({ className, ...props }, ref) => (_jsx("h3", { ref: ref, className: cn("text-lg font-semibold text-foreground", className), ...props })));
SheetTitle.displayName = "SheetTitle";
const SheetDescription = React.forwardRef(({ className, ...props }, ref) => (_jsx("p", { ref: ref, className: cn("text-sm text-muted-foreground", className), ...props })));
SheetDescription.displayName = "SheetDescription";
export { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetOverlay, SheetPortal, SheetTitle, SheetTrigger, };