Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling element #71

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}))
camertron marked this conversation as resolved.
Show resolved Hide resolved
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)
camertron marked this conversation as resolved.
Show resolved Hide resolved
})
})

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