Skip to content

Commit 7e3f448

Browse files
committed
feat: support direct use of the clipboard modifier
Introduced `data-clipboard-id` attribute instead of setting an elements `id`
1 parent 3169422 commit 7e3f448

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ http://jkusa.github.io/ember-cli-clipboard
1212

1313
## Usage
1414

15-
### Angle Bracket Invocation
15+
### CopyButton Component
1616

1717
```hbs
1818
<!-- Set text directly -->
@@ -48,7 +48,7 @@ http://jkusa.github.io/ember-cli-clipboard
4848
</CopyButton>
4949
```
5050

51-
### Arguments
51+
#### Arguments
5252

5353
- `text` - string value or action that returns a string to be copied
5454
- `target` - selector string of element or action that returns an element from which to copy text
@@ -59,7 +59,7 @@ http://jkusa.github.io/ember-cli-clipboard
5959

6060
Any HTML [button attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes) passed to the component will be "splatted" on the button element. The one exception to this is the `type` attribute due to this [issue](https://github.com/emberjs/ember.js/issues/18232) < Ember `3.25.x`.
6161

62-
### Actions
62+
#### Actions
6363

6464
The following clipboard.js custom events are sent as actions
6565

@@ -68,7 +68,21 @@ The following clipboard.js custom events are sent as actions
6868

6969
More information about the clipboard.js events can be found [here](https://github.com/zenorocha/clipboard.js/#events)
7070

71-
## Template Helper
71+
### Clipboard Element Modifier
72+
73+
Under the hood the `<CopyButton>` component is powered by a `{{clipboard}}` element modifier. This can be used directly as an alternative to the `<CopyButton>` component. It has the same argument contract as the `<CopyButton>` component except for the exclusion of the `buttonType` argument.
74+
75+
```hbs
76+
<button
77+
class='button is-outline'
78+
type='button'
79+
{{clipboard text='text to be copied' onSuccess=this.onSuccess}}
80+
>
81+
Click To Copy
82+
</button>
83+
```
84+
85+
### Template Helper
7286

7387
The helper `is-clipboard-supported` can be used to check if [clipboard.js](http://zenorocha.github.io/clipboard.js/) is supported or not.
7488

addon-test-support/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function _fireComponentAction(owner, selector, actionName) {
4747
*/
4848
function _getComponentBySelector(owner, selector = '.copy-btn') {
4949
const renderTree = Ember._captureRenderTree(owner);
50-
const guid = document.querySelector(selector).id;
50+
const guid = document.querySelector(selector).dataset.clipboardId;
5151
return _findComponentInstance(renderTree[0], guid);
5252
}
5353

addon/components/copy-button.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<button
22
class='copy-btn'
33
type={{this.buttonType}}
4-
id={{this.guid}}
4+
data-clipboard-id={{this.guid}}
55
...attributes
66
{{clipboard
77
text=this.text

addon/modifiers/clipboard.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { modifier } from 'ember-modifier';
22
import ClipboardJS from 'clipboard';
33
import { isBlank } from '@ember/utils';
44
import { capitalize } from '@ember/string';
5+
import { guidFor } from '@ember/object/internals';
56

67
const CLIPBOARD_EVENTS = ['success', 'error'];
78

@@ -27,7 +28,14 @@ export default modifier(function clipboard(element, params, hash) {
2728
element.setAttribute('data-clipboard-target', target);
2829
}
2930

30-
const trigger = delegateClickEvent === false ? element : `#${element.id}`;
31+
if (isBlank(element.dataset.clipboardId)) {
32+
element.setAttribute('data-clipboard-id', guidFor(element));
33+
}
34+
35+
const trigger =
36+
delegateClickEvent === false
37+
? element
38+
: `[data-clipboard-id=${element.dataset.clipboardId}]`;
3139

3240
const clipboard = new ClipboardJS(trigger, {
3341
text: typeof text === 'function' ? text : undefined,

tests/dummy/app/templates/application.hbs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@
123123
&lt;/CopyButton&gt;</CodeBlock>
124124
</DocSection>
125125

126+
<DocSection @title="Clipboard Element Modifier" as |showMessage|>
127+
<button
128+
class="application__copy-button"
129+
type="button"
130+
{{clipboard
131+
text='element modifier'
132+
onSuccess=(pipe (fn this.getSuccessMessage "copy") showMessage)
133+
}}
134+
>
135+
Copy Text
136+
</button>
137+
{{! template-lint-disable block-indentation no-unbalanced-curlies}}
138+
<CodeBlock>&lt;button
139+
type="button"
140+
\{{clipboard
141+
text="clipboard modifier"
142+
onSuccess=this.onSuccess
143+
}}
144+
&gt;
145+
Copy Text
146+
&lt;/button&gt;</CodeBlock>
147+
</DocSection>
148+
126149
<DocSection @title="Check If Clipboard Is Supported">
127150
<p class="application__supported-text">
128151
{{#if (is-clipboard-supported)}}

0 commit comments

Comments
 (0)