Data Display
Code Block
A captioned, scrollable code sample with a copy button and optional line numbers.
import { Button } from "@/components/ui/button"; export function SaveBar() { return ( <div className="flex justify-end gap-2"> <Button variant="outline">Cancel</Button> <Button>Save changes</Button> </div> );}npx aback-ui@latest add buttonInstallation
Copy the source with the CLI to own and edit it, or import it from the npm package.
npx aback-ui@latest add code-blockWrites components/ui/code-block.tsx and the components it builds on. First time in this project? Run npx aback-ui@latest init first, as the installation guide shows.
Usage
Import the parts you need from the installed file. The Code tab above has a complete, working example.
import { CodeBlock } from "@/components/ui/code-block";API reference
Props and composition points for Code Block. Native element props pass through.
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | None | The exact source. It is rendered as text and is what the copy button writes. Required. |
language / filename | string | None | Shown in the caption bar. The filename also names the scroll region. |
copyable / copyLabel | boolean / string | true / "Copy code" | Show the copy button and set its accessible name. |
showLineNumbers | boolean | false | Numbers lines with a CSS counter. Numbers are never selected or copied. |
maxHeight | number | string | None | Caps the height and scrolls, for long files. |
Dependencies: no extra packages, plus the shared @/lib/utils helper.
Accessibility
Semantics, keyboard behavior, and the decisions that keep the component operable.
- The pre element is focusable and named, so keyboard users can scroll long lines.
- The copy button is a Copy Button: it announces the result and reports a refused write.
- Code renders as text, never as injected HTML. Pass pre-highlighted markup through your own wrapper if you need colour.
- Line numbers are generated content and are not read as part of the code.
Keyboard
| Key | Action |
|---|---|
| Tab | Focus the code region, then the copy button. |
| Arrow keys | Scroll the focused code region. |
Used in production
The Aback products whose interfaces shaped this component.