Add more links and improve script checks
This commit is contained in:
parent
483f406037
commit
eccd4d8bfe
12 changed files with 264 additions and 205 deletions
|
|
@ -6,27 +6,29 @@ import DarkModeScriptInline from "./DarkModeScriptInline.astro";
|
|||
|
||||
<script>
|
||||
import { ENABLE_VIEW_TRANSITIONS } from "astro:env/client";
|
||||
type ColorScheme = "auto" | "dark" | "light";
|
||||
|
||||
const colorSchemeSetup = () => {
|
||||
let colorScheme = localStorage.colorScheme;
|
||||
let colorScheme: ColorScheme | undefined = localStorage.colorScheme;
|
||||
if (!colorScheme || colorScheme === "auto") {
|
||||
colorScheme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||
}
|
||||
const toggleColorScheme = (e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
if (colorScheme === "dark") {
|
||||
colorScheme = "light";
|
||||
document.body.classList.remove("dark");
|
||||
} else {
|
||||
colorScheme = "dark";
|
||||
document.body.classList.add("dark");
|
||||
}
|
||||
localStorage.colorScheme = colorScheme;
|
||||
};
|
||||
document.querySelectorAll<HTMLElementTagNameMap["button"]>("button[data-dark-mode]").forEach((button) => {
|
||||
button.classList.remove("hidden");
|
||||
button.style.removeProperty("display");
|
||||
button.setAttribute("aria-hidden", "false");
|
||||
button.addEventListener("click", (e) => {
|
||||
e.preventDefault();
|
||||
if (colorScheme === "dark") {
|
||||
colorScheme = "light";
|
||||
document.body.classList.remove("dark");
|
||||
} else {
|
||||
colorScheme = "dark";
|
||||
document.body.classList.add("dark");
|
||||
}
|
||||
localStorage.colorScheme = colorScheme;
|
||||
});
|
||||
button.addEventListener("click", toggleColorScheme);
|
||||
});
|
||||
};
|
||||
if (ENABLE_VIEW_TRANSITIONS) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue