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 all 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
4 changes: 4 additions & 0 deletions src/clipboard-copy-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ async function copy(button: HTMLElement) {
button.dispatchEvent(new CustomEvent('clipboard-copy', {bubbles: true}))
}

if (button.getAttribute('aria-disabled') === 'true') {
return
}

if (text) {
await copyText(text)
trigger()
Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('clipboard-copy element', function () {
describe('target element', function () {
const nativeClipboard = navigator.clipboard
let whenCopied

beforeEach(function () {
const container = document.createElement('div')
container.innerHTML = `
Expand All @@ -56,6 +57,9 @@ describe('clipboard-copy element', function () {
copiedText = text
return Promise.resolve()
},
readText() {
return Promise.resolve(copiedText)
},
})

whenCopied = new Promise(resolve => {
Expand Down Expand Up @@ -149,6 +153,31 @@ 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')

let fired = false
document.addEventListener(
'clipboard-copy',
() => {
fired = true
},
{once: true},
)

button.click()

await new Promise(setTimeout)
assert.equal(fired, false)
assert.equal(null, await navigator.clipboard.readText())
})
})

describe('shadow DOM context', function () {
Expand Down
7 changes: 5 additions & 2 deletions web-test-runner.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {esbuildPlugin} from '@web/dev-server-esbuild'
import {playwrightLauncher} from '@web/test-runner-playwright'
const browser = product =>
const getBrowser = product =>
playwrightLauncher({
product,
createBrowserContext: ({browser}) => {
return browser.newContext({permissions: ['clipboard-read']})
},
})

export default {
files: ['test/*'],
nodeResolve: true,
plugins: [esbuildPlugin({ts: true, target: 'es2020'})],
browsers: [browser('chromium')],
browsers: [getBrowser('chromium')],
testFramework: {
config: {
timeout: 1000,
Expand Down
Loading