Skip to content
Get started

Components

CopyButton

Unstable: copies text to the clipboard, then reads "Copied" for 2 seconds and announces it. It lives on the dark code surface of a CodeBlock. Import it from @nizivo/react/unstable/copy-button.

Tier: clientUnstable

Examples

Basic

On the dark code surface it lives on: a CodeBlock's Copy, which reads "Copied" for 2 seconds.

npm
npm i @nizivo/react
import type { JSX } from 'react';
import { CodeBlock } from '@nizivo/react/unstable/code-block';

export function CopyButtonExample(): JSX.Element {
  return <CodeBlock variant="command" items={[{ label: 'npm', code: 'npm i @nizivo/react' }]} />;
}

Import

import { CopyButton } from '@nizivo/react/unstable/copy-button';

// Once per app (Getting started), after
// @nizivo/tokens/tokens.css and @nizivo/react/css/base.css
import '@nizivo/react/css/copy-button.css';

Static or interactive

Client. It writes to the clipboard. How to choose.

Use

value is the text to copy. After a copy the button reads "Copied" for 2 seconds, and a polite live region says so; its name stays "Copy". onCopied hears the copy, and onCopyFailed hears why the clipboard refused (nothing claims a copy then). It is drawn for the dark CodeBlock surface.

Props

Client component Renders data-slot="copy-button" with the class nz-copy-button. Props: CopyButtonProps. Source: src/unstable/copy-button/copy-button.client.tsx.

NameTypeDefaultDescription
labelsPartial<CopyButtonLabels>–
onCopied(value: string) => void–
onCopyFailed(error: unknown) => void–
renderRenderProp<CopyButtonState>–

Must render a <button>.

valuerequiredstring–

The text to copy.

  • Also takes every attribute of <button> except children, value. (Omit<ComponentPropsWithRef<"button">, "value" | "children">)

render takes an element, or, in client code, a function of the props and this state (CopyButtonState):

NameTypeDefaultDescription
copiedrequiredboolean–

Other exports

CopyButtonLabels type

import type { CopyButtonLabels } from '@nizivo/react/unstable/copy-button'

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

NameTypeDefaultDescription
copiedrequiredstring'Copied'

Shown and announced for 2 seconds after a copy.

copyrequiredstring'Copy'
CopyButtonProps type

import type { CopyButtonProps } from '@nizivo/react/unstable/copy-button'

The props of CopyButton, listed above.

CopyButtonState type

import type { CopyButtonState } from '@nizivo/react/unstable/copy-button'

The render state of CopyButton, 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
copy-button
.nz-copy-button
CopyButton
root (takes render); interactive, focus: self
  • data-variant: "idle""copied"
    The variant prop; values in each component manifest

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-copy-button, .nz-copy-button-icon.

Tokens this stylesheet reads (8)

--nz-bg-code-elevated, --nz-font-label-s, --nz-radius-sm, --nz-size-icon-sm, --nz-space-1-5, --nz-space-2, --nz-text-code, --nz-text-inverse-accent

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:

  • copy-button: draws the focus outline itself on keyboard focus.

Keyboard

To be written.

On this page