blow/static/js/main.js

37 lines
1.7 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')
2021-11-06 11:54:14 +00:00
document.getElementById('dark').classList.add('hidden');
2021-11-05 18:14:48 +00:00
} else {
document.documentElement.classList.remove('dark')
2021-11-06 11:54:14 +00:00
document.getElementById('light').classList.add('hidden');
2021-11-05 18:14:48 +00:00
}
// 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-05 18:14:48 +00:00
let current_theme = ([...document.documentElement.classList].includes('dark')) ? 'dark' : 'light';
if (current_theme === 'dark') {
2021-11-06 11:50:13 +00:00
localStorage.theme = 'light';
document.documentElement.classList.remove('dark');
2021-11-06 11:54:14 +00:00
document.getElementById('light').classList.add('hidden');
document.getElementById('dark').classList.remove('hidden');
2021-11-05 18:14:48 +00:00
} else {
2021-11-06 11:50:13 +00:00
localStorage.theme = 'dark';
document.documentElement.classList.add('dark');
2021-11-06 11:54:14 +00:00
document.getElementById('dark').classList.add('hidden');
document.getElementById('light').classList.remove('hidden');
2021-11-05 18:14:48 +00:00
}
}