{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dialog-route-hash",
  "title": "Dialog Route Hash",
  "description": "A dialog whose open state is the URL fragment: a link to #help opens it, Back closes it, loading the page with the fragment shows it and focus returns to the opener.",
  "registryDependencies": [
    "dialog"
  ],
  "files": [
    {
      "path": "registry/patterns/dialog-route-hash/dialog-route-hash.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport {\n  Dialog,\n  DialogContent,\n  DialogDescription,\n  DialogHeader,\n  DialogTitle,\n} from \"@/components/ui/dialog\"\n\nexport interface DialogRouteHashProps {\n  /** The fragment that opens it, without the #. Default \"dialog\". */\n  hash?: string\n  title: string\n  description?: string\n  children: React.ReactNode\n  /** The element to return focus to when it closes. Default: the trigger that set the hash. */\n  returnFocusTo?: React.RefObject<HTMLElement | null>\n}\n\n/**\n * A dialog whose open state is the URL fragment: `#help` opens it, closing it\n * clears the fragment, and the Back button closes it (instead of leaving the\n * page), because opening pushes a history entry. A page loaded with the\n * fragment shows it straight away. Use `openRouteDialog(hash)` or a plain link\n * to `#help` to open it.\n */\nexport function DialogRouteHash({\n  hash = \"dialog\",\n  title,\n  description,\n  children,\n  returnFocusTo,\n}: DialogRouteHashProps) {\n  const [open, setOpen] = React.useState(false)\n  const opener = React.useRef<HTMLElement | null>(null)\n\n  React.useEffect(() => {\n    const sync = () => {\n      const match = window.location.hash === `#${hash}`\n      setOpen((was) => {\n        if (match && !was)\n          opener.current = document.activeElement as HTMLElement | null\n        return match\n      })\n    }\n    sync()\n    window.addEventListener(\"hashchange\", sync)\n    window.addEventListener(\"popstate\", sync)\n    return () => {\n      window.removeEventListener(\"hashchange\", sync)\n      window.removeEventListener(\"popstate\", sync)\n    }\n  }, [hash])\n\n  function close() {\n    if (window.location.hash === `#${hash}`) window.history.back()\n    else setOpen(false)\n  }\n\n  return (\n    <Dialog open={open} onOpenChange={(next) => !next && close()}>\n      <DialogContent\n        onCloseAutoFocus={(event) => {\n          event.preventDefault()\n          ;(returnFocusTo?.current ?? opener.current)?.focus()\n        }}\n      >\n        <DialogHeader>\n          <DialogTitle>{title}</DialogTitle>\n          {description ? (\n            <DialogDescription>{description}</DialogDescription>\n          ) : null}\n        </DialogHeader>\n        {children}\n      </DialogContent>\n    </Dialog>\n  )\n}\n\n/** Opens a route dialog by pushing its fragment, so Back closes it. */\nexport function openRouteDialog(hash = \"dialog\") {\n  window.location.hash = hash\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}