Code Block

An opinionated, server-highlighted code block. Ships Shiki coloured by the kit's own ramps (brand keywords, accent functions, success strings, warning constants), a copy button, optional line numbers, and dual-theme dark mode with zero client-side highlighting JavaScript. CodeBlockClient draws the same block inside a client component.

Installation

$
npx shadcn@latest add https://ui.mattdowney.com/r/code-block.json

Usage

import { CodeBlock } from "@/components/ui/code-block"

Examples

Default

A block of code with a copy button. Render it from a server file: it is an async component, and a client component cannot render it.

import { Button } from "@/components/ui/button"

export function Example() {
  return <Button variant="subtle">Save changes</Button>
}

Line numbers

Prefix each line with its number via showLineNumbers.

import { Button } from "@/components/ui/button"

export function Example() {
  return <Button variant="subtle">Save changes</Button>
}

Languages

Loaded up front: tsx, ts, jsx, js, bash, css, json, html, markdown, yaml. Any other language Shiki ships, like toml here, loads the first time a block asks for it. An unknown one renders as plain text.

npx shadcn@latest add https://ui.mattdowney.com/r/button.json
{
  "name": "ui.md",
  "type": "registry:ui"
}
[package]
name = "ui-md"
version = "1.0.0"

In a client component

CodeBlockClient for code inside a client tree, such as markdown rendered in the browser. The box and copy button render at once and the colours follow; pass html from highlightCode to skip that step.

title: Design gaps
tags:
  - kit
  - registry
draft: false

API Reference

PropTypeDefaultDescription
code*CodeBlockstringNoneThe source to highlight. Leading and trailing whitespace is trimmed.
languageCodeBlockstring"tsx"Language grammar to highlight with: any language Shiki ships. The common ten load with the highlighter and the rest on first use, so they cost nothing until a block asks. An unknown language falls back to plain text.
htmlCodeBlockClientstringNoneHTML from highlightCode(code, language), an async server helper exported by code-block. With it the block arrives coloured and the browser loads no highlighter. Without it the code shows plain, then Shiki loads in the browser and colours it. The other props work as on CodeBlock.
showLineNumbersCodeBlockbooleanfalsePrefixes each line with its line number.
framedCodeBlockbooleantrueDraws the block's own surface (edge, radius, fill). Turn it off to set the code inside a surface that already has one, such as a tab in a Card; the code keeps its inset and the copy button.
classNameCodeBlockstringNoneMerged onto the frame (the Card, or the plain wrapper when framed is off).
revealCopyButton"hover" | "always""hover"hover shows the button only while the pointer or focus is inside a group/code parent (always on touch). always keeps it shown, for a box with no such parent.
onCopyCopyButton() => voidNoneCalled after the text reaches the clipboard, never on a failed write. For analytics or a toast.
code*CodeBlockstringThe source to highlight. Leading and trailing whitespace is trimmed.
languageCodeBlockstring = "tsx"Language grammar to highlight with: any language Shiki ships. The common ten load with the highlighter and the rest on first use, so they cost nothing until a block asks. An unknown language falls back to plain text.
htmlCodeBlockClientstringHTML from highlightCode(code, language), an async server helper exported by code-block. With it the block arrives coloured and the browser loads no highlighter. Without it the code shows plain, then Shiki loads in the browser and colours it. The other props work as on CodeBlock.
showLineNumbersCodeBlockboolean = falsePrefixes each line with its line number.
framedCodeBlockboolean = trueDraws the block's own surface (edge, radius, fill). Turn it off to set the code inside a surface that already has one, such as a tab in a Card; the code keeps its inset and the copy button.
classNameCodeBlockstringMerged onto the frame (the Card, or the plain wrapper when framed is off).
revealCopyButton"hover" | "always" = "hover"hover shows the button only while the pointer or focus is inside a group/code parent (always on touch). always keeps it shown, for a box with no such parent.
onCopyCopyButton() => voidCalled after the text reaches the clipboard, never on a failed write. For analytics or a toast.