{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "relative-date-label",
  "title": "Relative Date Label",
  "description": "A date field that writes the date back as today, tomorrow, in 3 days, last Friday or 2 weeks ago in the viewer's language, picking the unit that reads best. Use this when a date should be read at a glance as how far away it is, not only as a number.",
  "registryDependencies": [
    "@tealmac/date-field",
    "@tealmac/date-time-core"
  ],
  "files": [
    {
      "path": "registry/patterns/relative-date-label/relative-date-label.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { DateField } from \"@/components/ui/date-field\"\nimport {\n  diffDays,\n  resolveLocale,\n  today as todayISO,\n} from \"@/components/ui/date-time-core\"\n\nexport interface RelativeDateLabelProps {\n  value?: string | null\n  defaultValue?: string | null\n  onValueChange?: (value: string | null) => void\n  today?: string\n  locale?: string\n}\n\n/** \"tomorrow\", \"in 3 days\", \"2 weeks ago\", \"last year\": the unit that reads best. */\nexport function relativeDateLabel(\n  date: string,\n  today: string,\n  locale?: string\n) {\n  const n = diffDays(today, date)\n  const abs = Math.abs(n)\n  const rtf = new Intl.RelativeTimeFormat(resolveLocale(locale), {\n    numeric: \"auto\",\n  })\n  if (abs < 14) return rtf.format(n, \"day\")\n  if (abs < 60) return rtf.format(Math.round(n / 7), \"week\")\n  if (abs < 365) return rtf.format(Math.round(n / 30.44), \"month\")\n  return rtf.format(Math.round(n / 365.25), \"year\")\n}\n\nexport function RelativeDateLabel({\n  value,\n  defaultValue = null,\n  onValueChange,\n  today,\n  locale,\n}: RelativeDateLabelProps) {\n  const loc = resolveLocale(locale)\n  const now = today ?? todayISO()\n  const [inner, setInner] = React.useState<string | null>(defaultValue)\n  const date = value === undefined ? inner : value\n\n  return (\n    <div className=\"flex flex-wrap items-center gap-3\">\n      <DateField\n        label=\"Date\"\n        locale={loc}\n        value={date}\n        onValueChange={(next) => {\n          if (value === undefined) setInner(next)\n          onValueChange?.(next)\n        }}\n      />\n      <p\n        role=\"status\"\n        className=\"text-muted-foreground text-sm first-letter:uppercase\"\n      >\n        {date ? relativeDateLabel(date, now, loc) : \"\"}\n      </p>\n    </div>\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}