Update theme toggle mechanism
This commit is contained in:
parent
22687225b9
commit
246417a553
15 changed files with 425 additions and 200 deletions
|
|
@ -2,13 +2,6 @@ document.querySelector('nav > button')?.addEventListener('click', (e) => {
|
|||
document.querySelector('nav').classList.toggle('visible');
|
||||
})
|
||||
|
||||
function applyTheme(theme){
|
||||
const html = document.querySelector('html')
|
||||
document.cookie = `theme=${theme}; path=/; expires=; SameSite=Strict; Secure`
|
||||
html.classList.remove('dark', 'light')
|
||||
html.classList.add(theme)
|
||||
}
|
||||
|
||||
document.querySelectorAll('.toggleTheme')?.forEach(
|
||||
element => element.addEventListener('click', (e) => {
|
||||
const newTheme = element.dataset.theme;
|
||||
|
|
@ -17,11 +10,10 @@ document.querySelectorAll('.toggleTheme')?.forEach(
|
|||
)
|
||||
);
|
||||
|
||||
const themeFromCookie = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("theme="))
|
||||
?.split("=")[1];
|
||||
document.querySelector('#themeDark').addEventListener('change', (e) => {
|
||||
setTheme(e.target.checked ? 'dark' : 'system');
|
||||
})
|
||||
|
||||
if (themeFromCookie) {
|
||||
applyTheme(themeFromCookie)
|
||||
}
|
||||
document.querySelector('#themeLight').addEventListener('change', (e) => {
|
||||
setTheme(e.target.checked ? 'light' : 'system');
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue