Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f30e32c

Browse files
mislavdgraham
authored andcommitted
Fire a custom clipboard-copied event
This is more predictable than native `copy` event and guarantees that the event will always be fired from the `<clipboard-copy>` element.
1 parent 7343b25 commit f30e32c

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ import 'clipboard-copy-element'
5252

5353
## Events
5454

55-
After copying to the clipboard, a [copy][] event is dispatched that can be
56-
used to notify the user with confirmation, like a tooltip or button highlight.
55+
After copying to the clipboard, a `clipboard-copied` event is dispatched from
56+
the `<clipboard-copy>` element:
5757

5858
```js
59-
document.addEventListener('copy', function(event) {
60-
const button = document.activeElement
59+
document.addEventListener('clipboard-copied', function(event) {
60+
const button = event.target
6161
button.classList.add('highlight')
6262
})
6363
```
6464

65-
[copy]: https://developer.mozilla.org/en-US/docs/Web/Events/copy
66-
6765
## Browser support
6866

6967
Browsers without native [custom element support][support] require a [polyfill][].

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
</style>
1212
<script>
13-
document.addEventListener('copy', function() {
13+
document.addEventListener('clipboard-copied', function() {
1414
const notice = document.getElementById('notice')
1515
notice.hidden = false
1616
setTimeout(function() {

src/clipboard-copy-element.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@ import {copyInput, copyNode, copyText} from './clipboard'
55
function copy(button: HTMLElement) {
66
const id = button.getAttribute('for')
77
const text = button.getAttribute('value')
8+
9+
function trigger() {
10+
button.dispatchEvent(new CustomEvent('clipboard-copied', {bubbles: true}))
11+
}
12+
813
if (text) {
9-
copyText(text)
14+
copyText(text).then(trigger)
1015
} else if (id) {
1116
const node = button.ownerDocument.getElementById(id)
12-
if (node) copyTarget(node)
17+
if (node) copyTarget(node).then(trigger)
1318
}
1419
}
1520

1621
function copyTarget(content: Element) {
1722
if (content instanceof HTMLInputElement || content instanceof HTMLTextAreaElement) {
1823
if (content.type === 'hidden') {
19-
copyText(content.value)
24+
return copyText(content.value)
2025
} else {
21-
copyInput(content)
26+
return copyInput(content)
2227
}
2328
} else if (content instanceof HTMLAnchorElement && content.hasAttribute('href')) {
2429
copyText(button, content.href)
2530
} else {
26-
copyNode(content)
31+
return copyNode(content)
2732
}
2833
}
2934

0 commit comments

Comments
 (0)