{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-today",
  "type": "registry:lib",
  "title": "Today hook",
  "description": "The current date, stable between server and client renders.",
  "categories": [
    "foundation"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/lib/use-today.ts",
      "content": "import { useMemo, useSyncExternalStore } from \"react\";\n\n/** Shared by calendar and date-range-picker, which both re-export it, so one copy keeps their \"today\" in step. */\nconst dateKey = (date: Date) =>\n  `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, \"0\")}-${String(date.getDate()).padStart(2, \"0\")}`;\nconst fromKey = (key: string) => {\n  const [year = 1970, month = 1, day = 1] = key.split(\"-\").map(Number);\n  return new Date(year, month - 1, day);\n};\n\n/** Today turns over at local midnight; returning to the tab, focusing the window or waking the device reads it again. */\nconst subscribeToday = (notify: () => void) => {\n  let timer = 0;\n  const schedule = () => {\n    const now = new Date();\n    const nextMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);\n    timer = window.setTimeout(\n      () => {\n        notify();\n        schedule();\n      },\n      nextMidnight.getTime() - now.getTime() + 1000,\n    );\n  };\n  const onVisible = () => {\n    if (document.visibilityState === \"visible\") notify();\n  };\n  schedule();\n  document.addEventListener(\"visibilitychange\", onVisible);\n  window.addEventListener(\"focus\", notify);\n  return () => {\n    window.clearTimeout(timer);\n    document.removeEventListener(\"visibilitychange\", onVisible);\n    window.removeEventListener(\"focus\", notify);\n  };\n};\nconst readToday = () => dateKey(new Date());\nconst serverToday = () => \"\";\n\n/** The viewer's local date. Undefined on the server and during hydration, so markup never depends on the server clock or time zone; afterwards it follows the real date across midnight. */\nexport function useToday() {\n  const key = useSyncExternalStore(subscribeToday, readToday, serverToday);\n  return useMemo(() => (key ? fromKey(key) : undefined), [key]);\n}\n",
      "type": "registry:lib",
      "target": "@components/iris/lib/use-today.ts"
    }
  ],
  "docs": "Docs and live preview: https://irisui.dev/components/use-today",
  "meta": {
    "kind": "lib",
    "docs": "https://irisui.dev/components/use-today"
  }
}
