UNPKG

lightswind

Version:

A professionally designed component library & templates market that brings together functionality, accessibility, and beautiful aesthetics for modern applications.

121 lines (120 loc) 6.59 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import * as React from "react"; import { cn } from "../lib/utils"; import { buttonVariants } from "./button"; const AlertDialogContext = React.createContext(undefined); const AlertDialog = ({ 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 newValue = typeof value === "function" ? value(open) : value; onOpenChange(newValue); } }, [isControlled, onOpenChange, open]); return (_jsx(AlertDialogContext.Provider, { value: { open, setOpen }, children: children })); }; const AlertDialogTrigger = React.forwardRef(({ children, asChild = false, ...props }, ref) => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error("AlertDialogTrigger must be used within an AlertDialog"); } const { setOpen } = context; const handleClick = (e) => { setOpen(true); // Call the original onClick if it exists if (props.onClick) { props.onClick(e); } }; // Remove onClick from props to avoid duplicate handlers const { onClick, ...otherProps } = props; if (asChild) { return (_jsx(_Fragment, { children: React.Children.map(children, child => { if (React.isValidElement(child)) { return React.cloneElement(child, { ...child.props, ref, onClick: handleClick }); } return child; }) })); } return (_jsx("button", { ref: ref, type: "button", onClick: handleClick, ...otherProps, children: children })); }); AlertDialogTrigger.displayName = "AlertDialogTrigger"; const AlertDialogPortal = ({ children }) => { return _jsx(_Fragment, { children: children }); }; const AlertDialogOverlay = React.forwardRef(({ className, ...props }, ref) => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error("AlertDialogOverlay must be used within an AlertDialog"); } const { open } = context; if (!open) return null; return (_jsx("div", { ref: ref, className: cn("fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0", className), ...props })); }); AlertDialogOverlay.displayName = "AlertDialogOverlay"; const AlertDialogContent = React.forwardRef(({ className, children, ...props }, ref) => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error("AlertDialogContent must be used within an AlertDialog"); } const { open } = context; if (!open) return null; return (_jsxs(AlertDialogPortal, { children: [_jsx(AlertDialogOverlay, {}), _jsx("div", { ref: ref, className: cn("fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg", className), ...props, children: children })] })); }); AlertDialogContent.displayName = "AlertDialogContent"; const AlertDialogHeader = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col space-y-2 text-center sm:text-left", className), ...props })); AlertDialogHeader.displayName = "AlertDialogHeader"; const AlertDialogFooter = ({ className, ...props }) => (_jsx("div", { className: cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", className), ...props })); AlertDialogFooter.displayName = "AlertDialogFooter"; const AlertDialogTitle = React.forwardRef(({ className, ...props }, ref) => (_jsx("h2", { ref: ref, className: cn("text-lg font-semibold", className), ...props }))); AlertDialogTitle.displayName = "AlertDialogTitle"; const AlertDialogDescription = React.forwardRef(({ className, ...props }, ref) => (_jsx("p", { ref: ref, className: cn("text-sm text-muted-foreground", className), ...props }))); AlertDialogDescription.displayName = "AlertDialogDescription"; const AlertDialogAction = React.forwardRef(({ className, ...props }, ref) => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error("AlertDialogAction must be used within an AlertDialog"); } const { setOpen } = context; const handleClick = (e) => { setOpen(false); // Call the original onClick if it exists if (props.onClick) { props.onClick(e); } }; // Remove onClick from props to avoid duplicate handlers const { onClick, ...otherProps } = props; return (_jsx("button", { ref: ref, className: cn(buttonVariants(), className), onClick: handleClick, ...otherProps })); }); AlertDialogAction.displayName = "AlertDialogAction"; const AlertDialogCancel = React.forwardRef(({ className, ...props }, ref) => { const context = React.useContext(AlertDialogContext); if (!context) { throw new Error("AlertDialogCancel must be used within an AlertDialog"); } const { setOpen } = context; const handleClick = (e) => { setOpen(false); // Call the original onClick if it exists if (props.onClick) { props.onClick(e); } }; // Remove onClick from props to avoid duplicate handlers const { onClick, ...otherProps } = props; return (_jsx("button", { ref: ref, className: cn(buttonVariants({ variant: "outline" }), "mt-2 sm:mt-0", className), onClick: handleClick, ...otherProps })); }); AlertDialogCancel.displayName = "AlertDialogCancel"; export { AlertDialog, AlertDialogPortal, AlertDialogOverlay, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel, };