tsimport {XDSCodeBlock} from '@xds/core/CodeBlock'
| Guidance | Practices |
|---|---|
| Do | Set the language prop to match the code content so syntax highlighting is accurate. Use "plaintext" when the language is unknown. |
| Do | Add a title when the code represents a file — it gives readers context and appears in the header bar alongside the copy button. |
| Do | Use XDSCode for short inline references like function names or CLI flags, and XDSCodeBlock for standalone multi-line snippets. |
| Don't | Enable line numbers on short snippets (under 5 lines) where they add clutter without helping navigation. |
| Don't | Nest a code block inside a scrollable container — use the maxHeight prop instead, which handles overflow natively. |
| Prop | Type | Description |
|---|---|---|
childrenrequired | ReactNode | Code content. |
xstyle | StyleXStyles | StyleX styles for layout customization. Must be a stylex.create() value. |
className | string | CSS class name for the root element. Prefer xstyle for styling. |
style | CSSProperties | Inline styles. Prefer xstyle for StyleX-optimized styling. |
data-testid | string | Test selector for automated testing frameworks. |
| Prop | Type | Description |
|---|---|---|
coderequired | string | The code string to display. |
language | string (default: 'plaintext') | Language for syntax highlighting. Use "plaintext" to disable. |
title | string | Filename or label shown in the header bar. |
hasLanguageLabel | boolean (default: true) | Show the language name in the header bar. Hidden when language is "plaintext". |
hasLineNumbers | boolean (default: false) | Show a line number gutter. |
highlightLines | number[] | 1-indexed line numbers to highlight. |
hasCopyButton | boolean (default: true) | Show a copy-to-clipboard button. |
onCopy | () => void | Callback after the code is copied. |
isWrapped | boolean (default: false) | Wrap long lines instead of enabling horizontal scroll. |
maxHeight | number | string | Max height before the block scrolls vertically. |
size | 'sm' | 'md' (default: 'md') | Text size variant. |
tokenizer | (code: string, language: string) => Array<{type: string; start: number; end: number}> | Custom tokenizer override for unsupported languages. |
isCollapsible | boolean (default: false) | Allow collapsing the code body into just the header bar. Starts expanded; the header becomes clickable to toggle. Only shows the toggle when the code exceeds collapsibleThreshold lines. |
collapsibleThreshold | number (default: 10) | Minimum number of lines before the collapse toggle appears. Below this threshold the code block renders normally even when isCollapsible is true. |
xstyle | StyleXStyles | StyleX styles for layout customization. Must be a stylex.create() value. |
className | string | CSS class name for the root element. Prefer xstyle for styling. |
style | CSSProperties | Inline styles. Prefer xstyle for StyleX-optimized styling. |
data-testid | string | Test selector for automated testing frameworks. |
tsx'use client';import {XDSCodeBlock} from '@xds/core/CodeBlock';const code = `{"name": "@xds/core","version": "0.0.5","dependencies": {"@stylexjs/stylex": "^0.17.5","react": "^19.0.0"},"scripts": {"build": "tsup","test": "vitest"}}`;export default function CodeBlockJSONConfig() {return (<XDSCodeBlockcode={code}language="json"title="package.json"hasLineNumbers/>);}
tsx'use client';import {XDSCodeBlock} from '@xds/core/CodeBlock';const code = `import {useState, useEffect} from 'react';interface User {id: string;name: string;email: string;}async function fetchUser(id: string): Promise<User> {const response = await fetch(\`/api/users/\${id}\`);if (!response.ok) {throw new Error(\`HTTP \${response.status}\`);}return response.json();}export function useUser(id: string) {const [user, setUser] = useState<User | null>(null);useEffect(() => {fetchUser(id).then(setUser);}, [id]);return user;}`;export default function CodeBlockHighlightedLines() {return (<XDSCodeBlockcode={code}language="typescript"title="useUser.ts"hasLineNumbershighlightLines={[9, 10, 11, 12, 13]}/>);}
tsx'use client';import {XDSCodeBlock} from '@xds/core/CodeBlock';const code = Array.from({length: 50},(_, i) => `const line${i + 1} = ${i + 1};`,).join('\n');export default function CodeBlockScrollableBlock() {return (<XDSCodeBlockcode={code}language="typescript"title="many-lines.ts"hasLineNumbersmaxHeight="100%"/>);}
tsx'use client';import {XDSCodeBlock} from '@xds/core/CodeBlock';import {XDSVStack} from '@xds/core/Stack';export default function CodeBlockBashCommand() {return (<XDSVStack gap={4}><XDSCodeBlockcode="npm install @xds/core @stylexjs/stylex"language="bash"hasCopyButton/><XDSCodeBlockcode={`curl -s https://api.example.com/status | jq '.services[] | select(.healthy == false)'`}language="bash"hasCopyButton/></XDSVStack>);}
tsx'use client';import {XDSCodeBlock} from '@xds/core/CodeBlock';const code = `import {useState, useEffect} from 'react';export function useUser(id: string) {const [user, setUser] = useState<User | null>(null);useEffect(() => {fetch(\`/api/users/\${id}\`).then(res => res.json()).then(setUser);}, [id]);return user;}`;export default function CodeBlockShowcase() {return (<XDSCodeBlockcode={code}language="typescript"title="useUser.ts"hasLineNumbershasCopyButton/>);}