Interactive previews
How a docs preview gets live controls, Reset and Copy code.
Interactive previews
Any preview in these docs can carry a controls panel. The panel sits inside the preview card: beside the canvas when the card is at least 760px wide, below it on narrower screens. It is open by default and the Controls button in the card header collapses it. Changing a control updates the previewed component's props straight away. Reset restores the defaults and Copy code copies the current props as JSX.
All of this lives in apps/docs/components/component-preview.tsx and apps/docs/components/preview-controls.tsx. A component opts in by passing a schema; nothing else is needed.
Opting in
Write the preview as a client component, declare the schema at module level (so it stays the same object between renders), and pass a render function as the child. It receives the current values, keyed by prop name.
"use client";
import { Badge } from "@ziteox/ui/badge";
import { ComponentPreview, type ControlSchema } from "@/components/component-preview";
const schema: ControlSchema = {
label: { type: "text", default: "New" },
size: { type: "number", default: 14, min: 10, max: 24, step: 1, unit: "px" },
color: { type: "color", default: "#21ab70" },
rounded: { type: "boolean", default: true },
variant: { type: "select", default: "solid", options: ["solid", "outline"] },
};
export function BadgePreview() {
return (
<ComponentPreview label="Badge" controls={schema} code={{ component: "Badge" }}>
{(values) => <Badge {...values} />}
</ComponentPreview>
);
}Register the preview in apps/docs/mdx-components.tsx and use it in the page as <BadgePreview />.
Control types
| Type | Renders | Fields |
|---|---|---|
number | Slider with the current value | default, min, max, step (default 1), unit |
color | Colour picker plus a text field (any CSS colour) | default (hex, for the picker) |
boolean | Switch | default |
select | Dropdown | default, options (strings or { label, value }) |
text | Text field | default, placeholder |
Every control also takes label (defaults to the prop name in words), description (a short note under the control) and code.
Copy code
code={{ component: "Name", children?: "...", extra?: ["className=\"demo\""], leading?: [...] }} turns on the Copy code button. It prints leading props first, then every control except those with code: false (skipping values that still match the active defaults), then any extra attributes. Use code: false for preview-only settings such as sample content that should not be copied. Without the code prop, only Reset is shown.
Style picker
Pass variants for curated styles in the header (segmented control, or a select on narrow widths). Each entry has an id, label, presets (values applied on select) and optional controls (keys to show). Reset restores the active style's presets. Preview-only keys with code: false are kept when switching styles if they were already set.
Other ComponentPreview props
| Prop | Description |
|---|---|
label | Caption in the header bar |
theme | "light" or "dark" for the stage only; the header keeps following the site theme |
variants | Curated styles for the header picker |
defaultVariant | Initial style id when variants is set |
controlsCollapsed | Start with the panel closed |
stageClassName | Extra class on the stage, e.g. to remove its padding |
Static previews keep working unchanged: pass plain children and no controls.