-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of flash of erroneously styled content
Using MUI's experimental `useColorScheme`. Fixes #1.
- Loading branch information
1 parent
fe698fb
commit 17180e8
Showing
11 changed files
with
113 additions
and
91 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,5 @@ | ||
import {getInitColorSchemeScript} from '@mui/material/styles'; | ||
|
||
export function onRenderBody({setPreBodyComponents}) { | ||
setPreBodyComponents([getInitColorSchemeScript()]); | ||
} |
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
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
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,11 @@ | ||
import {useCallback, useState} from 'react'; | ||
|
||
const useForceUpdate = () => { | ||
// eslint-disable-next-line react/hook-use-state | ||
const [, updateState] = useState<Record<string, unknown>>(); | ||
return useCallback(() => { | ||
updateState({}); | ||
}, []); | ||
}; | ||
|
||
export default useForceUpdate; |
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,25 @@ | ||
import {useRef, useEffect, useState} from 'react'; | ||
import useForceUpdate from './useForceUpdate'; | ||
|
||
/** | ||
* See https://gist.github.com/jaydenseric/a67cfb1b809b1b789daa17dfe6f83daa | ||
* | ||
* Do not use to avoid warning when calling setState on an unmounted component. | ||
* See https://github.com/facebook/react/pull/22114 | ||
*/ | ||
const useIsMounted = () => { | ||
const componentIsMounted = useRef(false); | ||
const forceUpdate = useForceUpdate(); | ||
|
||
useEffect(() => { | ||
componentIsMounted.current = true; | ||
forceUpdate(); | ||
return () => { | ||
componentIsMounted.current = false; | ||
}; | ||
}, [forceUpdate]); | ||
|
||
return () => componentIsMounted.current; | ||
}; | ||
|
||
export default useIsMounted; |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import {useContext} from 'react'; | ||
import ModeContext from './ModeContext'; | ||
import {useColorScheme} from '@mui/material/styles'; | ||
|
||
const useMode = () => useContext(ModeContext); | ||
const useMode = () => { | ||
const {mode: colorSchemeMode, systemMode} = useColorScheme(); | ||
return colorSchemeMode === 'system' ? systemMode : colorSchemeMode; | ||
}; | ||
|
||
export default useMode; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,8 @@ | ||
import {useMemo} from 'react'; | ||
import {createTheme} from '@mui/material/styles'; | ||
|
||
import useMode from './theme/useMode'; | ||
|
||
const useTheme = () => { | ||
const [mode] = useMode(); | ||
return useMemo( | ||
() => | ||
createTheme({ | ||
palette: { | ||
mode, | ||
}, | ||
}), | ||
[mode], | ||
); | ||
return useMemo(() => createTheme(), []); | ||
}; | ||
|
||
export default useTheme; |