Component
Copy and download
Published on
Description
Write the button you want, then mark it up as <button is="ds-copy"> or <button is="ds-download">. One writes a value to the clipboard, the other saves it as a text file. Without JavaScript neither button is upgraded, so give them nothing to do that the page cannot already do another way.
Both read the value the same way. data-value carries it directly; data-elem is a selector for the element holding it, read from value for a form control and trimmed text content for anything else. That selector is tried on the button’s parent before the document, so a button sitting beside what it acts on says code or output and needs no id. data-value wins when both are set.
Use them when the value is shown somewhere the reader can see, a DNS record, an API key, a header value, and the button sits beside it. For a whole block of preformatted text that carries its own controls, use Codeblock actions instead.
Example
Accessibility
- Both are native
<button>elements, so they are reachable by keyboard and activated with Enter or Space. - The accessible name comes from the markup you write. Add
class="visually-hidden"to the label for an icon-only button rather than dropping it. ds-copyswaps the text of the button's<span>toCopied
for two seconds and marks itaria-live="polite", so success is announced rather than shown only in colour. The authored text is what it restores, so translation stays in the markup.- A button with no
<span>still copies, it just has nothing to announce with.
References
Related
- Codeblock actions — the same two actions, built into a block of preformatted text
- Codeblock — for code samples that also need syntax highlighting
- Buttons — for the button markup itself
Variables
data-value— the value, written literallydata-elem— selector for the element holding the value, resolved inside the button’s parent first and then the document, used whendata-valueis absentdata-i18n-copied—ds-copyconfirmation, defaults toCopieddata-i18n-copy-failed—ds-copyfailure notice, defaults toCopy faileddata-filename—ds-downloadfile name, defaults todownload.txt