Copy button
A button that copies a value to the clipboard, shows 'Copied' for a moment and announces it.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- 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
Props
CopyButtonProps
| Prop | Type | Default | Description |
|---|---|---|---|
value required | string | – | The text written to the clipboard when the button is pressed. |
children | ReactNode | – | Visible text. Leave it out for an icon-only button that is named by `label`. |
label | string | 'Copy' | The accessible name of the icon-only button (the text button is named by its children). Default "Copy". |
copiedLabel | string | 'Copied' | What is shown (text button) and announced to screen readers after a successful copy. Default "Copied". |
failedLabel | string | 'Copy failed' | What is announced when the browser refuses to copy. Default "Copy failed". |
timeout | number | – | 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.
| Attribute | Stylesheet |
|---|---|
[data-copied] | copy-button.css |
CSS classes
| Class | Stylesheet |
|---|---|
.btn | 02-buttons.css |
.btn-block | 02-buttons.css |
.btn-danger | 02-buttons.css |
.btn-ghost | 02-buttons.css |
.btn-lg | 02-buttons.css |
.btn-line | 02-buttons.css |
.btn-sm | 02-buttons.css |
.btn-solid | 02-buttons.css |
.btn | icon-button.css |
.btn-icon | icon-button.css |
.btn-sm | icon-button.css |
.btn | copy-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.