CodeSnippet
React code block with language tabs, line numbers, a copy button, Show More collapsing, and an inline variant. Code stays left to right in Arabic pages.
CodeSnippet takes a languages array; more than one entry shows tabs. Use maxLines for a Show More toggle, and CodeSnippetInline for one-line commands. Code always renders left to right.
When to use
Use CodeSnippet in developer documentation, API portals, and integration guides. Pass several languages to show tabs, use maxLines for long samples, and CodeSnippetInline for single commands or values inside a sentence.
Examples
Props
Props declared by CodeSnippet. Native attributes of the underlying element pass through.
<CodeSnippet>
| Prop | Type | Default | Description |
|---|---|---|---|
languagesrequired | CodeSnippetLanguage[] | One entry per language. A single entry hides the tab bar (SDGA single-tab). | |
value | string | Controlled active language. | |
defaultValue | string | Uncontrolled initial active language (defaults to the first). | |
onValueChange | (value: string) => void | ||
lineNumbers | boolean | true | Render the line-number gutter. |
maxLines | number | Collapse to this many lines with a Show More toggle when exceeded. | |
renderCode | (ctx: { code: string; language: string; }) => ReactNode | Escape hatch: render highlighted code yourself; the active code still drives copy + line count. | |
onCopy | (text: string) => void | ||
copyLabel | string | 'Copy code' | Accessible name for the copy button. |
copiedLabel | string | 'Copied' | Live-region text announced after a successful copy. |
<CodeSnippetInline>
| Prop | Type | Default | Description |
|---|---|---|---|
childrenrequired | string | The command text - copied verbatim. | |
prefix | react.ReactNode | Leading label (e.g. npm) - rendered in gold. | |
onCopy | (text: string) => void | ||
copyLabel | string | 'Copy code' | Accessible name for the copy button. |
copiedLabel | string | 'Copied' | Live-region text announced after a successful copy. |
Accessibility
The copy button has an accessible name and announces success through a live region. The Show More toggle exposes aria-expanded.
Code is rendered inside <pre> and <code> with a forced left-to-right direction, so it reads correctly inside Arabic content. Tabs are keyboard operable.