BillTracker/client/public/theme-init.js

25 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

// Pre-bundle theme apply — avoids a light/dark flash. Mirrors applyTheme in
// client/contexts/ThemeContext.tsx (theme 'system' follows the OS).
// External file (not inline in index.html) so the CSP stays `script-src 'self'`
// with no inline-hash maintenance across build-tool upgrades.
(function () {
try {
var s = localStorage.getItem('bt-theme');
if (s === 'dark-purple') s = 'dark';
var theme = s === 'light' || s === 'dark' || s === 'system' ? s : 'dark';
var resolved =
theme === 'system'
? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'
: theme;
var root = document.documentElement;
if (resolved === 'dark') root.classList.add('dark');
else root.classList.remove('dark');
var m = document.querySelector('meta[name="theme-color"]');
if (m) m.setAttribute('content', resolved === 'dark' ? '#101417' : '#f8faf9');
} catch (e) {
/* localStorage/matchMedia unavailable — keep default */
}
})();