Dialog
Enfocar atención del usuario. Bloquear interacción. Comandar acción.
Instalación
Copia y pega el siguiente código en tu proyecto.
1'use client';23import { AnimatePresence, type HTMLMotionProps, motion, useReducedMotion } from 'motion/react';4import * as React from 'react';5import { cloneElement } from 'react';6import { createPortal } from 'react-dom';7import { cn } from '../lib/cn';8import {9 assignRef,10 focusFirst,11 lockPageScroll,12 trapFocus,13 useIsClient,14 useLayer,15} from '../lib/overlay';16import { backdrop, reveal, surface } from '../lib/motion';17import { useControllableState } from '../lib/use-controllable-state';1819type IconProps = React.SVGProps<SVGSVGElement> & {20 size?: number | string;21};2223function XIcon({ size = 24, strokeWidth = 1.5, ...props }: IconProps) {24 return (25 <svg26 xmlns="http://www.w3.org/2000/svg"27 width={size}28 height={size}29 viewBox="0 0 24 24"30 fill="none"31 stroke="currentColor"32 strokeWidth={strokeWidth}33 strokeLinecap="round"34 strokeLinejoin="round"35 aria-hidden="true"36 {...props}37 >38 <path d="M18 6 6 18" />39 <path d="m6 6 12 12" />40 </svg>41 );42}4344const OVERLAY_STYLE = { willChange: 'opacity' } as const;45const DIALOG_STYLE = { willChange: 'opacity, transform, filter' } as const;4647type SlotChildProps = Record<string, unknown> & { ref?: React.Ref<HTMLElement> };4849function mergeSlotProps(own: Record<string, unknown>, child: SlotChildProps) {50 const merged: Record<string, unknown> = { ...own, ...child };51 for (const [key, ownValue] of Object.entries(own)) {52 const childValue = child[key];53 if (54 /^on[A-Z]/.test(key) &&55 typeof ownValue === 'function' &&56 typeof childValue === 'function'57 ) {58 merged[key] = (...args: unknown[]) => {59 childValue(...args);60 ownValue(...args);61 };62 }63 }64 if (own.className || child.className) {65 merged.className = cn(66 own.className as string | undefined,67 child.className as string | undefined,68 );69 }70 if (own.style || child.style) {71 merged.style = { ...(own.style as object | undefined), ...(child.style as object | undefined) };72 }73 return merged;74}7576function useComposedRef<T extends HTMLElement>(77 first: React.Ref<T> | undefined,78 second: React.Ref<T> | React.Ref<HTMLElement> | undefined,79) {80 return React.useCallback(81 (node: T | null) => {82 assignRef(first, node);83 assignRef(second as React.Ref<T> | undefined, node);84 },85 [first, second],86 );87}8889type DialogContextType = {90 open: boolean;91 setOpen: React.Dispatch<React.SetStateAction<boolean>>;92 id: string;93 hasTitle: boolean;94 setHasTitle: (value: boolean) => void;95 hasDescription: boolean;96 setHasDescription: (value: boolean) => void;97};9899const DialogContext = React.createContext<DialogContextType | undefined>(undefined);100101const useDialogContext = () => {102 const context = React.use(DialogContext);103 if (!context) {104 throw new Error('useDialogContext must be used within a Dialog');105 }106 return context;107};108109interface DialogProps {110 children: React.ReactNode;111 defaultOpen?: boolean;112 open?: boolean;113 onOpenChange?: (open: boolean) => void;114}115116const DialogRoot = ({117 children,118 defaultOpen = false,119 open: controlledOpen,120 onOpenChange,121}: DialogProps) => {122 const [open, setOpen] = useControllableState({123 value: controlledOpen,124 defaultValue: defaultOpen,125 onChange: onOpenChange,126 });127 const [hasTitle, setHasTitle] = React.useState(false);128 const [hasDescription, setHasDescription] = React.useState(false);129 const id = React.useId();130131 const value = React.useMemo<DialogContextType>(132 () => ({ open, setOpen, id, hasTitle, setHasTitle, hasDescription, setHasDescription }),133 [open, setOpen, id, hasTitle, hasDescription],134 );135136 return <DialogContext value={value}>{children}</DialogContext>;137};138139interface DialogTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {140 asChild?: boolean;141}142143const DialogTrigger = ({144 asChild,145 onClick,146 children,147 ref,148 ...props149}: DialogTriggerProps & { ref?: React.Ref<HTMLButtonElement> }) => {150 const { open, setOpen } = useDialogContext();151 const slot = asChild && React.isValidElement<SlotChildProps>(children) ? children : null;152 const composedRef = useComposedRef<HTMLButtonElement>(ref, slot?.props.ref);153154 const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {155 setOpen(true);156 onClick?.(e);157 };158159 if (slot) {160 return cloneElement(slot, {161 ...mergeSlotProps({ ...props, onClick: handleClick }, slot.props),162 'aria-haspopup': 'dialog',163 'aria-expanded': open,164 ref: composedRef,165 });166 }167168 return (169 <button170 type="button"171 aria-haspopup="dialog"172 aria-expanded={open}173 onClick={handleClick}174 ref={ref}175 {...props}176 >177 {children}178 </button>179 );180};181DialogTrigger.displayName = 'DialogTrigger';182183interface DialogPortalProps {184 children: React.ReactNode;185}186187const DialogPortal = ({ children }: DialogPortalProps) => {188 const mounted = useIsClient();189190 if (!mounted) return null;191192 return createPortal(children, document.body);193};194195interface DialogCloseProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {196 asChild?: boolean;197}198199const DialogClose = ({200 asChild,201 onClick,202 children,203 ref,204 ...props205}: DialogCloseProps & { ref?: React.Ref<HTMLButtonElement> }) => {206 const { setOpen } = useDialogContext();207 const slot = asChild && React.isValidElement<SlotChildProps>(children) ? children : null;208 const composedRef = useComposedRef<HTMLButtonElement>(ref, slot?.props.ref);209210 const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {211 setOpen(false);212 onClick?.(e);213 };214215 if (slot) {216 return cloneElement(slot, {217 ...mergeSlotProps({ ...props, onClick: handleClick }, slot.props),218 ref: composedRef,219 });220 }221222 return (223 <button type="button" onClick={handleClick} ref={ref} {...props}>224 {children}225 </button>226 );227};228DialogClose.displayName = 'DialogClose';229230type DialogOverlayProps = HTMLMotionProps<'div'> & {231 className?: string;232};233234const DialogOverlay = ({235 className,236 ref,237 ...props238}: DialogOverlayProps & { ref?: React.Ref<HTMLDivElement> }) => {239 const { setOpen } = useDialogContext();240 const shouldReduceMotion = useReducedMotion();241242 return (243 <motion.div244 ref={ref}245 variants={shouldReduceMotion ? reveal : backdrop}246 initial="hidden"247 animate="visible"248 exit="exit"249 style={OVERLAY_STYLE}250 className={cn('bg-overlay fixed inset-0 z-500', className)}251 onClick={() => setOpen(false)}252 {...props}253 />254 );255};256DialogOverlay.displayName = 'DialogOverlay';257258interface DialogContentProps extends HTMLMotionProps<'div'> {259 children?: React.ReactNode;260 showClose?: boolean;261 closeLabel?: string;262}263264const DialogContent = ({265 className,266 children,267 showClose = true,268 closeLabel = 'Close',269 ref,270 ...props271}: DialogContentProps & { ref?: React.Ref<HTMLDivElement> }) => {272 const { id, open, setOpen, hasTitle, hasDescription } = useDialogContext();273 const contentRef = React.useRef<HTMLDivElement>(null);274 const composedRef = useComposedRef<HTMLDivElement>(contentRef, ref);275 const layer = useLayer(open, true);276 const triggerRef = React.useRef<HTMLElement | null>(null);277 const shouldReduceMotion = useReducedMotion();278279 React.useEffect(() => {280 if (!open) return;281282 triggerRef.current = document.activeElement as HTMLElement;283284 const handleKeyDown = (e: KeyboardEvent) => {285 if (e.defaultPrevented) return;286 const content = contentRef.current;287 if (!content) return;288289 if (e.key === 'Escape') {290 if (!layer.isTopmost()) return;291 e.preventDefault();292 setOpen(false);293 return;294 }295 if (layer.isTopmostModal() && !layer.isInsideUpperLayer(e.target)) trapFocus(e, content);296 };297298 document.addEventListener('keydown', handleKeyDown);299300 const frame = requestAnimationFrame(() => {301 if (contentRef.current) focusFirst(contentRef.current);302 });303304 const releasePageScroll = lockPageScroll();305306 return () => {307 cancelAnimationFrame(frame);308 document.removeEventListener('keydown', handleKeyDown);309 releasePageScroll();310 triggerRef.current?.focus();311 };312 }, [open, setOpen, layer]);313314 return (315 <AnimatePresence mode="wait">316 {open && (317 <DialogPortal>318 <DialogOverlay />319 <motion.div320 ref={composedRef}321 role="dialog"322 aria-modal="true"323 aria-labelledby={hasTitle ? `${id}-title` : undefined}324 aria-describedby={hasDescription ? `${id}-description` : undefined}325 tabIndex={-1}326 onClick={(e) => e.stopPropagation()}327 variants={shouldReduceMotion ? reveal : surface}328 initial="hidden"329 animate="visible"330 exit="exit"331 style={DIALOG_STYLE}332 className={cn(333 'bg-popover text-popover-foreground fixed top-[50%] left-[50%] z-500 grid max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-xl translate-x-[-50%] translate-y-[-50%] gap-4 overflow-y-auto rounded-lg p-6 shadow-lg outline-none',334 className,335 )}336 {...props}337 >338 {children}339 {showClose && (340 <DialogClose className="ring-offset-background focus-visible:ring-ring text-muted-foreground hover:text-foreground absolute end-2 top-2 rounded-sm p-2 transition-colors focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none disabled:pointer-events-none">341 <XIcon className="h-4 w-4" size={16} />342 <span className="sr-only">{closeLabel}</span>343 </DialogClose>344 )}345 </motion.div>346 </DialogPortal>347 )}348 </AnimatePresence>349 );350};351DialogContent.displayName = 'DialogContent';352353type DialogHeaderProps = React.HTMLAttributes<HTMLDivElement>;354355const DialogHeader = ({ className, ...props }: DialogHeaderProps) => {356 return (357 <div358 className={cn('flex flex-col space-y-1.5 text-center sm:text-start', className)}359 {...props}360 />361 );362};363DialogHeader.displayName = 'DialogHeader';364365type DialogFooterProps = React.HTMLAttributes<HTMLDivElement>;366367const DialogFooter = ({ className, ...props }: DialogFooterProps) => {368 return (369 <div370 className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', className)}371 {...props}372 />373 );374};375DialogFooter.displayName = 'DialogFooter';376377type DialogTitleProps = React.HTMLAttributes<HTMLHeadingElement>;378379const DialogTitle = ({380 className,381 children,382 ref,383 ...props384}: DialogTitleProps & { ref?: React.Ref<HTMLHeadingElement> }) => {385 const { id, setHasTitle } = useDialogContext();386387 React.useEffect(() => {388 setHasTitle(true);389 return () => setHasTitle(false);390 }, [setHasTitle]);391392 return (393 <h2394 id={`${id}-title`}395 ref={ref}396 className={cn('text-lg leading-none font-semibold tracking-tight', className)}397 {...props}398 >399 {children}400 </h2>401 );402};403DialogTitle.displayName = 'DialogTitle';404405type DialogDescriptionProps = React.HTMLAttributes<HTMLParagraphElement>;406407const DialogDescription = ({408 className,409 ref,410 ...props411}: DialogDescriptionProps & { ref?: React.Ref<HTMLParagraphElement> }) => {412 const { id, setHasDescription } = useDialogContext();413414 React.useEffect(() => {415 setHasDescription(true);416 return () => setHasDescription(false);417 }, [setHasDescription]);418419 return (420 <p421 id={`${id}-description`}422 ref={ref}423 className={cn('text-muted-foreground text-sm', className)}424 {...props}425 />426 );427};428DialogDescription.displayName = 'DialogDescription';429430const Dialog = Object.assign(DialogRoot, {431 Trigger: DialogTrigger,432 Portal: DialogPortal,433 Overlay: DialogOverlay,434 Close: DialogClose,435 Content: DialogContent,436 Header: DialogHeader,437 Footer: DialogFooter,438 Title: DialogTitle,439 Description: DialogDescription,440});441442export { Dialog };
Actualiza las rutas de importación para que coincidan con la estructura de tu proyecto.
Anatomía
1import { Dialog } from '@/components/dialog';
1<Dialog>2 <Dialog.Trigger asChild>3 <Button>Abrir Diálogo</Button>4 </Dialog.Trigger>5 <Dialog.Content>6 <Dialog.Header>7 <Dialog.Title>¿Estás absolutamente seguro?</Dialog.Title>8 <Dialog.Description>Esta acción no se puede deshacer.</Dialog.Description>9 </Dialog.Header>10 <Dialog.Footer>11 <Dialog.Close asChild>12 <Button variant="outline">Cancelar</Button>13 </Dialog.Close>14 <Button>Confirmar</Button>15 </Dialog.Footer>16 </Dialog.Content>17</Dialog>
Composición
Usa la siguiente composición para construir un
Dialog:Dialog├── Dialog.Trigger└── Dialog.Content├── Dialog.Header│ ├── Dialog.Title│ └── Dialog.Description└── Dialog.Footer└── Dialog.Close
Variantes
Alerta (Alert)
1'use client';23import { Button } from '@/components/button';4import { Dialog } from '@/components/dialog';56export function Alert() {7 return (8 <Dialog>9 <Dialog.Trigger asChild>10 <Button variant="destructive">Revoke key</Button>11 </Dialog.Trigger>12 <Dialog.Content>13 <Dialog.Header>14 <Dialog.Title>Revoke ci-deploy-prod?</Dialog.Title>15 <Dialog.Description>16 Every request signed with this key starts returning 401 right away. Two services used it17 in the last hour, so update your pipeline secrets before you revoke.18 </Dialog.Description>19 </Dialog.Header>20 <div className="bg-destructive-surface text-destructive-text rounded-md p-3 text-xs">21 <p className="font-mono">sk_live_9f2c...4ab1</p>22 <p className="mt-1">Created Mar 14, last used 42 minutes ago by deploy-runner</p>23 </div>24 <Dialog.Footer>25 <Dialog.Close asChild>26 <Button variant="outline">Keep key</Button>27 </Dialog.Close>28 <Dialog.Close asChild>29 <Button variant="destructive">Revoke key</Button>30 </Dialog.Close>31 </Dialog.Footer>32 </Dialog.Content>33 </Dialog>34 );35}
Referencia de API
Dialog
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
defaultOpen | boolean | false | Estado abierto inicial |
open | boolean | - | Estado abierto controlado |
onOpenChange | (open: boolean) => void | - | Callback cuando cambia el estado |
Dialog.Trigger
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
asChild | boolean | false | Renderizar como elemento hijo |
className | string | - | Clases CSS adicionales |
Dialog.Content
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
className | string | - | Clases CSS adicionales |
children | ReactNode | - | Contenido del diálogo |
showClose | boolean | true | Muestra el botón de cierre en la esquina |
closeLabel | string | 'Close' | Nombre accesible del botón de cierre |
Dialog.Header
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
className | string | - | Clases CSS adicionales |
Dialog.Title
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
className | string | - | Clases CSS adicionales |
Dialog.Description
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
className | string | - | Clases CSS adicionales |
Dialog.Footer
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
className | string | - | Clases CSS adicionales |
DialogClose
| Prop | Tipo | Por defecto | Descripción |
|---|---|---|---|
asChild | boolean | false | Renderizar como elemento hijo |
className | string | - | Clases CSS adicionales |
¿Encontraste algo que mejorar?
¿Notaste un error, tipografía o detalle faltante en esta página? Ayúdanos a mejorar la documentación abriendo un issue en GitHub.