Skip to content

Commit a043058

Browse files
authored
Merge pull request #71 from github/allow_disabling
Allow disabling element
2 parents 2ffd2e4 + 80570f3 commit a043058

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/clipboard-copy-element.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ async function copy(button: HTMLElement) {
88
button.dispatchEvent(new CustomEvent('clipboard-copy', {bubbles: true}))
99
}
1010

11+
if (button.getAttribute('aria-disabled') === 'true') {
12+
return
13+
}
14+
1115
if (text) {
1216
await copyText(text)
1317
trigger()

test/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('clipboard-copy element', function () {
4242
describe('target element', function () {
4343
const nativeClipboard = navigator.clipboard
4444
let whenCopied
45+
4546
beforeEach(function () {
4647
const container = document.createElement('div')
4748
container.innerHTML = `
@@ -56,6 +57,9 @@ describe('clipboard-copy element', function () {
5657
copiedText = text
5758
return Promise.resolve()
5859
},
60+
readText() {
61+
return Promise.resolve(copiedText)
62+
},
5963
})
6064

6165
whenCopied = new Promise(resolve => {
@@ -149,6 +153,31 @@ describe('clipboard-copy element', function () {
149153
const text = await whenCopied
150154
assert.equal(text, 'I am a link')
151155
})
156+
157+
it('does not copy when disabled', async function () {
158+
const target = document.createElement('div')
159+
target.innerHTML = 'Hello world!'
160+
target.id = 'copy-target'
161+
document.body.append(target)
162+
163+
const button = document.querySelector('clipboard-copy')
164+
button.setAttribute('aria-disabled', 'true')
165+
166+
let fired = false
167+
document.addEventListener(
168+
'clipboard-copy',
169+
() => {
170+
fired = true
171+
},
172+
{once: true},
173+
)
174+
175+
button.click()
176+
177+
await new Promise(setTimeout)
178+
assert.equal(fired, false)
179+
assert.equal(null, await navigator.clipboard.readText())
180+
})
152181
})
153182

154183
describe('shadow DOM context', function () {

web-test-runner.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import {esbuildPlugin} from '@web/dev-server-esbuild'
22
import {playwrightLauncher} from '@web/test-runner-playwright'
3-
const browser = product =>
3+
const getBrowser = product =>
44
playwrightLauncher({
55
product,
6+
createBrowserContext: ({browser}) => {
7+
return browser.newContext({permissions: ['clipboard-read']})
8+
},
69
})
710

811
export default {
912
files: ['test/*'],
1013
nodeResolve: true,
1114
plugins: [esbuildPlugin({ts: true, target: 'es2020'})],
12-
browsers: [browser('chromium')],
15+
browsers: [getBrowser('chromium')],
1316
testFramework: {
1417
config: {
1518
timeout: 1000,

0 commit comments

Comments
 (0)