How to build an accessible CodeSnippet in React

Code is rendered in semantic <code>/<pre>. The copy button is a real button with an aria-label that flips between the copy and copied messages so the action and its result are announced. Line numbers are aria-hidden so they are not read as content.

When to use a CodeSnippet

When not to use it

Keyboard interactions

Role group, verified at WCAG 2.2-AA.

Common mistakes

Avoid: <CodeSnippet variant="inline" code={"line1\nline2"} />

Prefer: <CodeSnippet variant="multi" code={"line1\nline2"} />

The inline variant is a single in-text chip; multi-line content belongs in the multi variant

Example

<CodeSnippet code="npx cascivo add button" language="bash" />

See the full CodeSnippet reference →