v22.18.0 release notes

CopyButton

CopyButton copies a value, the result of an async function or the text of an input or a code block to the clipboard, with an optional HTML version for a rich copy. It reuses the Button styles, shows a check and announces the result, falls back to execCommand outside secure contexts and comes with a pCopyToClipboard directive for any element. It has no dependency.

View as .md
import { CopyButton, CopyToClipboard } from '@exhaustive-ui/core/copybutton';

The same entry point exports the CopyToClipboard directive, for any element, and writeToClipboard, the function both use, to copy from your own code.

CopyButton copies its value to the clipboard when it is clicked. Its icon turns into a check and its text into "Copied" for a moment, and the result is announced to screen readers. It uses the Clipboard API in a secure context and an execCommand fallback elsewhere, so it also works on an intranet served over plain HTTP.

npm install @exhaustive-ui/core

CopyButton is a Button: severity, variant (text or outlined), size, rounded and raised work the same way. It is secondary by default. iconOnly hides the text, which stays the accessible name of the button; add tooltip to show it on hover and focus. In text and outlined buttons, the check icon is green after a copy.

target copies the text of an element instead of a value, read at the time of the click: the value of an input, a textarea or a select, the text content of any other element such as a code block. It takes a template reference, an ElementRef or a CSS selector.

import { CopyButton } from '@exhaustive-ui/core/copybutton';

@Component({
    imports: [CopyButton],
    template: `<p-copy-button value="Hello" />`
})
export class Example {}

value also takes a function, called on each click, that returns the text or a promise of it, for a text that is costly to build or comes from the server, such as a share link or a one-time token. While the promise is pending, the promise itself is given to the clipboard inside a ClipboardItem: Safari only accepts a write that starts during the click. A rejected promise emits copyError.

html writes an HTML version next to the text, a string or a function like value. Pasted into a word processor or a mail, the content keeps its formatting, links and tables; a plain text editor receives the value. Without ClipboardItem, the fallback sets both formats in the copy event.

ReleaseAngular
22.1022.x
21.x21.x

The pCopyToClipboard directive copies on a click on any element: a Button, a link, a menu item. Its value is the text, a function or, when empty, the text of the element itself; copyTarget, copyHtml, copyFeedbackDuration, copyLabels and copyDisabled work like the inputs of the button. The result is announced to screen readers and the element has a data-p-copy-state attribute, copied or failed, during the feedback, to style it. An element that is not a button, a link or a form field gets the button role, enters the tab order and copies with Enter and Space.

pnpm add @exhaustive-ui/core

feedbackDuration is how long, in milliseconds, the check icon and the "Copied" text stay, 2000 by default; 0 keeps the button as it is and only announces the result. copied gives the copied text and HTML, copyError the error when nothing could be copied, both with the original event; the button then shows a cross and "Copy failed". copy() starts a copy from your code and resolves to whether it succeeded. The icon template replaces the icons, with the state, idle, copied or failed, as context.

The texts, copy, copied and failed, are shown and announced. Translate them for one button with labels, or for the whole application with the copy, copied and copyFailed keys of the aria translation. Missing keys keep their English default.

provideExhaustive({
    translation: {
        aria: { copy: 'Copier', copied: 'Copié', copyFailed: 'Échec de la copie' }
    }
});

Screen Reader

CopyButton is a native button. Its visible text, "Copy", is its accessible name; with iconOnly, the same text becomes its aria-label. ariaLabel gives a more precise name, such as "Copy the API key"; when the text is visible, it has to contain it, so that voice control users can say what they see. The icons are hidden from assistive technologies.

The result is announced by a polite live region, "Copied" or "Copy failed", without moving the focus: the button keeps it, even when the fallback has to select a hidden text area to copy. During the feedback, the visible text and the name of an icon only button become the result too. The pCopyToClipboard directive announces through the live announcer of the Angular CDK, and gives the button role and a place in the tab order to an element that has none, such as a code sample; give it an aria-label that starts with its visible text, or with "Copy" followed by it. The texts are translated with labels or the aria translation.

The check and cross icons of text and outlined buttons are green and red with a 3:1 contrast or more in both color schemes, never the only sign of the result; their animation is disabled when the user prefers reduced motion.

<p-copy-button value="sk_live_51Hq" [iconOnly]="true" ariaLabel="Copy the API key" [tooltip]="true" />

<p-copy-button value="sk_live_51Hq" ariaLabel="Copy the API key" />

<code pCopyToClipboard aria-label="Copy npm install">npm install</code>

Keyboard Support

The button, and an element with the directive, are in the tab order.

KeyFunction
tabMoves focus to the button.
enter / spaceCopies the text and announces the result; the focus stays on the button.
escapeHides the tooltip, when tooltip is enabled.