Skip to content

Commit 1e01d1a

Browse files
committed
wrap up get/set storage methods into try/catch
1 parent c6a43fa commit 1e01d1a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

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

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

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

169
const storage = {
17-
get: init =>
18-
(hasStorage() && window.localStorage.getItem(STORAGE_KEY)) || init,
19-
set: value => hasStorage() && window.localStorage.setItem(STORAGE_KEY, value),
10+
get: init => {
11+
try {
12+
return window.localStorage.getItem(STORAGE_KEY) || init
13+
} catch (e) {
14+
console.warn(e)
15+
}
16+
},
17+
18+
set: value => {
19+
try {
20+
window.localStorage.setItem(STORAGE_KEY, value)
21+
} catch (e) {
22+
console.warn(e)
23+
}
24+
},
2025
}
2126

2227
export const getMediaQuery = () => {

0 commit comments

Comments
 (0)