Skip to content
Vertical Jump Designs-uiFront-End Studio
Get Pro

v3.9 is out.

Data display

CodeBlock

Free17.1 KB gzipped

A block of source code with a header (language and filename), a copy button, optional line numbers, highlighted lines, and diff gutters. Highlighting uses the built-in, dependency-free tokenizeCode, or your own highlighter.

Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · Keys: Tab, Arrow keys · Full report

Import

tsx
import { CodeBlock, Code } from "@vjdesigns/vjdesigns-ui";

Importing only CodeBlock adds about 17.1 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.

Examples

Basic

Built-in highlighting, a copy button, and an optional title and line numbers.

Greeting.tsxtsx
import { Button } from "@vjdesigns/vjdesigns-ui";

export function Greeting({ name }: { name: string }) {
  return <Button tone="primary">Hello, {name}</Button>;
}
tsx
import { CodeBlock } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";

export function Basic() {
  return (
    <div style={{ width: "100%" }}>
      <CodeBlock
        language="tsx"
        title="Greeting.tsx"
        showLineNumbers
        highlightLines={[4]}
        code={`import { Button } from "@vjdesigns/vjdesigns-ui";

export function Greeting({ name }: { name: string }) {
  return <Button tone="primary">Hello, {name}</Button>;
}`}
      />
    </div>
  );
}

Diff

diff tints added and removed lines and marks them with + and − signs, not color alone.

css
.card {
-  border-radius: 8px;
+  border-radius: var(--radius-xl);
   padding: var(--space-6);
}
tsx
import { CodeBlock } from "@vjdesigns/vjdesigns-ui";

export function Diff() {
  return (
    <div style={{ width: "100%" }}>
      <CodeBlock
        language="css"
        diff
        code={`.card {
-  border-radius: 8px;
+  border-radius: var(--radius-xl);
   padding: var(--space-6);
}`}
      />
    </div>
  );
}

Inline code

Code is for inline code in running text.

Run npm install, then import Button from the package root.

tsx
import { Code, Text } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";

export function InlineCode() {
  return (
    <Text>
      Run <Code>npm install</Code>, then import <Code tone="primary">Button</Code> from the package root.
    </Text>
  );
}

Accessibility

