CodeBlock
A code block highlighted on the server, with a copy button. Its entry is @k8ordo/ui/code-block.
Import
Highlighting happens on the server, so shiki never reaches the browser. It imports server-only, so importing it from a Client Component fails the build.
import { CodeBlock } from '@k8ordo/ui/code-block';Usage
export function Total({ items }: { items: Item[] }) {
const done = items.filter((item) => item.done).length;
return <p>{done} / {items.length}</p>;
}<CodeBlock code={source} lang="tsx" />File name
With title, the header shows it in place of the language. The header becomes the figure’s figcaption.
total.tsxexport function Total({ items }: { items: Item[] }) {
const done = items.filter((item) => item.done).length;
return <p>{done} / {items.length}</p>;
}<CodeBlock code={source} lang="tsx" title="total.tsx" />Line marks
marks marks lines by their 1-based number with highlight, add or remove. Additions and removals are shown with + and − as well as color.
const done = items.filter((item) => item.done).length;
const rate = done / items.length;
const rate = items.length === 0 ? 0 : done / items.length;<CodeBlock
code={source}
lang="ts"
marks={{ 1: 'highlight', 2: 'remove', 3: 'add' }}
/>Callouts
callouts puts a note right under a line, indented like the line. An array puts several, in order. The copy button copies code exactly, without the marks or the notes.
const done = items.filter((item) => item.done).length;
const rate = done / items.length;
const rate = items.length === 0 ? 0 : done / items.length;
Guard the division when the list is empty<CodeBlock
callouts={{ 3: 'Guard the division when the list is empty' }}
code={source}
lang="ts"
/>Colors and dark mode
The colors are fixed values matching k8o’s blog, not design tokens: shiki’s one-light in light and plastic in dark. They are written as light-dark(), so they follow the color-scheme that .dark switches. There is no outer border, and the language or the file name sits as a small label at the top of the same background. A language shiki does not know renders as plain text.
Props
code- Type:
string - Default: -
callouts?- Type:
Readonly<Record<number, string | readonly string[]>> - Default: -
lang?- Type:
string - Default:
'text' marks?- Type:
Readonly<Record<number, 'highlight' | 'add' | 'remove'>> - Default: -
title?- Type:
string - Default: -
| Prop | Type | Default |
|---|---|---|
code | string | - |
callouts? | Readonly<Record<number, string | readonly string[]>> | - |
lang? | string | 'text' |
marks? | Readonly<Record<number, 'highlight' | 'add' | 'remove'>> | - |
title? | string | - |
Inherited attributes (except the ones set internally): HTMLAttributes<HTMLElement>
The wording this component draws (labels, placeholders, and so on) comes from the message dictionary unless a prop sets it. To replace it, see: i18n