Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 7x 1x 7x 6x 6x 6x 7x 35x 6x 1x 6x 5x 5x 5x 6x 35x 35x 35x 35x 35x 35x 35x 35x 35x 36x 36x 36x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 8x 8x 28x 28x 28x 28x 53x 53x 53x 34x 34x 5x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 35x 35x 35x 35x 35x 35x 35x 35x 35x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // src/components/Dialog.tsx
import * as React from 'react';
import ReactDOM from 'react-dom';
import { XIcon } from 'lucide-react';
import { cn } from '../lib/utils';
// Define the type for the DialogContext
type DialogContextType = {
isOpen: boolean;
openDialog: () => void;
closeDialog: () => void;
};
// Create a context to manage dialog state and actions
const DialogContext = React.createContext<DialogContextType>({
isOpen: false,
openDialog: () => {},
closeDialog: () => {},
});
// Dialog (Root) Component
interface DialogProps {
open?: boolean;
defaultOpen?: boolean;
onOpenChange?: (open: boolean) => void;
children: React.ReactNode;
}
function Dialog({
open,
defaultOpen = false,
onOpenChange,
children,
}: DialogProps) {
const [internalOpen, setInternalOpen] = React.useState(defaultOpen);
const isControlled = open !== undefined;
const isOpen = isControlled ? open : internalOpen;
const openDialog = () => {
if (isControlled) {
onOpenChange?.(true);
} else {
setInternalOpen(true);
onOpenChange?.(true);
}
};
const closeDialog = () => {
if (isControlled) {
onOpenChange?.(false);
} else {
setInternalOpen(false);
onOpenChange?.(false);
}
};
const contextValue: DialogContextType = {
isOpen,
openDialog,
closeDialog,
};
return (
<DialogContext.Provider value={contextValue}>
{children}
</DialogContext.Provider>
);
}
// DialogTrigger Component
interface DialogTriggerProps extends React.HTMLAttributes<HTMLButtonElement> {
asChild?: boolean;
children?: React.ReactNode;
}
function DialogTrigger({ asChild, children, ...props }: DialogTriggerProps) {
const { openDialog } = React.useContext(DialogContext);
if (asChild && children && React.isValidElement(children)) {
return React.cloneElement(
children as React.ReactElement<{ onClick?: React.MouseEventHandler }>,
{
onClick: (e: React.MouseEvent) => {
const childOnClick = (
children as React.ReactElement<{
onClick?: React.MouseEventHandler;
}>
).props.onClick;
if (childOnClick) childOnClick(e);
openDialog();
},
}
);
}
return (
<button data-slot='dialog-trigger' onClick={openDialog} {...props}>
{children}
</button>
);
}
// DialogPortal Component
interface DialogPortalProps {
children: React.ReactNode;
}
function DialogPortal({ children }: DialogPortalProps) {
const { isOpen } = React.useContext(DialogContext);
if (!isOpen) return null;
return ReactDOM.createPortal(children, document.body);
}
// DialogClose Component
interface DialogCloseProps extends React.HTMLAttributes<HTMLButtonElement> {
asChild?: boolean;
children?: React.ReactNode;
}
function DialogClose({ asChild, children, ...props }: DialogCloseProps) {
const { closeDialog } = React.useContext(DialogContext);
if (asChild && children && React.isValidElement(children)) {
return React.cloneElement(
children as React.ReactElement<{ onClick?: React.MouseEventHandler }>,
{
onClick: (e: React.MouseEvent) => {
const childOnClick = (
children as React.ReactElement<{
onClick?: React.MouseEventHandler;
}>
).props.onClick;
if (childOnClick) childOnClick(e);
closeDialog();
},
}
);
}
return (
<button data-slot='dialog-close' onClick={closeDialog} {...props}>
{children}
</button>
);
}
// DialogOverlay Component
type DialogOverlayProps = React.HTMLAttributes<HTMLDivElement>;
function DialogOverlay({ className, ...props }: DialogOverlayProps) {
const { closeDialog } = React.useContext(DialogContext);
return (
<div
data-slot='dialog-overlay'
className={cn('fixed inset-0 z-50 bg-black/50', className)}
onClick={closeDialog}
{...props}
/>
);
}
// DialogContent Component
interface DialogContentProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
function DialogContent({ className, children, ...props }: DialogContentProps) {
const { closeDialog, isOpen } = React.useContext(DialogContext);
return (
<DialogPortal>
<DialogOverlay />
<div
role='dialog'
aria-modal='true'
data-slot='dialog-content'
data-state={isOpen ? 'open' : 'closed'}
className={cn(
'fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border bg-background p-6 shadow-lg duration-200 sm:max-w-lg',
className
)}
{...props}
>
{children}
<button
data-slot='dialog-close'
className="ring-offset-background focus:ring-ring absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-none disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
onClick={closeDialog}
>
<XIcon />
<span className='sr-only'>Close</span>
</button>
</div>
</DialogPortal>
);
}
// DialogHeader Component
type DialogHeaderProps = React.HTMLAttributes<HTMLDivElement>;
function DialogHeader({ className, ...props }: DialogHeaderProps) {
return (
<div
data-slot='dialog-header'
className={cn('flex flex-col gap-2 text-center sm:text-left', className)}
{...props}
/>
);
}
// DialogFooter Component
type DialogFooterProps = React.HTMLAttributes<HTMLDivElement>;
function DialogFooter({ className, ...props }: DialogFooterProps) {
return (
<div
data-slot='dialog-footer'
className={cn(
'flex flex-col-reverse gap-2 sm:flex-row sm:justify-end',
className
)}
{...props}
/>
);
}
// DialogTitle Component
type DialogTitleProps = React.HTMLAttributes<HTMLHeadingElement>;
function DialogTitle({ className, ...props }: DialogTitleProps) {
return (
<h2
data-slot='dialog-title'
className={cn('text-lg leading-none font-semibold', className)}
{...props}
/>
);
}
// DialogDescription Component
type DialogDescriptionProps = React.HTMLAttributes<HTMLParagraphElement>;
function DialogDescription({ className, ...props }: DialogDescriptionProps) {
return (
<p
data-slot='dialog-description'
className={cn('text-muted-foreground text-sm', className)}
{...props}
/>
);
}
// Export all components
export {
Dialog,
DialogTrigger,
DialogPortal,
DialogClose,
DialogOverlay,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
};
|