keroosha.cybergulag.today/quartz/components/scripts/darkmode.inline.ts

24 lines
835 B
TypeScript
Raw Normal View History

2023-06-07 06:58:26 +03: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 22:07:19 +03:00
2023-06-07 06:58:26 +03:00
window.addEventListener('DOMContentLoaded', () => {
const switchTheme = (e: any) => {
2023-06-07 05:48:37 +03:00
if (e.target.checked) {
2023-06-07 06:58:26 +03:00
document.documentElement.setAttribute('saved-theme', 'dark')
localStorage.setItem('theme', 'dark')
2023-06-07 05:48:37 +03:00
}
2023-06-07 06:58:26 +03:00
else {
document.documentElement.setAttribute('saved-theme', 'light')
localStorage.setItem('theme', 'light')
}
}
2023-06-07 05:48:37 +03:00
2023-06-07 06:58:26 +03:00
// Darkmode toggle
const toggleSwitch = document.querySelector('#darkmode-toggle') as HTMLInputElement
toggleSwitch.addEventListener('change', switchTheme, false)
if (currentTheme === 'dark') {
2023-06-07 05:48:37 +03:00
toggleSwitch.checked = true
}
})