Skip to content

Components · Content

note:Copy source: not an npm package.

CopyableCodeBlock

A code block with a filename or language header, optional line numbers and a copy button that handles failure honestly.

  • clipboard
  • code
  • live region
Dependencies
None beyond React
Source
src/components/collection/copyable-code-block/copyable-code-block.tsx

Preview & code

Options
use-session.tstsx
export async function refreshSession(signal?: AbortSignal) {  // The HttpOnly cookie is attached by the browser; JS never sees it.  const res = await fetch("/auth/refresh", { method: "POST", credentials: "include", signal });  if (res.status === 401) return null; // rotated or expired: sign in again  return (await res.json()) as { accessToken: string };}

When to use it

Technical writing and docs need code people can copy in one action, with feedback they can trust: including when the Clipboard API is blocked, which plain copy buttons usually ignore.

Usage

  1. Copy src/components/collection/copyable-code-block/copyable-code-block.tsx into your project (from the Code tab above).
  2. Import it from where you placed it. In this repo that’s @/components/collection/copyable-code-block/copyable-code-block.
usage.tsxtsx
import { CopyableCodeBlock } from "@/components/collection/copyable-code-block/copyable-code-block";​export function InstallStep() {  return (    <CopyableCodeBlock      language="bash"      filename="terminal"      code={"npm run build\nnpm run start"}      showLineNumbers    />  );}

API

CopyableCodeBlock props
PropTypeDefaultDescription
code* (required)string—The exact text displayed and copied. A trailing newline is trimmed.
languagestring—Shown in the header (and next to the filename on wider screens).
filenamestring—Shown in the header instead of the language.
showLineNumbersbooleanfalseRenders line numbers that are never selected or copied.
tokensHighlightToken[][]—Optional pre-highlighted lines. Their text must join back to code.
maxHeightstring—CSS length. Taller code scrolls inside a focusable region.
writeText(text: string) => Promise<void>Clipboard APIOverride the clipboard write (defaults to navigator.clipboard.writeText), for custom transports or tests. Client components only.
onCopy(result: "success" | "error") => void—Called after every copy attempt, e.g. for analytics. Client components only.
classNamestring—Extra classes on the outer figure.

* required

Accessibility

Tab
Moves to the copy button, then into the code area.
Enter / Space
Copies the code (on the button).
Arrow keys
Scroll long code once the code area has focus.
  • Copy results are announced through a polite live region, not only shown visually.
  • If copying fails, the code is selected and the announcement explains the keyboard shortcut.
  • The button's accessible name includes the filename or language, so several blocks on a page stay distinguishable.
  • Line numbers are aria-hidden and unselectable; token colours meet WCAG AA contrast in both themes.

Limits & tests

Limitations

  • Highlighting is external: pass tokens from your own highlighter, or none for plain text.
  • No line wrapping option; long lines scroll horizontally by design.
  • Function props (writeText, onCopy) can only be passed from client components.

Tests

tests/e2e/components.spec.ts

  • Copies the exact source text to the clipboard and announces success
  • Falls back to selecting the code and explaining the shortcut when the clipboard is denied
  • Line numbers toggle without affecting the copied text