Code Block
CodeBlock shows several lines of code in a panel that is set into the page.
CodeBlock shows several lines of code in a panel that is set into the page, such as a command to run, a file to copy or a sample API response. For a short piece of code inside a sentence, such as a prop name or a file path, use Code instead.
Long lines wrap only at spaces, so names and paths are never split, and a line with no spaces scrolls sideways. Set maxLines to limit the height to that many lines. The rest of the code scrolls rather than being cut off, so every line can still be reached with the mouse, the keyboard or a screen reader.
size sets the padding, the corner and the text size together. The panel uses the same fill as a Surface rather than a Card, because code is part of the page rather than an object placed on it. When the block sits inside a panel of the same kind, such as a Surface, set hosted to drop its own fill, border and padding.
CodeBlock doesn't highlight code itself. Use any highlighter that has a CSS-variables mode and point it at the --code- variables. Those colours are already checked for contrast against the panel in both light and dark mode.
Use topbar for a row at the top of the block, such as a file name and a copy button. When that row spans the full width, also set band so the first line of code starts below it. Use footer for a row at the bottom, such as a status line.
Props
Generated from the types. It lists what this component DECLARES: a prop that arrives from a shared type or from the platform is real and is not repeated here.
| Prop | Type | What it does |
|---|---|---|
band? | boolean | Moves the code down so that the topbar row doesn't cover the first line. |
children | React.ReactNode | The code. |
className? | string | Your classes, appended to the ones this component resolves rather than replacing them. |
footer? | React.ReactNode | Content for a row at the bottom of the block, such as an expand button or a status line. |
hosted? | boolean | Removes the block's own background, border and padding. |
maxLines? | number | The maximum height of the block, in lines of code. |
size? | "1" | "2" | "3" | "4" | The size step of the code block, from 1 to 4. |
style? | React.CSSProperties | Inline styles, merged last, so your value wins over anything the component resolved. |
topbar? | React.ReactNode | Content for a row at the top of the block, such as a file name, a language chip or a copy button. |
- Highlighting — Every app has its own highlighter and languages, so CodeBlock doesn't bundle one. Point your highlighter's CSS-variables mode at the
--code-variables instead. - A switch for wrapping — Lines always wrap at spaces, so names and paths are never split, and a line with no spaces scrolls. That works for every kind of code, so there is nothing to switch.
- A copy button, line numbers, and a collapse — Put a copy button in
topbar, and add line numbers in your highlighter. Long code scrolls withmaxLinesrather than collapsing, so no line is ever hidden. toneandemphasis— A block of code has no meaning to colour and no reason to be louder than another block. The syntax colours come from your highlighter and the--code-variables.- A Card as the panel — Code is part of the page rather than an object placed on it, so the panel uses the fill of a Surface instead of a Card.