Skip to content
WANCRAFT

C.194Typography & Text

Code Walkthrough

A presentation-style code walkthrough: each step highlights a line range, lines grow and collapse between steps, and captions explain.

NewReactMotionTailwindadvanced

Overview

Code Walkthrough presents a file the way a good talk does, one idea at a time. Each step supplies the whole file as it stands, the lines to emphasise and a caption. Highlighted lines get a tint and an accent bar while the rest dims. Between steps the component matches lines by stable keys, using an LCS on the line text unless you give explicit keys, so unchanged lines stay put while new lines grow in (marked + in the gutter) and removed lines collapse away on a spring. Move through the steps with the prev / next buttons or the step bars, use Left / Right / Home / End while it has focus, or turn on autoplay: it has a pause button, a progress bar, and pauses itself off screen and in hidden tabs. Syntax colour comes from your own pre-tokenized lines or a tiny built-in TS/TSX tokenizer (keywords, strings, comments, numbers, JSX tags and attributes), with separate palettes for light and dark. There are no highlighter dependencies.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/code-walkthrough.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/code-walkthrough.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/code-walkthrough.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { CodeWalkthrough } from "@/components/ui/code-walkthrough";

export function CodeWalkthroughExample() {
  const steps = [
    {
      title: "The signature",
      caption: "Start with a function whose name begins with use.",
      highlight: "1",
      code: "export function useToggle() {\n  return false;\n}",
    },
    {
      title: "Add state",
      caption: "Keep the value in state and return a toggle.",
      highlight: "4-6",
      code: 'import { useState } from "react";\n\nexport function useToggle() {\n  const [on, setOn] = useState(false);\n  const toggle = () => setOn((v) => !v);\n  return [on, toggle] as const;\n}',
    },
  ];

  return (
    <CodeWalkthrough steps={steps} />
  );
}

03Props

PropTypeDefaultDescription
stepsCodeWalkthroughStep[]—Required. { title?, caption, code (string | CodeLine[]), highlight? ("1-3, 7" | number[]) } per step.
labelstring"Code walkthrough"Accessible name of the section.
fileNamestring"example.tsx"File name in the code panel header.
captionPosition"right" | "bottom" | "none""right"Captions beside the code (stacked when narrow), below it, or not rendered.
showLineNumbersbooleantrueGutter line numbers.
dimOpacitynumber0.35Opacity of lines outside the highlighted range.
autoplaybooleanfalseAdvance automatically, with a pause button and progress bar.
intervalnumber4000Time per step while autoplaying, in ms.
loopbooleanfalseWrap around at the ends (buttons, keys and autoplay).
step / defaultStep / onStepChangenumber / number / (step) => void— / 0 / —Controlled or uncontrolled current step (0-based).
accentstring"#FF7448"Highlight bar, added-line marker, progress and focus colour.
CodeLinestring | { key?, text?, tokens?: { text, type? }[] }—Pre-tokenized lines (type: keyword | string | comment | number | tag | attr | fn | type | punct | plain) and explicit keys are optional.

04Features

  • 01Highlighted line ranges with a tint and accent bar; everything else dims
  • 02Diff-aware transitions: lines keep stable keys (LCS on text, or your own keys), so only real additions and removals animate
  • 03Added lines are marked + in the gutter for the step that introduces them
  • 04Prev / next, step bars, Left / Right / Home / End while focused
  • 05Optional autoplay with pause, a progress bar, and automatic pausing off screen or in hidden tabs
  • 06Built-in tiny TS/TSX tokenizer or pre-tokenized lines; theme-aware palettes; no highlighter dependency
motion · springmotion · layoutinput · clickinput · keyboardinput · focus

05Accessibility

  • A <section> with aria-roledescription="code walkthrough"; the code panel is a focusable, labelled region that scrolls horizontally.
  • Left / Right step back and forward, Home / End jump to the ends, whenever focus is inside the walkthrough.
  • The caption is a polite live region, so "Step 02 / 04" and its explanation are announced on every change.
  • Step bars are buttons named "Step 2: Hold the time in state" with aria-current="step"; prev / next and play / pause are labelled.
  • Autoplay can be paused at any time (WCAG 2.2.2) and stops advancing while off screen or in a hidden tab.
  • Line numbers and + markers are aria-hidden; with prefers-reduced-motion lines and captions change instantly.

06Limitations

  • The built-in tokenizer is intentionally small (TS/TSX-flavoured, line-based); pass pre-tokenized lines for other languages or exact highlighting.