Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

API REFERENCE · FORM CONTROLS

<dj-copy-button>

An icon-only button that copies text to the clipboard and shows whether it worked.

It is built on <dj-button>, so focus, keyboard use, and button semantics work as usual.

Try it in the playground

@dojo-ng/copy-button · v0.1.1

npm install @dojo-ng/copy-button

import "@dojo-ng/copy-button";

What it copies

  • The literal value, or, with from, the element with that id in the same root: its value for a form control, otherwise its textContent.
  • When both value and from are set, value wins.

Feedback

  • After a click, the icon changes to a check mark (copied) or an error mark for feedback-duration milliseconds, then changes back.
  • The accessible name changes with it (Copy, Copied, Copy failed), so screen reader users hear the result.
  • dj-copy fires with { value } on success, and dj-error on failure.

Requirements

  • Copying uses navigator.clipboard.writeText, which needs a secure context (https or localhost). There is no older fallback, so on plain http nothing is copied and the button shows its error state.

Properties

PropertyAttributeTypeDefault
valueThe literal text to copy. Wins over from when both are set. value string ""
fromId of an element in the same root to copy from (its value ?? textContent). from string None
feedbackDurationHow long (ms) the success/error state shows before reverting to the copy icon. feedback-duration number 2000

Events

EventDescription
dj-copydetail { value }
dj-error

CSS parts

Style these with dj-copy-button::part(name).

PartDescription
buttonthe composed <dj-button>

Methods

focus(options: FocusOptions)