| 1 | (function () |
| 2 | { |
| 3 | const btn = document.getElementById('chapterMenuBtn'); |
| 4 | const dropdown = document.getElementById('chapterDropdown'); |
| 5 | |
| 6 | function closeDropdown () |
| 7 | { |
| 8 | dropdown.classList.remove('open'); |
| 9 | btn.setAttribute('aria-expanded', 'false'); |
| 10 | } |
| 11 | |
| 12 | btn.addEventListener('click', () => |
| 13 | { |
| 14 | const isOpen = dropdown.classList.toggle('open'); |
| 15 | btn.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); |
| 16 | }); |
| 17 | |
| 18 | // Click outside closes it |
| 19 | document.addEventListener('click', (e) => |
| 20 | { |
| 21 | if (!e.target.closest('.chapterPicker')) closeDropdown(); |
| 22 | }); |
| 23 | |
| 24 | // Smooth scroll when selecting a chapter |
| 25 | dropdown.addEventListener('click', (e) => |
| 26 | { |
| 27 | const target = e.target.closest('button[data-target]'); |
| 28 | if (!target) return; |
| 29 | |
| 30 | const id = target.getAttribute('data-target'); |
| 31 | const el = document.getElementById(id); |
| 32 | if (el) |
| 33 | { |
| 34 | el.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 35 | closeDropdown(); |
| 36 | history.replaceState(null, '', '#' + id); // optional: update URL fragment |
| 37 | } |
| 38 | }); |
| 39 | })(); |