Code Block
Highlight code with sugar-high, with line numbers, a file name, and a copy button.
Preview
import { LocalTime, LocalTimeClock } from "@/components/ui/local-time";
export const Clock = () => (
<LocalTime timeZone="Europe/Warsaw">
<LocalTimeClock />
</LocalTime>
);import {
CodeBlock,
CodeBlockContent,
CodeBlockCopy,
CodeBlockHeader,
CodeBlockTitle,
} from "@/components/ui/code-block";
const CODE = `import { LocalTime, LocalTimeClock } from "@/components/ui/local-time";
export const Clock = () => (
<LocalTime timeZone="Europe/Warsaw">
<LocalTimeClock />
</LocalTime>
);`;
export const CodeBlockExample = () => (
<CodeBlock class="w-full max-w-lg" code={CODE} highlight={[[4, 6]]}>
<CodeBlockHeader>
<CodeBlockTitle>clock.tsx</CodeBlockTitle>
<CodeBlockCopy />
</CodeBlockHeader>
<CodeBlockContent />
</CodeBlock>
);Features
- Highlights with sugar-high, a small tokenizer with no runtime theme.
- Themes through CSS variables, so colors follow your tokens.
- Draws line numbers with CSS counters, so copied code stays clean.
- Highlights lines by number or range, and dims the rest.
- Confirms a copy with a check for a moment.
Installation
bunx --bun shadcn@latest add https://olekaleksander.dev/r/code-block-solid.jsonUsage
import {
CodeBlock,
CodeBlockHeader,
CodeBlockTitle,
CodeBlockCopy,
CodeBlockContent,
} from "@/components/ui/code-block";<CodeBlock code={code}>
<CodeBlockHeader>
<CodeBlockTitle>header.tsx</CodeBlockTitle>
<CodeBlockCopy />
</CodeBlockHeader>
<CodeBlockContent />
</CodeBlock>Highlighting lines
Pass one-based line numbers, or [start, end] ranges, to highlight. The other lines dim.
<CodeBlock code={code} highlight={[1, [4, 6]]}>
<CodeBlockContent />
</CodeBlock>API Reference
Every part takes class for styling, and parts that wrap content take children. CodeBlockHeader, CodeBlockTitle, CodeBlockCopy and CodeBlockContent take nothing else.
CodeBlock
| Prop | Type | Default |
|---|---|---|
code | string | – |
language | "tsx" | "shell" | "css" | "tsx" |
highlight | (number | [number, number])[] | – |