Skip to content

Commit

Permalink
Allow disabling element
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Sep 27, 2023
1 parent 838e663 commit a173559
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/clipboard-copy-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ async function copy(button: HTMLElement) {
button.dispatchEvent(new CustomEvent('clipboard-copy', {bubbles: true}))
}

if (button.getAttribute('aria-disabled') === 'true') {
button.dispatchEvent(new CustomEvent('clipboard-copy-nothing', {bubbles: true}))
return
}

if (text) {
await copyText(text)
trigger()
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('clipboard-copy element', function () {
document.addEventListener('clipboard-copy', () => resolve(copiedText), {
once: true,
})

document.addEventListener('clipboard-copy-nothing', () => resolve(null), {
once: true,
})
})
})

Expand Down Expand Up @@ -149,6 +153,20 @@ describe('clipboard-copy element', function () {
const text = await whenCopied
assert.equal(text, 'I am a link')
})

it('does not copy when disabled', async function () {
const target = document.createElement('div')
target.innerHTML = 'Hello world!'
target.id = 'copy-target'
document.body.append(target)

const button = document.querySelector('clipboard-copy')
button.setAttribute('aria-disabled', 'true')
button.click()

const text = await whenCopied
assert.equal(null, text)
})
})

describe('shadow DOM context', function () {
Expand Down

0 comments on commit a173559

Please sign in to comment.