Improved accessibility of sidebar navigation

This commit is contained in:
squidfunk 2023-01-08 12:48:46 +01:00
parent 5b35b7df57
commit f8f44f5837
7 changed files with 56 additions and 39 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -240,7 +240,7 @@
</script> </script>
{% endblock %} {% endblock %}
{% block scripts %} {% 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 %} {% for path in config.extra_javascript %}
<script src="{{ path | url }}"></script> <script src="{{ path | url }}"></script>
{% endfor %} {% endfor %}

View File

@ -13,12 +13,13 @@
{% set class = class ~ " md-nav__item--section" %} {% set class = class ~ " md-nav__item--section" %}
{% endif %} {% endif %}
<li class="{{ class }} md-nav__item--nested"> <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 %} {% set checked = "checked" if nav_item.active %}
{% if "navigation.expand" in features and not checked %} {% if expanded and not checked %}
<input class="md-nav__toggle md-toggle md-toggle--indeterminate" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" checked> {% set indeterminate = "md-toggle--indeterminate" %}
{% else %}
<input class="md-nav__toggle md-toggle" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
{% endif %} {% endif %}
<input class="md-nav__toggle md-toggle {{ indeterminate }}" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>
{% set indexes = [] %} {% set indexes = [] %}
{% if "navigation.indexes" in features %} {% if "navigation.indexes" in features %}
{% for nav_item in nav_item.children %} {% for nav_item in nav_item.children %}
@ -28,7 +29,7 @@
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% if not indexes %} {% 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 }} {{ nav_item.title }}
<span class="md-nav__icon md-icon"></span> <span class="md-nav__icon md-icon"></span>
</label> </label>

View File

@ -43,6 +43,8 @@ import {
import { configuration, feature } from "./_" import { configuration, feature } from "./_"
import { import {
at, at,
getActiveElement,
getElement,
getOptionalElement, getOptionalElement,
requestJSON, requestJSON,
setLocation, setLocation,
@ -186,6 +188,18 @@ keyboard$
if (typeof next !== "undefined") if (typeof next !== "undefined")
setLocation(next) setLocation(next)
break 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}`
)
}
} }
}) })

View File

@ -41,26 +41,23 @@
<!-- Render item with nested items --> <!-- Render item with nested items -->
<li class="{{ class }} md-nav__item--nested"> <li class="{{ class }} md-nav__item--nested">
{% set expanded = "navigation.expand" in features %}
{% set active = nav_item.active or expanded %}
<!-- Determine checked and indeterminate state -->
{% set checked = "checked" if nav_item.active %}
{% if expanded and not checked %}
{% set indeterminate = "md-toggle--indeterminate" %}
{% endif %}
<!-- Active checkbox expands items contained within nested section --> <!-- Active checkbox expands items contained within nested section -->
{% set checked = "checked" if nav_item.active %}
{% if "navigation.expand" in features and not checked %}
<input <input
class="md-nav__toggle md-toggle md-toggle--indeterminate" class="md-nav__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 }}" data-md-toggle="{{ path }}"
type="checkbox" type="checkbox"
id="{{ path }}" id="{{ path }}"
{{ checked }} {{ checked }}
/> />
{% endif %}
<!-- Determine all nested items that are index pages --> <!-- Determine all nested items that are index pages -->
{% set indexes = [] %} {% set indexes = [] %}
@ -74,7 +71,12 @@
<!-- Render toggle to expand nested items --> <!-- Render toggle to expand nested items -->
{% if not indexes %} {% 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 }} {{ nav_item.title }}
<span class="md-nav__icon md-icon"></span> <span class="md-nav__icon md-icon"></span>
</label> </label>