Avara UI

Shadcn Code Block

A soft, animated code block with a sliding file switcher, line numbers, line highlights and one-click copy.

Applies to this page's previews only

About Shadcn Code Block

What it is. Shadcn Code Block (CodeBlock) is a single code viewer for docs, tutorials and install guides. It shows a file chip or a sliding file switcher, line numbers, line marks, folding and a copy button.

How it is built. A built-in tokenizer colours js/ts/tsx/jsx, json, css, html, bash and python, and renders plain React nodes, so no highlighter library and no site stylesheet is needed. The text colours follow light and dark mode, and the surface follows your theme tokens. Motion is GSAP: the header and lines blur in once on load, and again when you switch files or unfold.

When to use it. Use it wherever readers copy code. For a terminal window use Terminal.

For AI agents

Hand your agent this brief or the Markdown page, not a screenshot.

When to use

  • Developer docs, tutorials, install steps and changelogs
  • Several related files, or package-manager commands, behind one switcher

When not to use

  • A terminal window with window chrome: use Terminal
  • Requests the reader can send and inspect: use API Playground

Agent notes

  • CodeBlock takes code, language (default "tsx") and fileName. Pass tabs instead of code for several files.
  • A tabs item is { code, language?, fileName?, label?, marks? }. language falls back to the block's language, and label overrides fileName on the tab.
  • marks is Record<number, "add" | "remove" | "focus">, keyed by 1-based line number.
  • Line numbers default to on, and off for shell snippets, which show a $ prompt instead. Override with showLineNumbers.
  • Files longer than maxLines + 3 (default 16) fold. Unknown languages use the JavaScript rules.
  • Copy always copies the raw source, without line numbers, marks or the $ prompt.
Install
npx shadcn@latest add https://avaraui.com/r/code-block.json
Registry name
@avara-ui/code-block, through the shadcn MCP server
Index
llms.txt lists every page, or add the agent skill once

Props Reference

PropTypeDefaultDescription
codestring-The raw source for a single file. Ignored when tabs is not empty.
languagestring"tsx"Picks the colours, the icon and the language label. Case-insensitive.
fileNamestring-The label in the header chip. Without it the chip shows the language name.
tabs{ code: string; language?: string; fileName?: string; label?: string; marks?: Record<number, LineMark> }[]-Groups several files behind one sliding-pill switcher.
marksRecord<number, "add" | "remove" | "focus">-Line marks for the single-file form, keyed by 1-based line number.
showLineNumbersbooleanon, off for shellShows the line-number gutter.
maxLinesnumber16Lines shown before a long file folds. 0 never folds.

Works with other components

Does CodeBlock require Prism or Shiki?

No. It runs a small built-in tokenizer, so there is nothing to configure and no highlighter in your bundle. It colours tokens by pattern and does not parse the language the way Shiki does, so rare syntax may be coloured loosely.

Which languages does CodeBlock highlight?

js, jsx, ts, tsx, json, css, html (and xml, svg), bash (and sh, shell, zsh), and python. text and markdown stay plain. Any other value uses the JavaScript rules.

How do I show several files or package managers?

Pass tabs. Each tab needs code. Use fileName for file tabs or label for short names such as npm and pnpm. The icon, colours, language label and copy button follow the active tab, and the arrow keys, Home and End move between tabs.

How do I highlight or diff lines?

Pass marks with 1-based line numbers: add and remove tint the line green or red and show a + or −, and focus adds a quiet tint. A tabbed block takes marks on each tab.

What does the copy button copy?

The active file's raw code, with no line numbers, marks or shell prompt. It uses the Clipboard API and falls back to a hidden textarea on non-secure pages. The icon turns into a check for two seconds.

Is it safe to show untrusted code?

Yes. Tokens are rendered as React text, never as injected HTML, so tags inside a snippet appear literally and do not run.