{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-alert-dialog","type":"registry:component","title":"Alert Dialog (Motion)","description":"AlertDialog with motion: it grows out of its button, its icon pops in, a click outside shakes it because it needs an answer, and confirm can be a press-and-hold.","dependencies":["motion","radix-ui"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/lib-locale.json","https://dashral.com/r/lib-portal.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/motion-tokens.json","https://dashral.com/r/alert-dialog.json","https://dashral.com/r/motion-button.json"],"files":[{"path":"registry/dashral/motion/alert-dialog.tsx","type":"registry:file","target":"components/dashral/motion/alert-dialog.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { AlertDialog as AlertDialogPrimitive } from \"radix-ui\"\nimport { AnimatePresence, animate, motion } from \"motion/react\"\nimport type { AlertDialogColor, AlertDialogProps as KitAlertDialogProps } from \"../alert-dialog\"\nimport { useLocale } from \"../lib/locale\"\nimport { cn } from \"../lib/utils\"\nimport { usePortalContainer } from \"../lib/portal\"\n\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { duration, ease, spring } from \"../lib/motion-tokens\"\nimport { Button } from \"./button\"\n\nexport type AlertDialogProps = KitAlertDialogProps & {\n  /**\n   * For the worst of them (deleting a whole workspace): confirm has to be pressed and held, not clicked. true holds\n   * for 1.2s, a number for that many milliseconds. See Button's holdToConfirm.\n   */\n  holdToConfirm?: boolean | number\n}\n\n// The icon's tinted circle, as the kit's AlertDialog has it. Every class is written out in full so Tailwind finds it.\nconst iconColors: Record<AlertDialogColor, string> = {\n  primary: \"bg-primary/10 text-primary-text\",\n  destructive: \"bg-destructive/10 text-destructive-text\",\n}\n\n/**\n * The kit's AlertDialog with motion: the same question, buttons, onConfirm and props. It grows out of the button that\n * opened it on a quick spring and shrinks back into it as it closes; its icon pops in (a destructive one gives a short\n * wiggle). A click outside doesn't close it, so the panel shakes: it needs an answer. Confirm shows it's working while\n * onConfirm's promise runs, and holdToConfirm makes it a press-and-hold for the worst of them. With reduced motion it\n * only fades and nothing shakes.\n */\nexport function AlertDialog({\n  trigger,\n  title,\n  description,\n  children,\n  icon,\n  color = \"primary\",\n  confirmLabel,\n  cancelLabel,\n  loadingText,\n  onConfirm,\n  confirmDisabled,\n  holdToConfirm,\n  className,\n  classNames,\n  open,\n  defaultOpen,\n  onOpenChange,\n  ...props\n}: AlertDialogProps) {\n  const portalContainer = usePortalContainer()\n  const { t } = useLocale()\n  const reduced = useReducedMotion()\n  // Kept here, so the panel can stay on screen while it leaves.\n  const [own, setOwn] = React.useState(defaultOpen ?? false)\n  const isOpen = open ?? own\n  const [pending, setPending] = React.useState(false)\n  const close = () => {\n    if (open === undefined) setOwn(false)\n    onOpenChange?.(false)\n  }\n  const change = (next: boolean) => {\n    // While onConfirm runs, it stays: Escape and Cancel wait too.\n    if (pending) return\n    if (open === undefined) setOwn(next)\n    onOpenChange?.(next)\n  }\n\n  async function confirm(event: React.MouseEvent<HTMLButtonElement>) {\n    // Radix would close it on this click; it closes here instead, after onConfirm (and its promise) is done.\n    event.preventDefault()\n    const result = onConfirm?.()\n    if (!(result instanceof Promise)) return close()\n    setPending(true)\n    try {\n      await result\n      setPending(false)\n      close()\n    } catch {\n      setPending(false)\n    }\n  }\n\n  // Where it grows from: the trigger's middle, turned into a point inside the panel (centred in the viewport).\n  const opener = React.useRef<HTMLButtonElement>(null)\n  const panel = React.useRef<HTMLDivElement>(null)\n  const [origin, setOrigin] = React.useState(\"50% 50%\")\n  React.useLayoutEffect(() => {\n    const node = panel.current\n    if (!isOpen || !node) return\n    const box = opener.current?.getBoundingClientRect()\n    if (!box || !box.width) return setOrigin(\"50% 50%\")\n    const left = (window.innerWidth - node.offsetWidth) / 2\n    const top = (window.innerHeight - node.offsetHeight) / 2\n    setOrigin(`${box.left + box.width / 2 - left}px ${box.top + box.height / 2 - top}px`)\n  }, [isOpen])\n\n  /** A click outside: it won't close, so it says so with a short shake. */\n  function insist() {\n    const node = panel.current\n    if (reduced || !node) return\n    animate(node, { x: [0, -8, 7, -4, 2, 0] }, { duration: 0.4, ease: \"easeOut\" })\n  }\n\n  return (\n    <AlertDialogPrimitive.Root data-slot=\"alert-dialog\" open={isOpen} onOpenChange={change} {...props}>\n      {trigger && (\n        <AlertDialogPrimitive.Trigger asChild ref={opener}>\n          {trigger}\n        </AlertDialogPrimitive.Trigger>\n      )}\n      <AnimatePresence>\n        {isOpen && (\n          <AlertDialogPrimitive.Portal forceMount container={portalContainer}>\n            <AlertDialogPrimitive.Overlay forceMount asChild>\n              <motion.div\n                data-slot=\"alert-dialog-overlay\"\n                // The blur stays put and only the opacity moves: animating a full-screen blur costs frames.\n                className=\"fixed inset-0 z-50 bg-black/40 backdrop-blur-[2px]\"\n                initial={{ opacity: 0 }}\n                animate={{ opacity: 1 }}\n                exit={{ opacity: 0, transition: { duration: duration.fast, ease: ease.exit } }}\n                transition={{ duration: duration.standard, ease: ease.enter }}\n                onPointerDown={insist}\n              />\n            </AlertDialogPrimitive.Overlay>\n            <AlertDialogPrimitive.Content forceMount asChild {...(description ? {} : { \"aria-describedby\": undefined })}>\n              {/* The shake runs on this outer box; the inner one grows and shrinks, so the two never fight over a transform. */}\n              <div ref={panel} className=\"fixed top-1/2 left-1/2 z-50 w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 sm:max-w-md\">\n                <motion.div\n                  data-slot=\"alert-dialog-content\"\n                  style={{ transformOrigin: origin }}\n                  initial={reduced ? { opacity: 0 } : { opacity: 0, scale: 0.6 }}\n                  animate={{ opacity: 1, scale: 1 }}\n                  exit={reduced ? { opacity: 0, transition: { duration: duration.fast } } : { opacity: 0, scale: 0.8, transition: { duration: duration.fast, ease: ease.exit } }}\n                  transition={reduced ? { duration: duration.fast } : { ...spring.appear, opacity: { duration: duration.fast, ease: ease.enter } }}\n                  className={cn(\"grid max-h-[calc(100dvh-2rem)] gap-5 overflow-y-auto overscroll-contain rounded-lg border bg-background p-6 shadow-lg\", className)}\n                >\n                  <div className=\"flex flex-col items-center gap-4 text-center sm:flex-row sm:items-start sm:text-start\">\n                    {icon && (\n                      <motion.span\n                        data-slot=\"alert-dialog-icon\"\n                        className={cn(\"grid size-10 shrink-0 place-items-center rounded-full [&_svg]:size-5\", iconColors[color])}\n                        initial={reduced ? false : { scale: 0.4, opacity: 0 }}\n                        animate={reduced ? { opacity: 1 } : color === \"destructive\" ? { scale: 1, opacity: 1, rotate: [0, -12, 10, -6, 0] } : { scale: 1, opacity: 1 }}\n                        transition={reduced ? { duration: duration.fast } : { ...spring.pop, delay: 0.08, rotate: { duration: 0.5, delay: 0.25, ease: \"easeOut\" } }}\n                      >\n                        {icon}\n                      </motion.span>\n                    )}\n                    <div className=\"grid gap-2\">\n                      <AlertDialogPrimitive.Title\n                        data-slot=\"alert-dialog-title\"\n                        className={cn(\"font-heading text-lg leading-tight font-medium tracking-[-0.02em]\", classNames?.title)}\n                      >\n                        {title}\n                      </AlertDialogPrimitive.Title>\n                      {description && (\n                        <AlertDialogPrimitive.Description data-slot=\"alert-dialog-description\" className={cn(\"text-sm text-muted-foreground\", classNames?.description)}>\n                          {description}\n                        </AlertDialogPrimitive.Description>\n                      )}\n                    </div>\n                  </div>\n                  {children}\n                  <div data-slot=\"alert-dialog-footer\" className={cn(\n            // Side by side, each taking its share, on a phone too; the main one wraps above only when the labels can't share a line.\n            \"flex flex-wrap-reverse gap-2 sm:flex-wrap sm:justify-end [&>*]:min-w-fit [&>*]:flex-1 sm:[&>*]:flex-none\",\n            classNames?.footer\n          )}>\n                    {/* Cancel takes the focus when it opens, so Enter never confirms by accident. */}\n                    <AlertDialogPrimitive.Cancel asChild>\n                      <Button variant=\"outline\" disabled={pending}>\n                        {cancelLabel ?? t(\"cancel\")}\n                      </Button>\n                    </AlertDialogPrimitive.Cancel>\n                    <AlertDialogPrimitive.Action asChild>\n                      <Button color={color} disabled={confirmDisabled} loading={pending} loadingText={loadingText} holdToConfirm={holdToConfirm} onClick={confirm}>\n                        {confirmLabel ?? t(\"continue\")}\n                      </Button>\n                    </AlertDialogPrimitive.Action>\n                  </div>\n                </motion.div>\n              </div>\n            </AlertDialogPrimitive.Content>\n          </AlertDialogPrimitive.Portal>\n        )}\n      </AnimatePresence>\n    </AlertDialogPrimitive.Root>\n  )\n}\n"}],"cssVars":{"theme":{"color-primary-text":"var(--primary-text, var(--primary))","color-destructive-foreground":"var(--destructive-foreground)","color-destructive-text":"var(--destructive-text)","font-heading":"var(--font-geist, \"Geist\"), var(--font-inter, \"Inter\"), var(--font-arabic, \"IBM Plex Sans Arabic\"), ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif"},"light":{"destructive-foreground":"oklch(0.985 0 0)","destructive-text":"#bd2800"},"dark":{"destructive-foreground":"oklch(0.2 0 0)","destructive-text":"#ff8a5c"}}}