Skip to content

Commit 3196a27

Browse files
committed
Dismiss sidebar when clicked outside on mobile view. Closes #91.
1 parent e305e11 commit 3196a27

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/js/sidebar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,16 @@ document.addEventListener('click', (e) => {
77
if (toggle) {
88
const layout = toggle.closest('[data-sidebar-layout]');
99
layout?.toggleAttribute('data-sidebar-open');
10+
return;
11+
}
12+
13+
// Dismiss sidebar when clicking outside (when sidebar is not an overlay).
14+
if (!e.target.closest('[data-sidebar]')) {
15+
const layout = document.querySelector('[data-sidebar-layout][data-sidebar-open]');
16+
// Hardcode breakpoint (for now) as there's no way to use a CSS variable in
17+
// the @media{} query which could've been picked up here.
18+
if (layout && window.matchMedia('(max-width: 768px)').matches) {
19+
layout.removeAttribute('data-sidebar-open');
20+
}
1021
}
1122
});

0 commit comments

Comments
 (0)