Data display
CodeBlock
Free17.1 KB gzippedA 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
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.
import { Button } from "@vjdesigns/vjdesigns-ui";
export function Greeting({ name }: { name: string }) {
return <Button tone="primary">Hello, {name}</Button>;
}
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.
.card {
- border-radius: 8px;
+ border-radius: var(--radius-xl);
padding: var(--space-6);
}
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.
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.
import { Button } from "@vjdesigns/vjdesigns-ui";
Highlighted: export const SaveButton = () => <Button>Save</Button>;
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.
npm install @vjdesigns/vjdesigns-ui
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
import { Button } from "@vjdesigns/vjdesigns-ui";
export function Greeting({ name }: { name: string }) {
return <Button tone="primary">Hello, {name}</Button>;
}
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
| Prop | Type | Default |
|---|---|---|
codeThe source to show and copy. Takes precedence over | string | — |
childrenThe source as a plain string, when | string | — |
languageLanguage, used for highlighting and shown as the header label (e.g. | CodeLanguage | — |
titleFilename or title shown in the header, e.g. | ReactNode | — |
showLineNumbersShows line numbers in a gutter. They're hidden from assistive technology and never selected or copied. | boolean | false |
startLineNumber of the first line, for excerpts. | number | 1 |
highlightLinesLines to emphasize, e.g. | readonly CodeLineSelector[] | — |
highlightToneTint of highlighted lines. | "neutral""primary""accent""success""warning""danger""info" | "primary" |
highlightLabelScreen-reader text added at the start of each highlighted line, e.g. | string | — |
diffDiff mode: lines starting with | boolean | false |
wrapSoft-wraps long lines instead of scrolling horizontally. | boolean | false |
maxHeightMaximum height before the code scrolls vertically. Numbers are pixels; strings are any CSS length. | number | string | — |
copyableShows a "Copy code" button in the header. | boolean | true |
onCopyStatusChangeCalled with the result of each copy attempt, then with | (status: CodeCopyStatus) => void | — |
copyLabelAccessible name of the copy button. | string | "Copy code" |
copiedLabelAnnouncement after a successful copy. | string | "Copied" |
copyErrorLabelAnnouncement when copying fails. | string | "Couldn't copy. Select the code and copy it manually." |
copiedDurationHow long the copied (or error) state shows, in ms. | number | 2000 |
highlighterReplaces the built-in tokenizer. See | CodeHighlighter | — |
regionLabelAccessible name of the scroll region when it overflows. | string | `"Code: {title or language}"` |
preClassNameExtra class for the | 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.
| Prop | Type | Default |
|---|---|---|
toneSemantic color of the soft background. | "neutral""primary""accent""success""warning""danger""info" | "neutral" |
childrenThe 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)
Checking the examples above…
Lighthouse audit
Light mode
Score 10035 passed · 0 failed · 30 not applicable · 10 to check by hand
Dark mode
Score 10035 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
| Key | Action |
|---|---|
| Tab | Moves to the copy button, then to the code when it scrolls. |
| Arrow keys | Scroll the focused code region. |
Roles and states
| Element | Role | States and properties |
|---|---|---|
| Scrolling code | region | aria-label, tabindex="0" |
| Copy button | button | aria-label |
| Live region | status | Announces "Copied" |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every 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) |
| Focus | Focus 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 readers | Names, 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) |
| Color | Meaning 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 reflow | At 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 targets | Click 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) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |