{ "name": "modal-transition", "type": "registry:ui", "files": [ { "path": "components/ui/modal-transition.tsx", "content": "// GENERATED FILE - do not edit.\n// Source: scripts/transitions-src/modal-transition.tsx\n// Re-generate: node scripts/generate-transitions.mjs\n\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n// Modal open/close transition — ported from transitions.dev\n// https://github.com/Jakubantalik/transitions.dev (MIT)\n// Scale + fade with separate open / close durations.\n\nconst MODAL_CSS = `\n.t-modal-scope {\n --modal-open-dur: 250ms;\n --modal-close-dur: 150ms;\n --modal-scale: 0.96;\n --modal-scale-close: 0.96;\n --modal-ease: cubic-bezier(0.22, 1, 0.36, 1);\n}\n.t-modal {\n transform-origin: center;\n transform: scale(var(--modal-scale));\n opacity: 0;\n pointer-events: none;\n transition:\n transform var(--modal-open-dur) var(--modal-ease),\n opacity var(--modal-open-dur) var(--modal-ease);\n will-change: transform, opacity;\n}\n.t-modal.is-open {\n transform: scale(1);\n opacity: 1;\n pointer-events: auto;\n}\n.t-modal.is-closing {\n transform: scale(var(--modal-scale-close));\n opacity: 0;\n pointer-events: none;\n transition:\n transform var(--modal-close-dur) var(--modal-ease),\n opacity var(--modal-close-dur) var(--modal-ease);\n}\n@media (prefers-reduced-motion: reduce) {\n .t-modal { transition: none !important; }\n}\n`\n\n// useInsertionEffect runs before the browser paints, so the resting state\n// these rules define lands on the first frame instead of after it. The DOM\n// query replaces a module-level flag, which stayed true if the tag was ever\n// removed and never fired for a second document.\nfunction useStyles() {\n React.useInsertionEffect(() => {\n if (typeof document === \"undefined\") return\n if (document.querySelector('style[data-t-modal]')) return\n const el = document.createElement(\"style\")\n el.setAttribute(\"data-t-modal\", \"\")\n el.textContent = MODAL_CSS\n document.head.appendChild(el)\n }, [])\n}\n\n// The duration variables live on the .t-*-scope wrapper, not on :root, so the\n// lookup has to start from the scope element. Custom properties inherit\n// downwards, so reading documentElement never sees them.\nfunction readMs(el: Element | null, name: string, fallback: number) {\n if (typeof window === \"undefined\" || !el) return fallback\n const raw = getComputedStyle(el).getPropertyValue(name).trim()\n if (!raw) return fallback\n const n = parseFloat(raw)\n if (!Number.isFinite(n)) return fallback\n return raw.endsWith(\"ms\") ? n : raw.endsWith(\"s\") ? n * 1000 : n\n}\n\nconst FOCUSABLE =\n 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])'\n\n// Only elements Tab can actually reach: a selector alone also matches hidden\n// controls and buttons that opt out with tabindex=\"-1\", such as menu items.\nfunction focusables(root: HTMLElement) {\n return Array.from(root.querySelectorAll(FOCUSABLE)).filter(\n (el) =>\n el.tabIndex >= 0 &&\n !el.closest(\"[inert]\") &&\n (el.checkVisibility?.({ visibilityProperty: true }) ??\n getComputedStyle(el).visibility !== \"hidden\"),\n )\n}\n\nexport function ModalTransition({\n children,\n triggerLabel = \"Open modal\",\n closeLabel = \"Close\",\n dialogLabel = \"Modal\",\n className,\n}: {\n children?: React.ReactNode\n triggerLabel?: string\n closeLabel?: string\n /** Accessible name for the dialog. */\n dialogLabel?: string\n className?: string\n}) {\n useStyles()\n const scopeRef = React.useRef(null)\n const triggerRef = React.useRef(null)\n const dialogRef = React.useRef(null)\n const dialogId = `${React.useId()}-dialog`\n const [state, setState] = React.useState<\"closed\" | \"open\" | \"closing\">(\"closed\")\n\n React.useEffect(() => {\n if (state !== \"closing\") return\n // Return focus to the trigger. This runs after the open-state effect\n // below has removed its focus guard, so the guard cannot pull it back.\n triggerRef.current?.focus()\n const ms = readMs(scopeRef.current, \"--modal-close-dur\", 150)\n const id = window.setTimeout(() => setState(\"closed\"), ms)\n return () => window.clearTimeout(id)\n }, [state])\n\n // The dialog mounts only once it is opening, so it would paint the open\n // state on its first frame and skip the transition. Add .is-open a frame\n // later, the same way number-pop-in defers its entry.\n const [entered, setEntered] = React.useState(false)\n React.useEffect(() => {\n if (state !== \"open\") {\n setEntered(false)\n return\n }\n let inner = 0\n const outer = requestAnimationFrame(() => {\n inner = requestAnimationFrame(() => setEntered(true))\n })\n return () => {\n cancelAnimationFrame(outer)\n cancelAnimationFrame(inner)\n }\n }, [state])\n\n // aria-modal promises that the rest of the page is out of reach, so keep\n // that promise: move focus in, trap Tab, close on Escape, and pull focus\n // back if it lands outside.\n React.useEffect(() => {\n if (state !== \"open\") return\n const dialog = dialogRef.current\n if (!dialog) return\n const first = () => focusables(dialog)[0] ?? dialog\n first().focus()\n\n const onKeyDown = (e: KeyboardEvent) => {\n // A nested widget such as a menu handles its own Escape first.\n if (e.key === \"Escape\" && !e.defaultPrevented) {\n e.preventDefault()\n setState(\"closing\")\n return\n }\n if (e.key !== \"Tab\") return\n const items = focusables(dialog)\n if (items.length === 0) {\n e.preventDefault()\n dialog.focus()\n return\n }\n const active = document.activeElement\n const inside = dialog.contains(active)\n if (e.shiftKey && (!inside || active === items[0])) {\n e.preventDefault()\n items[items.length - 1].focus()\n } else if (!e.shiftKey && (!inside || active === items[items.length - 1])) {\n e.preventDefault()\n items[0].focus()\n }\n }\n const onFocusIn = (e: FocusEvent) => {\n if (!dialog.contains(e.target as Node)) first().focus()\n }\n document.addEventListener(\"keydown\", onKeyDown)\n document.addEventListener(\"focusin\", onFocusIn)\n return () => {\n document.removeEventListener(\"keydown\", onKeyDown)\n document.removeEventListener(\"focusin\", onFocusIn)\n }\n }, [state])\n\n return (\n
\n setState(\"open\")}\n className=\"rounded-md border border-border bg-card px-3 py-1.5 text-sm font-medium text-foreground hover:bg-muted\"\n >\n {triggerLabel}\n \n {state !== \"closed\" && (\n \n {children ?? (\n
\n
\n
\n
\n
\n
\n )}\n setState(\"closing\")}\n className=\"mt-4 rounded-md border border-border bg-background px-2.5 py-1 text-xs font-medium text-foreground hover:bg-muted\"\n >\n {closeLabel}\n \n
\n )}\n
\n )\n}\n", "type": "registry:ui" } ], "registryDependencies": [ "utils" ], "meta": { "shared": true, "source": { "name": "transitions.dev", "url": "https://github.com/Jakubantalik/transitions.dev", "license": "MIT", "author": "Jakub Antalík" } } }