component
Code
Code in a sentence, or a block of it on its own plate.
non-interactive/foreground/positive/baseFalse<Link href="/docs/">Docs</Link>
<Link tone="quiet">GitHub</Link>Trueimport { Code } from 'rost/components/code/Code'
<p>Pass <Code>tone="quiet"</Code> for a footer.</p>
<Code block>{`npm run deploy`}</Code>
<Code block html={highlighted} /> src/components/code/Code.tsx
import type { HTMLAttributes, ReactNode } from 'react'
import './code.css'
export interface CodeProps extends HTMLAttributes<HTMLElement> {
/** A block of lines on its own plate. Without it, code in a sentence. */
readonly block?: boolean
/** Code already coloured by a highlighter, as HTML. */
readonly html?: string
/** The code as plain text, when there's no `html`. */
readonly children?: ReactNode
}
const b = 'rost-code'
/** Code in a sentence, or a block of it. Built from contracts/code.contract.yaml. */
export function Code({ block = false, html, className, children, ...rest }: CodeProps) {
const cls = [b, block && `${b}--block`, className].filter(Boolean).join(' ')
const inner = html !== undefined ? { dangerouslySetInnerHTML: { __html: html } } : { children }
if (!block) return <code className={cls} {...inner} {...rest} />
// A block that scrolls can be scrolled from the keyboard.
return <pre className={cls} tabIndex={0} {...rest}><code className={`${b}__text`} {...inner} /></pre>
}
The source for everything on this page: contracts/code.contract.yaml (opens in a new tab). The CSS, the docs and the checks in each app are built from it.
# yaml-language-server: $schema=../schema/component-contract.schema.json
schemaVersion: 2
id: code
name: Code
kind: component
status: review
summary: Code in a sentence, or a block of it on its own plate.
category: display
related: [ text ]
source:
tool: figma
fileKey: 1Zy0wiWKXQAvsfDBX3KR0d
nodeId: "705:104"
url: https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=705-104
extractedAt: "2026-11-04"
extractedBy: hand
frozenAt: "2026-11-04"
props:
block:
type: boolean
default: false
notes: A block of lines on its own plate, scrolling sideways when a line is too long. Without it, code inside a sentence.
html:
type: string
notes: "Code already coloured by a highlighter (the Rost site uses Shiki), as HTML. Rost has no syntax colours of its own."
children: { type: slot, notes: "The code as plain text, when there's no `html`." }
elements:
root:
notes: "A `<code>`, or for a block a `<pre>` with the `<code>` inside."
styles:
font-family:
value: "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace"
untokenised: "Rost has no mono family token yet. Where JetBrains Mono isn't loaded, the system mono stands in."
font-size: { value: 0.88em, untokenised: "In a sentence it follows the text around it. Mono looks bigger than General Sans at the same size, so a little smaller." }
overflow-wrap: { value: anywhere, assert: false, notes: A long token path breaks instead of pushing the page wider. }
line-height: { value: "1", assert: false, notes: "Mono sits on a different baseline from General Sans, so on the text's own line height it pushed the line a pixel taller. At 1 it stays inside the line." }
text:
notes: The `<code>` inside a block.
styles:
font: { value: inherit, assert: false }
variants:
- when: { block: true }
figmaNode: "705:102"
elements:
root:
display: { value: block }
overflow-x: { value: auto, assert: false }
overflow-wrap: { value: normal, assert: false, notes: "A block scrolls instead, so the lines keep their shape." }
padding-top: { token: spacing/gap/medium, value: 16px }
padding-bottom: { token: spacing/gap/medium, value: 16px }
padding-left: { token: spacing/gap/medium, value: 16px }
padding-right: { token: spacing/gap/medium, value: 16px }
border-radius: { token: spacing/radius/medium, value: 12px }
background: { token: non-interactive/background/secondary, value: "#171717" }
color: { token: non-interactive/foreground/primary, value: "#ededed" }
font-size: { token: typography/label/font-size, value: 12px }
line-height: { token: typography/line-height/20, value: 20px }
white-space: { value: pre, assert: false }
guidance:
status: proposed
use:
- title: A name in a sentence
text: "A prop, a token path or a file name inside running text: `tone=quiet`, `contracts/link.contract.yaml`."
show: block=false
- title: What to write
text: A few lines someone will copy. Coloured by a highlighter, or plain.
show: block=true
avoid:
- title: Emphasis
text: Making a word stand out. Code means the word is exactly what you type.
- title: Very long files
text: "A whole file on a page. Show the part that matters, and link to the rest."
behaviour: |-
In a sentence it's the mono family at a slightly smaller size, with no plate, so a line full of token paths stays calm. A block gets the secondary plate, 16px inside and 12px corners, and scrolls sideways rather than wrapping.
The colours inside a block come from the highlighter, not from Rost. On the Rost site that's Shiki's vesper in dark and GitHub's high-contrast theme in light, with the comment greys made solid, so every word is 4.5:1 on the plate. Syntax colours as tokens would be the next step, and I haven't needed them in an app yet.
accessibility:
element: "`<code>`, and `<pre>` around it for a block, so a screen reader keeps the spacing."
keyboard: "A block that scrolls gets `tabindex=\"0\"`, so it can be scrolled without a mouse."
content: Exactly what someone types. No ellipsis inside a line they'd copy.
Install
npx github:Runceanu/rost add code It lands in src/rost/code/, with what it needs and its contract. Install has the rest.
When to use
A name in a sentence
A prop, a token path or a file name inside running text:
tone=quiet,contracts/link.contract.yaml.What to write
A few lines someone will copy. Coloured by a highlighter, or plain.
When not to use
Emphasis
Making a word stand out. Code means the word is exactly what you type.
Very long files
A whole file on a page. Show the part that matters, and link to the rest.
Behaviour
In a sentence it's the mono family at a slightly smaller size, with no plate, so a line full of token paths stays calm. A block gets the secondary plate, 16px inside and 12px corners, and scrolls sideways rather than wrapping. The colours inside a block come from the highlighter, not from Rost. On the Rost site that's Shiki's vesper in dark and GitHub's high-contrast theme in light, with the comment greys made solid, so every word is 4.5:1 on the plate. Syntax colours as tokens would be the next step, and I haven't needed them in an app yet.
Accessibility
- Element
<code>, and<pre>around it for a block, so a screen reader keeps the spacing. - Keyboard
A block that scrolls gets
tabindex="0", so it can be scrolled without a mouse.
Content
Exactly what someone types. No ellipsis inside a line they'd copy.
Props
| Prop | Type | Values | Default |
|---|---|---|---|
block | boolean | | false |
html | string | | |
children | slot | | |
Values
Straight from the contract. Each value sits next to its token, because six tokens here all come out as #3a3a3a and the colour alone can't tell you which one is right.
Never changes
root
-
font-family'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospaceon purpose: Rost has no mono family token yet. Where JetBrains Mono isn't loaded, the system mono stands in. -
font-size0.88emon purpose: In a sentence it follows the text around it. Mono looks bigger than General Sans at the same size, so a little smaller.
overflow-wrapanywhere · line-height1
text
fontinherit
Only in some variants
These don't change: they're just set in some variants and not others.
Every variant
-
root.background#171717non-interactive/background/secondary -
root.border-radius12pxspacing/radius/medium -
root.color#ededednon-interactive/foreground/primary -
root.displayblocklayout -
root.font-size12pxtypography/label/font-size -
root.line-height20pxtypography/line-height/20 -
root.overflow-wrapnormalFigma frame size -
root.overflow-xautoFigma frame size -
root.padding-bottom16pxspacing/gap/medium -
root.padding-left16pxspacing/gap/medium -
root.padding-right16pxspacing/gap/medium -
root.padding-top16pxspacing/gap/medium -
root.white-spaceprelayout