Component Code

@fabiocv/component-code

A component beside the code that makes it, where every value is a control. Drag a number, switch a boolean, type over the text. Put it in your Storybook and docs, and they cannot drift from what they show.

Button.tsx
<Button variant= size= radius={8} disabled={} > </Button>
Preview

What for

  • Code as docs you can try

    Every prop is a control, and the docs never drift.

  • Learn by changing it

    Explain a layout or an effect with something to play with.

Put it in your docs

One component, 10.1 kB gzipped, with no dependencies beyond React. Give it your components in scope and the code renders as its own preview, so your docs cannot drift from what they show.

Terminal
npm install @fabiocv/component-code
Live3
Code
<Badge tone= count={3}> </Badge>
Preview
Badge.mdx
import { ComponentCode } from '@fabiocv/component-code'; import { Badge } from './Badge'; # Badge <ComponentCode interactive scope={{ Badge }} controls={{ tone: ['neutral', 'success', 'warning'], count: { min: 0, max: 99 }, }} code={` <Badge tone="success" count={3}> Live </Badge> `} />
Preview
PropWhat it sets
codeThe code as it is written.
scopeWhat the code can name: your components and values.
interactiveMakes each string, number, boolean and line of text in the code a control.
controlsHow a prop is changed, by its name or by Element.prop: a list of options, { min, max, step } for a number, or false to keep it as written.

Or plain HTML, CSS and JS

It works without React too. Import it from @fabiocv/component-code/html and give it your files.

const canvas = document.querySelector('canvas'); const context = canvas.getContext('2d'); const speed = 1; const size = 4; const gap = 16; function draw(time) { const { width, height } = canvas.getBoundingClientRect(); canvas.width = width * devicePixelRatio; canvas.height = height * devicePixelRatio; context.scale(devicePixelRatio, devicePixelRatio); context.fillStyle = getComputedStyle(canvas).color; for (let x = gap / 2; x < width; x += gap) { const wave = Math.sin(x / 40 + (time / 1000) * speed); context.beginPath(); context.arc(x, height / 2 + wave * 40, size, 0, Math.PI * 2); context.fill(); } requestAnimationFrame(draw); } requestAnimationFrame(draw);
Preview

What changed

0.5.02026-11-26A new home at ui.fabio.cv and mcp.fabio.cv, each tool named after itself, and mcp-ui-tools is now @fabiocv/mcp.
  • Its page is https://ui.fabio.cv/component-code/, and its code is in naso/ui.fabio.cv.
0.4.02026-11-19A title for the preview, hidden tabs, a side closed or shown alone, and a line to drag when the two are stacked.
  • title names the preview's tab, which is Preview without it.
  • tabs={false} hides the tabs.
  • collapsed="preview" or collapsed="code" starts that side closed, and dragging the line opens it.
  • only="preview" or only="code" shows that side alone, without the line.
  • Each works the same in @fabiocv/component-code/html.
  • Stacked on a narrow page, the line between the preview and its code can be dragged too, and shares their height between them.
0.3.02026-11-18A handle on the line between the preview and its code.
  • Hovering the line shows its handle: a bar in an indent cut into the code, which springs in, grows a little when pressed, and grabs.
  • With the code closed, the handle moves into the preview, so it stays in reach.
  • A Preview tab cut short by a narrow preview keeps its rounded corner.
  • A drag lets go however the button is released, and only the main button drags.
  • pnpm --filter @fabiocv/component-code dev rebuilds on every change, for linking the package from a local checkout.
0.2.22026-11-18Draggable panels for the preview and its code.
  • Drag the line between the preview and its code to resize them, or to close either one.
0.2.02026-11-17A keyboard-friendlier menu of a prop's options, a preview that changes its own code, and Orbit in the right colours.
  • The menu of a prop's options answers Home and End, jumps to an option by its first letter, and gives the focus back to the value on Escape.
  • A component in the preview can change its own code. For each value the code sets, such as checked={true}, it gets a handler such as onCheckedChange, and calling it writes the new value into the code.
  • Copy holds the code's top-right corner, and reset shows beside it once the code changes, so copy stays in one place.
  • Copy sits last in the code's corner, after reset, as it shows only on hover, so reset and the note keep their place.
0.1.12026-11-16Links to the new repository.
  • Links to its repository, github.com/naso/mcp-ui-tools.
0.1.02026-11-16The first release: Component Code, its MCP App and Orbit.
  • ComponentCode shows a component beside the code that makes it. With scope the preview is rendered from the code, so the two cannot differ. children sets the preview for code that cannot run from its text.
  • interactive turns each string, number, boolean and line of text in the code into a control: numbers drag and step with the arrow keys, booleans switch, and text is typed in place.
  • controls turns a prop into options picked from a menu, or sets a number's range and step.
  • Copy and reset, line numbers and syntax colours.
  • Light and dark, from the page's color-scheme or from theme.
  • note shows a line from an info icon beside reset, once the reader changes a value.
  • @fabiocv/component-code/html runs HTML, CSS and JS beside their code, with a tab for each file. The values named in controls become controls: CSS changes in place, and HTML and JS run the page again.