Branchwell

Jotley / website / src / app / theme-script.tsx

1export function ThemeScript() {
2 const codeToRunOnClient = `
3 (function() {
4 var effectiveTheme = 'light';
5
6 try {
7 var theme = localStorage.getItem('theme') || 'light';
8 effectiveTheme = theme;
9 if (theme === 'system') {
10 effectiveTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
11 }
12 } catch (e) {
13 effectiveTheme = 'light';
14 }
15
16 var root = document.documentElement;
17 root.classList.remove('light', 'dark');
18 root.classList.add(effectiveTheme);
19 root.dataset.theme = effectiveTheme;
20 root.style.colorScheme = effectiveTheme;
21 })()
22 `;
23
24 return (
25 <script
26 dangerouslySetInnerHTML={{ __html: codeToRunOnClient }}
27 suppressHydrationWarning
28 />
29 );
30}