-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Idea: hook into persisted context from other files
- Loading branch information
1 parent
081ce0c
commit d9d7b78
Showing
2 changed files
with
48 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react' | ||
|
||
import {isWeb} from '#/platform/detection' | ||
import { | ||
Schema, | ||
PersistedContext, | ||
PersistedSetStateContext, | ||
} from '#/state/persisted' | ||
|
||
export function useColorMode() { | ||
const {colorMode} = React.useContext(PersistedContext) | ||
const {setState} = React.useContext(PersistedSetStateContext) | ||
|
||
const setColorMode = React.useCallback( | ||
(colorMode: Schema['colorMode']) => { | ||
setState(s => ({ | ||
...s, | ||
colorMode, | ||
})) | ||
|
||
if (isWeb && typeof window !== 'undefined') { | ||
const html = window.document.documentElement | ||
// remove any other color mode classes | ||
html.className = html.className.replace(/colorMode--\w+/g, '') | ||
html.classList.add(`colorMode--${colorMode}`) | ||
} | ||
}, | ||
[setState], | ||
) | ||
|
||
return {colorMode, setColorMode} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters