blow/static/js/main.js

32 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-11-04 21:50:54 +00:00
document.addEventListener("DOMContentLoaded", function() {
2021-11-05 18:14:48 +00:00
// ---------------- Selected Navbar Link -------------------------
let navbar_links = document.querySelector('#nav-links').children;
let current_location = window.location.href;
let selected_navbar_link = [...navbar_links].find((item) => {
return (item.href === current_location)
})
selected_navbar_link.className = "bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium"
// ---------------- Switch Theme -------------------------
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark')
} else {
document.documentElement.classList.remove('dark')
}
// Switch theme action
2021-11-06 11:11:10 +00:00
document.getElementById('switch-theme').addEventListener('click', switchTheme);
2021-11-04 21:50:54 +00:00
});
2021-11-05 18:15:13 +00:00
function switchTheme() {
2021-11-06 11:00:57 +00:00
console.log("called")
2021-11-05 18:14:48 +00:00
let current_theme = ([...document.documentElement.classList].includes('dark')) ? 'dark' : 'light';
if (current_theme === 'dark') {
localStorage.theme = 'light'
document.documentElement.classList.remove('dark')
} else {
localStorage.theme = 'dark'
document.documentElement.classList.add('dark')
}
}