Skip to content

Commit 06085db

Browse files
committed
fix: handle unexpected value from localStorage
1 parent 29b36d6 commit 06085db

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/components/ThemeProvider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,18 @@ const themeDetectorScript = (function () {
5959
function themeFn() {
6060
try {
6161
const storedTheme = localStorage.getItem('theme') || 'auto'
62+
const validTheme = ['light', 'dark', 'auto'].includes(storedTheme)
63+
? storedTheme
64+
: 'auto'
6265

63-
if (storedTheme === 'auto') {
66+
if (validTheme === 'auto') {
6467
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)')
6568
.matches
6669
? 'dark'
6770
: 'light'
6871
document.documentElement.classList.add(autoTheme, 'auto')
6972
} else {
70-
document.documentElement.classList.add(storedTheme)
73+
document.documentElement.classList.add(validTheme)
7174
}
7275
} catch (e) {
7376
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)')

0 commit comments

Comments
 (0)