Skip to content

Commit 0a7eb69

Browse files
authored
Merge pull request #782 from eschaefer/no-localStorage-option
Option to not use localStorage to persist the color mode
2 parents f87d421 + 9d22950 commit 0a7eb69

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

packages/color-modes/src/custom-properties.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const reservedKeys = {
1313
useCustomProperties: true,
1414
initialColorModeName: true,
1515
initialColorMode: true,
16+
useLocalStorage: true,
1617
}
1718

1819
const toPixel = (key, value) => {
@@ -86,6 +87,6 @@ export const createColorStyles = (theme = {}) => {
8687
...styles,
8788
color: 'text',
8889
bg: 'background',
89-
}
90+
},
9091
})(theme)
9192
}

packages/color-modes/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const useColorModeState = (theme = {}) => {
5050

5151
// initialize state
5252
React.useEffect(() => {
53-
const stored = storage.get()
53+
const stored = theme.useLocalStorage !== false && storage.get()
5454
document.body.classList.remove('theme-ui-' + stored)
5555
if (!stored && theme.useColorSchemeMediaQuery) {
5656
const query = getMediaQuery()
@@ -62,7 +62,7 @@ const useColorModeState = (theme = {}) => {
6262
}, [])
6363

6464
React.useEffect(() => {
65-
if (!mode) return
65+
if (!mode || theme.useLocalStorage === false) return
6666
storage.set(mode)
6767
}, [mode])
6868

packages/color-modes/test/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,27 @@ test('initializes mode based on localStorage', () => {
209209
expect(mode).toBe('dark')
210210
})
211211

212+
test('does not initialize mode based on localStorage if useLocalStorage is set to false', () => {
213+
window.localStorage.setItem(STORAGE_KEY, 'dark')
214+
let mode
215+
const Button = props => {
216+
const [colorMode, setMode] = useColorMode()
217+
mode = colorMode
218+
return <button children="test" />
219+
}
220+
const tree = render(
221+
<ThemeProvider
222+
theme={{
223+
useLocalStorage: false,
224+
}}>
225+
<ColorModeProvider>
226+
<Button />
227+
</ColorModeProvider>
228+
</ThemeProvider>
229+
)
230+
expect(mode).toBe('default')
231+
})
232+
212233
test('retains initial context', () => {
213234
let context
214235
const Consumer = props => {

packages/css/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ export interface Theme {
569569
*/
570570
useBorderBox?: boolean
571571

572+
/**
573+
* If false, does not save color mode as a localStorage value.
574+
*/
575+
useLocalStorage?: boolean
576+
572577
/**
573578
* Define the colors that are available through this theme
574579
*/

packages/docs/src/pages/color-modes.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,22 @@ If you do not have a color mode named `dark` or `light`, this will have no effec
148148
}
149149
```
150150

151+
### Disable persisting color mode on `localStorage`
152+
153+
To disable `localStorage` add the `useLocalStorage: false` flag to your theme.
154+
155+
```js
156+
{
157+
useLocalStorage: false,
158+
colors: {
159+
text: '#000',
160+
background: '#fff',
161+
modes: {
162+
dark: {
163+
text: '#fff',
164+
background: '#000',
165+
}
166+
}
167+
}
168+
}
169+
```

packages/docs/src/pages/theming.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Flag | Default | Description
175175
`initialColorModeName` | `'default'` | The key used for the top-level color palette in `theme.colors`
176176
`useColorSchemeMediaQuery` | `false` | Initializes the color mode based on the `prefers-color-scheme` media query
177177
`useBorderBox` | `true` | Adds a global `box-sizing: border-box` style
178+
`useLocalStorage` | `true` | Persists the color mode in `localStorage`
178179

179180
## Example Theme
180181

0 commit comments

Comments
 (0)