{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "roomy",
  "title": "Roomy",
  "description": "Whether the screen is wider than a phone, so a part can show less, or open as a bottom sheet, where there is no room.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/lib/roomy.ts",
      "content": "import { useLayoutEffect, useState, useSyncExternalStore } from 'react'\n\n/*\n * Whether there is room. The screen: wider than a phone, 40rem, Tailwind's\n * `sm`; below it a Date picker shows one month and a User menu opens as a\n * bottom sheet. Rendered on the server as roomy, since most first loads are\n * on a desk. A container: narrower than a width in rem, for a part whose\n * container query lays it out and whose behaviour has to follow, such as\n * the Sidebar's Drawer and the AI chat popup's window that fills its\n * container.\n */\n\nconst ROOMY = '(min-width: 40rem)'\n\nfunction watchRoom(change: () => void) {\n  const query = window.matchMedia(ROOMY)\n  query.addEventListener('change', change)\n  return () => query.removeEventListener('change', change)\n}\n\nfunction isRoomy() {\n  return window.matchMedia(ROOMY).matches\n}\n\n/** True on a screen wider than a phone, and on the server. */\nexport function useRoomy(): boolean {\n  return useSyncExternalStore(watchRoom, isRoomy, () => true)\n}\n\n/** Whether a box `width` px wide is narrower than `rem` rem, at a root font size of `rootFontSize` px, as a container query in rem reads it. */\nexport function narrowerThan(width: number, rem: number, rootFontSize: number): boolean {\n  return width < rem * rootFontSize\n}\n\n/**\n * Whether `element` is narrower than `rem` rem, followed as it resizes and\n * read before the first paint. False until there is an element.\n */\nexport function useNarrowerThan(element: HTMLElement | null, rem: number): boolean {\n  const [narrow, setNarrow] = useState(false)\n  useLayoutEffect(() => {\n    if (!element) return\n    const measure = () => {\n      const rootFontSize = Number.parseFloat(\n        getComputedStyle(document.documentElement).fontSize,\n      )\n      setNarrow(narrowerThan(element.getBoundingClientRect().width, rem, rootFontSize))\n    }\n    measure()\n    const observer = new ResizeObserver(measure)\n    observer.observe(element)\n    return () => observer.disconnect()\n  }, [element, rem])\n  return narrow\n}\n",
      "type": "registry:lib",
      "target": "@lib/roomy.ts"
    }
  ],
  "type": "registry:lib"
}