npx logic2b@next add code-blockInstallation
npx logic2b@next add code-blockUsage
import { CodeBlock } from "@/components/ui/code-block"
<CodeBlock language="tsx" code={`const answer = 42`} />
The copy button writes code to the clipboard and shows a checkmark for two
seconds. Omit language to render a bare block with a floating copy button in
the corner.
Highlighting
CodeBlock is deliberately dependency-free and does not ship a syntax
highlighter. To add highlighting, render your highlighter’s output inside the
<pre> — e.g. pass Shiki/Prism-generated HTML — or wrap this component. Keeping
the base free of a highlighter means it stays tiny and framework-agnostic.
Live playground
Edit the component props and inspect the exact JSX before copying it. The preview loads only when this section enters the viewport.
<CodeBlock
code="const ready = true\nconsole.log(ready)"
language="ts"
/>const ready = true
console.log(ready)API reference
Generated from the public TypeScript exports in src/ui/code-block.tsx.
CodeBlockProps
Typeinterface CodeBlockProps extends Omit<React.ComponentProps<"div">, "children"> { /** The code to render and copy. */ code: string /** Optional language label shown in the header (display only — no highlighting). */ language?: string }CodeBlock
ComponentA styled code container with a one-click copy button. Dependency-free (no syntax highlighter): renders the code verbatim in a `<pre>` and copies it via the Clipboard API, with a copied-state affordance.
Props: CodeBlockProps| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
code | string | Yes | — | The code to render and copy. |
language | string | No | — | Optional language label shown in the header (display only — no highlighting). |
Accessibility contract
Machine-readable behavior and consumer responsibilities for code-block.
- Support
- Native semantics
- Pattern
- code region with copy action
- HTML pre/code/button
| Keys | Behavior |
|---|---|
| Enter Space | Activate the focused control. |
Built-in semantics
- Code uses pre/code semantics and the copy button updates its accessible label after success.
Consumer responsibilities
- Provide a language label when it helps comprehension and surface copy failures in application context if critical.