The copy button is a named button that confirms the copy. highlightLabel adds "Highlighted:" for screen readers before marked lines (it's off by default, since hand-selected text includes it). A long block scrolls in a named region that keyboard users can focus.

SaveButton.tsxtsx
import { Button } from "@vjdesigns/vjdesigns-ui";

Highlighted: export const SaveButton = () => <Button>Save</Button>;
tsx
import { CodeBlock } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";
import { Button } from "@vjdesigns/vjdesigns-ui";

export function Accessibility() {
  return (
    <div style={{ width: "100%" }}>
      <CodeBlock
        language="tsx"
        title="SaveButton.tsx"
        highlightLines={[3]}
        highlightLabel="Highlighted:"
        copyLabel="Copy the SaveButton example"
        code={'import { Button } from "@vjdesigns/vjdesigns-ui";\n\nexport const SaveButton = () => <Button>Save</Button>;'}
      />
    </div>
  );
}

Translated

The copy button's name, the "copied" confirmation and the error message come from the locale (French here). The code itself is never translated.

bash
npm install @vjdesigns/vjdesigns-ui
tsx
import { CodeBlock, LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import fr from "@vjdesigns/vjdesigns-ui/locales/fr";

export function Translated() {
  return (
    <LocaleProvider locale="fr" messages={fr}>
      <div style={{ width: "100%" }}>
        <CodeBlock language="bash" code="npm install @vjdesigns/vjdesigns-ui" />
      </div>
    </LocaleProvider>
  );
}

Right to left

The first example again, inside a right-to-left LocaleProvider (Arabic). Layout, spacing and arrow icons mirror, and the components' built-in text is translated. The example's own text stays in English.

Basic

Greeting.tsxtsx
import { Button } from "@vjdesigns/vjdesigns-ui";

export function Greeting({ name }: { name: string }) {
  return <Button tone="primary">Hello, {name}</Button>;
}
tsx
import { LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ar from "@vjdesigns/vjdesigns-ui/locales/ar";

// Basic is the "Basic" example above.
<LocaleProvider locale="ar" messages={ar}>
  <Basic />
</LocaleProvider>

Usage notes

Accessibility: renders <pre><code>. When the code overflows (scrolls), the <pre> becomes a focusable role="region" with a name, so keyboard users can scroll it. Copy results are announced in a polite live region.

API reference

CodeBlock

CodeBlock props
PropTypeDefault
code

The source to show and copy. Takes precedence over children. One leading newline and trailing whitespace are trimmed.

string—
children

The source as a plain string, when code isn't set.

string—
language

Language, used for highlighting and shown as the header label (e.g. "tsx", "css"). Unknown languages render plain.

CodeLanguage—
title

Filename or title shown in the header, e.g. "Button.tsx". Also names the scroll region.

ReactNode—
showLineNumbers

Shows line numbers in a gutter. They're hidden from assistive technology and never selected or copied.

booleanfalse
startLine

Number of the first line, for excerpts.

number1
highlightLines

Lines to emphasize, e.g. [2, "5-7"] (numbers as shown in the gutter). Marked with a tint and a left bar, not color alone.

readonly CodeLineSelector[]—
highlightTone

Tint of highlighted lines.

"neutral""primary""accent""success""warning""danger""info""primary"
highlightLabel

Screen-reader text added at the start of each highlighted line, e.g. "Highlighted:". Off by default because it's also included when a user selects and copies text by hand (the copy button never includes it).

string—
diff

Diff mode: lines starting with + / - get a success / danger tint and a + / - sign in a gutter. @@ lines are hunk headers.

booleanfalse
wrap

Soft-wraps long lines instead of scrolling horizontally.

booleanfalse
maxHeight

Maximum height before the code scrolls vertically. Numbers are pixels; strings are any CSS length.

number | string—
copyable

Shows a "Copy code" button in the header.

booleantrue
onCopyStatusChange

Called with the result of each copy attempt, then with "idle" when the confirmation resets.

(status: CodeCopyStatus) => void—
copyLabel

Accessible name of the copy button.

string"Copy code"
copiedLabel

Announcement after a successful copy.

string"Copied"
copyErrorLabel

Announcement when copying fails.

string"Couldn't copy. Select the code and copy it manually."
copiedDuration

How long the copied (or error) state shows, in ms.

number2000
highlighter

Replaces the built-in tokenizer. See CodeHighlighter.

CodeHighlighter—
regionLabel

Accessible name of the scroll region when it overflows.

string`"Code: {title or language}"`
preClassName

Extra class for the <pre> scroll container.

string—

Also accepts native <div> attributes, plus className, style and ref.

Code

Inline code inside running text, e.g. a prop name or a command. Renders a native <code> in the mono font on a soft tinted background. Its size follows the surrounding text.

Code props
PropTypeDefault
tone

Semantic color of the soft background.

"neutral""primary""accent""success""warning""danger""info""neutral"
children

The code text.

ReactNode—

Also accepts native <code> attributes, plus className, style and ref.

See what changed in each version in the Changelog.

Accessibility report

CodeBlock targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.

axe-core audit (live)

Running accessibility audit

Checking the examples above…

Lighthouse audit

Light mode

Score 100

35 passed · 0 failed · 30 not applicable · 10 to check by hand

Dark mode

Score 100

35 passed · 0 failed · 30 not applicable · 10 to check by hand

Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.

Built-in behavior

Keyboard

Keyboard
KeyAction
TabMoves to the copy button, then to the code when it scrolls.
Arrow keysScroll the focused code region.

Roles and states

Roles and states
ElementRoleStates and properties
Scrolling coderegionaria-label, tabindex="0"
Copy buttonbuttonaria-label
Live regionstatusAnnounces "Copied"

Manual checks

Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.

Manual accessibility checks
CheckWhat to testWCAG 2.2
KeyboardEvery action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck.2.1.1 (A), 2.1.2 (A)
FocusFocus moves in a logical order, is always visible, and isn't hidden behind sticky headers.2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA)
Screen readersNames, roles and states are announced correctly, and status changes are announced without moving focus.1.3.1 (A), 4.1.2 (A), 4.1.3 (AA)
ColorMeaning never depends on color alone; your own colors meet contrast in both modes.1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA)
Zoom and reflowAt 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed.1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA)
Pointer targetsClick and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap.2.5.8 (AA), 2.5.5 (AAA)
MotionAnimations respect the reduced-motion setting.2.3.3 (AAA)