Skip to content

Commit

Permalink
fix: solve dark theme loading issue (#145)
Browse files Browse the repository at this point in the history
* docs: correct spelling and pucntuation in heading.mdx

* feat: add my contribution profile

* feat: add my contribution profile

* feat: add my contribution profile

add changes in occupation and level

* Update suzy-g38.mdx

* docs: change in how to write professional commit

add some rules,specifications,headings and examples

* Update How-to-write-professional-commits.mdx

add example after git command

* fix: solve dark theme loading issue

---------

Co-authored-by: Suzy <sulagnaghosh52@gmail.com>
Co-authored-by: Priyankar Pal <88102392+priyankarpal@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 5, 2023
1 parent daea0b2 commit e292423
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/context/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ThemeContext = createContext<ThemeStateType>({
type ThemeProps = ComponentProps<'div'>;

const ThemeProvider: FC<ThemeProps> = ({ children, className, ...props }) => {
const [loading, setLoading] = useState<boolean>(true);
const [isDark, setIsDark] = useState<boolean>(false);
const [isThemeChanged, setIsThemeChanged] = useState<number>(0);
const [mode, setMode] = useState<Mode>(`light`);
Expand All @@ -43,10 +44,12 @@ const ThemeProvider: FC<ThemeProps> = ({ children, className, ...props }) => {
setIsDark(false);
}
}

useEffect(() => {
const darkModeMediaQuery = window.matchMedia(
'(prefers-color-scheme: dark)'
);

const isTokenFound = window.localStorage.getItem('prefers-theme');
if (isTokenFound) {
if (isTokenFound === 'dark') {
Expand All @@ -64,8 +67,10 @@ const ThemeProvider: FC<ThemeProps> = ({ children, className, ...props }) => {
setMode('light');
setIsDark(false);
}

darkModeMediaQuery.addEventListener('change', mediaQueryListener);
}
setLoading(false);
return () =>
darkModeMediaQuery.removeEventListener('change', mediaQueryListener);

Expand All @@ -79,9 +84,11 @@ const ThemeProvider: FC<ThemeProps> = ({ children, className, ...props }) => {
};
return (
<ThemeContext.Provider value={value}>
<div className={`${mode} ${className || ``}`} {...props}>
{children}
</div>
{!loading && (
<div className={`${mode} ${className || ``}`} {...props}>
{children}
</div>
)}
</ThemeContext.Provider>
);
};
Expand Down

1 comment on commit e292423

@vercel
Copy link

@vercel vercel bot commented on e292423 Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.