Code Block
Compound code display with three visual variants (hairline, bare, terminal), per-line diff/highlight states, an optional copy button, and syntax highlighting through a caller-provided lowlight instance. Renders as a <figure> with accessible name resolution via aria-labelledby (CodeBlock.Label) or aria-label fallback.
Installation
dgadd is not public on npm yet. Until the first release, pack @diffgazer/add from the repository and install that tarball into this app, which is what puts dgadd on pnpm exec.
The hosted registry is not public yet because r.b4r7.dev does not resolve. Use this source checkout or a local registry preview until the endpoint returns 200.
Diffgazer packages are not yet published to npm. Until the first release, pack @diffgazer/ui and @diffgazer/keys from the repository and install those tarballs.
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS once packages are available.
Usage
Examples
Hairline
Bare
Terminal with pane strip
Diff & Highlight states
Syntax highlighting
API Reference
CodeBlock
CodeBlockHeader
CodeBlockLabel
CodeBlockContent
CodeBlockLine
CodeBlockCopyButton
CodeBlockHighlight
Data attributes
Syntax highlighting
CodeBlock is highlighter-agnostic. Its core parts render structure — header, scrollable area, line numbers, copy button — but do not tokenize code. Pick one of three patterns to color output.
Pre-tokenized lines
Tokenize at build time and pass CodeBlockToken[] to each CodeBlockLine. Every span gets an inline color, so it renders without extra CSS:
This is the pattern used by <UsageSnippet /> above — tokens are generated at build time via Shiki, with colors mapped to CSS variables.
Shiki HTML with CSS variables
If a highlighter emits its own HTML — for example fumadocs-mdx or rehype-pretty-code with defaultColor: false — tokens arrive as spans that reference custom properties:
Add one CSS rule that resolves those properties to a real color, then apply the matching class to any ancestor inside CodeBlock:
.shiki is a descendant selector, so the class can sit on CodeBlockContent and apply to every token span the highlighter emits inside it. Define --code-keyword, --code-string, --code-comment, etc. in your theme to match your palette.
CodeBlockHighlight
Use CodeBlockHighlight when you want runtime syntax coloring through lowlight. Package consumers import it from @diffgazer/ui/components/code-block/highlight; copy, dgadd, and direct registry consumers add the separate ui/code-block-highlight item. Create a lowlight instance with the language set you need and pass it explicitly; the component does not load grammars at runtime.
Accessibility
Keyboard Navigation
CodeBlock.Content uses ScrollArea for the inner region. When content overflows, users can Tab to the code region and scroll it with keyboard keys.
| Key | Action |
|---|---|
| Tab | Moves focus to the scrollable code region or copy button. |
| Arrow / Page / Home / End | Scrolls the focused code region through ScrollArea keyboard handling. |
| Enter / Space | Activates CodeBlock.CopyButton when focused. |
Notes
Variants
variant="hairline" (default) renders a 1px soft border with a header row for filename + actions. variant="bare" removes all chrome and renders a 2px left rule that turns accent on hover; the header is suppressed. variant="terminal" centers the title in the header for a shell pane. A three-mark pane strip is opt-in in every variant via chrome="dots". All chrome is driven by [data-variant] and [data-chrome] selectors in code-block/code-block.css; consumers do not need to apply any classes manually.
Compound API
CodeBlock is the root <figure>. CodeBlock.Header holds the filename label and inline actions. CodeBlock.Label renders the filename and is registered as the accessible name via aria-labelledby. CodeBlock.Content is the scrollable code body — pass a string for auto-line splitting, or map line data to CodeBlock.Line children. CodeBlock.CopyButton copies a string to the clipboard with an aria-live announcement. CodeBlockHighlight (imported from @diffgazer/ui/components/code-block/highlight) renders syntax-colored code with a required caller-created lowlight instance.
Accessible Name
Precedence: aria-labelledby > aria-label > <CodeBlock.Label> > `label` prop > "<language> code" > "Code block". When you render <CodeBlock.Label>, the figure picks it up automatically via an internal id.
Line States
Each CodeBlock.Line exposes a `state` prop: "added" tints the row with --success-subtle and renders a sr-only "Added: " prefix; "removed" tints with --error-subtle and a "Removed: " prefix; "highlight" tints with foreground color. The +/- gutter sign keeps the full-strength --success/--error tone. The tint is applied to the row, not the <code>, so syntax-color themes remain readable.
Injected Syntax Highlighting
CodeBlockHighlight is split from the main <CodeBlock> bundle so consumers who never render it are not charged for syntax-highlighting code. Package consumers import it from @diffgazer/ui/components/code-block/highlight. Copy, dgadd, and direct registry consumers add the separate ui/code-block-highlight item. Install the optional `lowlight` peer, create an instance with the desired language set, and pass it through the required `lowlight` prop. The component emits highlight.js-compatible class names (hljs-keyword, hljs-string, ...) which the shared CSS maps onto the --code-* theme tokens.
Keyboard Scrolling
CodeBlock.Content renders a scrollable region wired through ScrollArea. The inner scroller is keyboard-focusable (tabIndex=0) and exposes the figure's accessible name to screen readers; the scroller is the landmark (role=region), not the figure. Users can Tab to the code area and scroll with arrow keys when content overflows.
Token-based Highlighting
Line content can be a plain string or an array of CodeBlockToken objects ({ text, color?, className? }) for syntax-colored output. No HTML parsing — tokens render directly as React elements. Use `.code-*` class names to bind to the shared theme tokens.
Source
Install via CLI: pnpm exec dgadd add ui/code-block.
Highlighted source loads after this disclosure opens. Browse the source repository.