Refs #31034 -- Improved accessibility of admin navigation sidebar.
This commit is contained in:
parent
42de52affe
commit
780473d756
|
@ -17,6 +17,7 @@
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
color: #447e9b;
|
color: #447e9b;
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
[dir="rtl"] .toggle-nav-sidebar {
|
[dir="rtl"] .toggle-nav-sidebar {
|
||||||
|
|
|
@ -2,18 +2,35 @@
|
||||||
{
|
{
|
||||||
const toggleNavSidebar = document.getElementById('toggle-nav-sidebar');
|
const toggleNavSidebar = document.getElementById('toggle-nav-sidebar');
|
||||||
if (toggleNavSidebar !== null) {
|
if (toggleNavSidebar !== null) {
|
||||||
|
const navLinks = document.querySelectorAll('#nav-sidebar a');
|
||||||
|
function disableNavLinkTabbing() {
|
||||||
|
for (const navLink of navLinks) {
|
||||||
|
navLink.tabIndex = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function enableNavLinkTabbing() {
|
||||||
|
for (const navLink of navLinks) {
|
||||||
|
navLink.tabIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const main = document.getElementById('main');
|
const main = document.getElementById('main');
|
||||||
let navSidebarIsOpen = localStorage.getItem('django.admin.navSidebarIsOpen');
|
let navSidebarIsOpen = localStorage.getItem('django.admin.navSidebarIsOpen');
|
||||||
if (navSidebarIsOpen === null) {
|
if (navSidebarIsOpen === null) {
|
||||||
navSidebarIsOpen = 'true';
|
navSidebarIsOpen = 'true';
|
||||||
}
|
}
|
||||||
|
if (navSidebarIsOpen === 'false') {
|
||||||
|
disableNavLinkTabbing();
|
||||||
|
}
|
||||||
main.classList.toggle('shifted', navSidebarIsOpen === 'true');
|
main.classList.toggle('shifted', navSidebarIsOpen === 'true');
|
||||||
|
|
||||||
toggleNavSidebar.addEventListener('click', function() {
|
toggleNavSidebar.addEventListener('click', function() {
|
||||||
if (navSidebarIsOpen === 'true') {
|
if (navSidebarIsOpen === 'true') {
|
||||||
navSidebarIsOpen = 'false';
|
navSidebarIsOpen = 'false';
|
||||||
|
disableNavLinkTabbing();
|
||||||
} else {
|
} else {
|
||||||
navSidebarIsOpen = 'true';
|
navSidebarIsOpen = 'true';
|
||||||
|
enableNavLinkTabbing();
|
||||||
}
|
}
|
||||||
localStorage.setItem('django.admin.navSidebarIsOpen', navSidebarIsOpen);
|
localStorage.setItem('django.admin.navSidebarIsOpen', navSidebarIsOpen);
|
||||||
main.classList.toggle('shifted');
|
main.classList.toggle('shifted');
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<div class="sticky toggle-nav-sidebar" id="toggle-nav-sidebar"></div>
|
{% load i18n %}
|
||||||
|
<button class="sticky toggle-nav-sidebar" id="toggle-nav-sidebar" aria-label="{% translate 'Toggle navigation' %}"></button>
|
||||||
<nav class="sticky" id="nav-sidebar">
|
<nav class="sticky" id="nav-sidebar">
|
||||||
{% include 'admin/app_list.html' with app_list=available_apps %}
|
{% include 'admin/app_list.html' with app_list=available_apps %}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
@ -97,7 +97,14 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
def test_sidebar_can_be_closed(self):
|
def test_sidebar_can_be_closed(self):
|
||||||
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
|
self.selenium.get(self.live_server_url + reverse('test_with_sidebar:auth_user_changelist'))
|
||||||
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
|
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
|
||||||
|
self.assertEqual(toggle_button.tag_name, 'button')
|
||||||
|
self.assertEqual(toggle_button.get_attribute('aria-label'), 'Toggle navigation')
|
||||||
|
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
|
||||||
|
self.assertEqual(link.get_attribute('tabIndex'), '0')
|
||||||
toggle_button.click()
|
toggle_button.click()
|
||||||
|
# Hidden sidebar is not reachable via keyboard navigation.
|
||||||
|
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
|
||||||
|
self.assertEqual(link.get_attribute('tabIndex'), '-1')
|
||||||
main_element = self.selenium.find_element_by_css_selector('#main')
|
main_element = self.selenium.find_element_by_css_selector('#main')
|
||||||
self.assertNotIn('shifted', main_element.get_attribute('class').split())
|
self.assertNotIn('shifted', main_element.get_attribute('class').split())
|
||||||
|
|
||||||
|
@ -115,7 +122,12 @@ class SeleniumTests(AdminSeleniumTestCase):
|
||||||
self.assertNotIn('shifted', main_element.get_attribute('class').split())
|
self.assertNotIn('shifted', main_element.get_attribute('class').split())
|
||||||
|
|
||||||
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
|
toggle_button = self.selenium.find_element_by_css_selector('#toggle-nav-sidebar')
|
||||||
|
# Hidden sidebar is not reachable via keyboard navigation.
|
||||||
|
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
|
||||||
|
self.assertEqual(link.get_attribute('tabIndex'), '-1')
|
||||||
toggle_button.click()
|
toggle_button.click()
|
||||||
|
for link in self.selenium.find_elements_by_css_selector('#nav-sidebar a'):
|
||||||
|
self.assertEqual(link.get_attribute('tabIndex'), '0')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.selenium.execute_script("return localStorage.getItem('django.admin.navSidebarIsOpen')"),
|
self.selenium.execute_script("return localStorage.getItem('django.admin.navSidebarIsOpen')"),
|
||||||
'true',
|
'true',
|
||||||
|
|
Loading…
Reference in New Issue