feat(website): add a persistent light and dark theme switch
This commit is contained in:
@@ -224,9 +224,7 @@ function initializeDinoDemo(demo) {
|
||||
stop(t("Paused. Press Play to continue."));
|
||||
}).observe(demo);
|
||||
}
|
||||
window
|
||||
.matchMedia("(prefers-color-scheme: dark)")
|
||||
.addEventListener("change", draw);
|
||||
window.addEventListener("website-theme-change", draw);
|
||||
demo.querySelector(".dino-fallback").setAttribute("hidden", "");
|
||||
demo.querySelector(".dino-controls").hidden = false;
|
||||
canvas.hidden = false;
|
||||
|
||||
@@ -284,6 +284,31 @@ section[id] {
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.theme-toggle {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
padding: 0;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 5px;
|
||||
background: var(--page);
|
||||
color: var(--muted);
|
||||
}
|
||||
.theme-toggle:hover {
|
||||
background: var(--soft);
|
||||
color: var(--ink);
|
||||
}
|
||||
.theme-toggle[hidden],
|
||||
.theme-toggle .theme-sun,
|
||||
:root[data-theme="dark"] .theme-toggle .theme-moon {
|
||||
display: none;
|
||||
}
|
||||
:root[data-theme="dark"] .theme-toggle .theme-sun {
|
||||
display: block;
|
||||
}
|
||||
.nav-github,
|
||||
.nav-discord,
|
||||
.nav-x {
|
||||
@@ -394,7 +419,7 @@ section[id] {
|
||||
font-weight: 600;
|
||||
}
|
||||
.button.home-setup-link:hover {
|
||||
background: #eef3fa;
|
||||
background: var(--soft);
|
||||
}
|
||||
html[dir="rtl"] .home-setup-link .button-arrow {
|
||||
transform: scaleX(-1);
|
||||
@@ -2606,96 +2631,94 @@ pre code {
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
--ink: #cdd6f4;
|
||||
--muted: #a6adc8;
|
||||
--line: #45475a;
|
||||
--soft: #181825;
|
||||
--accent: #89b4fa;
|
||||
--page: #1e1e2e;
|
||||
}
|
||||
::selection {
|
||||
background: #45475a;
|
||||
}
|
||||
.dino-stage {
|
||||
--dino-ground: #45475a;
|
||||
--dino-ink: #a6adc8;
|
||||
--dino-player: #89b4fa;
|
||||
--dino-crash: #f38ba8;
|
||||
--dino-eye: #181825;
|
||||
}
|
||||
.reconstruction-flywheel [stroke="#294f82"] {
|
||||
stroke: var(--accent);
|
||||
}
|
||||
.reconstruction-flywheel [stroke="#5b6570"] {
|
||||
stroke: var(--muted);
|
||||
}
|
||||
.skip-link,
|
||||
.button,
|
||||
.dino-buttons .button {
|
||||
color: var(--page);
|
||||
}
|
||||
.button {
|
||||
border-color: #bac2de;
|
||||
background: #bac2de;
|
||||
}
|
||||
.button:hover {
|
||||
border-color: #a6adc8;
|
||||
background: #a6adc8;
|
||||
}
|
||||
.code-key,
|
||||
.notion-channel,
|
||||
.prose-highlight,
|
||||
.step-selector button[aria-pressed="true"],
|
||||
.percent-operation button[aria-pressed="true"],
|
||||
.flow-result,
|
||||
.home-nav-links a[aria-current="location"] {
|
||||
background: #313244;
|
||||
}
|
||||
.copy-button:hover {
|
||||
background: #313244;
|
||||
}
|
||||
.step-comparison .is-active {
|
||||
background: #313244;
|
||||
color: #b4befe;
|
||||
}
|
||||
.article p,
|
||||
.article li,
|
||||
.guide-route span {
|
||||
color: #bac2de;
|
||||
}
|
||||
.prompt {
|
||||
border-left-color: #585b70;
|
||||
}
|
||||
.agent-terminal {
|
||||
border-color: var(--line);
|
||||
background: #181825;
|
||||
}
|
||||
.terminal-header {
|
||||
border-bottom-color: #313244;
|
||||
}
|
||||
.agent-terminal .copy-button:hover {
|
||||
background: #313244;
|
||||
}
|
||||
.agent-one-prompt {
|
||||
color: var(--ink);
|
||||
}
|
||||
.workflow-figure {
|
||||
background: var(--soft);
|
||||
}
|
||||
.flow-node {
|
||||
background: #313244;
|
||||
border-color: #585b70;
|
||||
}
|
||||
.flow-steps li:not(:last-child)::after,
|
||||
.notion-mini-flow li:not(:last-child)::after {
|
||||
color: #9399b2;
|
||||
}
|
||||
.percent-controls .percent-key {
|
||||
border-color: #585b70;
|
||||
}
|
||||
:root[data-theme="dark"] {
|
||||
color-scheme: dark;
|
||||
--ink: #cdd6f4;
|
||||
--muted: #a6adc8;
|
||||
--line: #45475a;
|
||||
--soft: #181825;
|
||||
--accent: #89b4fa;
|
||||
--page: #1e1e2e;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) ::selection {
|
||||
background: #45475a;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .dino-stage {
|
||||
--dino-ground: #45475a;
|
||||
--dino-ink: #a6adc8;
|
||||
--dino-player: #89b4fa;
|
||||
--dino-crash: #f38ba8;
|
||||
--dino-eye: #181825;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .reconstruction-flywheel [stroke="#294f82"] {
|
||||
stroke: var(--accent);
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .reconstruction-flywheel [stroke="#5b6570"] {
|
||||
stroke: var(--muted);
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .skip-link,
|
||||
:where(:root[data-theme="dark"]) .button,
|
||||
:where(:root[data-theme="dark"]) .dino-buttons .button {
|
||||
color: var(--page);
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .button {
|
||||
border-color: #bac2de;
|
||||
background: #bac2de;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .button:hover {
|
||||
border-color: #a6adc8;
|
||||
background: #a6adc8;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .code-key,
|
||||
:where(:root[data-theme="dark"]) .notion-channel,
|
||||
:where(:root[data-theme="dark"]) .prose-highlight,
|
||||
:where(:root[data-theme="dark"]) .step-selector button[aria-pressed="true"],
|
||||
:where(:root[data-theme="dark"]) .percent-operation button[aria-pressed="true"],
|
||||
:where(:root[data-theme="dark"]) .flow-result,
|
||||
:where(:root[data-theme="dark"]) .home-nav-links a[aria-current="location"] {
|
||||
background: #313244;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .copy-button:hover {
|
||||
background: #313244;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .step-comparison .is-active {
|
||||
background: #313244;
|
||||
color: #b4befe;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .article p,
|
||||
:where(:root[data-theme="dark"]) .article li,
|
||||
:where(:root[data-theme="dark"]) .guide-route span {
|
||||
color: #bac2de;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .prompt {
|
||||
border-left-color: #585b70;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .agent-terminal {
|
||||
border-color: var(--line);
|
||||
background: #181825;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .terminal-header {
|
||||
border-bottom-color: #313244;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .agent-terminal .copy-button:hover {
|
||||
background: #313244;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .agent-one-prompt {
|
||||
color: var(--ink);
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .workflow-figure {
|
||||
background: var(--soft);
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .flow-node {
|
||||
background: #313244;
|
||||
border-color: #585b70;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .flow-steps li:not(:last-child)::after,
|
||||
:where(:root[data-theme="dark"]) .notion-mini-flow li:not(:last-child)::after {
|
||||
color: #9399b2;
|
||||
}
|
||||
:where(:root[data-theme="dark"]) .percent-controls .percent-key {
|
||||
border-color: #585b70;
|
||||
}
|
||||
|
||||
@media (max-width: 360px) {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
// This small, blocking head script applies the theme before the first paint.
|
||||
(() => {
|
||||
const root = document.documentElement;
|
||||
const system = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const storageKey = "rea-theme";
|
||||
const parseChoice = (value) =>
|
||||
value === "light" || value === "dark" ? value : null;
|
||||
let choice = null;
|
||||
let storage = null;
|
||||
try {
|
||||
storage = window.localStorage;
|
||||
choice = parseChoice(storage.getItem(storageKey));
|
||||
} catch {
|
||||
// The switch still works when browser storage is unavailable.
|
||||
}
|
||||
|
||||
const applyTheme = () => {
|
||||
const theme = choice ?? (system.matches ? "dark" : "light");
|
||||
root.dataset.theme = theme;
|
||||
document.querySelector('meta[name="theme-color"]').content =
|
||||
theme === "dark" ? "#1e1e2e" : "#ffffff";
|
||||
window.dispatchEvent(new Event("website-theme-change"));
|
||||
};
|
||||
|
||||
applyTheme();
|
||||
system.addEventListener("change", () => {
|
||||
if (choice === null) applyTheme();
|
||||
});
|
||||
window.addEventListener("storage", (event) => {
|
||||
if (
|
||||
storage !== null &&
|
||||
event.storageArea === storage &&
|
||||
(event.key === storageKey || event.key === null)
|
||||
) {
|
||||
try {
|
||||
// Another tab may have written a newer choice while this event waited.
|
||||
choice = parseChoice(storage.getItem(storageKey));
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
applyTheme();
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
const button = document.querySelector("[data-theme-toggle]");
|
||||
if (button === null) return;
|
||||
const { t } = await import("./i18n.js");
|
||||
const updateLabel = () => {
|
||||
const label =
|
||||
root.dataset.theme === "dark"
|
||||
? t("Switch to light mode")
|
||||
: t("Switch to dark mode");
|
||||
button.setAttribute("aria-label", label);
|
||||
button.title = label;
|
||||
};
|
||||
window.addEventListener("website-theme-change", updateLabel);
|
||||
button.addEventListener("click", () => {
|
||||
choice = root.dataset.theme === "dark" ? "light" : "dark";
|
||||
try {
|
||||
storage?.setItem(storageKey, choice);
|
||||
} catch {
|
||||
// Retain the choice for this page even if it cannot be saved.
|
||||
}
|
||||
applyTheme();
|
||||
});
|
||||
updateLabel();
|
||||
button.hidden = false;
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user