Skip to content

Commit f8f44f5

Browse files
committed
Improved accessibility of sidebar navigation
1 parent 5b35b7d commit f8f44f5

File tree

7 files changed

+55
-38
lines changed

7 files changed

+55
-38
lines changed

material/assets/javascripts/bundle.2a903f2a.min.js renamed to material/assets/javascripts/bundle.029f727b.min.js

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/javascripts/bundle.029f727b.min.js.map

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/javascripts/bundle.2a903f2a.min.js.map

-8
This file was deleted.

material/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
</script>
241241
{% endblock %}
242242
{% block scripts %}
243-
<script src="{{ 'assets/javascripts/bundle.2a903f2a.min.js' | url }}"></script>
243+
<script src="{{ 'assets/javascripts/bundle.029f727b.min.js' | url }}"></script>
244244
{% for path in config.extra_javascript %}
245245
<script src="{{ path | url }}"></script>
246246
{% endfor %}

material/partials/nav-item.html

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
{% set class = class ~ " md-nav__item--section" %}
1414
{% endif %}
1515
<li class="{{ class }} md-nav__item--nested">
16+
{% set expanded = "navigation.expand" in features %}
17+
{% set active = nav_item.active or expanded %}
1618
{% set checked = "checked" if nav_item.active %}
17-
{% if "navigation.expand" in features and not checked %}
18-
<input class="md-nav__toggle md-toggle md-toggle--indeterminate" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" checked>
19-
{% else %}
20-
<input class="md-nav__toggle md-toggle" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
19+
{% if expanded and not checked %}
20+
{% set indeterminate = "md-toggle--indeterminate" %}
2121
{% endif %}
22+
<input class="md-nav__toggle md-toggle {{ indeterminate }}" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
2223
{% set indexes = [] %}
2324
{% if "navigation.indexes" in features %}
2425
{% for nav_item in nav_item.children %}
@@ -28,7 +29,7 @@
2829
{% endfor %}
2930
{% endif %}
3031
{% if not indexes %}
31-
<label class="md-nav__link" for="{{ path }}">
32+
<label class="md-nav__link" for="{{ path }}" tabindex="0" aria-expanded="{{ nav_item.active | tojson }}">
3233
{{ nav_item.title }}
3334
<span class="md-nav__icon md-icon"></span>
3435
</label>

src/assets/javascripts/bundle.ts

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import {
4343
import { configuration, feature } from "./_"
4444
import {
4545
at,
46+
getActiveElement,
47+
getElement,
4648
getOptionalElement,
4749
requestJSON,
4850
setLocation,
@@ -186,6 +188,18 @@ keyboard$
186188
if (typeof next !== "undefined")
187189
setLocation(next)
188190
break
191+
192+
/* Expand navigation, see https://bit.ly/3ZjG5io */
193+
case "Enter":
194+
const active = getActiveElement()
195+
if (active instanceof HTMLLabelElement) {
196+
const id = `[id="${active.htmlFor}"]`
197+
const input = getElement<HTMLInputElement>(id)
198+
active.setAttribute(
199+
"aria-expanded",
200+
`${input.checked = !input.checked}`
201+
)
202+
}
189203
}
190204
})
191205

src/partials/nav-item.html

+20-18
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,24 @@
4141

4242
<!-- Render item with nested items -->
4343
<li class="{{ class }} md-nav__item--nested">
44+
{% set expanded = "navigation.expand" in features %}
45+
{% set active = nav_item.active or expanded %}
4446

45-
<!-- Active checkbox expands items contained within nested section -->
47+
<!-- Determine checked and indeterminate state -->
4648
{% set checked = "checked" if nav_item.active %}
47-
{% if "navigation.expand" in features and not checked %}
48-
<input
49-
class="md-nav__toggle md-toggle md-toggle--indeterminate"
50-
data-md-toggle="{{ path }}"
51-
type="checkbox"
52-
id="{{ path }}"
53-
checked
54-
/>
55-
{% else %}
56-
<input
57-
class="md-nav__toggle md-toggle"
58-
data-md-toggle="{{ path }}"
59-
type="checkbox"
60-
id="{{ path }}"
61-
{{ checked }}
62-
/>
49+
{% if expanded and not checked %}
50+
{% set indeterminate = "md-toggle--indeterminate" %}
6351
{% endif %}
6452

53+
<!-- Active checkbox expands items contained within nested section -->
54+
<input
55+
class="md-nav__toggle md-toggle {{ indeterminate }}"
56+
data-md-toggle="{{ path }}"
57+
type="checkbox"
58+
id="{{ path }}"
59+
{{ checked }}
60+
/>
61+
6562
<!-- Determine all nested items that are index pages -->
6663
{% set indexes = [] %}
6764
{% if "navigation.indexes" in features %}
@@ -74,7 +71,12 @@
7471

7572
<!-- Render toggle to expand nested items -->
7673
{% if not indexes %}
77-
<label class="md-nav__link" for="{{ path }}">
74+
<label
75+
class="md-nav__link"
76+
for="{{ path }}"
77+
tabindex="0"
78+
aria-expanded="{{ nav_item.active | tojson }}"
79+
>
7880
{{ nav_item.title }}
7981
<span class="md-nav__icon md-icon"></span>
8082
</label>

0 commit comments

Comments
 (0)