Skip to content

Commit 4719f8c

Browse files
authored
Merge pull request #61 from github/kh-arrow-key-aria-disabled
Allow arrow key navigation on aria-disabled elements
2 parents 63a17e1 + 07b4b38 commit 4719f8c

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

examples/index.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
text-align: left;
2323
padding: 0;
2424
}
25+
[aria-disabled="true"] {
26+
color: gray;
27+
}
2528
</style>
2629
</head>
2730
<body>
28-
<h1>Base examples</h1>
31+
<h1>Examples</h1>
32+
<h2>Base examples</h2>
2933

3034
<details>
3135
<summary>Best robot: <span data-menu-button>Unknown</span></summary>
@@ -69,7 +73,19 @@ <h1>Base examples</h1>
6973
</details-menu>
7074
</details>
7175

72-
<h1>Autofocus example</h1>
76+
<h2>`aria-disabled="true" example</h2>
77+
<p>menu items with <code>aria-disabled="true"</code> should be keyboard navigable</p>
78+
<details>
79+
<summary data-menu-button autofocus>Least favorite robots</summary>
80+
<details-menu>
81+
<input type="text" autofocus />
82+
<button name="robot" value="Hubot" aria-disabled="true" role="menuitemradio" data-menu-button-text>Hubot</button>
83+
<button name="robot" value="Bender" role="menuitemradio" data-menu-button-text>Bender</button>
84+
<button name="robot" value="BB-8" role="menuitemradio" data-menu-button-text>BB-8</button>
85+
</details-menu>
86+
</details>
87+
88+
<h2>Autofocus example</h2>
7389
<p><code>summary</code> may have <code>autofocus</code> so it's the initially focused element in the page.</p>
7490
<p>
7591
Then any focusable element inside the popup can declare autofocus too, so it gets focus when the popup is opened.

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ function focusFirstItem(details: Element) {
147147
}
148148

149149
function sibling(details: Element, next: boolean): HTMLElement | null {
150-
const options = Array.from(
151-
details.querySelectorAll<HTMLElement>(
152-
'[role^="menuitem"]:not([hidden]):not([disabled]):not([aria-disabled="true"])'
153-
)
154-
)
150+
const options = Array.from(details.querySelectorAll<HTMLElement>('[role^="menuitem"]:not([hidden]):not([disabled])'))
155151
const selected = document.activeElement
156152
const index = selected instanceof HTMLElement ? options.indexOf(selected) : -1
157153
const found = next ? options[index + 1] : options[index - 1]

test/test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('details-menu element', function () {
9090
summary.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true}))
9191
assert(details.open, 'menu is open')
9292

93-
const last = [...details.querySelectorAll('[role="menuitem"]:not([disabled]):not([aria-disabled])')].pop()
93+
const last = [...details.querySelectorAll('[role="menuitem"]:not([disabled])')].pop()
9494
assert.equal(last, document.activeElement, 'arrow focuses last item')
9595
})
9696

@@ -219,7 +219,7 @@ describe('details-menu element', function () {
219219
summary.dispatchEvent(new MouseEvent('click', {bubbles: true}))
220220
details.dispatchEvent(new KeyboardEvent('keydown', {key: 'ArrowUp', bubbles: true}))
221221

222-
const notDisabled = details.querySelectorAll('[role="menuitem"]')[2]
222+
const notDisabled = details.querySelectorAll('[role="menuitem"]')[3]
223223
assert.equal(notDisabled, document.activeElement, 'arrow focuses on the last non-disabled item')
224224

225225
const disabled = details.querySelector('[aria-disabled="true"]')
@@ -423,7 +423,6 @@ describe('details-menu element', function () {
423423
<details>
424424
<summary>Click</summary>
425425
<details-menu>
426-
<button type="button" role="menuitem" aria-disabled="true">Hubot</button>
427426
<button type="button" role="menuitem" disabled>Bender</button>
428427
</details-menu>
429428
</details>

0 commit comments

Comments
 (0)