mirror of
https://github.com/alrayyes/wiki.git
synced 2025-05-03 07:18:14 +00:00
fix darkmode script load
This commit is contained in:
parent
4d3579ca98
commit
0813f127a3
4 changed files with 22 additions and 21 deletions
quartz/components
|
@ -1,3 +1,6 @@
|
|||
// @ts-ignore: this is safe, we don't want to actually make darkmode.inline.ts a module as
|
||||
// modules are automatically deferred and we don't want that to happen for critical beforeDOMLoads
|
||||
// see: https://v8.dev/features/modules#defer
|
||||
import darkmodeScript from "./scripts/darkmode.inline"
|
||||
import styles from '../styles/darkmode.scss'
|
||||
|
||||
|
|
|
@ -1,25 +1,23 @@
|
|||
export default "Darkmode"
|
||||
const userPref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'
|
||||
const currentTheme = localStorage.getItem('theme') ?? userPref
|
||||
document.documentElement.setAttribute('saved-theme', currentTheme)
|
||||
|
||||
const currentTheme = localStorage.getItem("theme")
|
||||
const theme =
|
||||
currentTheme ??
|
||||
(window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark")
|
||||
|
||||
document.documentElement.setAttribute("saved-theme", theme)
|
||||
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
const toggleSwitch = document.querySelector("#darkmode-toggle") as HTMLInputElement
|
||||
toggleSwitch.addEventListener("change", (e: any) => {
|
||||
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")
|
||||
document.documentElement.setAttribute('saved-theme', 'dark')
|
||||
localStorage.setItem('theme', 'dark')
|
||||
}
|
||||
})
|
||||
else {
|
||||
document.documentElement.setAttribute('saved-theme', 'light')
|
||||
localStorage.setItem('theme', 'light')
|
||||
}
|
||||
}
|
||||
|
||||
if (theme === "dark") {
|
||||
// Darkmode toggle
|
||||
const toggleSwitch = document.querySelector('#darkmode-toggle') as HTMLInputElement
|
||||
toggleSwitch.addEventListener('change', switchTheme, false)
|
||||
if (currentTheme === 'dark') {
|
||||
toggleSwitch.checked = true
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue