From a17355909d48d65cee1de4439c647c7642bc6545 Mon Sep 17 00:00:00 2001 From: Cameron Dutro Date: Wed, 27 Sep 2023 10:45:55 -0700 Subject: [PATCH] Allow disabling element --- src/clipboard-copy-element.ts | 5 +++++ test/test.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/clipboard-copy-element.ts b/src/clipboard-copy-element.ts index e889a7d..45467d6 100644 --- a/src/clipboard-copy-element.ts +++ b/src/clipboard-copy-element.ts @@ -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() diff --git a/test/test.js b/test/test.js index 7689e2a..abcdec2 100644 --- a/test/test.js +++ b/test/test.js @@ -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, + }) }) }) @@ -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 () {