{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pace","type":"registry:component","title":"Pace","description":"A goal for the month and whether you will make it: the running total climbs toward a goal line, the rest of the month is drawn at this pace, and the verdict says when.","author":"Hairline UI","dependencies":[],"registryDependencies":["https://hairlineui.com/r/number-roll.json?as=dependency"],"files":[{"path":"registry/pace.tsx","type":"registry:component","content":"// Hairline UI · pace · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n\"use client\";\n\nimport * as React from \"react\";\nimport { NumberRoll } from \"./number-roll\";\n\n/* ─────────────────────────────────────────────────────────\n * PACE: a goal for the month, and whether you'll make it\n *\n *   arrive    in three beats: the days so far rise one after\n *             another, each a column of hairlines as tall as the\n *             running total; then the rest of the month draws in\n *             ahead of today in finer lines, where this pace takes\n *             you; last, from the day the pace crosses the goal, the\n *             lines above the goal line light green, and the verdict\n *             comes in\n *   point     the day under the pointer lights and its neighbours\n *             fall away; a readout says what that day added and the\n *             total so far, or for a day ahead, where you'll be by\n *             then at this rate\n *   goal      a new target glides the goal line to its place and\n *             the verdict changes in place: on pace, reached, or\n *             short by how much\n *\n * Columns are masked stacks of 2px hairlines (finer 1px lines for\n * what's projected), faint where empty, so the month is one ruled\n * field the total climbs through. Arrow keys read each day.\n * ───────────────────────────────────────────────────────── */\n\nexport interface PaceDay {\n  key: string;\n  /** In the readout, e.g. \"Tue 18 Nov\". */\n  label: string;\n  /** Under the column, e.g. \"18\". */\n  tick?: string;\n  /** What the day added. Leave it out for days still to come. */\n  value?: number;\n}\n\nexport interface PaceProps extends React.HTMLAttributes<HTMLDivElement> {\n  /** Every day of the period, in order; days so far carry a value. */\n  days: PaceDay[];\n  target: number;\n  title?: string;\n  /** e.g. \"November\". Names the period in the verdict. */\n  period?: string;\n  format?: Intl.NumberFormatOptions;\n  locales?: string | string[];\n  /** Any CSS colour. Defaults to your first chart colour. */\n  color?: string;\n}\n\nconst EASE = \"cubic-bezier(0.16,1,0.3,1)\";\nconst FOCUS = \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/40\";\n/** The mark: 2px hairlines every 4px, counted up from the baseline. */\nconst RULE = \"repeating-linear-gradient(to top, #000 0 2px, transparent 2px 4px)\";\n/** What's projected: the same rhythm in 1px lines. */\nconst FINE = \"repeating-linear-gradient(to top, #000 0 1px, transparent 1px 4px)\";\nconst HEADROOM = 34;\n\nconst reducedQuery = \"(prefers-reduced-motion: reduce)\";\nconst subscribeReduced = (onChange: () => void) => {\n  const query = window.matchMedia(reducedQuery);\n  query.addEventListener(\"change\", onChange);\n  return () => query.removeEventListener(\"change\", onChange);\n};\nconst useReducedMotion = () =>\n  React.useSyncExternalStore(subscribeReduced, () => window.matchMedia(reducedQuery).matches, () => false);\n\n// Formatting waits for hydration: the server and the hydrating render share one fixed locale, then the visitor's takes over.\nconst subscribeNothing = () => () => {};\nconst useHydrated = () => React.useSyncExternalStore(subscribeNothing, () => true, () => false);\n\nfunction niceMax(value: number) {\n  if (value <= 0) return 1;\n  const step = 10 ** Math.floor(Math.log10(value));\n  for (const m of [1, 1.2, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]) if (m * step >= value) return m * step;\n  return 10 * step;\n}\n\nexport function Pace({\n  days,\n  target,\n  title = \"Revenue this month\",\n  period = \"the month\",\n  format,\n  locales,\n  color = \"var(--chart-1, var(--primary))\",\n  className = \"\",\n  ...props\n}: PaceProps) {\n  const reduced = useReducedMotion();\n  const hydrated = useHydrated();\n  const locale = locales ?? (hydrated ? undefined : \"en-US\");\n  const n = days.length;\n\n  // Days so far are the leading run with a value; the rest is ahead.\n  let elapsed = 0;\n  while (elapsed < n && typeof days[elapsed].value === \"number\") elapsed++;\n  const totals: number[] = [];\n  let run = 0;\n  for (let i = 0; i < elapsed; i++) totals.push((run += Math.max(0, days[i].value ?? 0)));\n  const sofar = run;\n  const rate = elapsed ? sofar / elapsed : 0;\n  for (let i = elapsed; i < n; i++) totals.push(sofar + rate * (i - elapsed + 1));\n  const forecast = n ? totals[n - 1] : 0;\n  const max = niceMax(Math.max(target, forecast, 1) * 1.04);\n  const hit = totals.findIndex((t) => t >= target);\n  const reached = hit >= 0 && hit < elapsed;\n  const onPace = hit >= 0;\n\n  const [visible, setVisible] = React.useState(false);\n  const [active, setActive] = React.useState<number | null>(null);\n  const [keyboard, setKeyboard] = React.useState(false);\n  const [plotH, setPlotH] = React.useState(160);\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const plotRef = React.useRef<HTMLDivElement>(null);\n  const colsRef = React.useRef<HTMLDivElement>(null);\n  const barH = Math.max(0, plotH - HEADROOM);\n\n  React.useLayoutEffect(() => {\n    const el = plotRef.current;\n    if (!el) return;\n    const measure = () => setPlotH((h) => (el.offsetHeight && el.offsetHeight !== h ? el.offsetHeight : h));\n    measure();\n    const observer = new ResizeObserver(measure);\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, []);\n\n  React.useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const observer = new IntersectionObserver(\n      ([entry]) => {\n        if (entry.isIntersecting) {\n          setVisible(true);\n          observer.disconnect();\n        }\n      },\n      { rootMargin: \"0px 0px -10% 0px\" },\n    );\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, []);\n\n  const pointAt = (clientX: number) => {\n    const r = colsRef.current?.getBoundingClientRect();\n    if (!r || !n) return null;\n    return Math.min(n - 1, Math.max(0, Math.floor(((clientX - r.left) / r.width) * n)));\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (!n) return;\n    const from = active ?? Math.max(0, elapsed - 1);\n    const moves: Record<string, number> = { ArrowLeft: from - 1, ArrowRight: from + 1, Home: 0, End: n - 1 };\n    if (e.key === \"Escape\") return setActive(null);\n    if (!(e.key in moves)) return;\n    e.preventDefault();\n    setKeyboard(true);\n    setActive(Math.min(n - 1, Math.max(0, active === null ? from : moves[e.key])));\n  };\n\n  const fmt = (v: number) => new Intl.NumberFormat(locale, format).format(v);\n  const short = (v: number) => new Intl.NumberFormat(locale, { ...format, notation: \"compact\", maximumFractionDigits: 1 }).format(v);\n  const slot = n ? 100 / n : 100;\n  const every = n > 16 ? 5 : 1;\n  const goalY = HEADROOM + (1 - target / max) * barH;\n\n  // Three beats on arrival: days so far, then the projection, then the goal day.\n  const step = Math.min(28, 640 / Math.max(1, n));\n  const pastEnd = elapsed * step + 520;\n  const aheadEnd = pastEnd + (n - elapsed) * step + 420;\n\n  const verdict = reached\n    ? `Goal reached on ${days[hit].label}`\n    : onPace\n      ? `On pace to reach it on ${days[hit].label}`\n      : `Short by ${short(target - forecast)} at this pace`;\n\n  const readout =\n    active === null\n      ? null\n      : active < elapsed\n        ? { label: days[active].label, value: `${fmt(days[active].value ?? 0)} · ${short(totals[active])} so far` }\n        : { label: `By ${days[active].label}`, value: `${short(totals[active])} at this pace` };\n\n  return (\n    <div ref={rootRef} className={`w-full rounded-[22px] bg-background shadow-[0_0_0_1px_var(--border)] ${className}`} {...props}>\n      {/* Its natural width where the parent sizes to its content (a centred flex column), without forcing it on narrow screens. */}\n      <div aria-hidden=\"true\" data-natural-width className=\"grid h-0\" style={{ gridTemplateColumns: \"minmax(0, 600px)\" }} />\n      <div className=\"@container w-full\">\n        <div className=\"p-4 @md:p-5 [--plot:170px] @md:[--plot:200px]\">\n          <p className=\"text-[12px] text-muted-foreground @md:text-[13px]\">{title}</p>\n          <div className=\"mt-0.5 flex flex-wrap items-baseline gap-x-2\">\n            <p className=\"text-[22px] font-medium leading-tight tracking-[-0.03em] text-foreground tabular-nums @md:text-[26px]\">\n              <NumberRoll value={visible ? sofar : 0} format={format} locales={locale} duration={1000} />\n            </p>\n            <p className=\"text-[13px] text-muted-foreground tabular-nums @md:text-[14px]\">of {fmt(target)}</p>\n          </div>\n          <p\n            className={`mt-1 inline-flex items-center gap-1.5 text-[11.5px] font-medium @md:text-[12.5px] ${\n              onPace ? \"text-emerald-600 dark:text-emerald-400\" : \"text-amber-600 dark:text-amber-400\"\n            }`}\n            style={{ opacity: visible ? 1 : 0, transition: reduced ? \"none\" : `opacity 420ms ${EASE} ${aheadEnd}ms` }}\n          >\n            <span aria-hidden=\"true\" className={`size-1.5 rounded-full ${onPace ? \"bg-emerald-500\" : \"bg-amber-500\"}`} />\n            {verdict}\n          </p>\n\n          <div\n            ref={plotRef}\n            role=\"group\"\n            tabIndex={0}\n            aria-label={`${title}: ${fmt(sofar)} of ${fmt(target)}. ${verdict}. Use the arrow keys to read each day.`}\n            onKeyDown={onKeyDown}\n            onBlur={() => keyboard && (setActive(null), setKeyboard(false))}\n            onPointerMove={(e) => {\n              if (e.pointerType === \"touch\" && e.buttons === 0) return;\n              setKeyboard(false);\n              setActive(pointAt(e.clientX));\n            }}\n            onPointerDown={(e) => setActive(pointAt(e.clientX))}\n            onPointerLeave={(e) => e.pointerType === \"mouse\" && setActive(null)}\n            className={`relative mt-4 touch-pan-y select-none rounded-lg @md:mt-5 ${FOCUS}`}\n            style={{ height: `calc(var(--plot) + ${HEADROOM}px)` }}\n          >\n            <div ref={colsRef} className=\"absolute inset-0\">\n              {days.map((d, i) => {\n                const past = i < elapsed;\n                const h = (totals[i] / max) * barH;\n                // Every day the total is over the goal lights its lines above the goal line.\n                const overGoal = Math.max(0, h - (target / max) * barH);\n                const delay = past ? i * step : pastEnd + (i - elapsed) * step;\n                const dim = active === null ? 1 : Math.max(0.35, 1 - Math.abs(i - active) * 0.16);\n                return (\n                  <div\n                    key={d.key}\n                    aria-hidden=\"true\"\n                    className=\"absolute bottom-0\"\n                    style={{ left: `${i * slot}%`, width: `${slot}%`, height: barH, opacity: dim, transition: reduced ? \"none\" : `opacity 240ms ${EASE}` }}\n                  >\n                    <div\n                      className=\"absolute inset-y-0 left-1/2 w-[min(14px,62%)] -translate-x-1/2\"\n                      style={{ maskImage: past ? RULE : FINE, WebkitMaskImage: past ? RULE : FINE }}\n                    >\n                      <div className=\"absolute inset-0 bg-foreground/[0.07]\" />\n                      <div\n                        className=\"absolute inset-x-0 bottom-0 overflow-clip\"\n                        style={{\n                          height: visible ? h : 0,\n                          background: color,\n                          opacity: past ? 1 : 0.7,\n                          transition: reduced ? \"none\" : `height 620ms ${EASE} ${delay}ms`,\n                        }}\n                      >\n                        {overGoal > 0 && (\n                          <div\n                            className=\"absolute inset-x-0 top-0 bg-emerald-500 dark:bg-emerald-400\"\n                            style={{ height: overGoal, opacity: visible ? 1 : 0, transition: reduced ? \"none\" : `opacity 360ms ${EASE} ${aheadEnd - 260 + Math.max(0, i - hit) * 40}ms` }}\n                          />\n                        )}\n                      </div>\n                    </div>\n                    {d.tick && (n - 1 - i) % every === 0 && (\n                      <span\n                        className={`absolute -bottom-5 left-1/2 -translate-x-1/2 whitespace-nowrap text-[10.5px] leading-4 ${i === elapsed - 1 ? \"font-medium text-foreground\" : \"text-muted-foreground\"}`}\n                      >\n                        {d.tick}\n                      </span>\n                    )}\n                  </div>\n                );\n              })}\n            </div>\n\n            {/* Today: a hairline between the days so far and the days ahead. */}\n            {elapsed > 0 && elapsed < n && (\n              <div aria-hidden=\"true\" className=\"pointer-events-none absolute bottom-0 w-px bg-foreground/25\" style={{ left: `${elapsed * slot}%`, top: HEADROOM - 6 }}>\n                <span className=\"absolute -top-4 left-1 whitespace-nowrap text-[10px] uppercase tracking-[0.08em] text-muted-foreground\">Today</span>\n              </div>\n            )}\n\n            {/* The goal: a line across the month, its figure sitting on the line at the start, where the early days are short. */}\n            <div\n              aria-hidden=\"true\"\n              className=\"pointer-events-none absolute inset-x-0 top-0\"\n              style={{ transform: `translateY(${goalY}px)`, transition: reduced ? \"none\" : `transform 620ms ${EASE}` }}\n            >\n              <div className=\"h-px bg-foreground/45\" />\n              <span className=\"absolute bottom-1.5 left-0 rounded-full bg-background px-2 py-px text-[10.5px] font-medium tabular-nums text-foreground shadow-[0_0_0_1px_var(--border)]\">\n                Goal {short(target)}\n              </span>\n            </div>\n\n            <div aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-0 border-t border-border\" />\n\n            {/* The readout: what the day added and where the total stood, or where this pace takes you by then. */}\n            <div\n              aria-hidden=\"true\"\n              className=\"pointer-events-none absolute left-0 top-0 flex h-[24px] -translate-x-1/2 items-center gap-1.5 whitespace-nowrap rounded-full bg-background px-2.5 text-[11.5px] shadow-[0_0_0_1px_var(--border)]\"\n              style={{\n                left: active !== null ? `clamp(110px, ${(active + 0.5) * slot}%, calc(100% - 110px))` : \"50%\",\n                opacity: readout ? 1 : 0,\n                transition: reduced ? \"none\" : `left 300ms ${EASE}, opacity 200ms ${EASE}`,\n              }}\n            >\n              <span className=\"text-muted-foreground\">{readout?.label ?? \" \"}</span>\n              <span className=\"font-medium tabular-nums text-foreground\">{readout?.value ?? \" \"}</span>\n            </div>\n          </div>\n          <div aria-hidden=\"true\" className=\"h-5\" />\n\n          <p aria-live=\"polite\" className=\"sr-only\">\n            {keyboard && readout ? `${readout.label}: ${readout.value}` : \"\"}\n          </p>\n          <div className=\"sr-only\">\n            <table>\n              <caption>\n                {title}: {fmt(sofar)} of {fmt(target)}. {verdict}. At this pace, {fmt(forecast)} by the end of {period}.\n              </caption>\n              <tbody>\n                {days.map((d, i) => (\n                  <tr key={d.key}>\n                    <th scope=\"row\">{d.label}</th>\n                    <td>{i < elapsed ? fmt(d.value ?? 0) : \"ahead\"}</td>\n                    <td>{i < elapsed ? `${fmt(totals[i])} so far` : `${fmt(totals[i])} at this pace`}</td>\n                  </tr>\n                ))}\n              </tbody>\n            </table>\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}],"css":{},"docs":"Docs and live preview: https://hairlineui.com/ui/pace. MIT licensed: https://hairlineui.com/license#components","meta":{"license":"MIT"}}