Code block
agentKostenlosMITCode mit Dateiname und Kopierknopf. Eigenes Syntax-Highlighting ist möglich, aber nicht nötig.
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/code-block.jsonVorschau
Live-Demo im dunklen Theme.
Code
Installieren
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/code-block.jsonMit eingetragener Registry (components.json)
npx shadcn@latest add @manniche/code-blockVerwendung
import { CodeBlock } from '@/components/code-block'
const code = `export function total(items: { price: number; qty: number }[]) {
return items.reduce((sum, i) => sum + i.price * i.qty, 0)
}`
export default function CodeBlockDemo() {
return <CodeBlock filename="lib/total.ts" code={code} lineNumbers />
}Quellcode
import { Check, Copy } from 'lucide-react'
import { useEffect, useState, type ReactNode } from 'react'
import { cn } from '@/lib/utils'
export type CodeBlockProps = {
/** The code as text. This is what the copy button copies. */
code: string
/** Shown in the header, e.g. "tsx" or "bash". */
language?: string
/** Shown in the header instead of the language, e.g. "src/app.tsx". */
filename?: string
/** Already highlighted markup (from Shiki or similar). Without it the plain code is shown. */
children?: ReactNode
/** Number the lines. */
lineNumbers?: boolean
/** Visible text and screen reader text. Keys: `copy`, `copied` and `code` (the header and region name when there is no filename or language). */
labels?: { copy?: string; copied?: string; code?: string }
/** Classes for the outer figure. */
className?: string
}
/**
* Code with a header and a copy button. It does no highlighting itself, so it adds no weight;
* pass highlighted markup as children if you want colours.
*/
export function CodeBlock({ code, language, filename, children, lineNumbers = false, labels = {}, className }: CodeBlockProps) {
const { copy: copyLabel = 'Copy', copied: copiedLabel = 'Copied', code: codeLabel = 'Code' } = labels
const title = filename ?? language ?? codeLabel
const [copied, setCopied] = useState(false)
useEffect(() => {
if (!copied) return
const id = setTimeout(() => setCopied(false), 1800)
return () => clearTimeout(id)
}, [copied])
async function copy() {
try {
await navigator.clipboard.writeText(code)
setCopied(true)
} catch {
// The clipboard can be blocked (an insecure page, a denied permission). The code can still be selected by hand.
}
}
const lines = code.replace(/\n$/, '').split('\n')
return (
<figure className={cn('overflow-hidden rounded-2xl border bg-card text-card-foreground', className)}>
<figcaption className="flex items-center justify-between gap-3 border-b bg-muted/50 py-1 pr-1 pl-4">
<span className="truncate font-mono text-[0.8125rem] text-muted-foreground">{title}</span>
<button
type="button"
onClick={copy}
className="inline-flex min-h-11 shrink-0 items-center gap-1.5 rounded-xl px-3 text-sm text-muted-foreground transition-colors duration-150 hover:bg-muted hover:text-foreground motion-reduce:transition-none"
>
{copied ? <Check className="size-4 text-success" aria-hidden /> : <Copy className="size-4" aria-hidden />}
<span aria-live="polite">{copied ? copiedLabel : copyLabel}</span>
</button>
</figcaption>
<pre className="max-h-[28rem] overflow-auto py-3 font-mono text-[0.8125rem] leading-6" tabIndex={0} role="region" aria-label={title}>
{children ? (
<div className="px-4">{children}</div>
) : lineNumbers ? (
<code className="grid grid-cols-[auto_1fr]">
{lines.map((line, i) => (
<span key={i} className="contents">
<span className="pr-4 pl-4 text-right text-muted-foreground select-none" aria-hidden>
{i + 1}
</span>
<span className="pr-4">{line || ' '}</span>
</span>
))}
</code>
) : (
<code className="block px-4">{code}</code>
)}
</pre>
</figure>
)
}Props
CodeBlockProps
| Prop | Typ | Standard | Was sie tut |
|---|---|---|---|
code erforderlich | string | – | The code as text. This is what the copy button copies. |
language | string | – | Shown in the header, e.g. "tsx" or "bash". |
filename | string | – | Shown in the header instead of the language, e.g. "src/app.tsx". |
children | ReactNode | – | Already highlighted markup (from Shiki or similar). Without it the plain code is shown. |
lineNumbers | boolean | false | Number the lines. |
labels | { copy?: string; copied?: string; code?: string } | {} | Visible text and screen reader text. Keys: copy, copied and code (the header and region name when there is no filename or language). |
className | string | – | Classes for the outer figure. |
A11y
Tastatur
The copy button is a native button. The code area is a scrollable pre with tabIndex 0, so it can be focused and scrolled with the keyboard.
Screenreader
The block is a figure with the file name, else the language, else Code as its caption. The code area is a region with that same name, so a focused scroll area is announced with a name. The button text lives in an aria-live polite span, so Copy becomes Copied, which resets after 1.8 seconds. The words come from labels (copy, copied, code). Line numbers are aria-hidden and unselectable. If the clipboard is blocked, nothing changes and the code can still be selected by hand.
Reduzierte Bewegung
Under reduced motion the colour transition on the copy button is off.