Skip to main content

CodeBlock

Conversationai

Shiki-highlighted code block with a copy button, filename and language selector.

import { CodeBlock } from "@elabs-ai/components-ai";
Install
npm install @elabs-ai/components-tokens @elabs-ai/components-ui @elabs-ai/components-ai

Skipping the @source line is the most common first failure: Tailwind does not scan node_modules, so every component renders unstyled. The one-time wiring

Prompt for your coding agent

Paste it into Claude Code, Cursor, Codex or any other coding agent. Text in [brackets] is yours to fill in.

Add the brand-ui CodeBlock component (from @elabs-ai/components-ai) to this project.

1. Read https://elabs-ai.com/llms.txt first: it maps every package and the rules. If you can use MCP servers, connect https://elabs-ai.com/mcp and use its search, docs and tokens tools. If not, run the same lookups in the terminal: `npx -y @elabs-ai/components-cli search "<what you need>"` and `npx -y @elabs-ai/components-cli docs <Component>`.
2. Look CodeBlock up before writing code: `npx -y @elabs-ai/components-cli docs CodeBlock` prints its real props, variants, what it pairs with and what to avoid.
3. If @elabs-ai/components-ai is not installed yet: `pnpm add @elabs-ai/components-tokens @elabs-ai/components-ui @elabs-ai/components-ai` (or `npm install @elabs-ai/components-tokens @elabs-ai/components-ui @elabs-ai/components-ai`). If this is the first brand-ui package in the project, wire it once: import `@elabs-ai/components-tokens/styles.css` plus the `themes/light.css` and `themes/dark.css` stylesheets in the CSS entry, add one Tailwind `@source` line per installed @elabs-ai package (Tailwind does not scan node_modules, so without it everything renders unstyled), and wrap the app root in `ThemeProvider` from `@elabs-ai/components-tokens`.
4. Use it here: [the screen or file, and what it should show]
5. Rules: use only @elabs-ai/components-* components and the semantic tokens — no raw colours, no raw font sizes, no arbitrary Tailwind values. Look up each component's real props with the docs tool before you use it; never guess a prop.
6. Done means: the typecheck passes, `npx -y @elabs-ai/components-cli audit src --strict` reports nothing, and you have opened the app and looked at every screen you touched in the light and the dark theme.

Avoid

  • Rendering a produced document with it — markdown assets go through MarkdownView; CodeBlock is for code.
  • Hand-rolling a copy button — CodeBlockCopyButton already handles the copied-state swap and its label.
  • Loading the highlighter eagerly on a page that may never show code — Shiki is a heavy dependency (the `eager-heavy-deps` rule in `pnpm check`).

Works with

Contains

  • CodeBlockHeader
  • CodeBlockContent
  • CodeBlockCopyButton

Pairs with

Avoid next to

  • a document the user is meant to READ — that is MarkdownView

Examples

4 live examples

Dark Theme

Markup Tags

isStreaming

API

CodeBlock

PropTypeDescription
coderequiredstring
languagerequiredBundledLanguage
showLineNumbersboolean
wrapbooleanSoft-wrap long lines instead of scrolling horizontally. Use in narrow embeds (e.g. a side rail) so content is not silently clipped. Default: false.
isStreamingbooleancode is arriving incrementally (loading-states.md isStreaming) — code keeps rendering progressively (build-up), NOT a skeleton, since a streaming code block is not fetch-then-show. Shows a Shimmer generating cue below the content as the in-progress affordance. @default false

Also accepts: HTMLAttributes<HTMLDivElement>

CodeBlockCopyButton

PropTypeDescription
onCopy() => void
onError(error: Error) => void
timeoutnumber

Also accepts: ComponentProps<typeof Button>

CodeBlockLanguageSelector

Also accepts: ComponentProps<typeof Select>

CodeBlockLanguageSelectorContent

Also accepts: ComponentProps<typeof SelectContent>

CodeBlockLanguageSelectorItem

Also accepts: ComponentProps<typeof SelectItem>

CodeBlockLanguageSelectorTrigger

Also accepts: ComponentProps<typeof SelectTrigger>

CodeBlockLanguageSelectorValue

Also accepts: ComponentProps<typeof SelectValue>