const navbar = document.querySelector('.navbar') const togglers = document.querySelectorAll('.main-nav-toggler') const modeSelectors = document.querySelectorAll('.switch-mode-collapsed') const colorsBG = ['body', 'secondary', 'tertiary'] function updateNavbar () { if (window.scrollY > 75) { navbar.classList.add('nav-active') const storedTheme = localStorage.getItem('theme') navbar.setAttribute('data-bs-theme', storedTheme) } else { navbar.classList.remove('nav-active') const defaultTheme = navbar.getAttribute('data-bs-overlay') navbar.setAttribute('data-bs-theme', defaultTheme) } } if ((navbar !== null) && (window.performance.getEntriesByType)) { if (window.performance.getEntriesByType('navigation')[0].type === 'reload') { updateNavbar() } } if (navbar !== null && togglers !== null) { // observe state changes to the site's color mode const html = document.querySelector('html') const config = { attributes: true, attributeFilter: ['data-bs-theme'] } const Observer = new MutationObserver((mutationrecords) => { updateNavbar() }) Observer.observe(html, config) // initialize background color const color = (navbar.getAttribute('data-navbar-color') || 'body') const bg = colorsBG.includes(color) ? `var(--bs-${color}-bg)` : `var(--bs-navbar-color-${color})` navbar.style.setProperty('--bs-navbar-expanded-color', bg) // set the navbar background color to opaque when scrolling past a breakpoint window.onscroll = () => { updateNavbar() } // set the navbar background color to opaque when expanded for (let i = 0; i < togglers.length; ++i) { togglers[i].onclick = () => { navbar.classList.toggle('navbar-expanded') } } // invoke the navbar toggler for each mode switcher to collapse the main menu afterwards for (let i = 0; i < modeSelectors.length; ++i) { modeSelectors[i].onclick = () => { for (let j = 0; j < togglers.length; ++j) { const toggler = togglers[j] if (toggler.getAttribute('aria-expanded') === 'true') { toggler.click() } } } } }