{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "live",
  "title": "Live",
  "description": "What shows that something is live, and the time it keeps: a pulse whose ring breathes while a value is live and fades once it stops, and `liveNow()`, the time now on the page’s clock, slowed where the page is slowed.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/lib/live.tsx",
      "content": "'use client'\n\nimport { PAGE_TIME } from '@/registry/lib/resize'\nimport type { ComponentProps } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * What shows that something is live, and the time it keeps. The pulse is a\n * loop that runs while a value is live, timed by `layout` at an even pace\n * (motion rule 8). The clock is the page's own time as a Web Animation\n * plays it (`PAGE_TIME`), so a live chart's scroll and the rows stamped by it\n * keep step when the playground's Speed slows the page.\n */\n\n/**\n * The clock's animation, made once for the page; the wall's time at its\n * start; and how far the wall's time was ahead of the page's own, which\n * only a device that slept, or a wall clock set, changes.\n */\nlet clock: { animation: Animation; epoch: number; wall: number } | null = null\n\n/** How far the wall's time is ahead of the page's own, which animations run on. */\nconst wallAhead = () => Date.now() - performance.now()\n\nfunction pageClock() {\n  if (clock && clock.animation.playState !== 'idle') return clock\n  const animation = document.documentElement.animate([], {\n    duration: Number.MAX_SAFE_INTEGER,\n    id: PAGE_TIME,\n  })\n  // Started on the page's time now, not on the next frame as a pending animation starts, which\n  // can come long after on a busy page and leave the clock behind what was timed from it before.\n  const started = Number(document.timeline.currentTime ?? performance.now())\n  animation.startTime = started\n  const wall = wallAhead()\n  clock = { animation, epoch: wall + started, wall }\n  return clock\n}\n\n/**\n * The time now, in ms since 1970, as the page's clock keeps it: the same\n * as `Date.now()`, but slowed where the page is slowed, such as in the\n * playground. Rows a live chart shows are stamped\n * with `Date.now()` or with this. On the server, `Date.now()`.\n */\nfunction liveNow(): number {\n  if (typeof document === 'undefined') return Date.now()\n  const now = pageClock()\n  // A device that slept skips the wall's time ahead of the page's: the clock skips with it.\n  const wall = wallAhead()\n  if (Math.abs(wall - now.wall) > 1000) {\n    now.epoch += wall - now.wall\n    now.wall = wall\n  }\n  // An animation's time moves only when a frame is drawn, and a hidden page draws none, so the\n  // clock runs on from the last frame at its own rate.\n  const sinceFrame =\n    performance.now() - Number(document.timeline.currentTime ?? performance.now())\n  return (\n    now.epoch +\n    Number(now.animation.currentTime ?? 0) +\n    Math.max(0, sinceFrame) * now.animation.playbackRate\n  )\n}\n\n/** How fast the page's clock runs: 1, or slower where the page is slowed. */\nfunction liveRate(): number {\n  if (typeof document === 'undefined') return 1\n  return pageClock().animation.playbackRate\n}\n\n/**\n * Calls `then` once `ms` have passed on the page's clock, so a wait slows\n * with the page, even when it is slowed part-way through it. Gives a function that\n * cancels it.\n */\nfunction afterPageTime(ms: number, then: () => void): () => void {\n  const wait = document.documentElement.animate([], {\n    duration: Math.max(0, ms),\n    id: PAGE_TIME,\n  })\n  wait.playbackRate = liveRate()\n  let cancelled = false\n  wait.finished.then(\n    () => {\n      if (!cancelled) then()\n    },\n    // Cancelling it rejects `finished`.\n    () => {},\n  )\n  return () => {\n    cancelled = true\n    wait.cancel()\n  }\n}\n\n/**\n * The pulse beside something live: a dot whose ring breathes out while it\n * is live, and fades once it isn't. Sized and coloured by its parent\n * (`size-2`, `text-success`).\n */\nfunction LivePulse({\n  live = true,\n  className,\n  ...props\n}: ComponentProps<'span'> & {\n  /** Whether the ring breathes; it fades where it is once off. */\n  live?: boolean\n}) {\n  return (\n    <span\n      aria-hidden\n      data-slot='live-pulse'\n      data-live={live ? '' : undefined}\n      className={cn('relative flex', className)}\n      {...props}\n    >\n      <span className='absolute inset-0 rounded-full bg-current opacity-0 transition-opacity duration-(--motion-exit-duration) ease-(--motion-exit-fade-easing) in-data-live:animate-[ping_calc(var(--motion-layout-duration)*6)_ease-in-out_infinite] in-data-live:opacity-60 motion-reduce:animate-none' />\n      <span className='relative size-full rounded-full bg-current forced-colors:bg-[CanvasText]' />\n    </span>\n  )\n}\n\nexport { afterPageTime, liveNow, LivePulse, liveRate }\n",
      "type": "registry:lib",
      "target": "@lib/live.tsx"
    }
  ],
  "type": "registry:lib"
}