404 and 500 drafts

This commit is contained in:
2026-11-06 15:42:52 +01:00
parent b17f346b25
commit 785e403370
14 changed files with 335 additions and 95 deletions
+4 -3
View File
@@ -239,9 +239,10 @@ defmodule ChesstrainerWeb.Layouts do
@doc """
Provides the system / light / dark colour-mode switch.
The click handling and `aria-checked` syncing live in the script in
`root.html.heex`, which runs before paint and also on pages without LiveView
(the landing page uses the marketing bundle).
The click handling and `aria-checked` syncing live in `theme_script.html.heex`,
which runs before paint and also on pages without LiveView (the landing page
uses the marketing bundle). Error pages include it explicitly because they do
not render through the root layout.
"""
def colour_mode_toggle(assigns) do
~H"""
@@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta :if={assigns[:noindex]} name="robots" content="noindex" />
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title
@@ -63,79 +64,7 @@
src={assigns[:js_entry] || ~p"/assets/js/app.js"}
>
</script>
<script>
(() => {
const systemMode = () => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const storedMode = () => {
try { return localStorage.getItem("phx:theme"); } catch (_) { return null; }
};
const persistMode = (mode) => {
try {
if (mode === "system") localStorage.removeItem("phx:theme");
else localStorage.setItem("phx:theme", mode);
} catch (_) {
// Storage can be blocked (private modes, embedded browsers); the
// mode still applies for this page load.
}
};
const selectedMode = () => {
const source = document.documentElement.getAttribute("data-theme-source");
return source === "system" ? "system" : document.documentElement.getAttribute("data-theme");
};
const syncChecked = () => {
document.querySelectorAll("[data-phx-theme]").forEach((button) => {
button.setAttribute("aria-checked", String(button.dataset.phxTheme === selectedMode()));
});
};
const setMode = (mode) => {
if (mode !== "system" && mode !== "light" && mode !== "dark") return;
if (mode === "system") {
persistMode("system");
document.documentElement.setAttribute("data-theme", systemMode());
document.documentElement.setAttribute("data-theme-source", "system");
} else {
persistMode(mode);
document.documentElement.setAttribute("data-theme", mode);
document.documentElement.setAttribute("data-theme-source", "user");
}
syncChecked();
};
if (!document.documentElement.hasAttribute("data-theme")) {
setMode(storedMode() || "system");
}
document.addEventListener("DOMContentLoaded", syncChecked);
// Plain DOM so the switch also works on controller-rendered pages
// (the landing page ships no LiveView runtime).
document.addEventListener("click", (event) => {
const button = event.target instanceof Element && event.target.closest("[data-phx-theme]");
if (button) setMode(button.dataset.phxTheme);
});
window.addEventListener("storage", (event) => {
if (event.key === "phx:theme") setMode(event.newValue || "system");
});
window.addEventListener("phx:set-theme", (event) => {
setMode(event.target?.dataset?.phxTheme);
});
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
if (document.documentElement.getAttribute("data-theme-source") === "system") {
document.documentElement.setAttribute("data-theme", systemMode());
}
});
})();
</script>
<Layouts.theme_script />
</head>
<body>
<Layouts.navbar current_scope={@current_scope} />
@@ -0,0 +1,73 @@
<script>
(() => {
const systemMode = () => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const storedMode = () => {
try { return localStorage.getItem("phx:theme"); } catch (_) { return null; }
};
const persistMode = (mode) => {
try {
if (mode === "system") localStorage.removeItem("phx:theme");
else localStorage.setItem("phx:theme", mode);
} catch (_) {
// Storage can be blocked (private modes, embedded browsers); the
// mode still applies for this page load.
}
};
const selectedMode = () => {
const source = document.documentElement.getAttribute("data-theme-source");
return source === "system" ? "system" : document.documentElement.getAttribute("data-theme");
};
const syncChecked = () => {
document.querySelectorAll("[data-phx-theme]").forEach((button) => {
button.setAttribute("aria-checked", String(button.dataset.phxTheme === selectedMode()));
});
};
const setMode = (mode) => {
if (mode !== "system" && mode !== "light" && mode !== "dark") return;
if (mode === "system") {
persistMode("system");
document.documentElement.setAttribute("data-theme", systemMode());
document.documentElement.setAttribute("data-theme-source", "system");
} else {
persistMode(mode);
document.documentElement.setAttribute("data-theme", mode);
document.documentElement.setAttribute("data-theme-source", "user");
}
syncChecked();
};
if (!document.documentElement.hasAttribute("data-theme")) {
setMode(storedMode() || "system");
}
document.addEventListener("DOMContentLoaded", syncChecked);
// Plain DOM so the switch also works on controller-rendered pages
// (the landing page ships no LiveView runtime).
document.addEventListener("click", (event) => {
const button = event.target instanceof Element && event.target.closest("[data-phx-theme]");
if (button) setMode(button.dataset.phxTheme);
});
window.addEventListener("storage", (event) => {
if (event.key === "phx:theme") setMode(event.newValue || "system");
});
window.addEventListener("phx:set-theme", (event) => {
setMode(event.target?.dataset?.phxTheme);
});
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
if (document.documentElement.getAttribute("data-theme-source") === "system") {
document.documentElement.setAttribute("data-theme", systemMode());
}
});
})();
</script>