Code Block

Highlighted code with a file header, copy, tabs, and long blocks that fold.

Category
AI Chat
Access
Free with an account
Source
356 lines · one file
States
6 drawn

The decision inside it

A highlighter that cannot throw

The colouring is a small tokenizer in the file, not a parser or a dependency. It reads half a line as happily as a whole one, so a block that is still streaming is coloured on every render instead of flashing plain and then re-highlighting.

When to use it

Use Code Block for code in a chat answer, a doc or a settings page: highlighted, numbered, copyable, with marked lines and several files as tabs. Set maxLines so a long answer folds, and streaming while a model is still writing it. For a proposed change to existing files, use Code Diff View.

Installation

How you get it

Paste this into Claude Code, Cursor, Codex or any coding agent. It installs the file and uses it as it is, instead of writing a new one.

Use Liro's Code Block for this.
Install it: npx shadcn@latest add @liro/code-block
(the @liro registry is set up in components.json, with LIRO_KEY in .env.local)
Import it from @/components/ui/code-block.
Do not rewrite or restyle it. Pass my data through its props
and keep its built-in states.

For the full brief with every prop and the mistakes assistants usually make, use Copy prompt above. Free components install through the @liro registry with your CLI token: see the Namespaced tab.

Usage

components/answer-code.tsx
import { CodeBlock } from "@/components/ui/code-block";

export function AnswerCode({ code, done }: { code: string; done: boolean }) {
  return (
    <CodeBlock
      filename="app/api/signing/route.ts"
      language="ts"
      code={code}
      streaming={!done}
      maxLines={24}
      highlightLines={[9, 10]}
      onCopy={() => console.info("copied")}
    />
  );
}

API reference

“Copy prompt” above puts a full brief on your clipboard, install command, dependencies, props, and the mistakes an assistant usually makes with this component.

Props

Read from the component’s own TypeScript interface when the site is built, so this table cannot drift from the file you install.

TypeNotes
codestring NoThe code. Ignored when `tabs` is set.
languagestring NoThe language, for colouring and the label: "ts", "tsx", "js", "bash", "json", "py", "css", "html".
filenamestring NoShown in the header in place of the language.
showLineNumbersboolean NoNumbers down the left. Default true.
highlightLinesnumber[] NoOne-based line numbers to mark, e.g. the lines an answer changed.
maxLinesnumber NoCollapse after this many lines, with "Show N more lines". Default 0, never.
wrapboolean NoBreak long lines instead of scrolling sideways. Default false.
tabs{ label: string; code: string; language?: string }[] NoSeveral files or package managers, one tab each.
streamingboolean NoThe code is still arriving: draws a caret at the end and keeps the block expanded.
onCopy(code: string) => void NoCalled with the code after it is copied.
actionsReact.ReactNode NoExtra buttons in the header, before Copy: "Insert", "Run".
lookLook NoLayered rests the code in a tray with the header in its margin. Flat is one card. Default layered.

Dependencies

npm packages

clsxlucide-reacttailwind-merge

Liro foundations, installed alongside it

liro-theme

States

All of them are drawn by the component. None is left for you to design.

  • File
  • Marked lines
  • Collapsed
  • Tabs
  • Streaming
  • Copied

Accessibility

  • Keyboard reachable throughout, in visual order
  • Focus trapped in overlays, Escape closes them
  • Focus returns to whatever opened it
  • Live regions announce what changed, not that something did
  • Honours prefers-reduced-motion

Responsive behavior

  • Sizes to its container, not the viewport
  • Usable from 320px up
  • Pointer-based interaction, so trackpad and touch both work
  • No hover-only affordances