|
| 1 | +--- |
| 2 | +import { t, getLocaleFromUrl, type Locale } from "../../lib/i18n"; |
| 3 | +
|
| 4 | +interface Props { |
| 5 | + locale?: Locale; |
| 6 | +} |
| 7 | +
|
| 8 | +const locale = Astro.props.locale ?? getLocaleFromUrl(Astro.url); |
| 9 | +--- |
| 10 | + |
| 11 | +<button |
| 12 | + type="button" |
| 13 | + class="theme-toggle font-mono" |
| 14 | + aria-label={t(locale, "theme.toggle.aria")} |
| 15 | + aria-pressed="false" |
| 16 | + data-theme-toggle |
| 17 | +> |
| 18 | + <span class="theme-toggle__icon theme-toggle__icon--sun" aria-hidden="true">☀</span> |
| 19 | + <span class="theme-toggle__sep" aria-hidden="true">/</span> |
| 20 | + <span class="theme-toggle__icon theme-toggle__icon--moon" aria-hidden="true">☾</span> |
| 21 | +</button> |
| 22 | + |
| 23 | +<script> |
| 24 | + // ThemeToggle — binary light/dark switch with persistence. |
| 25 | + // |
| 26 | + // Behavior: |
| 27 | + // - On first visit (no localStorage "ts-theme"), the page follows OS |
| 28 | + // preference, and this script also listens to OS changes live. |
| 29 | + // - Once the user clicks the toggle, we commit to an explicit value |
| 30 | + // ("light" or "dark") in localStorage and stop listening to OS. |
| 31 | + // |
| 32 | + // The initial `data-theme` on <html> is set by the no-FOUC script in |
| 33 | + // BaseLayout; this script only handles click + live OS updates. |
| 34 | + |
| 35 | + const STORAGE_KEY = "ts-theme"; |
| 36 | + const root = document.documentElement; |
| 37 | + |
| 38 | + function currentTheme(): "light" | "dark" { |
| 39 | + if (root.dataset.theme === "dark") return "dark"; |
| 40 | + if (root.dataset.theme === "light") return "light"; |
| 41 | + // data-theme attribute missing → fall back to resolved OS preference |
| 42 | + return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; |
| 43 | + } |
| 44 | + |
| 45 | + function applyTheme(theme: "light" | "dark") { |
| 46 | + root.dataset.theme = theme; |
| 47 | + document.querySelectorAll<HTMLButtonElement>("[data-theme-toggle]").forEach((btn) => { |
| 48 | + btn.setAttribute("aria-pressed", theme === "dark" ? "true" : "false"); |
| 49 | + }); |
| 50 | + window.dispatchEvent(new CustomEvent("theme-change", { detail: { theme } })); |
| 51 | + } |
| 52 | + |
| 53 | + // Sync aria-pressed on load (data-theme already set by no-FOUC script) |
| 54 | + applyTheme(currentTheme()); |
| 55 | + |
| 56 | + document.querySelectorAll<HTMLButtonElement>("[data-theme-toggle]").forEach((btn) => { |
| 57 | + btn.addEventListener("click", () => { |
| 58 | + const next = currentTheme() === "dark" ? "light" : "dark"; |
| 59 | + applyTheme(next); |
| 60 | + try { |
| 61 | + localStorage.setItem(STORAGE_KEY, next); |
| 62 | + } catch (e) { |
| 63 | + /* noop */ |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + // Live OS change listener — only active while user has NOT made an |
| 69 | + // explicit choice (no value in localStorage). |
| 70 | + const mq = window.matchMedia("(prefers-color-scheme: dark)"); |
| 71 | + mq.addEventListener("change", (ev) => { |
| 72 | + let stored: string | null = null; |
| 73 | + try { |
| 74 | + stored = localStorage.getItem(STORAGE_KEY); |
| 75 | + } catch (e) { |
| 76 | + /* noop */ |
| 77 | + } |
| 78 | + if (stored === "light" || stored === "dark") return; |
| 79 | + applyTheme(ev.matches ? "dark" : "light"); |
| 80 | + }); |
| 81 | +</script> |
| 82 | + |
| 83 | +<style> |
| 84 | + .theme-toggle { |
| 85 | + display: inline-flex; |
| 86 | + align-items: center; |
| 87 | + gap: var(--space-1); |
| 88 | + font-size: var(--text-xs); |
| 89 | + padding: 0; |
| 90 | + color: var(--c-gray-400); |
| 91 | + background: none; |
| 92 | + border: none; |
| 93 | + cursor: pointer; |
| 94 | + line-height: 1; |
| 95 | + } |
| 96 | + |
| 97 | + .theme-toggle:focus-visible { |
| 98 | + outline: 2px solid var(--c-ink); |
| 99 | + outline-offset: 3px; |
| 100 | + } |
| 101 | + |
| 102 | + .theme-toggle__icon { |
| 103 | + color: var(--c-gray-400); |
| 104 | + transition: color var(--transition-fast); |
| 105 | + font-size: 0.95rem; |
| 106 | + } |
| 107 | + |
| 108 | + .theme-toggle__sep { |
| 109 | + color: var(--c-gray-300); |
| 110 | + } |
| 111 | + |
| 112 | + /* Active glyph matches current mode */ |
| 113 | + html[data-theme="dark"] .theme-toggle__icon--moon, |
| 114 | + html:not([data-theme="dark"]) .theme-toggle__icon--sun { |
| 115 | + color: var(--c-ink); |
| 116 | + } |
| 117 | + |
| 118 | + .theme-toggle:hover .theme-toggle__icon { |
| 119 | + color: var(--c-ink); |
| 120 | + } |
| 121 | +</style> |
0 commit comments