Skip to content

Commit

Permalink
Improved accessibility of sidebar navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Jan 8, 2023
1 parent 5b35b7d commit f8f44f5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 38 deletions.

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions material/assets/javascripts/bundle.029f727b.min.js.map

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions material/assets/javascripts/bundle.2a903f2a.min.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion material/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.2a903f2a.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.029f727b.min.js' | url }}"></script>
{% for path in config.extra_javascript %}
<script src="{{ path | url }}"></script>
{% endfor %}
Expand Down
11 changes: 6 additions & 5 deletions material/partials/nav-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
{% set class = class ~ " md-nav__item--section" %}
{% endif %}
<li class="{{ class }} md-nav__item--nested">
{% set expanded = "navigation.expand" in features %}
{% set active = nav_item.active or expanded %}
{% set checked = "checked" if nav_item.active %}
{% if "navigation.expand" in features and not checked %}
<input class="md-nav__toggle md-toggle md-toggle--indeterminate" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" checked>
{% else %}
<input class="md-nav__toggle md-toggle" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
{% if expanded and not checked %}
{% set indeterminate = "md-toggle--indeterminate" %}
{% endif %}
<input class="md-nav__toggle md-toggle {{ indeterminate }}" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
{% set indexes = [] %}
{% if "navigation.indexes" in features %}
{% for nav_item in nav_item.children %}
Expand All @@ -28,7 +29,7 @@
{% endfor %}
{% endif %}
{% if not indexes %}
<label class="md-nav__link" for="{{ path }}">
<label class="md-nav__link" for="{{ path }}" tabindex="0" aria-expanded="{{ nav_item.active | tojson }}">
{{ nav_item.title }}
<span class="md-nav__icon md-icon"></span>
</label>
Expand Down
14 changes: 14 additions & 0 deletions src/assets/javascripts/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
import { configuration, feature } from "./_"
import {
at,
getActiveElement,
getElement,
getOptionalElement,
requestJSON,
setLocation,
Expand Down Expand Up @@ -186,6 +188,18 @@ keyboard$
if (typeof next !== "undefined")
setLocation(next)
break

/* Expand navigation, see https://bit.ly/3ZjG5io */
case "Enter":
const active = getActiveElement()
if (active instanceof HTMLLabelElement) {
const id = `[id="${active.htmlFor}"]`
const input = getElement<HTMLInputElement>(id)
active.setAttribute(
"aria-expanded",
`${input.checked = !input.checked}`
)
}
}
})

Expand Down
38 changes: 20 additions & 18 deletions src/partials/nav-item.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,24 @@

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

<!-- Active checkbox expands items contained within nested section -->
<!-- Determine checked and indeterminate state -->
{% set checked = "checked" if nav_item.active %}
{% if "navigation.expand" in features and not checked %}
<input
class="md-nav__toggle md-toggle md-toggle--indeterminate"
data-md-toggle="{{ path }}"
type="checkbox"
id="{{ path }}"
checked
/>
{% else %}
<input
class="md-nav__toggle md-toggle"
data-md-toggle="{{ path }}"
type="checkbox"
id="{{ path }}"
{{ checked }}
/>
{% if expanded and not checked %}
{% set indeterminate = "md-toggle--indeterminate" %}
{% endif %}

<!-- Active checkbox expands items contained within nested section -->
<input
class="md-nav__toggle md-toggle {{ indeterminate }}"
data-md-toggle="{{ path }}"
type="checkbox"
id="{{ path }}"
{{ checked }}
/>

<!-- Determine all nested items that are index pages -->
{% set indexes = [] %}
{% if "navigation.indexes" in features %}
Expand All @@ -74,7 +71,12 @@

<!-- Render toggle to expand nested items -->
{% if not indexes %}
<label class="md-nav__link" for="{{ path }}">
<label
class="md-nav__link"
for="{{ path }}"
tabindex="0"
aria-expanded="{{ nav_item.active | tojson }}"
>
{{ nav_item.title }}
<span class="md-nav__icon md-icon"></span>
</label>
Expand Down

0 comments on commit f8f44f5

Please sign in to comment.