Code block preview

Code block

Code, terminal output, logs and diffs in a quiet panel: a header with the file name and a copy button, built-in highlighting, line numbers that are never copied, added / removed / highlighted lines, wrapping, and folding long blocks to a few lines.

  • Group
    Code
  • Origin
    Adapted · shadcn/ui Code Block · source
  • License
    MIT
  • Added
    Oct 23
npx shadcn@latest add @deank/code-block

The registry stays private. Install from the repo with a GitHub token — see the README.

Usage

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

<CodeBlock>Label</CodeBlock>

Props

PropTypeDefaultDescription
code?stringShortcut: the code to show. Renders a header (when title is set), a copy button and the content for you. Leave out to compose the parts yourself.
language?CodeLanguage"text"Language for the built-in highlighter when code is set.
title?ReactNodeFile name or label in the header (shortcut mode).
lineNumbers?booleanfalseNumber the lines (shortcut mode).
wrap?booleanfalseWrap long lines instead of scrolling sideways (shortcut mode).
highlight?number[]Zero-based line indexes to mark (shortcut mode).
maxLines?numberFold to this many lines with a "Show more" button (shortcut mode).
variant?CodeBlockVariant"default"destructive tints the block for errors and failed commands.
size?CodeBlockSize"default"sm is denser, for tool output inside a chat or a tool call.
children?ReactNode

Also accepts every prop of Omit<HTMLAttributes<HTMLDivElement>, "title">.

When to use

Showing code anywhere: docs and READMEs, install commands, an agent's commands and their output, error logs, and diffs of what changed. Use `size="sm"` inside chats, tool calls and tight panels.

When not to use

A live, rendered HTML page (artifact-preview); a whole tool call with arguments, status and result (tool-call-card); editable code (use an editor such as CodeMirror or Monaco); a single inline term in a sentence (use a `<code>` element).

Related

Tool call card · Artifact preview · Message actions