{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "timeline-track",
  "title": "Timeline Track",
  "description": "Schedule track with an axis of ticks and one item per row, placed along the span.",
  "dependencies": [
    "class-variance-authority@^0.7.1"
  ],
  "registryDependencies": [
    "@pestoui/style",
    "@pestoui/utils"
  ],
  "files": [
    {
      "path": "pestoui/components/timeline-track.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cva, type VariantProps } from \"class-variance-authority\"\n\nimport { cn } from \"@/lib/utils\"\n\n/* Rows stack in source order, one item per row. A schedule answers \"what is\n   happening at 09:00\", so two items that share an hour belong on two lines —\n   packing them onto one row would be a bin-packing component answering a\n   different question, and it would reshuffle rows every time the data moved. */\nconst timelineTrackVariants = cva(\"flex w-full flex-col\", {\n  variants: {\n    size: {\n      sm: \"gap-1\",\n      default: \"gap-1.5\",\n    },\n  },\n  defaultVariants: {\n    size: \"default\",\n  },\n})\n\n/* The tone lives in the leading edge and a wash of the same colour behind the\n   row; the label stays --foreground in every tone. Ten toned pills with ten\n   coloured labels reads as a ransom note, and the edge already carries the\n   coding on its own.\n\n   Tint strengths follow the badge/banner scale rather than one flat number:\n   warning is the palest hue in the set and needs a few more points before it\n   registers, and destructive is the only one dense enough against a dark canvas\n   to want its own step up. */\nconst timelineItemVariants = cva(\n  /* min-w-6 stops a fifteen-minute item on a ten-hour track from collapsing to\n     an invisible sliver — below roughly 4% of the range the pill would be\n     narrower than the leading edge drawn on it. */\n  \"flex min-w-6 items-center overflow-hidden border-l-2 font-medium text-foreground\",\n  {\n    variants: {\n      tone: {\n        selected: \"border-selected bg-selected/10\",\n        info: \"border-info bg-info/10\",\n        success: \"border-success bg-success/10\",\n        warning: \"border-warning bg-warning/15\",\n        destructive: \"border-destructive bg-destructive/10 dark:bg-destructive/20\",\n        muted: \"border-muted-foreground bg-muted-foreground/10\",\n        \"chart-1\": \"border-chart-1 bg-chart-1/12\",\n        \"chart-2\": \"border-chart-2 bg-chart-2/12\",\n        \"chart-3\": \"border-chart-3 bg-chart-3/12\",\n        \"chart-4\": \"border-chart-4 bg-chart-4/12\",\n        \"chart-5\": \"border-chart-5 bg-chart-5/12\",\n      },\n      /* Radius is chosen against the row height, not by eye: rounded-md is\n         12.8px on a 36px pill and rounded-sm is 9.6px on a 28px one, so both\n         stay under the half-edge point where the ends would round away into a\n         lozenge. The pill grows horizontally, which is why neither value may be\n         picked from the wide end of the scale. */\n      size: {\n        sm: \"h-7 gap-1.5 rounded-sm px-2 text-xs\",\n        default: \"h-9 gap-2 rounded-md px-2.5 text-sm\",\n      },\n    },\n    defaultVariants: {\n      tone: \"selected\",\n      size: \"default\",\n    },\n  }\n)\n\ntype TimelineTone = NonNullable<VariantProps<typeof timelineItemVariants>[\"tone\"]>\ntype TimelineSize = NonNullable<VariantProps<typeof timelineItemVariants>[\"size\"]>\n\n/* A label centred on its own gridline would hang half off the track at either\n   end, so the outer two anchor inward and only the interior ones centre. */\nconst TICK_ANCHOR = {\n  start: \"translate-x-0\",\n  middle: \"-translate-x-1/2\",\n  end: \"-translate-x-full\",\n} as const\n\nfunction tickAnchor(index: number, count: number) {\n  if (index === 0) return TICK_ANCHOR.start\n  if (index === count - 1) return TICK_ANCHOR.end\n  return TICK_ANCHOR.middle\n}\n\nconst percent = (fraction: number) => `${fraction * 100}%`\n\nconst clamp01 = (value: number) => Math.min(1, Math.max(0, value))\n\n/* Ticks are the consumer's own words for these positions, so a value that lands\n   on one borrows its label — that is what lets the hidden \"08:00 to 10:00\" speak\n   in the axis's units without a second prop carrying the same strings twice.\n\n   The tolerance is not defensive padding. Ticks are spaced by dividing the range\n   into equal steps, and that round-trip is lossy in binary floating point:\n   (2 - 0) / 3 * 3 evaluates to 1.9999999999999998, so an exact hit would fail an\n   integer test and silently fall back to the raw number. */\nfunction tickLabelAt(value: number, start: number, span: number, ticks: string[]) {\n  const steps = ticks.length - 1\n  if (steps < 1) return null\n\n  const position = ((value - start) / span) * steps\n  const nearest = Math.round(position)\n  if (Math.abs(position - nearest) > 1e-6) return null\n\n  return ticks[nearest] ?? null\n}\n\ntype TimelineTrackContextValue = {\n  start: number\n  span: number\n  size: TimelineSize\n  format: (value: number) => string\n}\n\nconst TimelineTrackContext = React.createContext<TimelineTrackContextValue | null>(\n  null\n)\n\nfunction useTimelineTrack() {\n  const track = React.useContext(TimelineTrackContext)\n\n  if (!track) {\n    throw new Error(\"TimelineItem must be used within <TimelineTrack>\")\n  }\n\n  return track\n}\n\ninterface TimelineTrackProps extends React.ComponentProps<\"div\"> {\n  /** Left edge of the axis, in whatever unit the items use. */\n  start: number\n  /** Right edge of the axis. Must be greater than `start`. */\n  end: number\n  /** Axis labels, spread evenly from `start` to `end`. Each also draws a gridline. */\n  ticks?: string[]\n  /** Default row height for the items inside. Each item may override it. */\n  size?: TimelineSize\n  /** Renders a position for the hidden span text. Defaults to the tick it lands on. */\n  formatValue?: (value: number) => string\n}\n\nfunction TimelineTrack({\n  className,\n  start,\n  end,\n  ticks = [],\n  size = \"default\",\n  formatValue,\n  children,\n  ...props\n}: TimelineTrackProps) {\n  const span = end - start\n\n  /* Every position on this track is a fraction of `span`, so a reversed or empty\n     range is not a layout to degrade gracefully — it is a division by zero.\n     Render nothing and let the caller notice, rather than emitting a stack of\n     rows offset by NaN%. */\n  if (!Number.isFinite(span) || span <= 0) {\n    return null\n  }\n\n  const format =\n    formatValue ??\n    ((value: number) => tickLabelAt(value, start, span, ticks) ?? String(value))\n\n  /* One tick still deserves its gridline; it just has no interval to sit at the\n     end of, so it anchors at the start rather than dividing by zero. */\n  const steps = Math.max(ticks.length - 1, 1)\n\n  return (\n    <TimelineTrackContext.Provider value={{ start, span, size, format }}>\n      <div\n        data-slot=\"timeline-track\"\n        data-size={size}\n        className={cn(\"w-full\", className)}\n        {...props}\n      >\n        {ticks.length > 0 ? (\n          /* The axis is scaffolding for the eye only. Every item already states\n             its own span in the same units, so reading the ticks aloud would\n             announce a bare list of times detached from anything they describe. */\n          <div\n            data-slot=\"timeline-track-ticks\"\n            aria-hidden=\"true\"\n            className=\"relative mb-2 h-4\"\n          >\n            {ticks.map((tick, index) => (\n              <span\n                key={index}\n                className={cn(\n                  \"absolute top-0 text-xs tabular-nums text-muted-foreground\",\n                  tickAnchor(index, ticks.length)\n                )}\n                style={{ left: percent(index / steps) }}\n              >\n                {tick}\n              </span>\n            ))}\n          </div>\n        ) : null}\n\n        {/* Clipping here is the backstop behind the per-item clamp: an item held\n            at the right edge still keeps its minimum width, and this is what\n            stops those few pixels from hanging off the end of the axis. */}\n        <div data-slot=\"timeline-track-rows\" className=\"relative overflow-hidden\">\n          {ticks.length > 0 ? (\n            <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\">\n              {ticks.map((_, index) => (\n                <span\n                  key={index}\n                  className={cn(\n                    \"absolute inset-y-0 w-px bg-border\",\n                    /* The last gridline sits exactly on the right edge, where a\n                       1px rule falls outside the clipped box and disappears —\n                       pull it back inside by its own width. */\n                    index === ticks.length - 1 && \"-translate-x-full\"\n                  )}\n                  style={{ left: percent(index / steps) }}\n                />\n              ))}\n            </div>\n          ) : null}\n\n          <ol\n            data-slot=\"timeline-track-list\"\n            /* Preflight strips list-style, which drops list semantics in Safari\n               — restore them explicitly, same as the stepper does. */\n            role=\"list\"\n            className={cn(timelineTrackVariants({ size }))}\n          >\n            {children}\n          </ol>\n        </div>\n      </div>\n    </TimelineTrackContext.Provider>\n  )\n}\n\ninterface TimelineItemProps extends React.ComponentProps<\"li\"> {\n  /** Start position on the track's axis. */\n  from: number\n  /** End position on the track's axis. */\n  to: number\n  label: React.ReactNode\n  /** Trailing slot — an avatar stack, a count, a status dot. */\n  media?: React.ReactNode\n  tone?: TimelineTone\n  /** Overrides the track's size for this row alone. */\n  size?: TimelineSize\n  /** Replaces the hidden span text. Defaults to the track's formatted `from`–`to`. */\n  rangeLabel?: string\n}\n\nfunction TimelineItem({\n  className,\n  from,\n  to,\n  label,\n  media,\n  tone = \"selected\",\n  size,\n  rangeLabel,\n  ...props\n}: TimelineItemProps) {\n  const track = useTimelineTrack()\n  const resolvedSize = size ?? track.size\n  const end = track.start + track.span\n\n  /* Both edges are clamped into the window rather than allowed to overflow: an\n     item beginning before the track does is drawn from the left edge, not from a\n     negative offset that would slide it out from under its own axis. An item\n     entirely outside the range survives as a hairline pinned to the edge it\n     overran, which is the honest picture — it is on the schedule, just not in\n     the window being shown. */\n  const left = clamp01((from - track.start) / track.span)\n  const right = clamp01((to - track.start) / track.span)\n  const clipped = from < track.start || to > end\n\n  return (\n    <li\n      data-slot=\"timeline-item\"\n      data-tone={tone}\n      data-size={resolvedSize}\n      data-clipped={clipped || undefined}\n      className={cn(timelineItemVariants({ tone, size: resolvedSize }), className)}\n      /* Percentages are the only unit that keeps a row pinned to its hour as the\n         track is resized, and neither the offset nor the width can be a utility\n         — both are continuous numbers. Both resolve against the row list's\n         width, and clamping guarantees offset + width never exceeds 100%, so a\n         row cannot push its own container wider. */\n      style={{ marginLeft: percent(left), width: percent(right - left) }}\n      {...props}\n    >\n      <span className=\"min-w-0 flex-1 truncate\">{label}</span>\n      {/* Where the pill sits is the only thing on screen saying when this\n          happens, and position says nothing at all to a screen reader — or to\n          anyone whose item got clamped. The span is spoken in full, from the\n          unclamped values, so a clipped row still reports its real hours. */}\n      <span className=\"sr-only\">\n        , {rangeLabel ?? `${track.format(from)} to ${track.format(to)}`}\n      </span>\n      {media ? (\n        <span data-slot=\"timeline-item-media\" className=\"shrink-0\">\n          {media}\n        </span>\n      ) : null}\n    </li>\n  )\n}\n\nexport { TimelineTrack, TimelineItem, timelineTrackVariants, timelineItemVariants }\nexport type { TimelineTrackProps, TimelineItemProps, TimelineTone, TimelineSize }\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}