Back to Consul

with-copyable

ui/packages/consul-ui/app/modifiers/with-copyable.mdx

1.22.72.5 KB
Original Source

with-copyable

Modifier for adding copy-to-clipboard functionality to any component to allow the user to easily copy specified text to their clipboard by clicking on something. Mainly an Ember flavoured wrapper for Clipboard.js which the modifier uses for all its functionality.

You can either explicitly specify the content to be copied to the users clipboard using the first (and only) parameter but if this is omitted it will use the content (innerText) of the DOM element it is attached to.

Usually you will want to provide a success and error callback which you can provide with named parameters. An escape hatch through to Clipboard.js options is also provided via the options named parameter.

hbs
<figure>
  <figcaption>Explicitly specifying the text to be copied as the first parameter</figcaption>
  <button
    {{with-copyable "Copied text"
      success=(action (mut this.copied) value="text")
      error=(noop)
    }}
    type="button"
  >Click me</button>

  <pre>Clipboard Contents:
{{this.copied}}</pre>
</figure>
hbs
<figure>
  <figcaption>Defaulting to the innerText of the DOM element</figcaption>
  <button
    {{with-copyable success=(action (mut this.copied) value="text")}}
    type="button"
  >Click <span>
me</span></button>

  <pre>Clipboard Contents:
{{this.copied}}</pre>
</figure>

The Clipboard.js class is provided via a clipboard/os Service, also includes a clipboard/local-storage Service that automatically replaces the OS based clipboard during testing to enable you to assert for text that would be copied to the clipboard. During acceptance testing there is a specific step specifically for this so you don't have to think about it:

gherkin
Scenario:
  When I click copyButton
  Then I copied "stringToCopy"

Positional Arguments

ArgumentTypeDefaultDescription
valueStringThe innerText of the elementThe string to be copied to the clipboard on click

Named Arguments

ArgumentTypeDefaultDescription
successFunction(e) => {}A function to be called when the text has been successfully copied to the users clipboard
errorFunction(e) => {}A function to be called when there was an error copying text to the users clipboard
optionsObject{}An object containing any documented Clipboard.js options

See