{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "relative-time-card",
  "title": "RelativeTimeCard",
  "description": "\"2 hours ago\", with the exact local and UTC time on hover or focus.",
  "registryDependencies": [
    "https://creance.pages.dev/r/context-card.json",
    "https://creance.pages.dev/r/creance-base.json",
    "https://creance.pages.dev/r/description.json"
  ],
  "files": [
    {
      "path": "relative-time-card/RelativeTimeCard.css",
      "content": ".cr-relative-time {\n  border-radius: 4px;\n  text-decoration: underline dotted;\n  text-underline-offset: 3px;\n  cursor: help;\n}\n\n.cr-relative-time:focus-visible {\n  outline: var(--cr-focus-width) solid var(--cr-color-focus);\n  outline-offset: var(--cr-focus-offset);\n}\n",
      "type": "registry:file",
      "target": "@components/creance/relative-time-card/RelativeTimeCard.css"
    },
    {
      "path": "relative-time-card/RelativeTimeCard.tsx",
      "content": "'use client';\n\nimport { useNow } from '@/lib/creance/useNow';\nimport { cx } from '@/lib/creance/cx';\nimport { Description } from '@/components/creance/description/Description';\nimport { ContextCard } from '@/components/creance/context-card/ContextCard';\nimport './RelativeTimeCard.css';\n\nexport interface RelativeTimeCardProps {\n  date: Date;\n  /** BCP 47 locale. Defaults to the browser's. */\n  locale?: string;\n  /** Override \"now\", for tests and screenshots. */\n  now?: Date;\n  className?: string;\n}\n\nconst units: [Intl.RelativeTimeFormatUnit, number][] = [\n  ['year', 31_536_000],\n  ['month', 2_592_000],\n  ['week', 604_800],\n  ['day', 86_400],\n  ['hour', 3_600],\n  ['minute', 60],\n  ['second', 1],\n];\n\n/** \"2 hours ago\", in the person's language. */\nexport function formatRelative(date: Date, now: Date, locale?: string) {\n  const seconds = Math.round((date.getTime() - now.getTime()) / 1000);\n  const [unit, size] = units.find(([, s]) => Math.abs(seconds) >= s) ?? ['second', 1];\n  return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(Math.round(seconds / size), unit);\n}\n\n/**\n * A relative time that updates every minute. Hover or focus it to see the exact time in your\n * time zone and in UTC. The `<time>` element carries the machine-readable date.\n */\nexport function RelativeTimeCard({ date, locale, now, className }: RelativeTimeCardProps) {\n  const tick = useNow(60_000, !now);\n  const current = now ?? (tick === undefined ? undefined : new Date(tick));\n  const exact = (timeZone?: string) =>\n    new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone }).format(date);\n\n  return (\n    <ContextCard\n      content={\n        <Description\n          items={[\n            { term: 'Your time', details: exact() },\n            { term: 'UTC', details: exact('UTC') },\n            ...(current ? [{ term: 'Relative', details: formatRelative(date, current, locale) }] : []),\n          ]}\n        />\n      }\n    >\n      {/* Focusable so keyboard users can reveal the exact time, the same way hover does. */}\n      {/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}\n      <time dateTime={date.toISOString()} tabIndex={0} className={cx('cr-relative-time', className)}>\n        {/* Until mounted the current time is unknown, so the exact UTC time stands in. */}\n        {current ? formatRelative(date, current, locale) : exact('UTC')}\n      </time>\n    </ContextCard>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/creance/relative-time-card/RelativeTimeCard.tsx"
    }
  ],
  "categories": [
    "molecule",
    "overlays"
  ],
  "type": "registry:ui"
}