Code Block
A code listing with a copy button; syntax highlighting loads on demand.
tsx
export function Counter() {
const [count, setCount] = useState(0);
// Highlighting loads on demand.
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}When to use
- Use when
- Showing a code listing, such as a model's answer or an example, with a copy button.
- Avoid when
- The code is editable; use an editor. Inline code belongs in running text.
Installation
The source lands in src/components/ui/code-block.tsx, along with any Shelf items it builds on, and .shelf/lock.json records what was installed. It's yours to edit.
Usage
Built on
- Packages
@stylexjs/stylex,shiki- Shelf items
- Copy button,
foundations,utils - Figma
- Optional native Figma library. See Designers.
Verified
- Size
- 3.8 kB gzipped on its own, 305 kB with the Shelf items and packages it builds on. JS and CSS in a production build, without React.
- Accessibility
- Axe passes on 3 stories
- Behavior
- 2 interaction tests in Storybook
- React Doctor
- React Doctor finds nothing