{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dialog","type":"registry:ui","title":"Dialog","description":"An accessible modal that springs in over a blurred backdrop and hands focus back on close.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/dialog.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { useEffect, useId, useRef, useSyncExternalStore, type KeyboardEvent, type ReactNode } from \"react\";\nimport { createPortal } from \"react-dom\";\n\ntype DialogProps = {\n  open: boolean;\n  onOpenChange: (open: boolean) => void;\n  title: ReactNode;\n  description?: ReactNode;\n  children?: ReactNode;\n  /** Close when the blurred backdrop is clicked. Escape always closes. */\n  closeOnBackdrop?: boolean;\n  /** Show the close button in the corner. */\n  showClose?: boolean;\n  /**\n   * Portal target. Defaults to document.body and covers the viewport.\n   * Pass an element to cover only that element; null waits until it exists.\n   */\n  container?: HTMLElement | null;\n  /** Extra classes for the panel. */\n  className?: string;\n};\n\nconst FOCUSABLE =\n  'a[href], button:not([disabled]), input:not([disabled]):not([type=\"hidden\"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])';\n\nconst noopSubscribe = () => () => {};\n\n/** True only in the browser, after hydration: portals never touch document on the server. */\nfunction useIsClient() {\n  return useSyncExternalStore(\n    noopSubscribe,\n    () => true,\n    () => false,\n  );\n}\n\n/** An accessible modal: focus moves in and back out, Tab stays inside, the page behind stops scrolling. */\nexport function Dialog({\n  open,\n  onOpenChange,\n  title,\n  description,\n  children,\n  closeOnBackdrop = true,\n  showClose = true,\n  container,\n  className = \"\",\n}: DialogProps) {\n  const isClient = useIsClient();\n  const reduce = useReducedMotion();\n  const panelRef = useRef<HTMLDivElement>(null);\n  const titleId = useId();\n  const descriptionId = useId();\n\n  // While open: lock scroll, move focus inside, and hand it back to the trigger on close.\n  useEffect(() => {\n    if (!open || container === null) return;\n    const trigger = document.activeElement instanceof HTMLElement ? document.activeElement : null;\n    const lockTarget = container ?? document.body;\n    const gutter = container ? 0 : window.innerWidth - document.documentElement.clientWidth;\n    const previous = { overflow: lockTarget.style.overflow, paddingRight: lockTarget.style.paddingRight };\n    lockTarget.style.setProperty(\"overflow\", \"hidden\");\n    if (gutter > 0) lockTarget.style.setProperty(\"padding-right\", `${gutter}px`);\n\n    const frame = requestAnimationFrame(() => {\n      const panel = panelRef.current;\n      if (!panel) return;\n      const target = panel.querySelector<HTMLElement>(\"[data-autofocus]\") ?? panel.querySelector<HTMLElement>(FOCUSABLE) ?? panel;\n      target.focus({ preventScroll: true });\n    });\n\n    return () => {\n      cancelAnimationFrame(frame);\n      lockTarget.style.setProperty(\"overflow\", previous.overflow);\n      lockTarget.style.setProperty(\"padding-right\", previous.paddingRight);\n      if (trigger?.isConnected) trigger.focus({ preventScroll: true });\n    };\n  }, [open, container]);\n\n  function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {\n    if (e.key === \"Escape\") {\n      e.stopPropagation();\n      onOpenChange(false);\n      return;\n    }\n    if (e.key !== \"Tab\") return;\n    // Keep Tab and Shift+Tab inside the panel.\n    const panel = e.currentTarget;\n    const items = Array.from(panel.querySelectorAll<HTMLElement>(FOCUSABLE)).filter((el) => el.getClientRects().length > 0);\n    if (items.length === 0) {\n      e.preventDefault();\n      panel.focus();\n      return;\n    }\n    const first = items[0];\n    const last = items[items.length - 1];\n    const current = document.activeElement;\n    if (e.shiftKey && (current === first || current === panel)) {\n      e.preventDefault();\n      last.focus();\n    } else if (!e.shiftKey && current === last) {\n      e.preventDefault();\n      first.focus();\n    }\n  }\n\n  if (!isClient || container === null) return null;\n  const position = container ? \"absolute\" : \"fixed\";\n\n  return createPortal(\n    <AnimatePresence>\n      {open ? (\n        <div key=\"dialog\" className={`${position} pointer-events-auto inset-0 z-[100] grid place-items-center p-4`}>\n          <motion.div\n            aria-hidden=\"true\"\n            className=\"absolute inset-0 bg-ink-950/60 backdrop-blur-sm\"\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n            transition={{ duration: 0.2 }}\n            onClick={closeOnBackdrop ? () => onOpenChange(false) : undefined}\n          />\n          <motion.div\n            ref={panelRef}\n            role=\"dialog\"\n            aria-modal=\"true\"\n            aria-labelledby={titleId}\n            aria-describedby={description ? descriptionId : undefined}\n            tabIndex={-1}\n            onKeyDown={onKeyDown}\n            initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.9, y: 14 }}\n            animate={{ opacity: 1, scale: 1, y: 0 }}\n            exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96, y: 6, transition: { duration: 0.16 } }}\n            transition={reduce ? { duration: 0.15 } : { type: \"spring\", stiffness: 420, damping: 30 }}\n            className={`relative w-full max-w-md rounded-3xl border border-ink-600 bg-ink-900 p-6 shadow-[0_40px_120px_-30px_rgb(0_0_0/0.95)] outline-none ${className}`}\n          >\n            <h2 id={titleId} className=\"pr-8 font-display text-lg font-extrabold text-bone\">\n              {title}\n            </h2>\n            {description ? (\n              <p id={descriptionId} className=\"mt-2 text-sm leading-relaxed text-bone-muted\">\n                {description}\n              </p>\n            ) : null}\n            {children ? <div className=\"mt-5\">{children}</div> : null}\n            {/* Last in DOM order, so initial focus lands on the content instead. */}\n            {showClose ? (\n              <button\n                type=\"button\"\n                onClick={() => onOpenChange(false)}\n                aria-label=\"Close\"\n                className=\"absolute right-4 top-4 grid size-8 place-items-center rounded-full text-bone-dim transition-colors hover:bg-ink-700 hover:text-bone\"\n              >\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" className=\"size-4\" aria-hidden=\"true\">\n                  <path d=\"m4 4 8 8m0-8-8 8\" strokeLinecap=\"round\" />\n                </svg>\n              </button>\n            ) : null}\n          </motion.div>\n        </div>\n      ) : null}\n    </AnimatePresence>,\n    container ?? document.body,\n  );\n}\n"}]}