{
  "title": "Animated Timeline Playback Scrubber",
  "description": "Interactive timeline play/pause scrubber with keyframe animation.",
  "name": "tanstack-charts-timeline-playback-scrubber",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card",
    "button"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/timeline-playback-scrubber.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, lineY } from \"@tanstack/charts\";\nimport { handleX } from \"@tanstack/charts/interaction/handle\";\nimport { controlledSignal } from \"@tanstack/charts/interaction/signal\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useEffect, useMemo, useState } from \"react\";\n\nimport { scaleLinear, scaleUtc } from \"d3-scale\";\nimport { Pause, Play } from \"lucide-react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StockDatum {\n    date: Date;\n    close: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): StockDatum[] {\n    const results: StockDatum[] = [];\n    let price = 160;\n    const baseDate = new Date(Date.UTC(2024, 0, 1)).getTime();\n\n    for (let i = 0; i < 20; i++) {\n        price += Math.sin(i * 0.5) * 5 + (Math.random() - 0.45) * 6;\n        results.push({\n            date: new Date(baseDate + i * 7 * 24 * 60 * 60 * 1000),\n            close: Number(Math.max(100, price).toFixed(2)),\n        });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as StockDatum;\n    if (!d || !d.date) return { title: \"\", rows: [] };\n    const dateObj = d.date instanceof Date ? d.date : new Date(d.date);\n    const titleStr = isNaN(dateObj.getTime())\n        ? \"\"\n        : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\n    return {\n        title: titleStr,\n        rows: [{ label: \"Close Price\", value: `$${d.close.toFixed(2)}`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function TimelinePlaybackScrubber() {\n    const data = useMemo(() => generateDemoData(), []);\n    const dates = useMemo(() => data.map((d) => d.date), [data]);\n\n    const [currentFrame, setCurrentFrame] = useState<Date>(dates[0]);\n    const [isPlaying, setIsPlaying] = useState<boolean>(false);\n\n    useEffect(() => {\n        if (!isPlaying) return;\n        const interval = setInterval(() => {\n            setCurrentFrame((prev) => {\n                const prevTime = prev instanceof Date ? prev.getTime() : new Date(prev).getTime();\n                const idx = dates.findIndex(\n                    (d) => (d instanceof Date ? d.getTime() : new Date(d).getTime()) === prevTime,\n                );\n                if (idx < 0 || idx >= dates.length - 1) {\n                    setIsPlaying(false);\n                    return prev;\n                }\n                return dates[idx + 1];\n            });\n        }, 600);\n\n        return () => clearInterval(interval);\n    }, [dates, isPlaying]);\n\n    const activeDatum = useMemo(() => {\n        const frameTime = currentFrame instanceof Date ? currentFrame.getTime() : new Date(currentFrame).getTime();\n        return (\n            data.find((d) => (d.date instanceof Date ? d.date.getTime() : new Date(d.date).getTime()) === frameTime) ||\n            data[0]\n        );\n    }, [currentFrame, data]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"playback-line\",\n                            x: \"date\",\n                            y: \"close\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.5,\n                        }),\n                        dot(data, {\n                            id: \"playback-points\",\n                            x: \"date\",\n                            y: \"close\",\n                            fill: \"var(--chart-1)\",\n                            r: 3.5,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\n                            axis: {\n                                ticks: {\n                                    format: (v: any) => {\n                                        const dateObj = v instanceof Date ? v : new Date(v);\n                                        return isNaN(dateObj.getTime())\n                                            ? \"\"\n                                            : dateObj.toLocaleDateString(\"en-US\", {\n                                                  month: \"short\",\n                                                  day: \"numeric\",\n                                                  timeZone: \"UTC\",\n                                              });\n                                    },\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Stock Close ($)\" },\n                        },\n                    },\n                    controls: [\n                        handleX({\n                            id: \"playback-frame\",\n                            value: controlledSignal<Date, any>(currentFrame, (next) =>\n                                setCurrentFrame(next instanceof Date ? next : new Date(next)),\n                            ),\n                            values: dates,\n                            cross: { edge: \"bottom\", offset: 24 },\n                            ruleStyle: { fill: \"var(--chart-2)\" },\n                            handleStyle: {\n                                fill: \"var(--chart-2)\",\n                                stroke: \"var(--background)\",\n                                strokeWidth: 2,\n                            },\n                            ariaLabel: \"Timeline playback frame scrubber\",\n                        }),\n                    ],\n                    margin: { top: 24, right: 24, bottom: 45, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\n                    tooltip: {\n                        use: tooltip,\n                        className:\n                            \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                        anchor: \"pointer\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [currentFrame, data, dates],\n    );\n\n    const fmtDate = (d: any) => {\n        const dateObj = d instanceof Date ? d : new Date(d);\n        return isNaN(dateObj.getTime())\n            ? \"\"\n            : dateObj.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\n    };\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Animated Timeline Playback Scrubber</CardTitle>\n                <CardDescription>\n                    Interactive timeline play/pause scrubber stepping through sequential data frames\n                </CardDescription>\n            </CardHeader>\n            <CardContent className=\"space-y-3\">\n                <div className=\"flex items-center justify-between\">\n                    <Button\n                        size=\"sm\"\n                        variant=\"default\"\n                        className=\"h-8 text-xs\"\n                        onClick={() => setIsPlaying((prev) => !prev)}>\n                        {isPlaying ? <Pause className=\"mr-1.5 h-3.5 w-3.5\" /> : <Play className=\"mr-1.5 h-3.5 w-3.5\" />}\n                        {isPlaying ? \"Pause\" : \"Play Timeline\"}\n                    </Button>\n                    <span className=\"text-muted-foreground font-mono text-xs\">\n                        Active: <strong className=\"text-foreground\">{fmtDate(activeDatum.date)}</strong> ($\n                        {activeDatum.close.toFixed(2)})\n                    </span>\n                </div>\n                <div className=\"h-[230px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={230}\n                        ariaLabel=\"Animated Timeline Playback Scrubber Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    {isPlaying\n                        ? \"Playback active - stepping through timeline...\"\n                        : \"Drag orange handle or click Play to scrub\"}\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `handleX` control provides draggable playhead handle along bottom X axis rail\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default TimelinePlaybackScrubber;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Interactive timeline play/pause scrubber with keyframe animation. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}