Code Block
Displays code, terminal output and diffs with an optional header, line numbers and a copy button.
src/routes/api/chat/+server.ts
import { convertToModelMessages, streamText } from "ai"import type { RequestHandler } from "./$types"
export const POST: RequestHandler = async ({ request }) => { const { messages } = await request.json()
const result = streamText({ model: "anthropic/claude-sonnet-4.5", system: "You are a helpful assistant. Answer in one short paragraph.", messages: await convertToModelMessages(messages), })
return result.toUIMessageStreamResponse()}Installation
pnpm add @heyramzi/uiUsage
import * as CodeBlock from "@heyramzi/ui/shadcn/code-block";<CodeBlock.Root>
<CodeBlock.Header>
<CodeBlock.Title>route.ts</CodeBlock.Title>
<CodeBlock.Actions>
<CodeBlock.CopyButton value={code} />
</CodeBlock.Actions>
</CodeBlock.Header>
<CodeBlock.Content {code} lineNumbers />
</CodeBlock.Root>Keyboard and accessibility
| Key | Does |
|---|---|
| Tab | Moves focus to the code, then to the copy button. |
| Arrow Left Arrow Right | With the code focused, scrolls a long line sideways. |
- The code area takes focus so a keyboard can scroll it. Its focus ring is drawn inside the frame, so a clipping wrapper can't hide it.
- The copy button is the kit's copy button, named "Copy" and announcing "Copied".
API reference
CodeBlock.Root
variant"default" | "destructive"- Default
"default" size"default" | "sm"- Default
"default" refbindableHTMLDivElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLDivElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "default" |
size | "default" | "sm" | "default" |
ref bindable | HTMLDivElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements | |
CodeBlock.Header
refbindableHTMLDivElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLDivElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
ref bindable | HTMLDivElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements | |
CodeBlock.Title
refbindableHTMLDivElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLDivElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
ref bindable | HTMLDivElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements | |
CodeBlock.Actions
refbindableHTMLDivElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLDivElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
ref bindable | HTMLDivElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements | |
CodeBlock.CopyButton
sizestring- Default
"md" ...rest- Every prop of
CopyButton
| Prop | Type | Default |
|---|---|---|
size | string | "md" |
...rest | Every prop of CopyButton | |
CodeBlock.Content
codestring- Plain text to render. Split into lines when `lineNumbers` is set.
wrapboolean- Default
false lineNumbersboolean- Default
false startLinenumber- Default
1 refbindableHTMLPreElement | null- Default
null classstringstylestringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLPreElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
code Plain text to render. Split into lines when `lineNumbers` is set. | string | |
wrap | boolean | false |
lineNumbers | boolean | false |
startLine | number | 1 |
ref bindable | HTMLPreElement | null | null |
class | string | |
style | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLPreElement> from svelte/elements | |
CodeBlock.Line
variant"default" | "added" | "removed" | "highlighted"- Default
"default" codestring | CodeBlockLineToken[]- The line as plain text, or as highlighted tokens. When it is set, `children` is ignored.
refbindableHTMLSpanElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLSpanElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
variant | "default" | "added" | "removed" | "highlighted" | "default" |
code The line as plain text, or as highlighted tokens. When it is set, `children` is ignored. | string | CodeBlockLineToken[] | |
ref bindable | HTMLSpanElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLSpanElement> from svelte/elements | |
CodeBlock.Footer
refbindableHTMLDivElement | null- Default
null classstringchildrenSnippet...rest- Every prop of
HTMLAttributes<HTMLDivElement>fromsvelte/elements
| Prop | Type | Default |
|---|---|---|
ref bindable | HTMLDivElement | null | null |
class | string | |
children | Snippet | |
...rest | Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements | |