mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* commented out lesson and learning strip until lessons are established * reword the hero section * change layout and working on puzzle section - remove top div as it was redundant * amend flash message on replay * switch cancel session for return home * remove system theme toggle since this is the default, leaving only light and dark toggles * add keyboard and screen reader support for play (#72) First draft covering the accessibility work in #72:\n\n- labelled SAN/UCI move input, validated against the tablebase and applied\n by the board hook, plus an accessible promotion picker\n- live-region announcements for moves, checks, checkmates and solve/fail,\n and a hidden text summary of the position\n- accessible labels, focus ring, skip link, keyboard-friendly review table\n and non-colour grade cues\n- WCAG AA text contrast roles and a responsive play surface\n- regression tests for move entry, announcements and link-style CTAs
94 lines
3.3 KiB
Plaintext
94 lines
3.3 KiB
Plaintext
<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.
|
|
}
|
|
};
|
|
|
|
// There is no system button, so report the effective theme. This keeps the
|
|
// visible light/dark control in sync when the page is following the OS.
|
|
const selectedMode = () => document.documentElement.getAttribute("data-theme");
|
|
|
|
const syncChecked = () => {
|
|
document.querySelectorAll("[data-phx-theme]").forEach((button) => {
|
|
const checked = button.dataset.phxTheme === selectedMode();
|
|
button.setAttribute("aria-checked", String(checked));
|
|
button.tabIndex = checked ? 0 : -1;
|
|
});
|
|
};
|
|
|
|
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);
|
|
});
|
|
|
|
// Arrow keys move between the radio options, as a radiogroup should.
|
|
document.addEventListener("keydown", (event) => {
|
|
const button = event.target instanceof Element && event.target.closest("[data-phx-theme]");
|
|
if (!button) return;
|
|
|
|
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
|
|
|
|
event.preventDefault();
|
|
const buttons = Array.from(document.querySelectorAll("[data-phx-theme]"));
|
|
const offset = event.key === "ArrowRight" ? 1 : -1;
|
|
const index = (buttons.indexOf(button) + offset + buttons.length) % buttons.length;
|
|
const next = buttons[index];
|
|
|
|
if (next) {
|
|
setMode(next.dataset.phxTheme);
|
|
next.focus();
|
|
}
|
|
});
|
|
|
|
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>
|