Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions htdocs/js/System/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,62 @@
const threshold = 768;
let currentWidth = window.innerWidth;
const content = document.getElementById('content');
const masthead = document.querySelector('.masthead');
const toggleButton = document.getElementById('toggle-sidebar');
const skipLink = document.getElementById('skip-to-main-content');

// Make masthead, content, and skip to main content link inert while the drawer is open.
let sidebarBackdrop = null;

const handleEscapeKey = (e) => {
if (e.key !== 'Escape') return;
navigation_element.classList.remove('toggle-width');
content.classList.remove('toggle-width');
closeNarrowScreenDrawer();
toggleButton?.focus();
};

const closeNarrowScreenDrawer = () => {
if (!sidebarBackdrop) return;
sidebarBackdrop.remove();
sidebarBackdrop = null;
document.body.classList.remove('no-scroll');
content.inert = false;
if (masthead) masthead.inert = false;
if (skipLink) skipLink.inert = false;
document.removeEventListener('keydown', handleEscapeKey);
};

const openNarrowScreenDrawer = () => {
content.inert = true;
if (masthead) masthead.inert = true;
if (skipLink) skipLink.inert = true;
document.body.classList.add('no-scroll');
document.addEventListener('keydown', handleEscapeKey);

sidebarBackdrop = document.createElement('div');
sidebarBackdrop.classList.add('sidebar-backdrop');
document.body.append(sidebarBackdrop);
sidebarBackdrop.addEventListener('click', () => {
navigation_element.classList.remove('toggle-width');
content.classList.remove('toggle-width');
closeNarrowScreenDrawer();
toggleButton?.focus();
});
};

const toggleSidebar = () => {
navigation_element.classList.toggle('toggle-width');
navigation_element.classList.remove('invisible');
content.classList.toggle('toggle-width');

if (currentWidth <= threshold) {
const overlay = document.createElement('div');
overlay.classList.add('sidebar-backdrop');
document.body.append(overlay);
overlay.addEventListener('click', () => {
overlay.remove();
navigation_element.classList.toggle('toggle-width');
content.classList.toggle('toggle-width');
document.body.classList.remove('no-scroll');
});
document.body.classList.add('no-scroll');
if (sidebarBackdrop) closeNarrowScreenDrawer();
else openNarrowScreenDrawer();
}
};

document.getElementById('toggle-sidebar')?.addEventListener('click', toggleSidebar);
toggleButton?.addEventListener('click', toggleSidebar);

if (currentWidth <= threshold) navigation_element.classList.add('invisible');

Expand All @@ -52,8 +87,7 @@
) {
currentWidth = window.innerWidth;
toggleSidebar();
document.body.classList.remove('no-scroll');
document.querySelectorAll('.sidebar-backdrop').forEach((overlay) => overlay.remove());
closeNarrowScreenDrawer();
}
currentWidth = window.innerWidth;
});
Expand Down
2 changes: 1 addition & 1 deletion templates/layouts/system.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
%
<body>
<%= link_to 'Skip to main content' => '#page-title',
class => 'visually-hidden-focusable bg-light p-2 m-3 position-absolute' =%>
id => 'skip-to-main-content', class => 'visually-hidden-focusable bg-light p-2 m-3 position-absolute' =%>
%
% # Header
<div class="masthead" role="banner">
Expand Down