Skip to content

Commit d1829f8

Browse files
committed
Fix uncaught localStorage exception
1 parent 4c98d59 commit d1829f8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/theme-ui/src/color-modes.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import { useThemeUI } from './context'
55
import { createColorStyles } from './custom-properties'
66

77
const STORAGE_KEY = 'theme-ui-color-mode'
8-
const HAS_STORAGE = typeof Storage !== 'undefined'
8+
const hasStorage = () => {
9+
try {
10+
return typeof window.localStorage !== undefined
11+
} catch (e) {
12+
return false
13+
}
14+
}
915

1016
const storage = {
11-
get: init => HAS_STORAGE && window.localStorage.getItem(STORAGE_KEY) || init,
12-
set: value => HAS_STORAGE && window.localStorage.setItem(STORAGE_KEY, value),
17+
get: init =>
18+
(hasStorage() && window.localStorage.getItem(STORAGE_KEY)) || init,
19+
set: value => hasStorage() && window.localStorage.setItem(STORAGE_KEY, value),
1320
}
1421

1522
export const getMediaQuery = () => {

0 commit comments

Comments
 (0)