Code Block

Composable code blocks with copying, line numbers, token styling, and diff states.

Loading preview…

Installation

Usage

Compose the header, content, and footer independently. Pass plain text to Content, or compose Line elements for diff states and styled token arrays. CopyButton copies its explicit value.

1<script lang="ts">
2  import * as CodeBlock from "~/components/code-block";
3  const code = "npm install svelte";
4</script>
5
6<CodeBlock.Root>
7  <CodeBlock.Header>
8    <CodeBlock.Title>Terminal</CodeBlock.Title>
9    <CodeBlock.Actions><CodeBlock.CopyButton value={code} /></CodeBlock.Actions>
10  </CodeBlock.Header>
11  <CodeBlock.Content {code} lineNumbers />
12</CodeBlock.Root>

Root supports variant="default" | "destructive" and size="default" | "sm". Content supports wrap, lineNumbers, and startLine. Line supports default, added, removed, and highlighted variants. Token arrays accept content, style, and class; syntax highlighting is supplied by the consumer.