Skip to content
Eskais UI

Editor

Code Block

CodeBlock and CodeTabs: syntax highlighting for ts, tsx, js, jsx, json, css, html, bash, md and diff with Lezer parsers loaded on first use, rendered as React elements in syntax tokens that hold 4.5:1; a filename header, copy (an agent action too), line numbers, highlighted lines, wrap and a max height. Code for inline code.

Example

save.tsx
import { Button } from "@/components/ui/button"
export function Save({ onSave }: { onSave: () => Promise<void> }) {  // A returned Promise keeps the button pending until it settles  return <Button onClick={onSave}>Save changes</Button>}
@layer ui-sdk;@import "tailwindcss";

Inline, npx shadcn add @eskais/code-block reads as code inside a sentence.

Installation

npx shadcn@latest add @eskais/code-block
npm packages@lezer/common@1.5.5@lezer/highlight@1.2.5@lezer/javascript@1.5.6@lezer/json@1.0.3@lezer/css@1.3.9@lezer/html@1.3.13@lezer/markdown@1.8.1lucide-react

Usage

Import
import {  highlight,  syntaxHighlighter,  loadParser,  needsParser,  normalizeLanguage,  parserFor,  syntaxToneOf,  watchParser,} from "@/components/ui/code-highlight"import { codeBlock } from "@/components/ui/code-block-primitive"import { Code, CodeBlock, normalizeLanguage } from "@/components/ui/code-block"import { CodeTabs } from "@/components/ui/code-tabs"

Use CodeBlock for any code you show: a filename title, copy, line numbers, highlighted lines, wrap and maxHeight. CodeTabs holds several files; Code is inline code.

API reference

CodeBlock

PropTypeDefault
coderequiredstring–
languagets, tsx, js, jsx, json, css, html, bash/sh, md, diff or plaintext; aliases like "typescript" work too.CodeLanguage | (string & {})–
titleA filename or caption in a header above the code.ReactNode–
actionsContent at the end of the header, before the copy button, like a language badge.ReactNode–
copyableShows a copy button. Agents can always call the block's `copy` action.booleantrue
onCopyCodeBlockOptions["onCopy"]–
lineNumbersbooleanfalse
highlightLinesOne-based line numbers or inclusive ranges, marked with a fill and a start edge.readonly LineRange[]–
wrapbooleanfalse
maxHeightA CSS length, like "24rem"; the code scrolls inside past it.string–
labelsPartial<CodeBlockLabels>–

Also takes <div> attributes except children, title, onCopy.

Code

Takes <code> attributes.

CodeTabs

PropTypeDefault
filesrequiredreadonly CodeFile[]–
aria-labelrequiredNames the tab list, like "Installation files".string–
valuestring–
defaultValuestring–
onValueChange(value: string) => void–

Also takes Pick< CodeBlockProps, "lineNumbers" | "wrap" | "maxHeight" | "copyable" | "onCopy" | "labels" | "className" >.

Other exports

highlightsyntaxHighlighterloadParserneedsParsernormalizeLanguageparserForsyntaxToneOfwatchParsercodeBlocktype CodeLanguagetype CodeLinetype CodeTokentype DiffKindtype SyntaxTonetype CodeBlockCopyDetailstype CodeBlockOptionstype CodeBlockLabelstype CodeBlockPropstype CodePropstype LineRangetype CodeFiletype CodeTabsProps

Accessibility

  • Highlighting is React elements in syntax tokens, each 4.5:1 on the code surface and on a highlighted line, in both modes and every palette.
  • A block that scrolls takes focus so the keyboard can scroll it; line numbers are hidden from screen readers and from selection.
  • Copying announces Copied in a status region; highlighted and diff lines also carry a start edge or their +/- sign, not colour alone.

Agent actions

Each block is a group named by its title (or aria-label) whose value is the code, with a copy action. The copy button is hidden from agents so every block's copy has one name.

Runs agent.snapshot() from @ui-sdk/react/agent on the live preview above.