Skip to main content
Login

Sign in to access your account

Copy - National Design System

A script that copies text to the clipboard when the user clicks a button, then shows a checkmark

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:

  • the text is a code sample: Code
  • the user shares the page link to a social network: Share

Markup

Preview
<button type="button" class="nds-btn nds-secondary-outline nds-copy" data-copy="REF-2026-04-19-7A3F"> <i class="nds-icon nds-hgi-copy-01"></i> <span class="nds-label">Copy reference</span> </button>

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, then data-copy-target, then the code of a surrounding .nds-code block. 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-label that 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-announce when 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().

var button = document.querySelector('#copy-request-link'); var link = location.origin + '/requests/' + requestId; NDS.Copy.writeText(link).then(function (ok) { if (ok) NDS.Copy.flash(button, { duration: 3000 }); });

The full API is in the banner of _js/nds-copy.js.

Last Modified Date: 02/11/2026 - 03:47 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks