|
35 | 35 | // console.log('section-body not found')
|
36 | 36 | // }
|
37 | 37 | // }
|
| 38 | + |
| 39 | + const header = document.querySelector('header') |
| 40 | + const navbarToggle = document.querySelector('.nav-toggle') |
| 41 | + const sectionBody = document.querySelector('.sections-body') |
| 42 | + const toolbar = document.querySelector('.toolbar') |
| 43 | + |
| 44 | + if (!navbarToggle) return |
| 45 | + |
| 46 | + navbarToggle.addEventListener('click', toggleMobileNav) |
| 47 | + |
| 48 | + function toggleMobileNav (e) { |
| 49 | + e.stopPropagation() |
| 50 | + document.documentElement.classList.toggle('is-clipped--navbar') |
| 51 | + this.classList.toggle('is-active') |
| 52 | + const menuId = this.dataset.target |
| 53 | + const menu = document.getElementById(menuId) |
| 54 | + if (menu) { |
| 55 | + menu.classList.toggle('is-active') |
| 56 | + if (menu.classList.contains('is-active')) { |
| 57 | + adjustMenuHeight(menu) |
| 58 | + } else { |
| 59 | + menu.style.maxHeight = '' |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function adjustMenuHeight (menu) { |
| 65 | + const expectedMaxHeight = window.innerHeight - menu.getBoundingClientRect().top |
| 66 | + const actualMaxHeight = parseInt(window.getComputedStyle(menu).maxHeight, 10) |
| 67 | + if (actualMaxHeight !== expectedMaxHeight) { |
| 68 | + menu.style.maxHeight = expectedMaxHeight + 'px' |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (!sectionBody) return |
| 73 | + |
| 74 | + let windowWidth = window.innerWidth |
| 75 | + adjustLayoutForWindowSize(windowWidth) |
| 76 | + |
| 77 | + window.addEventListener('resize', function () { |
| 78 | + const newWindowWidth = window.innerWidth |
| 79 | + if (newWindowWidth !== windowWidth) { |
| 80 | + windowWidth = newWindowWidth |
| 81 | + adjustLayoutForWindowSize(windowWidth) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + function adjustLayoutForWindowSize (windowWidth) { |
| 86 | + if (windowWidth < 1024) { |
| 87 | + removeHeaderAndAdjustPadding() |
| 88 | + } else { |
| 89 | + restoreHeaderAndAdjustPadding() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + function removeHeaderAndAdjustPadding () { |
| 94 | + if (header && document.body.contains(header)) { |
| 95 | + header.remove() |
| 96 | + document.body.style.paddingTop = '0' |
| 97 | + if (toolbar) toolbar.style.top = '0' |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + function restoreHeaderAndAdjustPadding () { |
| 102 | + if (header && !document.body.contains(header)) { |
| 103 | + document.body.insertBefore(header, document.body.firstChild) |
| 104 | + document.body.style.paddingTop = '3.5rem' |
| 105 | + if (toolbar) toolbar.style.top = '3.5rem' |
| 106 | + } |
| 107 | + } |
38 | 108 | })()
|
0 commit comments