# Code Block

Displays code, terminal output and diffs with an optional header, line numbers and a copy button.

Section: Components / Data display
Page: https://ui.heyramzi.com/components/code-block
Import path: `@heyramzi/ui/shadcn/code-block`

## Import

```ts
import * as CodeBlock from "@heyramzi/ui/shadcn/code-block";
import { useHighlightedCode } from "@heyramzi/ui/shadcn/hooks/use-highlighted-code.svelte";
import { Button } from "@heyramzi/ui/shadcn/button";
```

## Usage

```svelte
<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>
```

## Demo

```svelte
<script lang="ts">
	import { ArrowBendDownLeft, DownloadSimple, FileCode } from "phosphor-svelte";
	import * as CodeBlock from "@heyramzi/ui/shadcn/code-block";
	import { useHighlightedCode } from "@heyramzi/ui/shadcn/hooks/use-highlighted-code.svelte";
	import { Button } from "@heyramzi/ui/shadcn/button";

	const code = `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()
}`;

	function download(value: string, filename: string) {
		const url = URL.createObjectURL(new Blob([value], { type: "text/plain" }));
		const link = document.createElement("a");
		link.href = url;
		link.download = filename;
		link.click();
		URL.revokeObjectURL(url);
	}

	let wrap = $state(false);

	const lines = useHighlightedCode(
		() => code,
		() => "ts"
	);
</script>

<div class="w-full max-w-lg">
	<CodeBlock.Root>
		<CodeBlock.Header>
			<CodeBlock.Title>
				<FileCode aria-hidden="true" />
				src/routes/api/chat/+server.ts
			</CodeBlock.Title>
			<CodeBlock.Actions>
				<Button
					variant="ghost"
					size="sm"
					icon
					aria-label="Wrap lines"
					aria-pressed={wrap}
					class="aria-pressed:text-foreground"
					onclick={() => (wrap = !wrap)}
				>
					<ArrowBendDownLeft aria-hidden="true" />
				</Button>
				<Button
					variant="ghost"
					size="sm"
					icon
					aria-label="Download"
					onclick={() => download(code, "+server.ts")}
				>
					<DownloadSimple aria-hidden="true" />
				</Button>
				<CodeBlock.CopyButton value={code} />
			</CodeBlock.Actions>
		</CodeBlock.Header>
		<CodeBlock.Content {wrap} lineNumbers>
			{#each lines.current as line, index (index)}
				<CodeBlock.Line
					variant={index >= 6 && index <= 10 ? "highlighted" : "default"}
					code={line}
				/>
			{/each}
		</CodeBlock.Content>
	</CodeBlock.Root>
</div>
```

## API

### CodeBlock.Root

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `variant` | `"default"` \| `"destructive"` | `"default"` |  |
| `size` | `"default"` \| `"sm"` | `"default"` |  |
| `ref` | `HTMLDivElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLDivElement>` from `svelte/elements`.

### CodeBlock.Header

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `ref` | `HTMLDivElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLDivElement>` from `svelte/elements`.

### CodeBlock.Title

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `ref` | `HTMLDivElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLDivElement>` from `svelte/elements`.

### CodeBlock.Actions

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `ref` | `HTMLDivElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLDivElement>` from `svelte/elements`.

### CodeBlock.CopyButton

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `size` | `string` | `"md"` |  |

Every prop of `CopyButton`.

### CodeBlock.Content

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `code` | `string` |  | Plain text to render. Split into lines when `lineNumbers` is set. |
| `wrap` | `boolean` | `false` |  |
| `lineNumbers` | `boolean` | `false` |  |
| `startLine` | `number` | `1` |  |
| `ref` | `HTMLPreElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `style` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLPreElement>` from `svelte/elements`.

### CodeBlock.Line

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `variant` | `"default"` \| `"added"` \| `"removed"` \| `"highlighted"` | `"default"` |  |
| `code` | `string \| CodeBlockLineToken[]` |  | The line as plain text, or as highlighted tokens. When it is set, `children` is ignored. |
| `ref` | `HTMLSpanElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLSpanElement>` from `svelte/elements`.

### CodeBlock.Footer

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `ref` | `HTMLDivElement \| null` | `null` | bindable |
| `class` | `string` |  |  |
| `children` | `Snippet` |  |  |

Every prop of `HTMLAttributes<HTMLDivElement>` from `svelte/elements`.
