Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
1.6 kB · 53 lines
Astro
at pds-branding
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354---// replaces starlight's three-way theme select with a light/dark toggle. it// keeps starlight's storage key, so the head script still applies the stored// theme before paint, and follows the system until someone picks oneimport Button from "$lib/components/ui/Button.svelte";import Sun from "~icons/lucide/sun";import Moon from "~icons/lucide/moon";---
<docs-theme-toggle> <Button variant="ghost" icon={Sun} aria-label="Switch to dark mode" class="text-foreground-muted dark:hidden" /> <Button variant="ghost" icon={Moon} aria-label="Switch to light mode" class="hidden text-foreground-muted dark:inline-flex" /></docs-theme-toggle>
<script> const storageKey = "starlight-theme"; const system = matchMedia("(prefers-color-scheme: dark)");
const stored = () => { const theme = localStorage.getItem(storageKey); return theme === "light" || theme === "dark" ? theme : undefined; }; const apply = (theme: "light" | "dark") => { document.documentElement.dataset.theme = theme; };
system.addEventListener("change", (e) => { if (!stored()) apply(e.matches ? "dark" : "light"); });
class DocsThemeToggle extends HTMLElement { connectedCallback() { for (const button of this.querySelectorAll("button")) { button.addEventListener("click", () => { const next = document.documentElement.dataset.theme === "dark" ? "light" : "dark"; localStorage.setItem(storageKey, next); apply(next); }); } } } customElements.define("docs-theme-toggle", DocsThemeToggle);</script>