mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-06-29 21:25:57 -04:00
22 lines
967 B
JavaScript
22 lines
967 B
JavaScript
/* eslint-disable no-var */
|
|
|
|
// A super tiny script to restore dark mode off the bat (to hopefully avoid blinding flashes of white).
|
|
// NOTE: This is inlined by Hugo/esbuild (see layouts/partials/head/restore-theme.html) instead of Webpack.
|
|
|
|
import { getDarkPref } from "./src/utils/theme.js";
|
|
|
|
try {
|
|
var cl = document.documentElement.classList;
|
|
var pref = getDarkPref();
|
|
|
|
// set `<html class="dark">` if either the user has explicitly toggled in the past or if their OS is in dark mode
|
|
if (pref === "true" || (!pref && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
|
|
cl.remove("light");
|
|
cl.add("dark");
|
|
}
|
|
|
|
// TODO: fix real-time switching (works but bulb icon isn't updated)
|
|
// window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => e.matches && setDark(true));
|
|
// window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", (e) => e.matches && setDark(false));
|
|
} catch (error) {}
|