Code Block
A readable, copyable source-code surface with optional tokenization.
Code Block displays source, terminal output, logs, and diffs. Use the automatic API for a concise highlighted block or compose its parts when the header, actions, lines, and footer need application-specific controls.
const answer = 42;return answer;Import
Section titled “Import”import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockFooter, CodeBlockHeader, CodeBlockLine, CodeBlockTitle,} from "@zentara-co/zenos-ui";Composition
Section titled “Composition”Use CodeBlock.Header for a title and persistent actions. CodeBlock.Content is keyboard-focusable and always reads left to right, while the header continues to follow the document direction.
$ pnpm build> zenos build ✓ Compiled successfully✓ Generated component documentation<CodeBlock> <CodeBlock.Header> <CodeBlock.Title>Terminal</CodeBlock.Title> <CodeBlock.Actions> <CodeBlock.CopyButton value={output} /> </CodeBlock.Actions> </CodeBlock.Header> <CodeBlock.Content>{output}</CodeBlock.Content> <CodeBlock.Footer>Completed</CodeBlock.Footer></CodeBlock>Passing code, language, and the existing display props remains the shortest way to render tokenized source. Consumer code can migrate to composition only when it needs the additional slots.
Actions and wrapping
Section titled “Actions and wrapping”Place CodeBlock.Actions directly under the root to float it at the logical top end. Floating actions appear on hover, keyboard focus, and touch devices. Wrapping is controlled so applications can connect it to any button or preference.
Error: The generated response exceeded the available context window. Remove older messages or summarize the conversation before trying again.Line numbers and diffs
Section titled “Line numbers and diffs”For plain string content, set lineNumbers and optionally startLine on CodeBlock.Content. For per-line emphasis, compose CodeBlock.Line with variant="highlighted", "added", or "removed". Diff markers and line numbers are excluded from text selection.
return <p>Hello, {name}</p>;return <p className="text-sm">Hello, {name}!</p>;// Keep the new greeting compact.Automatic line numbers and highlights
Section titled “Automatic line numbers and highlights”Use line numbers for source references and highlightLines to direct attention to a specific change.
type Finding = { id: string; severity: "low" | "high";}; const finding: Finding = { id: "F-1042", severity: "high",};The sm, md, and lg sizes change the code type scale and line height.
const size = "sm";const size = "md";const size = "lg";Containers
Section titled “Containers”card provides complete surface chrome. section removes the outer border and background for embedding in an existing surface.
pnpm add @zentara-co/zenos-uipnpm add @zentara-co/zenos-uiLong content
Section titled “Long content”Wrap long lines when horizontal scrolling would interrupt the surrounding layout. Collapsible blocks keep lengthy examples compact while preserving an accessible disclosure control.
const notification = "This long line wraps inside narrow containers instead of requiring horizontal scrolling.";const rule1 = evaluateControl("security-control-1");const rule2 = evaluateControl("security-control-2");const rule3 = evaluateControl("security-control-3");const rule4 = evaluateControl("security-control-4");const rule5 = evaluateControl("security-control-5");const rule6 = evaluateControl("security-control-6");const rule7 = evaluateControl("security-control-7");const rule8 = evaluateControl("security-control-8");const rule9 = evaluateControl("security-control-9");const rule10 = evaluateControl("security-control-10");const rule11 = evaluateControl("security-control-11");const rule12 = evaluateControl("security-control-12");Use a max-h-* and overflow-y-auto class on composed CodeBlock.Content when a log needs a fixed height. Put a show-more control in CodeBlock.Footer when expanding content is more appropriate than an inner scrollbar.
Destructive output
Section titled “Destructive output”Set variant="destructive" on the root for failed commands, stack traces, or other error output. The treatment uses semantic danger tokens in both light and dark themes.
The header and floating actions use logical positioning and follow the page direction. Code content always uses dir="ltr" so source, terminal output, and line gutters retain their expected reading order.
CodeBlock.Root CodeBlock.Header CodeBlock.Title CodeBlock.Actions CodeBlock.CopyButton CodeBlock.Content CodeBlock.Line CodeBlock.Footer
API
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | — | Source rendered in the block. |
children | ReactNode | — | Composable header, actions, content, lines, and footer. |
variant | default | destructive | default | Semantic surface treatment for ordinary or failed output. |
language | string | plaintext | Language label and tokenizer hint. |
title | string | — | Optional header title. |
size | sm | md | lg | md | Code type scale. |
hasLanguageLabel | boolean | true | Shows the normalized language name in the header. |
hasLineNumbers | boolean | false | Shows line-number gutter. |
hasCopyButton | boolean | true | Shows the copy action. |
highlightLines | number[] | [] | One-based line numbers to emphasize. |
onCopy | () => void | — | Runs after the source is copied successfully. |
container | card | section | card | Outer chrome. |
Content lineNumbers / startLine | boolean / number | false / 1 | Numbers composed string content from the requested first line. |
Content wrap | boolean | false | Wraps composed output instead of scrolling it horizontally. |
Line variant | default | highlighted | added | removed | default | Emphasizes a line or renders it as part of a diff. |
isWrapped / isCollapsible | boolean | false | Long-line and long-snippet behavior. |
collapsibleThreshold | number | 10 | Minimum line count before the collapse control appears. |
maxHeight / width | number | string / string | — | Constrains the scroll pane height and root width. |
highlightMode | auto | ranges | spans | auto | Identifies how highlighting metadata is interpreted. |
tokenizer | CodeBlockTokenizer | — | Optional syntax tokenizer. |
Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props,
including className, style, aria-*, and data-*.
Accessibility
Section titled “Accessibility”Code is rendered with native pre and code elements. The scrollable content is keyboard-focusable, line numbers and diff markers are hidden from assistive technology, and copy controls announce both “Copy code” and their successful “Copied” state. Collapsible automatic blocks expose their expanded state with aria-expanded.
The composable behavior is informed by the shadcn Code Block while retaining Zenos tokenization, semantic colors, sizing, and legacy convenience props.