feat: support non-singleton darkmode

This commit is contained in:
Jacky Zhao 2025-03-10 11:44:47 -07:00
parent 23df17233d
commit dcaf806190
3 changed files with 12 additions and 15 deletions

View file

@ -1,6 +1,4 @@
// @ts-ignore: this is safe, we don't want to actually make darkmode.inline.ts a module as // @ts-ignore
// 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 darkmodeScript from "./scripts/darkmode.inline"
import styles from "./styles/darkmode.scss" import styles from "./styles/darkmode.scss"
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types" import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
@ -9,12 +7,12 @@ import { classNames } from "../util/lang"
const Darkmode: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => { const Darkmode: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
return ( return (
<button class={classNames(displayClass, "darkmode")} id="darkmode"> <button class={classNames(displayClass, "darkmode")}>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink" xmlnsXlink="http://www.w3.org/1999/xlink"
version="1.1" version="1.1"
id="dayIcon" class="dayIcon"
x="0px" x="0px"
y="0px" y="0px"
viewBox="0 0 35 35" viewBox="0 0 35 35"
@ -29,7 +27,7 @@ const Darkmode: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps)
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink" xmlnsXlink="http://www.w3.org/1999/xlink"
version="1.1" version="1.1"
id="nightIcon" class="nightIcon"
x="0px" x="0px"
y="0px" y="0px"
viewBox="0 0 100 100" viewBox="0 0 100 100"

View file

@ -25,12 +25,11 @@ document.addEventListener("nav", () => {
emitThemeChangeEvent(newTheme) emitThemeChangeEvent(newTheme)
} }
// Darkmode toggle for (const darkmodeButton of document.getElementsByClassName("darkmode")) {
const themeButton = document.querySelector("#darkmode") as HTMLButtonElement darkmodeButton.addEventListener("click", switchTheme)
if (themeButton) { window.addCleanup(() => darkmodeButton.removeEventListener("click", switchTheme))
themeButton.addEventListener("click", switchTheme)
window.addCleanup(() => themeButton.removeEventListener("click", switchTheme))
} }
// Listen for changes in prefers-color-scheme // Listen for changes in prefers-color-scheme
const colorSchemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)") const colorSchemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
colorSchemeMediaQuery.addEventListener("change", themeChange) colorSchemeMediaQuery.addEventListener("change", themeChange)

View file

@ -29,19 +29,19 @@
} }
:root[saved-theme="dark"] .darkmode { :root[saved-theme="dark"] .darkmode {
& > #dayIcon { & > .dayIcon {
display: none; display: none;
} }
& > #nightIcon { & > .nightIcon {
display: inline; display: inline;
} }
} }
:root .darkmode { :root .darkmode {
& > #dayIcon { & > .dayIcon {
display: inline; display: inline;
} }
& > #nightIcon { & > .nightIcon {
display: none; display: none;
} }
} }