feat(theme): add theme toggle and pre-mount theme bootstrap

Add a self-contained theme toggle component under
public/src/components/theme/ that toggles the dark class on the root
element and stores the choice under the beat-theme key in
localStorage. Apply the stored theme, or the prefers-color-scheme
default when nothing is stored, before the app mounts so a cold load
never paints the wrong theme.

The pre-paint script in index.html and both 404 copies reads the same
beat-theme key, so the theme is already on the root element when the
first frame is painted.
This commit is contained in:
Locust agent
2026-11-02 18:03:56 +00:00
parent bbb9ce3ae9
commit 640a04d483
7 changed files with 147 additions and 48 deletions
+7 -6
View File
@@ -32,14 +32,15 @@
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Dark mode initialization (prevents flash) -->
<!-- Theme initialization (prevents flash) -->
<script>
(function() {
const theme = localStorage.theme;
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (theme === 'dark' || (!theme && systemDark)) {
document.documentElement.classList.add('dark');
}
var stored = null;
try { stored = window.localStorage.getItem('beat-theme'); } catch (e) { stored = null; }
var systemDark = false;
try { systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches; } catch (e) { systemDark = false; }
var dark = stored === 'dark' || (stored !== 'light' && systemDark);
document.documentElement.classList.toggle('dark', dark);
})();
</script>
</head>