Overview
Copy works on any button with nds-copy. The button copies the text in its data-copy attribute, or the text of another element on the page. After the copy, the button shows a checkmark for 2 seconds, and a screen reader announces a message.
Pick another component when:
Markup
Variants
Label Swap and Announcement go on the copy button. Label Swap needs a .nds-label in the button, so it is off on Icon Only.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Button (default) | — | — | A button with a label that copies a fixed text from data-copy |
| Structure | Icon Only | canon #copy-icon-only |
— | A small button next to the value it copies, in a list of details |
| Structure | From Element | canon #copy-target |
— | The button copies the text of another element, named by data-copy-target |
| Label Swap | Label Swap (hint: The label reads “Copied” after the copy) | [data-copy-label="Copied"] |
.nds-copy:not(.nds-icon-only) |
The label reads “Copied” for 2 seconds after the copy |
| Announcement | Announcement (hint: What a screen reader says after the copy) | [data-copy-announce="Reference number copied"] |
.nds-copy |
The text a screen reader announces after the copy. Without it, the script announces data-copy-label, then “Copied” («تم النسخ» on an Arabic page) |
Behavior
From Element
data-copy-target holds a CSS selector. At each click, the script copies the text of the first element that matches, without spaces at the start and end. Use it for a value that changes after the page loads, or a value that is already on the page.
Label Swap
data-copy-label replaces the text of the button’s .nds-label while the checkmark shows. Then the script puts the old text back. With no value, it shows “Copied” in the page’s language. On a button with no .nds-label, only the screen reader says the text.
Built-in Features
-
Automatic Start
One click listener on the page serves every
.nds-copy, including the buttons you add later. You write no script. -
Text Order
The script copies the first text it finds:
data-copy, thendata-copy-target, then the code of a surrounding.nds-codeblock. In a code block with tabs, it copies the code of the open tab. -
Success Feedback
The icon changes to a checkmark for 2 seconds. During that time, the button takes no clicks.
-
Screen Reader Announcement
The shared live region announces each copy, so a screen reader user gets the same confirmation.
-
HTTP Fallback
When the browser blocks the Clipboard API, such as on a test server without HTTPS, the script copies through a hidden text box.
-
Script Control
Your own script can copy a text, or show the checkmark on a button, with
NDS.Copy.
Best Practices
- Give an icon-only button an
aria-labelthat names the value, such as “Copy reference number”. “Copy” alone does not tell a screen reader user what is copied. - Show the value next to an icon-only button, so the user sees what is copied.
- Add
data-copy-announcewhen many copy buttons sit together and the label must not change, such as a list of codes. The screen reader then says which value was copied. - Add a copy button only to values that users copy: reference numbers, codes, phone numbers, email addresses. Do not add one to every row of a long list.
- In a Dropmenu item, add
data-no-auto-close. Without it, the menu closes before the user sees the checkmark.
API
Data Attributes
The script reads these attributes on .nds-copy and on the elements that match an addListeners() selector.
| Attribute | Element | Effect |
|---|---|---|
data-copy |
.nds-copy |
The text to copy. The script reads it first. An empty value copies nothing |
data-copy-target |
.nds-copy |
A CSS selector. The script copies the text of the first match. It reads it only when data-copy is absent |
data-copy-label |
.nds-copy with a .nds-label |
The label text while the checkmark shows. With no value: “Copied” in the page’s language |
data-copy-announce |
.nds-copy |
The text the live region announces after the copy. Default: data-copy-label, then “Copied” («تم النسخ» on an Arabic page) |
data-status="success" |
.nds-copy |
The script sets it after a copy, and removes it after duration. While it is set, .nds-copy and .nds-btn show the success color, and .nds-copy takes no clicks |
aria-disabled="true" |
.nds-copy |
The script sets and removes it with data-status |
JavaScript
The NDS.Copy script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Copy.init() |
Listens for clicks on every .nds-copy. The loader calls it |
NDS.Copy.addListeners(selector, options) |
Listens for clicks on your own selector, including elements added later. A second call with the same selector replaces the first. Add nds-btn to the elements for the success color |
NDS.Copy.writeText(text) |
Writes the text to the clipboard. Returns a promise of true or false. Empty text returns false |
NDS.Copy.flash(button, options) |
Shows the checkmark, the label swap and the announcement, with no copy. A call during a flash does nothing. It calls onRestore when the button returns to normal |
NDS.Copy.copyFrom(button, options) |
Finds the button’s text, copies it, and shows the checkmark. Returns a promise of true or false |
addListeners(), flash() and copyFrom() take these options:
| Option | Default | Effect |
|---|---|---|
duration |
2000 |
The time in ms that the button shows the checkmark |
onRestore |
— | A function the script calls when the button returns to normal |
Copy fires no events. Use the promise from writeText() or copyFrom().
The full API is in the banner of _js/nds-copy.js.