Skip to content
Get started

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.

Tier: clientUnstable

Examples

Basic

A command: one install line per package manager.

npm i @nizivo/react
import 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.

NameTypeDefaultDescription
defaultValuestringthe first

The label of the version shown first.

itemsrequiredreadonly CodeBlockItem[]–

The versions, in tab order; one shows no tabs.

labelsPartial<CodeBlockLabels>–
renderRenderProp<Record<string, never>>–

Must render a <div>.

variant"command" | "snippet"'snippet'

The kind of code: a one-line shell command after a "$", or a snippet.

  • Also takes every attribute of <div> except children; defaultValue is redeclared above. (Omit<ComponentPropsWithRef<"div">, "children">)

render takes an element, or, in client code, a function of the props and no state.

Other exports

CodeBlockItem type

import type { CodeBlockItem } from '@nizivo/react/unstable/code-block'

One version of the code: "pnpm" and its command, "tsx" and its snippet.

NameTypeDefaultDescription
coderequiredstring–
labelrequiredstring–
CodeBlockLabels type

import type { CodeBlockLabels } from '@nizivo/react/unstable/code-block'

CodeBlock's own strings (§16.4): serialisable, English defaults.

NameTypeDefaultDescription
versionsrequiredstring'Versions'

The tablist's name.

CodeBlockProps type

import 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.

SlotRendered byData attributes
code-block
.nz-code-block
CodeBlock
root (takes render)
  • data-variant: "command""snippet" ← variant
    The variant prop; values in each component manifest
code-block-code
.nz-code-block-code
CodeBlock
inner 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.

On this page