Skip to content

Commit 46fac71

Browse files
authored
Merge pull request #31 from github/esc
Fix escape keydown trapping
2 parents cb084b1 + 09748bb commit 46fac71

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ function keydown(event: KeyboardEvent) {
221221

222222
switch (event.key) {
223223
case 'Escape':
224-
close(details)
225-
event.preventDefault()
226-
event.stopPropagation()
224+
if (details.hasAttribute('open')) {
225+
close(details)
226+
event.preventDefault()
227+
event.stopPropagation()
228+
}
227229
break
228230
case 'ArrowDown':
229231
{

test/test.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ describe('details-menu element', function() {
9696
const [first, second, rest] = details.querySelectorAll('[role="menuitem"]')
9797
assert(rest)
9898

99-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown'}))
99+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', bubbles: true}))
100100
assert.equal(first, document.activeElement, 'arrow down focuses first item')
101101

102-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown'}))
102+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', bubbles: true}))
103103
assert.equal(second, document.activeElement, 'arrow down focuses second item')
104104

105-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp'}))
105+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true}))
106106
assert.equal(first, document.activeElement, 'arrow up focuses first item')
107107
})
108108

@@ -116,11 +116,23 @@ describe('details-menu element', function() {
116116
first.focus()
117117
assert.equal(first, document.activeElement)
118118

119-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'Escape'}))
119+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'Escape', bubbles: true}))
120120
assert.equal(summary, document.activeElement, 'escape focuses summary')
121121
assert(!details.open, 'details toggles closed')
122122
})
123123

124+
it('allow propagation on escape if details is closed', function() {
125+
const details = document.querySelector('details')
126+
const summary = details.querySelector('summary')
127+
128+
document.addEventListener('keydown', event => {
129+
if (event.key === 'Escape') summary.textContent = 'Propagated'
130+
})
131+
132+
summary.dispatchEvent(new KeyboardEvent('keydown', {key: 'Escape', bubbles: true}))
133+
assert.equal(summary.textContent, 'Propagated')
134+
})
135+
124136
it('updates the button label with text', function() {
125137
const details = document.querySelector('details')
126138
const summary = details.querySelector('summary')
@@ -197,7 +209,7 @@ describe('details-menu element', function() {
197209

198210
summary.focus()
199211
summary.dispatchEvent(new MouseEvent('click', {bubbles: true}))
200-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp'}))
212+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true}))
201213

202214
const notDisabled = details.querySelectorAll('[role="menuitem"]')[2]
203215
assert.equal(notDisabled, document.activeElement, 'arrow focuses on the last non-disabled item')
@@ -217,7 +229,7 @@ describe('details-menu element', function() {
217229

218230
summary.focus()
219231
summary.dispatchEvent(new MouseEvent('click', {bubbles: true}))
220-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp'}))
232+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true}))
221233

222234
const disabled = details.querySelector('[disabled]')
223235
document.addEventListener('details-menu-selected', () => eventCounter++, true)
@@ -365,7 +377,7 @@ describe('details-menu element', function() {
365377
summary.dispatchEvent(new MouseEvent('click', {bubbles: true}))
366378
assert.equal(summary, document.activeElement, 'summary remains focused on toggle')
367379

368-
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown'}))
380+
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowDown', bubbles: true}))
369381
assert.equal(summary, document.activeElement, 'summary remains focused on navigation')
370382
})
371383
})

0 commit comments

Comments
 (0)