{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "timeline",
  "title": "Timeline",
  "description": "Keeps a component's schedule in a pure function of its props: named elements with a start, a length and beats, which a timing prop can move, trim or stretch one by one.",
  "files": [
    {
      "path": "registry/remocn/timeline/index.ts",
      "content": "import { useMemo } from \"react\";\n\n// A component's schedule, kept apart from its drawing: which elements the\n// animation has (an entry, a word, a finale), when each starts and how long\n// it runs. The schedule is a pure function of the props, so it can be read\n// without rendering anything, and a `timing` prop can move, trim or stretch\n// single elements without touching the code that draws them.\n//\n//   // timeline.ts, next to the component\n//   export const titleTimeline = defineTimeline<TitleElementId, TitleProps>(\n//     ({ words = WORDS }, { length }) =>\n//       words.map((word, k) => ({\n//         id: `word:${k}`,\n//         lane: \"Words\",\n//         label: word,\n//         start: k * 8,\n//         length: 8,\n//         follows: k > 0 ? `word:${k - 1}` : undefined,\n//         beats: { rise: 12 },\n//       })),\n//   );\n//\n//   // index.tsx\n//   const tl = useTimeline(titleTimeline, props, length, timing);\n//   const word = tl.get(\"word:0\");\n//   interpolate(t, [word.start, word.start + word.beat(\"rise\")], [0, 1]);\n//\n// Every number is in frames of the 30 fps scene timeline, after `delay` and\n// `speed`: those still apply to the whole animation, `timing` works inside it.\n// Element ids are public API: a saved `timing` refers to them, so an id once\n// shipped keeps its meaning.\n\n/** One timed part of an animation, as its schedule lays it out. */\nexport interface TimelineElement<Id extends string = string> {\n  /** Stable name a `timing` override refers to: `entry:0`, `finale`. */\n  id: Id;\n  /** Row the element sits on in a timeline view. */\n  lane: string;\n  /** What the element shows, for people reading the timeline. */\n  label: string;\n  /** First frame of the element. */\n  start: number;\n  /** Frames until the next element of the lane takes over, or the end. */\n  length: number;\n  /**\n   * The element this one comes after. When that element's end moves, this\n   * one moves with it, keeping the gap between them.\n   */\n  follows?: Id;\n  /**\n   * Named frame counts inside the element, measured from its start: how long\n   * a roll takes, when a second part begins. `stretch` scales them.\n   */\n  beats?: Record<string, number>;\n}\n\n/** A change to one element, made from outside the component. */\nexport interface TimingOverride {\n  /** New first frame. Wins over the shift from `follows`. */\n  start?: number;\n  /** New length, trimming or extending the element. Wins over `stretch`. */\n  length?: number;\n  /** Multiplies the length and every beat: 2 plays the element half as fast. */\n  stretch?: number;\n  /** Stops following: the element keeps its own start when the one before moves. */\n  detach?: boolean;\n}\n\n/** Overrides by element id: the `timing` prop of a component. */\nexport type Timing<Id extends string = string> = Partial<\n  Record<Id, TimingOverride>\n>;\n\n/** A component's schedule: props and the scene length in, elements out. */\nexport type Schedule<Id extends string, Props> = (\n  props: Props,\n  scene: { length: number },\n) => TimelineElement<Id>[];\n\n/** An element after `timing` has been applied. */\nexport interface ResolvedElement<Id extends string = string>\n  extends TimelineElement<Id> {\n  /** `start + length`. */\n  end: number;\n  beats: Record<string, number>;\n  /** A beat by name, stretched. Throws on a name the schedule didn't set. */\n  beat(name: string): number;\n  /**\n   * 0 → 1 over the element, or over its first `beat` frames when a beat is\n   * named. Clamped on both sides.\n   */\n  progress(frame: number, beat?: string): number;\n}\n\nexport interface ResolvedTimeline<Id extends string = string> {\n  /** All elements, in the order the schedule returned them. */\n  elements: ResolvedElement<Id>[];\n  /** An element by id. Throws on an id the schedule didn't return. */\n  get(id: Id): ResolvedElement<Id>;\n  /** The elements of one lane, in order. */\n  lane(name: string): ResolvedElement<Id>[];\n}\n\n/** Declares a schedule. Elements come back in time order. */\nexport function defineTimeline<Id extends string, Props>(\n  schedule: Schedule<Id, Props>,\n): Schedule<Id, Props> {\n  return schedule;\n}\n\n/** Applies `timing` to a schedule's elements and carries the `follows` chains. */\nexport function resolve<Id extends string>(\n  elements: TimelineElement<Id>[],\n  timing: Timing<Id> = {},\n): ResolvedTimeline<Id> {\n  const byId = new Map<Id, TimelineElement<Id>>(elements.map((e) => [e.id, e]));\n  const done = new Map<Id, ResolvedElement<Id>>();\n  const visiting = new Set<Id>();\n\n  const place = (element: TimelineElement<Id>): ResolvedElement<Id> => {\n    const known = done.get(element.id);\n    if (known) return known;\n    if (visiting.has(element.id)) {\n      throw new Error(`timeline: \"${element.id}\" follows itself`);\n    }\n    visiting.add(element.id);\n\n    const override: TimingOverride = timing[element.id] ?? {};\n    const stretch = override.stretch ?? 1;\n    const beats: Record<string, number> = {};\n    for (const [name, frames] of Object.entries(element.beats ?? {})) {\n      beats[name] = frames * stretch;\n    }\n\n    let start = element.start;\n    const leader = element.follows ? byId.get(element.follows) : undefined;\n    if (leader && !override.detach) {\n      // The leader's end moved by this much; this element moves as far.\n      start += place(leader).end - (leader.start + leader.length);\n    }\n    if (override.start !== undefined) start = override.start;\n    const length = override.length ?? element.length * stretch;\n\n    const beat = (name: string) => {\n      const frames = beats[name];\n      if (frames === undefined) {\n        throw new Error(`timeline: \"${element.id}\" has no beat \"${name}\"`);\n      }\n      return frames;\n    };\n    const resolved: ResolvedElement<Id> = {\n      ...element,\n      start,\n      length,\n      end: start + length,\n      beats,\n      beat,\n      progress: (frame, name) => {\n        const span = name === undefined ? length : beat(name);\n        if (span <= 0) return frame >= start ? 1 : 0;\n        return Math.min(1, Math.max(0, (frame - start) / span));\n      },\n    };\n    visiting.delete(element.id);\n    done.set(element.id, resolved);\n    return resolved;\n  };\n\n  const all = elements.map(place);\n  return {\n    elements: all,\n    get: (id) => {\n      const element = done.get(id);\n      if (!element) throw new Error(`timeline: no element \"${id}\"`);\n      return element;\n    },\n    lane: (name) => all.filter((e) => e.lane === name),\n  };\n}\n\n/**\n * The component side: runs the schedule and applies `timing`, once per\n * change of its inputs. `length` is the scene length the schedule fits into,\n * in the same frames the component counts.\n */\nexport function useTimeline<Id extends string, Props>(\n  schedule: Schedule<Id, Props>,\n  props: Props,\n  length: number,\n  timing?: Timing<Id>,\n): ResolvedTimeline<Id> {\n  return useMemo(\n    () => resolve(schedule(props, { length }), timing),\n    [schedule, props, length, timing],\n  );\n}\n",
      "type": "registry:lib",
      "target": "lib/remocn/timeline.ts"
    }
  ],
  "type": "registry:lib"
}
