When to use it#
- Use it for a short text people will paste somewhere else: a case or reference number, a link, a code.
- Show the text on the page next to the button and pass its element as
textRef, so the text can be selected if copying fails. - When a page has more than one, give each button a name that says what it copies: “Copy case number”.
- Copying works only on a secure page (HTTPS, or localhost while you build). Elsewhere it fails, and the text in
textRefis selected instead. - Not for a secret such as a password or a token. Not for a code sample with a label: use a CodeBlock, which has a copy button built in.
Example#
PK-2026-004217
import { CopyButton } from '@kvirn-ui/react'
import { useRef } from 'react'
import { useCopyButtonTexts } from './texts.ts'
export function DefaultCopyButton() {
const { texts } = useCopyButtonTexts()
const numberRef = useRef<HTMLElement>(null)
return (
<p>
<span ref={numberRef}>{texts.referenceNumber}</span>{' '}
<CopyButton text={texts.referenceNumber} textRef={numberRef} />
</p>
)
}Use cases#
A case number to keep
A resident gets a case number after sending an application and needs it again on the phone or in a later form. Name the button after what it copies, and pass the element that shows the number so it is selected if copying fails.
Your case number is PK-2026-004217
import { CopyButton } from '@kvirn-ui/react'
import { useRef } from 'react'
import { useCopyButtonTexts } from './texts.ts'
export function ReferenceNumber() {
const { texts } = useCopyButtonTexts()
const numberRef = useRef<HTMLElement>(null)
return (
<p>
{texts.yourNumber} <strong ref={numberRef}>{texts.referenceNumber}</strong>{' '}
<CopyButton text={texts.referenceNumber} textRef={numberRef}>
{texts.copyNumber}
</CopyButton>
</p>
)
}Props in this example:
Information: Did you know?
The result shows as words next to the button: “Copied” for five seconds. A failure message stays until the next press, so a slow reader still has the instruction (3.3.1 Error Identification).
A copy button for each case in a list
A caseworker or a resident has several cases on one page. A bare “Copy” repeated down the list tells a screen reader user nothing. Give each button a name of its own with aria-label, which says which number it copies.
- Building permit, case number PK-2026-004217
- Parking permit, case number PK-2026-004218
import { CopyButton } from '@kvirn-ui/react'
import { useRef } from 'react'
import { useCopyButtonTexts } from './texts.ts'
function CaseRow({ title, number }: { title: string; number: string }) {
const { texts } = useCopyButtonTexts()
const numberRef = useRef<HTMLElement>(null)
return (
<li>
{title}, {texts.caseNumber} <strong ref={numberRef}>{number}</strong>{' '}
<CopyButton text={number} textRef={numberRef} aria-label={texts.copyNamed(number)} />
</li>
)
}
export function Several() {
const { texts } = useCopyButtonTexts()
return (
<ul>
<CaseRow title={texts.buildingPermit} number={texts.referenceNumber} />
<CaseRow title={texts.parkingPermit} number={texts.secondReferenceNumber} />
</ul>
)
}Props in this example:
Warning: Don't forget
Keep the visible word “Copy” in the name, best at the start, so someone using voice control can say what they see (2.5.3 Label in Name).
The result in your own words and place
Use the hook when you need your own button element, or the result in a place of your choice. Pass the same words in messages, so what people see is what a screen reader announces.
Your case number is PK-2026-004217
import { useCopyButton } from '@kvirn-ui/react'
import { useRef } from 'react'
import { useCopyButtonTexts } from './texts.ts'
export function OwnCue() {
const { texts } = useCopyButtonTexts()
const numberRef = useRef<HTMLElement>(null)
const copy = useCopyButton({
text: texts.referenceNumber,
textRef: numberRef,
messages: { label: texts.copyNumber, copied: texts.copiedCue, failed: texts.failedCue },
})
return (
<p>
{texts.yourNumber} <strong ref={numberRef}>{texts.referenceNumber}</strong>{' '}
<button {...copy.buttonProps}>{copy.label}</button>
{copy.status === 'copied' ? <span> {texts.copiedCue}</span> : null}
{copy.status === 'failed' ? <span> {texts.failedCue}</span> : null}
</p>
)
}Props in this example:
messageson useCopyButton (options)
Information: Did you know?
With the component, status={false} removes the built-in result text. The button still gets data-status for your own styling.
All your case numbers in one press
A resident has three open cases and wants every number in a message to the office. Pass a function as text: it is read when the button is pressed, so the list is joined then. Point textRef at the whole list, so a refused copy selects every number.
Your open cases:
- PK-2026-004217
- PK-2026-004218
- PK-2026-004219
import { CopyButton } from '@kvirn-ui/react'
import { useRef } from 'react'
import { useCopyButtonTexts } from './texts.ts'
export function AllNumbers() {
const { texts } = useCopyButtonTexts()
const listRef = useRef<HTMLUListElement>(null)
const numbers = [texts.referenceNumber, texts.secondReferenceNumber, texts.thirdReferenceNumber]
return (
<div>
<p>{texts.yourCases}</p>
<ul ref={listRef}>
{numbers.map((number) => (
<li key={number}>{number}</li>
))}
</ul>
<CopyButton text={() => numbers.join('\n')} textRef={listRef}>
{texts.copyAll}
</CopyButton>
</div>
)
}Props in this example:
A copy button that waits for the number
The case number exists only after the application is sent. Until then the button is off, but with focusableWhenDisabled it stays in the Tab order, and the text next to it says why. A disabled button copies nothing and shows no result.
You get a case number when you send the application.
import { Button, CopyButton } from '@kvirn-ui/react'
import { useId, useRef, useState } from 'react'
import { useCopyButtonTexts } from './texts.ts'
export function NotReady() {
const { texts } = useCopyButtonTexts()
const numberRef = useRef<HTMLElement>(null)
const reasonId = useId()
const [isSent, setIsSent] = useState(false)
return (
<div>
<Button className="kv-button--primary" onClick={() => setIsSent(true)}>
{texts.sendApplication}
</Button>
<output>{isSent ? texts.sent : null}</output>
{isSent ? (
<p>
{texts.yourNumber} <strong ref={numberRef}>{texts.referenceNumber}</strong>
</p>
) : (
<p id={reasonId}>{texts.notYet}</p>
)}
<CopyButton
text={texts.referenceNumber}
textRef={numberRef}
disabled={!isSent}
focusableWhenDisabled
aria-describedby={isSent ? undefined : reasonId}
>
{texts.copyNumber}
</CopyButton>
</div>
)
}Props in this example:
disabledon CopyButtonfocusableWhenDisabledon CopyButton
Information: Did you know?
Link the reason to the button with aria-describedby, as the Button page advises. Where you can, don’t disable it at all: show the button when there is something to copy.
Accessibility#
What it does for you#
- APG pattern: Button
- Deviations: none
- Native elements used:
<button type="button">. Activation and the Tab stop are the browser's own. The result is announced through the shared live region ofKvirnProvider(the Announcer) and drawn in a plain<span>after the button.
A CopyButton is a Button that writes a text to the clipboard. Its accessible name is its label, "Copy", and it never changes to "Copied": the result is a status message (4.1.3), not a new name. When the browser refuses (no permission, an insecure context, no Clipboard API), the button announces the failure and selects the text where it is shown, so the user can copy it by hand.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| CopyButton | <button> → button | none by default | type="button", class="kv-button". Name from its content: copyButton.label ("Copy"), or your children. Never changed by the result |
| disabled | native disabled | As Button: not focusable, copies nothing. data-disabled | |
| disabled and focusable | aria-disabled="true" | As Button (focusableWhenDisabled): stays in the Tab order and copies nothing | |
| result | none | data-status is idle, copied or failed. copied returns to idle after 5 s, or at once when the next press starts. failed stays until the next press starts and never times out. It carries no name or description. The user hears the result through the announcement | |
| status | <span> → generic | none: not a live region, no role, not aria-describedby | class="kv-copy-status", a sibling after the button, never inside it (it would join the name). Decorative check / warning Icon (aria-hidden) plus the words copyButton.copied or copyButton.failed, so what is seen is what is heard. Not rendered while idle or disabled. status={false} renders none. Not focusable |
| text | textRef element | none | On failure its contents are selected (the browser selects the text). The selection is not focus: focus stays on the button |
useCopyButton gives the same buttonProps, a label and a status for your own <button> and your own status text.
What you need to do#
- Put the text on the page where the user can see it, and pass its element as
textRef, so a failed copy can select it. Without atextRef, the failure is announced and nothing is selected. - When a page has more than one CopyButton, give each a name that says what it copies (
children: "Copy reference number", oraria-labelfrom your translations), and keep the visible words in the name (2.5.3). - Wrap the app in
KvirnProvider: the result is announced through it. - Don't copy a secret to the clipboard.
- Copy needs a secure context (HTTPS or localhost). Where the browser has no Clipboard API or refuses the write, it fails, and the text is selected instead.
- The theme-less result cue is yours: with
status={false}oruseCopyButton, draw words (not colour alone) fromdata-statusafter the button. Never put it in the name. - A text in another language goes in a nested
KvirnProviderwithuseLocale().localePropson your own element (3.1.2).
Warning: Don't forget
Wrap your app in KvirnProvider. Without it nothing is announced, and a screen reader user never hears that the text was copied (4.1.3 Status Messages).
Visual and modes#
Headless: CopyButton ships no CSS. With @kvirn-ui/theme/theme.css it is a Button (kv-button): see the Button page. The result text (kv-copy-status) uses the normal text size and colour with an icon in the same colour, so no colour carries it (1.4.1) and no new colour pair is needed.
- Focus indicator, target size, contrast, forced colours: the Button's.
- Reduced motion: no motion.
- Forced colours: the result words and icon use the system's text colour. RTL: the status sits at the inline end, and the icons don't mirror.
- Reflow: no horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA). The reflow sweep checks every story at that width for horizontal overflow. The failure text wraps below the button.
Focus management#
- Initial focus: not moved. CopyButton never moves focus.
- Trap: no.
- Restore to: not applicable. After a copy, a failure or a selection, focus stays on the button.
- Never obscured by: CopyButton renders no overlay.
WCAG success criteria#
- 4.1.3 Status Messages (AA):
copyButton.copiedandcopyButton.failedthrough the live region. - 1.4.1 Use of Color (A), 1.3.1 Info and Relationships (A): the visible status is words plus a decorative icon.
- 3.3.1 Error Identification (A): a failure says what happened and what to do, and the failure text stays until the next press, so a slow reader or a magnifier user still has the instruction.
- 4.1.2 Name, Role, Value (A): role
button, a stable name,disabled/aria-disabledexposed. - 2.1.1 Keyboard (A), 2.4.3 Focus Order (A): native button; focus never moves (the Keyboard rows).
- 2.5.3 Label in Name (A): the visible text is the name, and the status never replaces it.
Keyboard#
- Focus
- native
- Selection follows focus
- n/a
- Arrows wrap
- n/a
- Shortcuts
- none
A native <button>: one Tab stop, in DOM order, with no tabindex. Enter and Space are native.
| Key | Where | What happens |
|---|---|---|
| Tab | CopyButton | Moves focus to the button Test: copy-button.test.tsx › keyboard › Tab moves focus to the copy button |
| Shift+Tab | CopyButton | Moves focus to the previous focusable element, and off the button Test: copy-button.test.tsx › keyboard › Shift+Tab moves focus off the copy button |
| Tab | Disabled CopyButton | Skips the button Test: copy-button.test.tsx › keyboard › a disabled copy button is skipped by Tab |
| Tab | Focusable disabled CopyButton | Moves focus to the button, which copies nothing Test: copy-button.test.tsx › keyboard › a focusable disabled copy button is reached by Tab and copies nothing |
| Enter | CopyButton | Copies the text and announces the result. Focus stays on the button Test: copy-button.test.tsx › keyboard › Enter copies the text and keeps focus on the button |
| Space | CopyButton | Copies on key up, not on key down, and announces the result. Focus stays on the button Test: copy-button.test.tsx › keyboard › Space copies on key up and keeps focus on the button |
| Enter | CopyButton, the browser refuses | Announces the failure and selects the text. Focus stays on the button Test: copy-button.test.tsx › failure › Enter on a refused copy selects the text and keeps focus on the button |
Escape, arrow keys and Home / End are not handled.
Announcements#
CopyButton tells screen reader users about these changes without moving focus. Each message waits until the screen reader is quiet (polite) or interrupts it (assertive).
| When | What it says | Message key | Politeness |
|---|---|---|---|
| The text was written to the clipboard | copyButton.copied | Polite | |
| The browser refused to write it | copyButton.failed | Assertive |
The text in all five languages
Change the text#
For your whole app, pass messages to KvirnProvider. For one CopyButton, pass its messages prop.
<KvirnProvider messages={{ copyButton: { copied: '…' } }}>Warning: Don't forget
Announcements need a KvirnProvider around your app, outside any <form>, or an Announcer of your own. Without one nothing is announced, and a development warning says so.
API reference#
import { CopyButton, useCopyButton } from '@kvirn-ui/react'CopyButton
Renders A Button: <button type="button"> with the role button. It takes every attribute of a Button except type and passes ref to it. After the button it renders <span class="kv-copy-status"> with the result, while the result is copied or failed.
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | (() => string) | Required | The text to write to the clipboard. A function is read when the button is activated. |
textRef | RefObject<Element | null> | – | The element that shows the text. When the browser refuses to write, its contents are selected so the user can copy them by hand. |
onCopied | (text: string) => void | – | Called with the text after it was written to the clipboard. |
onCopyError | (text: string) => void | – | Called with the text when it could not be written to the clipboard. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called when the button is activated, and never while it is disabled. |
children | ReactNode | the message copyButton.label | Your own label, which replaces the message, so its language is yours to set. Say what it copies when a page has more than one. |
messages | Partial<{ label, copied, failed }> | – | Per-instance overrides of the three messages. See Strings. |
status | boolean | true | false removes the result text after the button. The result is still announced and data-status is still set, so you can draw your own cue in words. |
disabled | boolean | false | Blocks copying. As Button: without focusableWhenDisabled it is skipped by Tab. |
focusableWhenDisabled | boolean | false | With disabled, keeps the button in the Tab order. Copying stays blocked. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-button | always | The class of Button, which CopyButton renders. |
data-status | idle, copied or failed | The result of the last copy. copied returns to idle after 5 seconds. failed stays until the next press. A hook for your own cue: it is no name and no announcement. |
kv-copy-status | while copied or failed | The result text after the button, with a decorative icon. It is not there while idle or disabled, or with status={false}. It is not a live region: the announcement carries the news. |
data-disabled | present or absent | The button is disabled, natively or focusable. |
data-focus-visible | present or absent | The button has keyboard focus (:focus-visible). |
useCopyButton
Options
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | (() => string) | Required | The text to write. A function is read when the button is activated. |
textRef | RefObject<Element | null> | – | The element that shows the text. Its contents are selected when copying fails. |
onCopied | (text: string) => void | – | Called with the text after it was written. |
onCopyError | (text: string) => void | – | Called with the text when it could not be written. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called when the button is activated, and never while it is disabled. |
messages | Partial<{ label, copied, failed }> | – | Per-instance message overrides. |
disabled | boolean | false | Mirrors HTML disabled. |
focusableWhenDisabled | boolean | false | Keeps a disabled button in the Tab order with aria-disabled="true". |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
buttonProps | CopyButtonPartProps | – | Spread on a <button>: the props of Button, and data-status. |
label | string | – | The message copyButton.label: the default text of the button. |
status | 'idle' | 'copied' | 'failed' | – | The result of the last copy. copied is idle again after 5 seconds. failed stays until the next press. |
isDisabled | boolean | – | Whether the button is disabled. |
isFocusVisible | boolean | – | Whether the button has keyboard (:focus-visible) focus. |
Strings
These are the default texts in @kvirn-ui/i18n.
For your whole app, pass messages to KvirnProvider. For one CopyButton, pass its messages prop.
copyButton.label
The visible text and the accessible name. It never changes to the result.
| Language | Default text |
|---|---|
| Svenska | Kopiera |
| Suomi | Kopioi |
| Norsk bokmål | Kopier |
| Norsk nynorsk | Kopier |
| English | Copy |
copyButton.copied
Announced politely and shown after the button when the text was written to the clipboard.
| Language | Default text |
|---|---|
| Svenska | Kopierat |
| Suomi | Kopioitu |
| Norsk bokmål | Kopiert |
| Norsk nynorsk | Kopiert |
| English | Copied |
copyButton.failed
Announced assertively and shown after the button when the browser refused. It stays until the next press. The text in textRef is selected.
| Language | Default text |
|---|---|
| Svenska | Det gick inte att kopiera. Markera texten och kopiera den själv. |
| Suomi | Kopiointi epäonnistui. Valitse teksti ja kopioi se itse. |
| Norsk bokmål | Kunne ikke kopiere. Marker teksten og kopier den selv. |
| Norsk nynorsk | Kunne ikkje kopiere. Marker teksten og kopier han sjølv. |
| English | Could not copy. Select the text and copy it yourself. |
Known issues#
- WebKit is not automated. Keyboard rows run in Vitest browser mode on Chromium. WebKit is not covered by the automated tests, so check Safari with your own users and assistive technology.
- The status is not a live region on purpose: the Announcer already speaks it, and a second region would say it twice. Browse mode reads the same words.
- Selection is not read in browse mode by every screen reader: the failure message tells the user to copy the text themselves.
- Your own strings: if you override the strings, have a native speaker check them.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | copy-button.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | copy-button.test.tsx (axe and role, name and state): useCopyButton gives the label and a status for your own button |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 7 rows, each names a test in copy-button.test.tsx |
| Focus order and management | pass | n/a | pass | copy-button.test.tsx: Enter on a refused copy selects the text and keeps focus on the button |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
What to check#
- Tab to the button: you hear its name and "button". The name stays the same after a press.
- Press Enter, then Space: the result is announced once each time and focus stays on the button.
- After a copy, paste elsewhere and check the text.
- Make the copy fail (blocked clipboard or an insecure page): the failure is announced, the text is selected and its instruction stays on screen.
- With several buttons, each name says what it copies.
- A focusable disabled button is reached and read as unavailable. Enter and Space copy nothing.