website/layouts/partials/js-global.html
steverusso 0c70a13a3a close the nav via 'going back' in the browser (#47)
This PR makes it so that opening the nav used a hash in the browser's location. This way, we can program the 'back' action in the browser to close the nav.

Haven't tested on mobile yet, might be a bit quirky.

Co-authored-by: Steve Russo <steverusso@pm.me>
Reviewed-on: #47
Reviewed-by: jeff <jeff@simplesystems.tech>
Co-authored-by: steverusso <steverusso@protonmail.com>
Co-committed-by: steverusso <steverusso@protonmail.com>
2022-04-25 16:38:53 +00:00

68 lines
1.4 KiB
HTML

<script>
function openNavDrawer() {
navDrawerOverlay.classList.remove('d-none')
navDrawer.style.transform = 'translate(0)'
}
function closeNavDrawer() {
navDrawerOverlay.classList.add('d-none')
navDrawer.style.transform = 'translate(100%)'
}
function currentTheme() {
var _isDark = JSON.parse(localStorage.getItem('themeIsDark'))
return _isDark
}
function storeTheme(_isDark) {
localStorage.setItem('themeIsDark', _isDark)
}
function setThemeTo(_isDark) {
var elems = [
...document.body.getElementsByTagName("*"),
document.body,
]
elems.forEach(el => {
if (_isDark) {
el.classList.remove('light')
el.classList.add('dark')
} else {
el.classList.remove('dark')
el.classList.add('light')
}
})
initToggle(_isDark)
storeTheme(_isDark)
}
function toggleTheme() {
var th = currentTheme()
if (th === null) {
th = window.matchMedia("(prefers-color-scheme: dark)").matches
}
setThemeTo(!th)
}
function initToggle(_isDark) {
if (_isDark) {
thToggle.innerHTML = '<i class="material-icons">wb_sunny</i>'
} else {
thToggle.innerHTML = '<i class="material-icons">brightness_2</i>'
}
}
(function() {
var th = currentTheme()
if (th === null) {
initToggle(window.matchMedia("(prefers-color-scheme: dark)").matches)
return
}
setThemeTo(th)
initToggle(th)
closeNavDrawer()
})()
</script>