quartz-research-note/quartz/components/scripts/darkmode.inline.ts

25 lines
875 B
TypeScript
Raw Normal View History

2023-06-07 03:58:26 +00:00
const userPref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'
const currentTheme = localStorage.getItem('theme') ?? userPref
document.documentElement.setAttribute('saved-theme', currentTheme)
2023-06-03 19:07:19 +00:00
2023-07-02 20:08:29 +00:00
document.addEventListener("nav", () => {
2023-06-07 03:58:26 +00:00
const switchTheme = (e: any) => {
2023-06-07 02:48:37 +00:00
if (e.target.checked) {
2023-06-07 03:58:26 +00:00
document.documentElement.setAttribute('saved-theme', 'dark')
localStorage.setItem('theme', 'dark')
2023-06-07 02:48:37 +00:00
}
2023-06-07 03:58:26 +00:00
else {
document.documentElement.setAttribute('saved-theme', 'light')
localStorage.setItem('theme', 'light')
}
}
2023-06-07 02:48:37 +00:00
2023-06-07 03:58:26 +00:00
// Darkmode toggle
const toggleSwitch = document.querySelector('#darkmode-toggle') as HTMLInputElement
2023-07-02 20:08:29 +00:00
toggleSwitch.removeEventListener('change', switchTheme)
toggleSwitch.addEventListener('change', switchTheme)
2023-06-07 03:58:26 +00:00
if (currentTheme === 'dark') {
2023-06-07 02:48:37 +00:00
toggleSwitch.checked = true
}
})