Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

CopyButton

A button that copies a text to the clipboard, such as a case number. A button that copies a short text, such as a case number, so people can paste it somewhere else. The result shows as words next to the button, and a screen reader (software that reads the screen aloud) announces it. If the browser refuses, the text is selected so people can copy it by hand.

Package
@kvirn-ui/react
Pattern
APG Button
Testing
Automated. Core 0 of 2, Release 0 of 3

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 textRef is 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

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

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

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

Props in this example:

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

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.

Props in this example:

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 of KvirnProvider (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.

PartElement / roleARIANotes
CopyButton<button> → buttonnone by defaulttype="button", class="kv-button". Name from its content: copyButton.label ("Copy"), or your children. Never changed by the result
disablednative disabledAs Button: not focusable, copies nothing. data-disabled
disabled and focusablearia-disabled="true"As Button (focusableWhenDisabled): stays in the Tab order and copies nothing
resultnonedata-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> → genericnone: not a live region, no role, not aria-describedbyclass="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
texttextRef elementnoneOn 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 a textRef, 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", or aria-label from 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} or useCopyButton, draw words (not colour alone) from data-status after the button. Never put it in the name.
  • A text in another language goes in a nested KvirnProvider with useLocale().localeProps on 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.copied and copyButton.failed through 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-disabled exposed.
  • 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.

KeyWhereWhat happens
TabCopyButtonMoves focus to the button
Test: copy-button.test.tsx › keyboard › Tab moves focus to the copy button
Shift+TabCopyButtonMoves focus to the previous focusable element, and off the button
Test: copy-button.test.tsx › keyboard › Shift+Tab moves focus off the copy button
TabDisabled CopyButtonSkips the button
Test: copy-button.test.tsx › keyboard › a disabled copy button is skipped by Tab
TabFocusable disabled CopyButtonMoves 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
EnterCopyButtonCopies 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
SpaceCopyButtonCopies 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
EnterCopyButton, the browser refusesAnnounces 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).

WhenWhat it saysMessage keyPoliteness
The text was written to the clipboardcopyButton.copiedPolite
The browser refused to write itcopyButton.failedAssertive

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.

PropTypeDefaultDescription
textstring | (() => string)RequiredThe text to write to the clipboard. A function is read when the button is activated.
textRefRefObject<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.
onClickMouseEventHandler<HTMLButtonElement>–Called when the button is activated, and never while it is disabled.
childrenReactNodethe message copyButton.labelYour own label, which replaces the message, so its language is yours to set. Say what it copies when a page has more than one.
messagesPartial<{ label, copied, failed }>–Per-instance overrides of the three messages. See Strings.
statusbooleantruefalse 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.
disabledbooleanfalseBlocks copying. As Button: without focusableWhenDisabled it is skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the button in the Tab order. Copying stays blocked.
Class or attributeValuesMeaning
kv-buttonalwaysThe class of Button, which CopyButton renders.
data-statusidle, copied or failedThe 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-statuswhile copied or failedThe 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-disabledpresent or absentThe button is disabled, natively or focusable.
data-focus-visiblepresent or absentThe button has keyboard focus (:focus-visible).

useCopyButton

Options

PropTypeDefaultDescription
textstring | (() => string)RequiredThe text to write. A function is read when the button is activated.
textRefRefObject<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.
onClickMouseEventHandler<HTMLButtonElement>–Called when the button is activated, and never while it is disabled.
messagesPartial<{ label, copied, failed }>–Per-instance message overrides.
disabledbooleanfalseMirrors HTML disabled.
focusableWhenDisabledbooleanfalseKeeps a disabled button in the Tab order with aria-disabled="true".

Result

PropTypeDefaultDescription
buttonPropsCopyButtonPartProps–Spread on a <button>: the props of Button, and data-status.
labelstring–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.
isDisabledboolean–Whether the button is disabled.
isFocusVisibleboolean–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.

LanguageDefault text
SvenskaKopiera
SuomiKopioi
Norsk bokmålKopier
Norsk nynorskKopier
EnglishCopy

copyButton.copied

Announced politely and shown after the button when the text was written to the clipboard.

LanguageDefault text
SvenskaKopierat
SuomiKopioitu
Norsk bokmålKopiert
Norsk nynorskKopiert
EnglishCopied

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.

LanguageDefault text
SvenskaDet gick inte att kopiera. Markera texten och kopiera den själv.
SuomiKopiointi epäonnistui. Valitse teksti ja kopioi se itse.
Norsk bokmålKunne ikke kopiere. Marker teksten og kopier den selv.
Norsk nynorskKunne ikkje kopiere. Marker teksten og kopier han sjølv.
EnglishCould 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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasscopy-button.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasscopy-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)passn/apass7 rows, each names a test in copy-button.test.tsx
Focus order and managementpassn/apasscopy-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)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

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.