{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "timeline-chart",
  "title": "Timeline chart",
  "description": "Mini Gantt whose bars grow from their start date on view, with progress fills, milestones, group headers, day / week / month ticks, a today marker and arrow-key rows.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/timeline-chart.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring, ease as easing } from \"@/lib/goodui-motion\"\n\nexport type TimelineRow = {\n  /** Defaults to the label. */\n  key?: string\n  label: string\n  /** ISO date, e.g. \"2026-11-02\". */\n  start: string\n  /** ISO date; omit for a milestone. */\n  end?: string\n  /** 0 to 1, fills the bar. */\n  progress?: number\n  /** Any CSS color. Defaults to the theme chart colors by group order (--chart-1 … --chart-5). */\n  color?: string\n  /** Rows with the same group sit under one header. */\n  group?: string\n  /** Draws a diamond on the start date instead of a bar. */\n  milestone?: boolean\n}\n\nexport type TimelineChartLabels = {\n  today: string\n  summary: (name: string, rows: number, from: string, to: string) => string\n  /** Announced for the active row. */\n  row: (label: string, start: string, end: string | null, days: string, progress: string | null) => string\n  days: (n: number) => string\n  milestone: string\n  columnTask: string\n  columnStart: string\n  columnEnd: string\n  columnProgress: string\n  noData: string\n}\nconst DEFAULT_LABELS: TimelineChartLabels = {\n  today: \"Today\",\n  summary: (name, rows, from, to) => `${name ? name + \": \" : \"\"}${rows} rows from ${from} to ${to}`,\n  row: (label, start, end, days, progress) => `${label}: ${end ? `${start} to ${end}, ${days}` : start}${progress ? `, ${progress}` : \"\"}`,\n  days: (n) => `${n} ${n === 1 ? \"day\" : \"days\"}`,\n  milestone: \"Milestone\",\n  columnTask: \"Task\",\n  columnStart: \"Start\",\n  columnEnd: \"End\",\n  columnProgress: \"Progress\",\n  noData: \"No data\",\n}\n\nexport type TimelineChartProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  rows: TimelineRow[]\n  /** Chart name for screen readers and the table caption. */\n  title?: React.ReactNode\n  /** First day shown (ISO). Defaults to the earliest start, minus a day. */\n  from?: string\n  /** Last day shown (ISO). Defaults to the latest end, plus a day. */\n  to?: string\n  /** ISO date of the today marker. Pass it in (no clock is read during render). */\n  today?: string\n  rowHeight?: number\n  /** Width of the label column in px. */\n  labelWidth?: number\n  /** First weekday for week ticks: 1 Monday (default) or 0 Sunday. */\n  weekStart?: 0 | 1\n  locale?: string\n  /** Formats dates for the tooltip and screen readers. */\n  formatDate?: (date: Date) => string\n  /** Seconds a bar takes to grow; bars stagger by 60 ms. */\n  duration?: number\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<TimelineChartLabels>\n}\n\nfunction useReduce() {\n  const prefers = useReducedMotion()\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  return mounted && !!prefers\n}\n\nfunction useInView(ref: React.RefObject<Element | null>, threshold = 0.2) {\n  const [inView, setInView] = React.useState(false)\n  React.useEffect(() => {\n    const el = ref.current\n    if (!el || inView) return\n    if (typeof IntersectionObserver === \"undefined\") {\n      setInView(true)\n      return\n    }\n    const io = new IntersectionObserver(\n      ([e]) => {\n        if (e?.isIntersecting) {\n          setInView(true)\n          io.disconnect()\n        }\n      },\n      { threshold },\n    )\n    io.observe(el)\n    return () => io.disconnect()\n  }, [ref, inView, threshold])\n  return inView\n}\n\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState<number | null>(null)\n  React.useLayoutEffect(() => {\n    const el = ref.current\n    if (!el) return\n    // A hidden parent (display:none) measures 0: keep the last real width instead of collapsing the chart.\n    const measure = () => {\n      const w = Math.round(el.clientWidth)\n      if (w > 0) setWidth(Math.max(60, w))\n    }\n    measure()\n    const ro = new ResizeObserver(measure)\n    ro.observe(el)\n    return () => ro.disconnect()\n  }, [ref])\n  return width\n}\n\nconst DAY = 86400000\nfunction parseDate(s: string) {\n  const m = /^(\\d{4})-(\\d{2})-(\\d{2})/.exec(s)\n  return m ? new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])) : new Date(s)\n}\nconst addDays = (d: Date, n: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n)\nconst daysBetween = (a: Date, b: Date) => Math.round((b.getTime() - a.getTime()) / DAY)\n\n/** Mini Gantt: rows of bars that grow from their start date on view, with progress fills, milestones, group headers, a today marker and arrow-key rows. */\nexport function TimelineChart({\n  rows,\n  title,\n  from: fromProp,\n  to: toProp,\n  today,\n  rowHeight = 36,\n  labelWidth = 128,\n  weekStart = 1,\n  locale = \"en-US\",\n  formatDate,\n  duration = 0.9,\n  labels,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: TimelineChartProps) {\n  const l = useLabels(\"timeline-chart\", DEFAULT_LABELS, labels)\n  const reduce = useReduce()\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const trackRef = React.useRef<HTMLDivElement>(null)\n  const inView = useInView(rootRef)\n  const trackW = useWidth(trackRef)\n  const [active, setActive] = React.useState<number | null>(null)\n  const dateFmt = React.useMemo(() => formatDate ?? ((d: Date) => new Intl.DateTimeFormat(locale, { month: \"short\", day: \"numeric\" }).format(d)), [formatDate, locale])\n  const pct = React.useMemo(() => new Intl.NumberFormat(locale, { style: \"percent\", maximumFractionDigits: 0 }), [locale])\n\n  const items = rows.map((r, i) => {\n    const start = parseDate(r.start)\n    const end = r.end && !r.milestone ? parseDate(r.end) : null\n    return { ...r, key: r.key ?? r.label, index: i, start, end }\n  })\n  const starts = items.map((r) => r.start.getTime())\n  const ends = items.map((r) => (r.end ?? r.start).getTime())\n  const from = fromProp ? parseDate(fromProp) : items.length ? addDays(new Date(Math.min(...starts)), -1) : new Date(2026, 0, 1)\n  const to = toProp ? parseDate(toProp) : items.length ? addDays(new Date(Math.max(...ends)), 2) : addDays(from, 30)\n  const span = Math.max(1, to.getTime() - from.getTime())\n  const posOf = (d: Date) => Math.min(100, Math.max(0, ((d.getTime() - from.getTime()) / span) * 100))\n  const spanDays = span / DAY\n\n  // Ticks: days, week starts or month starts, thinned until labels have room.\n  const ticks: { date: Date; text: string }[] = []\n  const unit = spanDays <= 21 ? \"day\" : spanDays <= 130 ? \"week\" : \"month\"\n  const tickFmt = new Intl.DateTimeFormat(locale, unit === \"month\" ? { month: \"short\" } : unit === \"day\" ? { day: \"numeric\" } : { month: \"short\", day: \"numeric\" })\n  const monthDay = new Intl.DateTimeFormat(locale, { month: \"short\", day: \"numeric\" })\n  for (let d = new Date(from.getFullYear(), from.getMonth(), from.getDate()); d <= to; d = unit === \"month\" ? new Date(d.getFullYear(), d.getMonth() + 1, 1) : addDays(d, 1)) {\n    if (unit === \"week\" && d.getDay() !== weekStart) continue\n    if (unit === \"month\" && d.getDate() !== 1) {\n      d = new Date(d.getFullYear(), d.getMonth() + 1, 1)\n      if (d > to) break\n    }\n    ticks.push({ date: d, text: unit === \"day\" && (d.getDate() === 1 || ticks.length === 0) ? monthDay.format(d) : tickFmt.format(d) })\n  }\n  const pxPerTick = trackW && ticks.length > 1 ? (trackW * (posOf(ticks[1]?.date ?? to) - posOf(ticks[0]?.date ?? from))) / 100 : 999\n  const every = pxPerTick < 44 ? Math.ceil(44 / Math.max(1, pxPerTick)) : 1\n\n  // Groups keep their first-seen order; rows without a group come first.\n  const groups: { name: string | undefined; rows: typeof items }[] = []\n  for (const r of items) {\n    const g = groups.find((x) => x.name === r.group)\n    if (g) g.rows.push(r)\n    else groups.push({ name: r.group, rows: [r] })\n  }\n  const colorOf = (r: (typeof items)[number]) => r.color ?? `var(--chart-${((groups.findIndex((g) => g.name === r.group) + 5) % 5) + 1})`\n  const todayDate = today ? parseDate(today) : null\n  const showToday = todayDate && todayDate >= from && todayDate <= to\n\n  const name = typeof title === \"string\" ? title : \"\"\n  const summary = ariaLabel ?? (items.length ? l.summary(name, items.length, dateFmt(from), dateFmt(to)) : l.noData)\n  const act = active !== null ? items[active] : undefined\n  const rowText = (r: (typeof items)[number]) =>\n    l.row(r.label, dateFmt(r.start), r.end ? dateFmt(r.end) : null, r.end ? l.days(daysBetween(r.start, r.end) + 1) : l.milestone, r.progress !== undefined && r.end ? pct.format(r.progress) : null)\n  const announce = act ? rowText(act) : \"\"\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (!items.length) return\n    if ([\"ArrowUp\", \"ArrowDown\", \"ArrowLeft\", \"ArrowRight\"].includes(e.key)) {\n      e.preventDefault()\n      const dir = e.key === \"ArrowUp\" || e.key === \"ArrowLeft\" ? -1 : 1\n      setActive((i) => Math.min(items.length - 1, Math.max(0, (i ?? (dir > 0 ? -1 : items.length)) + dir)))\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault()\n      setActive(e.key === \"Home\" ? 0 : items.length - 1)\n    } else if (e.key === \"Escape\") setActive(null)\n  }\n\n  const ready = inView || reduce\n  const tipLeft = act ? posOf(act.start) : 0\n  const tipRight = tipLeft > 55\n  let order = 0\n\n  return (\n    <div ref={rootRef} className={cn(\"pc-timeline-chart flex w-full min-w-0 flex-col gap-2\", className)} {...props}>\n      <GooduiTokens />\n      <div\n        role=\"group\"\n        aria-roledescription=\"timeline\"\n        aria-label={summary}\n        tabIndex={0}\n        onKeyDown={onKeyDown}\n        onFocus={() => setActive((i) => i ?? (items.length ? 0 : null))}\n        onBlur={() => setActive(null)}\n        onPointerLeave={() => setActive(null)}\n        className=\"relative grid rounded-md outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n        style={{ gridTemplateColumns: `${labelWidth}px minmax(0,1fr)` }}\n      >\n        <div className=\"h-6\" />\n        <div ref={trackRef} dir=\"ltr\" className=\"relative h-6\">\n          {ticks.map((t, i) =>\n            i % every === 0 && !(showToday && todayDate && (Math.abs(posOf(t.date) - posOf(todayDate)) * (trackW ?? 0)) / 100 < 34) ? (\n              <span key={i} className=\"absolute top-1 -translate-x-1/2 text-[10px] whitespace-nowrap text-muted-foreground\" style={{ left: `${posOf(t.date)}%` }}>\n                {t.text}\n              </span>\n            ) : null,\n          )}\n          {showToday && todayDate && (\n            <span className=\"absolute top-0.5 z-[1] -translate-x-1/2 rounded-full bg-primary px-1.5 text-[10px] leading-4 font-medium text-primary-foreground\" style={{ left: `${posOf(todayDate)}%` }}>\n              {l.today}\n            </span>\n          )}\n        </div>\n        {groups.map((g) => (\n          <React.Fragment key={g.name ?? \"__none\"}>\n            {g.name !== undefined && (\n              <>\n                <div className=\"flex h-7 items-end pb-1 text-[11px] font-semibold tracking-wide text-muted-foreground uppercase\">{g.name}</div>\n                <div className=\"h-7\" />\n              </>\n            )}\n            {g.rows.map((r) => {\n              const i = r.index\n              const k = order++\n              const isActive = active === i\n              const left = posOf(r.start)\n              const widthPct = r.end ? Math.max(0.8, posOf(addDays(r.end, 1)) - left) : 0\n              const barPx = trackW ? (trackW * widthPct) / 100 : 0\n              const color = colorOf(r)\n              return (\n                <React.Fragment key={r.key}>\n                  <div\n                    onPointerEnter={() => setActive(i)}\n                    className={cn(\"flex items-center truncate rounded-s-md pe-3 text-sm transition-colors\", isActive ? \"bg-muted/60 font-medium\" : \"text-foreground/85\")}\n                    style={{ height: rowHeight }}\n                  >\n                    <span className=\"truncate\">{r.label}</span>\n                  </div>\n                  <div dir=\"ltr\" onPointerEnter={() => setActive(i)} className={cn(\"relative rounded-e-md transition-colors\", isActive && \"bg-muted/60\")} style={{ height: rowHeight }}>\n                    {ticks.map((t, ti) => (\n                      <span key={ti} className=\"absolute inset-y-0 w-px bg-border\" style={{ left: `${posOf(t.date)}%` }} />\n                    ))}\n                    {r.end ? (\n                      <motion.div\n                        className=\"absolute top-1/2 h-[22px] origin-left overflow-hidden rounded-[5px]\"\n                        style={{ left: `${left}%`, width: `${widthPct}%`, y: \"-50%\", background: `color-mix(in oklab, ${color} 26%, var(--card))` }}\n                        initial={false}\n                        animate={{ scaleX: ready ? 1 : 0 }}\n                        transition={reduce ? { duration: 0 } : { duration, delay: k * 0.06, ease: easing.outExpo }}\n                      >\n                        {r.progress !== undefined && <span className=\"absolute inset-y-0 left-0 rounded-[5px]\" style={{ width: `${Math.min(1, Math.max(0, r.progress)) * 100}%`, background: color }} />}\n                        {r.progress !== undefined && barPx >= 52 && (\n                          <span className=\"absolute inset-y-0 right-1.5 flex items-center\">\n                            <span className=\"rounded-sm bg-card/85 px-1 text-[10px] leading-4 font-medium text-foreground/85 tabular-nums\">{pct.format(r.progress)}</span>\n                          </span>\n                        )}\n                      </motion.div>\n                    ) : (\n                      <motion.span\n                        className=\"absolute top-1/2 size-3 -translate-x-1/2 rotate-45 rounded-[2px]\"\n                        style={{ left: `${left}%`, y: \"-50%\", background: color }}\n                        initial={false}\n                        animate={{ scale: ready ? 1 : 0, rotate: 45 }}\n                        transition={reduce ? { duration: 0 } : { ...spring.play, delay: k * 0.06 + 0.2 }}\n                      />\n                    )}\n                  </div>\n                </React.Fragment>\n              )\n            })}\n          </React.Fragment>\n        ))}\n        {showToday && todayDate && (\n          <span className=\"pointer-events-none absolute top-6 bottom-0 w-px bg-primary\" style={{ left: `calc(${labelWidth}px + (100% - ${labelWidth}px) * ${posOf(todayDate) / 100})` }} aria-hidden=\"true\" />\n        )}\n        {items.length === 0 && <div className=\"col-span-2 grid h-24 place-items-center text-sm text-muted-foreground\">{l.noData}</div>}\n        {act && (\n          <div\n            className=\"pointer-events-none absolute z-10 flex flex-col gap-0.5 rounded-md border bg-popover px-2.5 py-1.5 text-xs whitespace-nowrap text-popover-foreground shadow-[var(--pc-e2)]\"\n            style={{ left: `calc(${labelWidth}px + (100% - ${labelWidth}px) * ${tipLeft / 100})`, top: 24, transform: `translateX(${tipRight ? \"calc(-100% - 8px)\" : \"8px\"})` }}\n          >\n            <span className=\"flex items-center gap-1.5 font-semibold\">\n              <span className=\"inline-block size-2 rounded-[2px]\" style={{ background: colorOf(act) }} />\n              {act.label}\n            </span>\n            <span className=\"text-muted-foreground\">{act.end ? `${dateFmt(act.start)} – ${dateFmt(act.end)} · ${l.days(daysBetween(act.start, act.end) + 1)}` : `${dateFmt(act.start)} · ${l.milestone}`}</span>\n            {act.progress !== undefined && act.end && <span className=\"tabular-nums\">{pct.format(act.progress)}</span>}\n          </div>\n        )}\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {announce}\n        </span>\n      </div>\n      {items.length > 0 && (\n        <div className=\"sr-only\">\n          <table>\n          <caption>{summary}</caption>\n          <thead>\n            <tr>\n              <th scope=\"col\">{l.columnTask}</th>\n              <th scope=\"col\">{l.columnStart}</th>\n              <th scope=\"col\">{l.columnEnd}</th>\n              <th scope=\"col\">{l.columnProgress}</th>\n            </tr>\n          </thead>\n          <tbody>\n            {items.map((r) => (\n              <tr key={r.key}>\n                <th scope=\"row\">{r.label}</th>\n                <td>{dateFmt(r.start)}</td>\n                <td>{r.end ? dateFmt(r.end) : l.milestone}</td>\n                <td>{r.progress !== undefined && r.end ? pct.format(r.progress) : \"\"}</td>\n              </tr>\n            ))}\n          </tbody>\n        </table>\n        </div>\n      )}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/timeline-chart.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}