XDSCode@xds/core · CodeBlock

Usage

CodeBlock renders syntax-highlighted code with line numbers, a copy button, and optional collapsible sections. Use XDSCodeBlock for multi-line snippets like source files, terminal commands, and configuration examples. Use XDSCode for inline references to function names, variables, or CLI flags within body text.

Best practices

GuidancePractices
DoSet the language prop to match the code content so syntax highlighting is accurate. Use "plaintext" when the language is unknown.
DoAdd a title when the code represents a file — it gives readers context and appears in the header bar alongside the copy button.
DoUse XDSCode for short inline references like function names or CLI flags, and XDSCodeBlock for standalone multi-line snippets.
Don'tEnable line numbers on short snippets (under 5 lines) where they add clutter without helping navigation.
Don'tNest a code block inside a scrollable container — use the maxHeight prop instead, which handles overflow natively.

Anatomy

ElementDescription
Header BarShows the title, language label, and copy button. Appears when any of these props are set.
Line NumbersNumbered gutter along the left edge. Enable with hasLineNumbers.
Code BodyrequiredThe syntax-highlighted code content.
Highlighted LinesBackground accent on specific lines to draw attention.
Copy ButtonCopies the code string to the clipboard. Shown by default.

Import

ts
import {XDSCode} from '@xds/core/CodeBlock'

Props

PropTypeDescription
childrenrequired
ReactNodeCode content.
xstyle
StyleXStylesStyleX styles for layout customization. Must be a stylex.create() value.
className
stringCSS class name for the root element. Prefer xstyle for styling.
style
CSSPropertiesInline styles. Prefer xstyle for StyleX-optimized styling.
data-testid
stringTest selector for automated testing frameworks.

Examples

Common configurations, variations, and states.
Code — Content TypesInline code used for variables, terminal commands, CSS properties, file paths, and keyboard shortcuts. Shows how XDSCode adapts to different kinds of technical content.
tsx
'use client';
import {XDSCode} from '@xds/core/CodeBlock';
import {XDSText} from '@xds/core/Text';
import {XDSVStack} from '@xds/core/Stack';
export default function CodeVariousContent() {
return (
<XDSVStack gap={3}>
<XDSText type="body">
Variable: <XDSCode>const count = 0</XDSCode>
</XDSText>
<XDSText type="body">
Terminal: <XDSCode>yarn build --watch</XDSCode>
</XDSText>
<XDSText type="body">
CSS property: <XDSCode>border-radius: 8px</XDSCode>
</XDSText>
<XDSText type="body">
File path: <XDSCode>packages/core/src/CodeBlock/XDSCode.tsx</XDSCode>
</XDSText>
<XDSText type="body">
Keyboard shortcut: <XDSCode>Ctrl+Shift+P</XDSCode>
</XDSText>
</XDSVStack>
);
}
Code — InlineInline code references mixed within a paragraph of body text. Use XDSCode to mark up function names, hooks, or API terms so they stand out from surrounding prose.
tsx
'use client';
import {XDSCode} from '@xds/core/CodeBlock';
import {XDSText} from '@xds/core/Text';
export default function CodeInlineInParagraph() {
return (
<XDSText type="body">
Use <XDSCode>useState</XDSCode> for local state and{' '}
<XDSCode>useEffect</XDSCode> for side effects. If you need shared state
across components, consider <XDSCode>useContext</XDSCode> or a state
management library.
</XDSText>
);
}
Code — Text SizesInline code rendered inside heading, body, supporting, and label text. XDSCode automatically matches the font size of its parent text element.
tsx
'use client';
import {XDSCode} from '@xds/core/CodeBlock';
import {XDSText} from '@xds/core/Text';
import {XDSHeading} from '@xds/core/Text';
import {XDSVStack} from '@xds/core/Stack';
export default function CodeAcrossTextSizes() {
return (
<XDSVStack gap={3}>
<XDSHeading level={3}>
Heading with <XDSCode>inline code</XDSCode>
</XDSHeading>
<XDSText type="body">
Body text with <XDSCode>inline code</XDSCode>
</XDSText>
<XDSText type="supporting">
Supporting text with <XDSCode>inline code</XDSCode>
</XDSText>
<XDSText type="label">
Label text with <XDSCode>inline code</XDSCode>
</XDSText>
</XDSVStack>
);
}

Showcase source

tsx
'use client';
import {XDSCode} from '@xds/core/CodeBlock';
import {XDSText} from '@xds/core/Text';
import {XDSStack} from '@xds/core/Layout';
export default function CodeShowcase() {
return (
<XDSStack direction="vertical" gap={3}>
<XDSText type="body">
Run <XDSCode>npm install @xds/core</XDSCode> to add the package.
</XDSText>
<XDSText type="body">
Use the <XDSCode>variant</XDSCode> prop to switch between{' '}
<XDSCode>primary</XDSCode>, <XDSCode>secondary</XDSCode>, and{' '}
<XDSCode>ghost</XDSCode> styles.
</XDSText>
</XDSStack>
);
}