Skip to content
h v3.13.3
Search the docs
Find a component, section or page.

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/ui

Usage

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"
ref bindable HTMLDivElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLDivElement> from svelte/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

ref bindable HTMLDivElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements
Prop Type Default
ref bindable HTMLDivElement | null null
class string
children Snippet
...rest Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements

CodeBlock.Title

ref bindable HTMLDivElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements
Prop Type Default
ref bindable HTMLDivElement | null null
class string
children Snippet
...rest Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements

CodeBlock.Actions

ref bindable HTMLDivElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements
Prop Type Default
ref bindable HTMLDivElement | null null
class string
children Snippet
...rest Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements

CodeBlock.CopyButton

size string
Default "md"
...rest
Every prop of CopyButton
Prop Type Default
size string "md"
...rest Every prop of CopyButton

CodeBlock.Content

code string
Plain text to render. Split into lines when `lineNumbers` is set.
wrap boolean
Default false
lineNumbers boolean
Default false
startLine number
Default 1
ref bindable HTMLPreElement | null
Default null
class string
style string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLPreElement> from svelte/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"
code string | CodeBlockLineToken[]
The line as plain text, or as highlighted tokens. When it is set, `children` is ignored.
ref bindable HTMLSpanElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLSpanElement> from svelte/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
ref bindable HTMLDivElement | null
Default null
class string
children Snippet
...rest
Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements
Prop Type Default
ref bindable HTMLDivElement | null null
class string
children Snippet
...rest Every prop of HTMLAttributes<HTMLDivElement> from svelte/elements