mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 19:46:23 +00:00
23 lines
835 B
TypeScript
23 lines
835 B
TypeScript
const userPref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'
|
|
const currentTheme = localStorage.getItem('theme') ?? userPref
|
|
document.documentElement.setAttribute('saved-theme', currentTheme)
|
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
|
const switchTheme = (e: any) => {
|
|
if (e.target.checked) {
|
|
document.documentElement.setAttribute('saved-theme', 'dark')
|
|
localStorage.setItem('theme', 'dark')
|
|
}
|
|
else {
|
|
document.documentElement.setAttribute('saved-theme', 'light')
|
|
localStorage.setItem('theme', 'light')
|
|
}
|
|
}
|
|
|
|
// Darkmode toggle
|
|
const toggleSwitch = document.querySelector('#darkmode-toggle') as HTMLInputElement
|
|
toggleSwitch.addEventListener('change', switchTheme, false)
|
|
if (currentTheme === 'dark') {
|
|
toggleSwitch.checked = true
|
|
}
|
|
})
|