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. Ask for it in your chat, or put it in your Storybook and docs.

Button.tsx
<Button variant= size= radius={8} disabled={} > </Button>
Preview
Why
  • In the chat, with your agent

    Review an agent's work

    Try the API it wrote in the chat, before it lands in your codebase.

  • Say what broke

    Break it, then say so. The agent already has the code as you left it.

  • In your codebase, as a package

    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.

MCP

Show it in the chat

Ask your agent for a component and say “show it in Component Code”. It writes the component, and the chat shows it running beside the code that uses it, with every value a control. Change a value until something breaks, then just say what is wrong. The agent already has the code as you left it.

Build me a toggle with a label and a disabled state. Show it in Component Code.

Toggle.tsx
<Toggle label= checked={} disabled={} />
Preview

Here it is. Switch it, or change its label and state in the code.

Add it to your agent:

Prompt

Add the component-code MCP server to this agent. It runs with the command npx -y @fabiocv/component-code-mcp. Add it the way this agent adds MCP servers, then tell me if I need to restart for its tools to load.

The component can import React and nothing else, and the code that uses it is plain JSX: elements, props and text. A component keeps any state inside itself.

React

Put it in your docs

One component, 8.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'] }} code={` <Badge tone="success" count={3}> Live </Badge> `} />
Preview

It reads the code rather than running it: elements, props, text, strings, numbers, booleans, lists, objects and the names in scope. Anything else says why in place of the preview. For code with a hook, pass the preview as children.

PropWhat it sets
codeThe code as it is written. The blank lines around it and its shared indent are dropped.
scopeWhat the code can name: your components and values. With it, the preview is the code itself, rendered.
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.
onChangeCalled with { prop, value, code } as the reader changes a value.
childrenThe preview, for code that cannot run from its text, such as code with a hook.
fileThe file named in the code's tab. Code when left out.
themelight or dark. Without it, the page's color-scheme decides.
noteA line shown from an info icon beside reset, once the reader changes a value.
HTML

Or plain HTML, CSS and JS

For a canvas, an SVG or anything without React, import it from @fabiocv/component-code/html and give it files. They run together in a sandboxed frame, a tab for each, and the values you name in controls become controls. It is 8.1 kB gzipped.

<canvas></canvas>
Preview

A CSS value changes as you drag it, so a script that reads custom properties each frame follows along. A change to the HTML or JS runs the page again. Name a value by its attribute, as r or circle.r, its CSS property or custom property, or the JS const it sets. In the chat, ask for plain HTML, CSS and JS.

Ideas

What to ask for

Copy a prompt, then make it yours.

Try it before you build it

See a component working, and change it, before any of it is in your codebase.

Prompt

Build me a pricing card with a plan name, a price, a list of features and a highlighted state. Show it in Component Code.

Tune the numbers

Drag sizes, durations and spacing until it feels right, then copy the code.

Prompt

Make a progress ring with its size, stroke width and value as numbers. Show it in Component Code so I can drag them.

Look at a component from your repo

Bring a component into the chat to try its props, without running the app.

Prompt

Read src/components/Avatar.tsx and show it in Component Code with its sizes as options and its name as text.

Learn by changing it

Explain a layout or an effect with something to play with, not only words.

Prompt

Explain CSS flexbox with a small component in Component Code where I can change the direction, the alignment and the gap.

Check the edge cases

Type a long name, an empty one or an emoji, and see where it breaks.

Prompt

Show our Tag in Component Code with its label as text, so I can try long labels, empty ones and emoji.

Document your design system

Have your agent write docs with live examples that cannot drift from the code.

Prompt

Write a Storybook MDX page for our Badge that uses ComponentCode from @fabiocv/component-code, with its tones as options and its count as a number.

Changelog

What changed

0.1.0Unreleased

@fabiocv/component-code

  • 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.

@fabiocv/component-code-mcp

  • show_in_component_code shows a component in the chat in Component Code, as an MCP App.
  • show_html_in_component_code shows HTML, CSS and JS running in the chat.
  • As the reader changes a value, the agent gets the code as it now is, and what the preview threw, with their next message, in hosts that take model context. An info icon tells the reader the chat sees this state.
  • Runs over stdio with npx @fabiocv/component-code-mcp, and over HTTP at https://mcp-ui-tools.fabio.cv/component-code/mcp.