UNPKG

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.

116 lines (115 loc) 7.08 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; // @ts-nocheck import * as React from "react"; import { createPortal } from "react-dom"; import { cn } from "../../lib/utils"; import { X } from "lucide-react"; import { motion, AnimatePresence } from "framer-motion"; // Import HTMLMotionProps const DrawerContext = React.createContext(undefined); function useDrawerContext() { const context = React.useContext(DrawerContext); if (!context) { throw new Error("useDrawerContext must be used within a Drawer"); } return context; } const Drawer = ({ children, defaultOpen = false, open: controlledOpen, onOpenChange, side = "bottom", }) => { 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(DrawerContext.Provider, { value: { open, setOpen, side }, children: children })); }; const DrawerTrigger = React.forwardRef(({ children, asChild = false, ...props }, ref) => { const { setOpen } = useDrawerContext(); if (asChild && React.isValidElement(children)) { return React.cloneElement(children, { ...props, ref, onClick: (e) => { children.props.onClick?.(e); props.onClick?.(e); setOpen(true); }, }); } return (_jsx("button", { ref: ref, type: "button", onClick: () => setOpen(true), ...props, children: children })); }); DrawerTrigger.displayName = "DrawerTrigger"; const DrawerContent = React.forwardRef(({ children, className, ...props }, ref) => { const { open, setOpen, side } = useDrawerContext(); const variants = { 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 sideOrigins = { top: "top center", bottom: "bottom center", left: "center left", right: "center right", }; return createPortal(_jsx(AnimatePresence, { children: open && (_jsxs("div", { className: cn("fixed inset-0 z-50 flex mx-auto", side === "top" && "flex-col items-center justify-start", side === "bottom" && "flex-col items-center justify-end", side === "left" && "flex-row items-start justify-start", side === "right" && "flex-row items-start justify-end"), children: [_jsx(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.3 }, className: "absolute inset-0 bg-background/80 backdrop-blur-sm", onClick: () => setOpen(false), "aria-hidden": "true" }), _jsxs(motion.div, { ref: ref, initial: variants[side].initial, animate: variants[side].animate, exit: variants[side].exit, transition: { type: "spring", damping: 25, stiffness: 300, opacity: { duration: 0.2 } }, style: { transformOrigin: sideOrigins[side] }, className: cn("relative z-50 bg-background shadow-2xl outline-none border-border/50", (side === "top" || side === "bottom") && "w-full rounded-t-3xl border-t max-h-[90vh] overflow-y-auto", (side === "left") && "h-full w-3/4 max-w-sm border-r rounded-r-3xl overflow-y-auto", (side === "right") && "h-full w-3/4 max-w-sm border-l rounded-l-3xl overflow-y-auto", className), role: "dialog", "aria-modal": "true", ...Object.keys(props).reduce((acc, key) => { if (key === 'onDrag' || key === 'onAnimationStart' || key === 'onTransitionEnd') { return acc; } acc[key] = props[key]; return acc; }, {}), children: [(side === "bottom" || side === "top") && (_jsx("div", { className: "mx-auto my-2 h-1.5 w-16 rounded-full bg-muted" })), children, _jsxs("button", { onClick: () => setOpen(false), className: "absolute right-4 top-6 rounded-sm opacity-70 \r\n ring-offset-background transition-opacity hover:opacity-100 \r\n focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none", "aria-label": "Close drawer", children: [_jsx(X, { className: "h-4 w-4 lg:h-8 lg:w-8 z-999" }), _jsx("span", { className: "sr-only", children: "Close drawer" })] })] })] })) }), document.body); }); DrawerContent.displayName = "DrawerContent"; const DrawerClose = React.forwardRef(({ children, asChild = false, ...props }, ref) => { const { setOpen } = useDrawerContext(); if (asChild && React.isValidElement(children)) { return React.cloneElement(children, { ...props, ref, onClick: (e) => { children.props.onClick?.(e); props.onClick?.(e); setOpen(false); }, }); } return (_jsx("button", { ref: ref, type: "button", onClick: () => setOpen(false), ...props, children: children })); }); DrawerClose.displayName = "DrawerClose"; const DrawerHeader = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col space-y-2 text-center sm:text-left p-4", className), ...props })); DrawerHeader.displayName = "DrawerHeader"; const DrawerFooter = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2 p-4", className), ...props })); DrawerFooter.displayName = "DrawerFooter"; const DrawerTitle = React.forwardRef(({ className, ...props }, ref) => (_jsx("h3", { ref: ref, className: cn("text-lg font-semibold text-foreground", className), ...props }))); DrawerTitle.displayName = "DrawerTitle"; const DrawerDescription = React.forwardRef(({ className, ...props }, ref) => (_jsx("p", { ref: ref, className: cn("text-sm text-muted-foreground", className), ...props }))); DrawerDescription.displayName = "DrawerDescription"; export { Drawer, DrawerTrigger, DrawerContent, DrawerClose, DrawerHeader, DrawerFooter, DrawerTitle, DrawerDescription, };