Playground

Copy button

A button that copies a value to the clipboard, shows 'Copied' for a moment and announces it.

  • Built on native elements
  • Native button; the icon-only form is named by the label prop (default 'Copy').
  • The result is announced in a polite role=status region: 'Copied' or 'Copy failed'.
  • The text button changes its visible text to the copied label and resets itself.
  • Styles in one file: 02-buttons.css

Import

import '@monoline/css';
import {CopyButton} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add copy-button

Usage

import {CopyButton} from '@monoline/react';

<div style={{display: 'flex', gap: 'var(--s3)', alignItems: 'center'}}>
  <code>npm i @monoline/react</code>
  <CopyButton value="npm i @monoline/react" label="Copy install command" size="sm" />
  <CopyButton value="npm i @monoline/react" variant="solid">Copy command</CopyButton>
</div>

Examples

Icon and text

The icon form needs no text; both announce "Copied".

Props

CopyButtonProps

PropTypeDefaultDescription
value requiredstring–The text written to the clipboard when the button is pressed.
childrenReactNode–Visible text. Leave it out for an icon-only button that is named by `label`.
labelstring'Copy'The accessible name of the icon-only button (the text button is named by its children). Default "Copy".
copiedLabelstring'Copied'What is shown (text button) and announced to screen readers after a successful copy. Default "Copied".
failedLabelstring'Copy failed'What is announced when the browser refuses to copy. Default "Copy failed".
timeoutnumber–How long the copied state lasts, in milliseconds. Default 2000.
onCopied((value: string) => void)–Called with the copied text after a successful copy.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-copied]copy-button.css

CSS classes

ClassStylesheet
.btn02-buttons.css
.btn-block02-buttons.css
.btn-danger02-buttons.css
.btn-ghost02-buttons.css
.btn-lg02-buttons.css
.btn-line02-buttons.css
.btn-sm02-buttons.css
.btn-solid02-buttons.css
.btnicon-button.css
.btn-iconicon-button.css
.btn-smicon-button.css
.btncopy-button.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.btn {
  border-color: var(--accent);
}
.btn[data-copied] {
  background: var(--wash);
}

Accessibility

  • Native button; the icon-only form is named by the label prop (default 'Copy').
  • The result is announced in a polite role=status region: 'Copied' or 'Copy failed'.
  • The text button changes its visible text to the copied label and resets itself.