-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmobile-nav.js
More file actions
32 lines (28 loc) · 1.14 KB
/
mobile-nav.js
File metadata and controls
32 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
document.addEventListener('DOMContentLoaded', function() {
const sidebarBtn = document.querySelector('.sidebar-btn');
const sidebar = document.getElementById('sidebar');
if (sidebarBtn && sidebar) {
// Set initial ARIA state
sidebarBtn.setAttribute('aria-expanded', 'false');
// Toggle sidebar when button is clicked
sidebarBtn.addEventListener('click', function() {
const isExpanded = this.getAttribute('aria-expanded') === 'true';
this.setAttribute('aria-expanded', !isExpanded);
sidebar.classList.toggle('active');
});
// Close sidebar when clicking outside
document.addEventListener('click', function(e) {
if (!sidebar.contains(e.target) && !sidebarBtn.contains(e.target)) {
sidebar.classList.remove('active');
sidebarBtn.setAttribute('aria-expanded', 'false');
}
});
// Close sidebar when pressing Escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape' && sidebar.classList.contains('active')) {
sidebar.classList.remove('active');
sidebarBtn.setAttribute('aria-expanded', 'false');
}
});
}
});