docs / core / code-snippet
CodeSnippet
a code block with a copy button. the highlighting is painted from the theme's own tokens, so it never clashes with the surface it sits on the way a borrowed dark palette would.
intensity · recedesrsc · client
provenance
authored in usvalayer
core / primitivesintensity
recedes · safe anywhere, including dense surfacescomposition
docs pages, Panel bodies, Dialog bodies, anywhere prose explains codeTerminal is the one-line sibling for shell commandsnot an editorone block per thing to copy, never nesteda11y
the copy button is labelledjest-axedependencies
highlight.js · Button from the same packagelive demo · try it out
usage
import { CodeSnippet } from "@usva-ui/react/primitives/code-snippet";
<CodeSnippet
label="lib/utils.ts"
language="typescript"
code={source}
/>customize
codethe text shown and copied
languagehighlight.js grammar, plain to skip
labelheader caption, empty for no header bar
notequiet right side of the header
copyableoff for output and logs
usage
import { CodeSnippet } from "@usva-ui/react/primitives/code-snippet";
<CodeSnippet
label="usage"
code={source}
/>props
| prop | type | default | notes |
|---|---|---|---|
| code | string | — | the text shown and copied. |
| language | string | "tsx" | any grammar in highlight.js's common set. "plain" or an unknown name renders as-is. |
| label | ReactNode | — | the header caption, e.g. a file path. without it there is no header bar. |
| note | ReactNode | — | the quiet right side of the header bar. |
| copyable | boolean | true | off for output, logs, anything not meant to be pasted. |
| onCopied | (value: string) => void | — | fires after a successful copy, never on a denied one. |
| preClassName | string | — | extra classes for the scrolling pre, e.g. max-h-[32rem] for long sources. |
get it
bun add @usva-ui/reactusage
import { CodeSnippet } from "@usva-ui/react/primitives/code-snippet";
<CodeSnippet
label="lib/utils.ts"
language="typescript"
code={source}
/>