Code block

Free

Code the agent shows you in its answer, in a grey well: the file with its folders quiet, Copy, the syntax in six picked inks and a write-in replay. A well of JSON masks any key as it renders, and Copy copies the mask.

npx shadcn@latest add https://pixilui.com/r/code-block.json

Preview

A key in the code renders masked, and Copy copies the mask, never the key.
Block
Numbers
Fold

When to use

Use it when

  • Code the agent wrote or quotes in a chat answer: a route, a config, a query.
  • A tool's arguments or result as JSON (CodeJson), with keys masked.
  • A long file: fold it at a few lines under Show all N lines.

Use something else when

  • A change to a file the person should keep or undo: use Diff.
  • A command and what it printed: use Command.
  • A plain code sample on a docs page: a pre is enough.

Installation

Terminal
npx shadcn@latest add https://pixilui.com/r/code-block.json

Adds code-block.tsx with button.tsx, kbd.tsx and tooltip.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.

Usage

example.tsx
import { CodeBlock } from "@/components/pixilui/code-block";

<CodeBlock code={code} />

Fills its container. Long lines wrap under their own column with no sideways scroll; at 390 nothing spills.

API reference

PropTypeDefault
codeRequiredstring–The code. A key in it (sk_live_…, whsec_…, ghp_…) is masked as it renders, and Copy copies the mask.
langstring"TypeScript"The language, shown when there is no path (default TypeScript).
pathstring""The file ("app/api/refunds/route.ts"): the head shows it, folders grey, the file brighter. Without it, the language.
foldnumber–Show this many lines, then a fade and "Show all N lines".
numbersbooleanfalseLine numbers (default off).
wordsPartial<CodeWords>–Every word it says.
copyIconReactNode–Copy's icon (the locked copy-check).

Also takes every native <figure> attribute, and a ref.

Keyboard

TabReaches Copy, each masked key (its tip says it copies masked), Show all, and the body when it scrolls sideways.
Enter SpaceCopies, or folds and opens the block.

Accessibility

  • A masked key is role img named with what it is and its last four (Stripe secret key, masked, ends in 4242).
  • Copy says what it copied in a status line (Copied, key masked); its tip is only a description.
  • Line numbers are aria-hidden; a body wider than the well becomes a named region that takes focus, so a keyboard can scroll it.

Motion

  • Show all: the well's height glides open and shut (the system's resize, 280ms on the out curve).
  • Copy: the icon turns into its check and the masked key lights once, its dots rippling (380ms).
  • The replay: each line writes in from its left edge a line after another (45ms apart, 380ms each) under the Writing shimmer.
  • Reduced motion: it folds, copies and writes at once.
Tool callA secret in a tool's arguments stays masked, and Copy copies the mask, never the key.
DiffKeep folds the old lines away, Undo erases the new ones, and the numbers stay right.
CommandWhile it runs, the output prints in line by line and Stop ends it with ^C.

PixilUI Pro opens soon. The first 20 licenses are $99.