Code Block

Multi-line code with a copy button and optional line numbers.

Installation

  1. Add the registryOnce per project
  2. Add code-block

Usage

Pass onCopy to run something after a successful copy, such as showing a toast.

Examples

With a label

label adds a header, such as a file name. lineNumbers numbers each line.

save-button.tsx

Long lines

Long lines scroll sideways rather than wrapping.

.env

Syntax highlighting

Code Block doesn't highlight code itself. Highlight it with a library such as Highlights or Shiki and pass one element per line to highlightedLines.

Highlights runs in the browser too, but it adds its WebAssembly to the bundle, so highlight on the server or ahead of time where you can.

Props

CodeBlock takes the props of a <div>, apart from children.

CodeBlock