Skip to content

Commit 28453b9

Browse files
authored
Update ColorSchemeInjector
Moved to a named scriptContent constant for readability
1 parent 10ad8fe commit 28453b9

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/app/[locale]/ColorSchemeInjector.tsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@ import {
77
} from "~/lib/constants"
88

99
export const ColorSchemeInjector = () => {
10+
const scriptContent = `
11+
(() => {
12+
const STORAGE_KEY = "${DARK_MODE_STORAGE_KEY}";
13+
const storedValue = localStorage.getItem(STORAGE_KEY);
14+
15+
// Determine color scheme
16+
if (storedValue === null || storedValue === undefined) {
17+
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
18+
const colorScheme = prefersDark ? "${COLOR_SCHEME_DARK}" : "${COLOR_SCHEME_LIGHT}";
19+
document.documentElement.classList.add(colorScheme);
20+
} else {
21+
const isDark = storedValue === "true";
22+
document.documentElement.classList.add(isDark ? "${COLOR_SCHEME_DARK}" : "${COLOR_SCHEME_LIGHT}");
23+
}
24+
})();
25+
`.trim()
26+
1027
return (
1128
<script
1229
suppressHydrationWarning
13-
dangerouslySetInnerHTML={{
14-
__html: `(() => {
15-
let DARK_MODE_STORAGE_KEY = "${DARK_MODE_STORAGE_KEY}";
16-
17-
let data = {}
18-
const isDark = localStorage.getItem(DARK_MODE_STORAGE_KEY)
19-
if (typeof isDark === "undefined") {
20-
const currentColorScheme = window.matchMedia("(prefers-color-scheme: dark)")
21-
.matches
22-
? "${COLOR_SCHEME_DARK}"
23-
: "${COLOR_SCHEME_LIGHT}"
24-
document.documentElement.classList.add(currentColorScheme)
25-
} else {
26-
document.documentElement.classList.add(
27-
isDark === "true" ? "${COLOR_SCHEME_DARK}" : "${COLOR_SCHEME_LIGHT}",
28-
)
29-
}
30-
})();`,
31-
}}
32-
></script>
30+
dangerouslySetInnerHTML={{ __html: scriptContent }}
31+
/>
3332
)
3433
}

0 commit comments

Comments
 (0)