Components
CodeBlock
Unstable: copyable code on a dark surface, a shell command per package manager or a snippet per language, with tabs and a Copy button. Import it from @nizivo/react/unstable/code-block.
Examples
Basic
A command: one install line per package manager.
npm i @nizivo/reactimport type { JSX } from 'react';
import { CodeBlock } from '@nizivo/react/unstable/code-block';
export function CodeBlockExample(): JSX.Element {
return (
<CodeBlock
variant="command"
items={[
{ label: 'npm', code: 'npm i @nizivo/react' },
{ label: 'pnpm', code: 'pnpm add @nizivo/react' },
{ label: 'yarn', code: 'yarn add @nizivo/react' },
]}
/>
);
}
Snippet
A snippet: a component and its stylesheet.
import { Button } from '@nizivo/react/button';
export function Save() {
return <Button variant="primary">Save product</Button>;
}import type { JSX } from 'react';
import { CodeBlock } from '@nizivo/react/unstable/code-block';
export function CodeBlockSnippetExample(): JSX.Element {
return (
<CodeBlock
items={[
{
label: 'tsx',
code: [
"import { Button } from '@nizivo/react/button';",
'',
'export function Save() {',
' return <Button variant="primary">Save product</Button>;',
'}',
].join('\n'),
},
{ label: 'css', code: `@import '@nizivo/react/styles.css';` },
]}
/>
);
}
Import
import { CodeBlock } from '@nizivo/react/unstable/code-block';
// Once per app (Getting started), after
// @nizivo/tokens/tokens.css and @nizivo/react/css/base.css and @nizivo/react/css/copy-button.css and @nizivo/react/css/tabs.css
import '@nizivo/react/css/code-block.css';Static or interactive
Client. Its versions are Tabs and its Copy is a CopyButton. How to choose.
Content
items lists the versions (label and code), in tab order; one item shows its label instead of
tabs. variant="command" puts a "$" before one shell line (it is not copied); a snippet shows
lines as written. Copy copies the version shown. Code never wraps: a long line scrolls sideways,
and the code takes focus so a keyboard can scroll it too.
Props
Client component Renders data-slot="code-block" with the class nz-code-block. Props: CodeBlockProps. Source: src/unstable/code-block/code-block.client.tsx.
| Name | Type | Default | Description |
|---|---|---|---|
defaultValue | string | the first | The label of the version shown first. |
itemsrequired | readonly CodeBlockItem[] | – | The versions, in tab order; one shows no tabs. |
labels | Partial<CodeBlockLabels> | – | |
render | RenderProp<Record<string, never>> | – | Must render a |
variant | "command" | "snippet" | 'snippet' | The kind of code: a one-line shell command after a "$", or a snippet. |
- Also takes every attribute of
<div>exceptchildren;defaultValueis redeclared above. (Omit<ComponentPropsWithRef<"div">, "children">)
render takes an element, or, in client code, a function of the props and no state.
Other exports
CodeBlockItemtypeimport type { CodeBlockItem } from '@nizivo/react/unstable/code-block'One version of the code: "pnpm" and its command, "tsx" and its snippet.
Name Type Default Description coderequiredstring– labelrequiredstring– CodeBlockLabelstypeimport type { CodeBlockLabels } from '@nizivo/react/unstable/code-block'CodeBlock's own strings (§16.4): serialisable, English defaults.
Name Type Default Description versionsrequiredstring'Versions'The tablist's name.
CodeBlockPropstypeimport type { CodeBlockProps } from '@nizivo/react/unstable/code-block'The props of
CodeBlock, listed above.
Data attributes and slots
Style parts by class (.nz-<slot>) and state by data attribute. Attribute meanings come from contracts/vocabulary.json.
| Slot | Rendered by | Data attributes |
|---|---|---|
code-block.nz-code-block | CodeBlockroot (takes render) |
|
code-block-code.nz-code-block-code | CodeBlockinner element (CSS only); interactive, focus: self | none |
CSS hooks
Set a hook on the component's class or any ancestor to restyle it without specificity games; leave it unset for the default shown.
This stylesheet reads no public hook.
Layers: nz.components. Classes: .nz-code-block, .nz-code-block-bar, .nz-code-block-code, .nz-code-block-label, .nz-code-block-prompt, .nz-code-block-tabs, .nz-tabs-list, .nz-tabs-tab.
Tokens this stylesheet reads (14)
--nz-bg-code, --nz-bg-code-elevated, --nz-border-width-default, --nz-font-label-s, --nz-font-mono-m, --nz-radius-lg, --nz-radius-xs, --nz-space-0-5, --nz-space-1, --nz-space-2, --nz-space-3, --nz-space-4, --nz-text-code, --nz-text-code-muted
Accessibility
These parts are interactive. Each gets the focus outline and touch-action rule from @nizivo/react/css/base.css (the nz.base layer), so the floor survives a restyle:
code-block-code: draws the focus outline itself on keyboard focus.
Keyboard
To be written.
Citation
Unstable: a numbered link after a claim in an AI answer, to the source behind it. Its accessible name says "Source 1: …", not just "1". Import it from @nizivo/react/unstable/citation.
Combobox
A text input that suggests items from a list as the reader types, such as a country in a checkout form or search suggestions. Input's box, with Select's popup and rows; React Aria's ComboBox inside.