Skip to content

Commit

Permalink
fix(color-mode): fix clearing color mode (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Oct 5, 2022
1 parent 0843917 commit 1bb5cb8
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions packages/core/src/colorModes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,13 @@ export function useColorModeState(
// Add mode className
const customPropertiesEnabled = checkHasCustomPropertiesEnabled(theme)

const manualSetRef = React.useRef(false)
const manuallySetMode = React.useCallback((value: string | null) => {
manualSetRef.current = true
setMode(value)
setMode(value || null)
if (value) {
storage.set(value)
} else {
storage.clear()
}
}, [])

// Set initial color mode in lazy
Expand All @@ -237,17 +240,6 @@ export function useColorModeState(
}
}, [])

// Store mode preference
useIsomorphicLayoutEffect(() => {
if (manualSetRef.current) {
if (mode) {
storage.set(mode)
} else {
storage.clear()
}
}
}, [mode])

// Sync system mode
useIsomorphicLayoutEffect(() => {
const storedMode = storage.get()
Expand Down

0 comments on commit 1bb5cb8

Please sign in to comment.