{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"activity-timeline","type":"registry:component","title":"Activity timeline","description":"New events slide in at the top as the line grows to meet them.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/activity-timeline.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nexport type ActivityKind = \"commit\" | \"comment\" | \"deploy\";\nexport type ActivityEvent = {\n  id: string;\n  kind: ActivityKind;\n  actor: string;\n  action: string;\n  target: string;\n  detail: string;\n  // Epoch milliseconds.\n  at: number;\n};\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\n// The row opens its own height while the content blurs in just behind it, so\n// the space is there before the words arrive.\nconst OPEN = { duration: 0.28, ease: EASE_OUT };\nconst REVEAL = { duration: 0.25, ease: EASE_OUT, delay: 0.06 };\nconst LEAVE = { duration: 0.15, ease: EASE_OUT };\nconst INSTANT = { duration: 0 };\n// Relative times only change by the minute, so a 15s tick is plenty and keeps\n// re-renders rare.\nconst TICK = 15_000;\n\nexport function relativeTime(ms: number) {\n  const s = Math.max(0, Math.round(ms / 1000));\n  if (s < 45) return \"just now\";\n  const m = Math.round(s / 60);\n  if (m < 60) return `${m}m ago`;\n  const h = Math.round(m / 60);\n  if (h < 24) return `${h}h ago`;\n  return `${Math.round(h / 24)}d ago`;\n}\n\nexport function ActivityTimeline({\n  events,\n  now,\n  label = \"Activity\",\n  className,\n}: {\n  events: ActivityEvent[];\n  // Passed in so every row agrees on the time and one tick updates them all.\n  now: number;\n  label?: string;\n  className?: string;\n}) {\n  const reduceMotion = useReducedMotion();\n\n  return (\n    <ol aria-label={label} className={cn(\"flex flex-col\", className)}>\n      <AnimatePresence initial={false}>\n        {events.map((event, i) => (\n          <motion.li\n            key={event.id}\n            initial={{ height: 0, opacity: 0 }}\n            animate={{ height: \"auto\", opacity: 1 }}\n            exit={{ height: 0, opacity: 0, transition: reduceMotion ? INSTANT : LEAVE }}\n            transition={reduceMotion ? INSTANT : OPEN}\n            className=\"relative overflow-hidden\"\n          >\n            {i < events.length - 1 && (\n              // Runs from 4px under this node to 4px above the next one. It\n              // lives in the row, so a new row opening draws its own segment\n              // and the line grows with it.\n              <span\n                aria-hidden\n                className=\"absolute top-9 bottom-1 left-[15.5px] w-px bg-border\"\n              />\n            )}\n            <motion.div\n              initial={\n                reduceMotion\n                  ? { opacity: 0 }\n                  : { opacity: 0, filter: \"blur(4px)\", y: -6 }\n              }\n              animate={{ opacity: 1, filter: \"blur(0px)\", y: 0 }}\n              transition={REVEAL}\n              className=\"flex gap-3 pb-5\"\n            >\n              <span\n                aria-hidden\n                className=\"grid size-8 shrink-0 place-items-center rounded-full bg-background text-muted-foreground inset-ring inset-ring-border\"\n              >\n                <KindIcon kind={event.kind} />\n              </span>\n              {/* 6px down centers the 20px first line on the 32px node. */}\n              <div className=\"min-w-0 flex-1 pt-1.5\">\n                <div className=\"flex items-baseline justify-between gap-3\">\n                  <p className=\"min-w-0 text-sm text-muted-foreground\">\n                    <span className=\"font-medium text-foreground\">{event.actor}</span>{\" \"}\n                    {event.action}{\" \"}\n                    <span className=\"font-medium text-foreground\">{event.target}</span>\n                  </p>\n                  <span className=\"shrink-0 text-xs text-muted-foreground tabular-nums\">\n                    {relativeTime(now - event.at)}\n                  </span>\n                </div>\n                <p\n                  className={cn(\n                    \"mt-1 text-sm text-muted-foreground\",\n                    event.kind === \"commit\" && \"font-mono text-[13px]\",\n                  )}\n                >\n                  {event.detail}\n                </p>\n              </div>\n            </motion.div>\n          </motion.li>\n        ))}\n      </AnimatePresence>\n    </ol>\n  );\n}\n\nfunction KindIcon({ kind }: { kind: ActivityKind }) {\n  return (\n    <svg\n      viewBox=\"0 0 16 16\"\n      className=\"size-4\"\n      fill=\"none\"\n      stroke=\"currentColor\"\n      strokeWidth={1.5}\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n    >\n      {kind === \"commit\" && (\n        <>\n          <circle cx=\"8\" cy=\"8\" r=\"2.5\" />\n          <path d=\"M1.75 8h3.75M10.5 8h3.75\" />\n        </>\n      )}\n      {kind === \"comment\" && (\n        <path d=\"M3.5 2.75h9a1.25 1.25 0 0 1 1.25 1.25v6a1.25 1.25 0 0 1-1.25 1.25H7.5l-3 2.5v-2.5h-1A1.25 1.25 0 0 1 2.25 10V4A1.25 1.25 0 0 1 3.5 2.75Z\" />\n      )}\n      {kind === \"deploy\" && <path d=\"M8 10.5V2.75M4.75 6 8 2.75 11.25 6M3 13.25h10\" />}\n    </svg>\n  );\n}\n\ntype Template = Omit<ActivityEvent, \"id\" | \"at\">;\n\nconst POOL: Template[] = [\n  { kind: \"commit\", actor: \"Yash\", action: \"pushed to\", target: \"main\", detail: \"fix: debounce the search input\" },\n  { kind: \"comment\", actor: \"Mira\", action: \"commented on\", target: \"#482\", detail: \"Looks good, one nit on the spacing.\" },\n  { kind: \"deploy\", actor: \"Deploys\", action: \"shipped\", target: \"v2.4.1\", detail: \"Production, build finished in 48s\" },\n  { kind: \"commit\", actor: \"Arjun\", action: \"pushed to\", target: \"feat/timeline\", detail: \"feat: relative times that update\" },\n  { kind: \"comment\", actor: \"Yash\", action: \"replied on\", target: \"#479\", detail: \"Pinned it to the top, try again?\" },\n  { kind: \"deploy\", actor: \"Deploys\", action: \"shipped\", target: \"preview-913\", detail: \"Preview for feat/timeline is ready\" },\n];\n\n// Offsets for the seed rows. Built from a start time captured once, so the\n// server and client render the same \"2m ago\" text.\nconst SEED: { template: Template; ago: number }[] = [\n  { template: POOL[1], ago: 2 * 60_000 },\n  { template: POOL[0], ago: 18 * 60_000 },\n  { template: POOL[2], ago: 60 * 60_000 },\n  { template: POOL[4], ago: 3 * 60 * 60_000 },\n  { template: POOL[3], ago: 26 * 60 * 60_000 },\n];\n// Older rows fall off the bottom so the feed never grows without bound.\nconst MAX_EVENTS = 20;\n\nexport default function ActivityTimelineDemo() {\n  const [start] = useState(() => Date.now());\n  const [now, setNow] = useState(start);\n  const [events, setEvents] = useState<ActivityEvent[]>(() =>\n    SEED.map(({ template, ago }, i) => ({ ...template, id: `seed-${i}`, at: start - ago })),\n  );\n  const [announcement, setAnnouncement] = useState(\"\");\n  const next = useRef(0);\n\n  useEffect(() => {\n    const id = setInterval(() => setNow(Date.now()), TICK);\n    return () => clearInterval(id);\n  }, []);\n\n  const add = () => {\n    const n = next.current++;\n    const template = POOL[(n + 2) % POOL.length];\n    const at = Date.now();\n    setNow(at);\n    setEvents((list) => [{ ...template, id: `new-${n}`, at }, ...list].slice(0, MAX_EVENTS));\n    setAnnouncement(`${template.actor} ${template.action} ${template.target}`);\n  };\n\n  return (\n    <div className=\"w-[min(440px,100%)] overflow-hidden rounded-[24px] bg-background shadow-raised\">\n      <div className=\"flex items-center justify-between gap-3 border-b border-border py-3 pr-3 pl-5\">\n        <h2 className=\"text-base font-medium text-foreground\">Activity</h2>\n        <button\n          type=\"button\"\n          onClick={add}\n          className=\"h-9 touch-manipulation rounded-full bg-muted px-3.5 text-sm font-medium text-foreground outline-hidden transition-[scale,background-color] duration-150 ease-out select-none hover:bg-border focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[background-color]\"\n        >\n          Simulate event\n        </button>\n      </div>\n      {/* Fixed height with its own scroll, so new rows never push the page\n          or the button. The bottom fades out to show there is more. */}\n      <div\n        tabIndex={0}\n        role=\"region\"\n        aria-label=\"Activity feed\"\n        className=\"h-[400px] overflow-y-auto overscroll-contain px-5 pt-5 pb-6 outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-foreground\"\n        style={{\n          maskImage: \"linear-gradient(to bottom, black calc(100% - 32px), transparent)\",\n        }}\n      >\n        <ActivityTimeline events={events} now={now} />\n      </div>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {announcement}\n      </span>\n    </div>\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["data"],"docs":"From ui lab: https://lab.xevrion.dev/lab/activity-timeline"}