Skip to content

Commit 202237d

Browse files
authored
Merge pull request #51 from pablonete/autofocus-fix
Autofocus fix
2 parents 73048a1 + 342e80f commit 202237d

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

examples/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
</style>
2525
</head>
2626
<body>
27+
<h1>Base examples</h1>
28+
2729
<details>
2830
<summary>Best robot: <span data-menu-button>Unknown</span></summary>
2931
<details-menu>
@@ -60,6 +62,20 @@
6062
</details-menu>
6163
</details>
6264

65+
<h1>Autofocus example</h1>
66+
<p><code>summary</code> may have <code>autofocus</code> so it's the initially focused element in the page.</p>
67+
<p>Then any focusable element inside the popup can declare autofocus too, so it gets focus when the popup is opened.</p>
68+
69+
<details>
70+
<summary data-menu-button autofocus>Autofocus picker</summary>
71+
<details-menu>
72+
<input type="text" autofocus />
73+
<button type="submit" name="robot" value="Hubot" role="menuitemradio" data-menu-button-text>Hubot</button>
74+
<button type="submit" name="robot" value="Bender" role="menuitemradio" data-menu-button-text>Bender</button>
75+
<button type="submit" name="robot" value="BB-8" role="menuitemradio" data-menu-button-text>BB-8</button>
76+
</details-menu>
77+
</details>
78+
6379
<script type="text/javascript">
6480
document.addEventListener('details-menu-selected', e => console.log(e))
6581
</script>

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function closeCurrentMenu(details: Element) {
132132

133133
function autofocus(details: Element): boolean {
134134
if (!details.hasAttribute('open')) return false
135-
const input = details.querySelector<HTMLElement>('[autofocus]')
135+
const input = details.querySelector<HTMLElement>('details-menu [autofocus]')
136136
if (input) {
137137
input.focus()
138138
return true

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,22 @@ describe('details-menu element', function () {
601601

602602
assert.equal(input, document.activeElement, 'toggle open focuses on [autofocus]')
603603
})
604+
605+
it('summary autofocus should not impact with inner autofocus element', function () {
606+
const details = document.querySelector('details')
607+
const summary = details.querySelector('summary')
608+
const input = details.querySelector('input')
609+
610+
// Summary is the initial element of the entire page, while input is the initial element in the popup
611+
summary.setAttribute('autofocus', '')
612+
613+
summary.focus()
614+
details.open = true
615+
summary.dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', bubbles: true}))
616+
details.dispatchEvent(new CustomEvent('toggle'))
617+
618+
assert.equal(document.activeElement, input, 'toggle open focuses on [autofocus]')
619+
})
604620
})
605621

606622
describe('closing the menu', function () {

0 commit comments

Comments
 (0)