Code Block
Styled code block with header, line numbers, tabs, and copy.
A compact 14px code block with a filename/language header, optional line numbers, lightweight syntax highlighting, and full-source copy. Pass tabs for switchable examples. Blocks longer than 16 lines initially show 8 lines, with an accessible expand/collapse control; copy always uses the full active tab. Line numbers are off by default.
Which CodeBlock? This one is dependency-free — it highlights with a small built-in tokenizer, so it costs nothing to render a snippet in a marketing page or a docs card.
CodeBlockfromyunui/contentis the Shiki-backed one: VS Code-grade highlighting, line highlighting and an edit action, at the cost of loading a grammar. Use that for real code, this for illustration.
Import
import { CodeBlock } from "@yuhuanowo/yunui/patterns";Basic
CodeBlock
import { Button } from "@yuhuanowo/yunui";
export function App() {return <Button variant="primary">Click me</Button>;}With a filename
Set filename to show a file label instead of the language badge, and showLineNumbers={false} to hide the gutter.
Filename
const greeting = "Hello, YunUI";Tabs
Provide a tabs array to switch between related snippets in one block.
Tabs
npm install @yuhuanowo/yunuiProps
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | — | |
| code* | string | — | The code to display (ignored when `tabs` are provided). |
| copyable | boolean | true | Show the copy-to-clipboard button. |
| filename | string | — | Filename shown in the header instead of the language badge. |
| language | string | text | Language label, also used by the lightweight highlighter. |
| showLineNumbers | boolean | false | Show line numbers in the gutter. |
| tabs | Tab[] | — | Render multiple switchable code tabs instead of a single `code` block. |