-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dark mode): added theme change with no flash
- Loading branch information
1 parent
1fb96d9
commit 3abd1a9
Showing
4 changed files
with
50 additions
and
20 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
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,31 @@ | ||
import { BaseStyles, NextNProgress, PrimerThemeProvider, SSRProvider } from '@/TabNewsUI'; | ||
import { useEffect, useLayoutEffect, useState } from 'react'; | ||
import { createGlobalStyle } from 'styled-components'; | ||
|
||
// script to be called before interactive in _document.js | ||
// document.documentElement.setAttribute('data-no-flash', true) | ||
const NoFleshGlobalStyle = createGlobalStyle`html[data-no-flash='true']:root {visibility: hidden}`; | ||
const removeNoFlashStyle = () => setTimeout(() => document.documentElement.removeAttribute('data-no-flash')); | ||
const useBrowserLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect; | ||
|
||
export default function ThemeProvider({ children, ...props }) { | ||
const [colorMode, setColorMode] = useState('day'); | ||
|
||
useBrowserLayoutEffect(() => { | ||
const cachedColorMode = localStorage.getItem('colorMode') || 'auto'; | ||
setColorMode(cachedColorMode); | ||
removeNoFlashStyle(); | ||
}, []); | ||
|
||
return ( | ||
<SSRProvider> | ||
<PrimerThemeProvider colorMode={colorMode} {...props}> | ||
<BaseStyles> | ||
<NextNProgress options={{ showSpinner: false }} /> | ||
<NoFleshGlobalStyle /> | ||
{children} | ||
</BaseStyles> | ||
</PrimerThemeProvider> | ||
</SSRProvider> | ||
); | ||
} |