Skip to content

Commit

Permalink
Issue#51 Fix flickering issue in dark mode during website load
Browse files Browse the repository at this point in the history
  • Loading branch information
zJUNAIDz committed Oct 7, 2024
1 parent aadca4c commit df7d12a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/docs-ui/src/providers/ColorMode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { createContext, useContext, useEffect, useState } from 'react';
import React, { createContext, useContext, useEffect, useLayoutEffect, useState } from 'react';

export type ColorMode = 'light' | 'dark';

Expand All @@ -23,16 +23,18 @@ export const ColorModeProvider = ({ children }: ColorModeProviderProps) => {
const toggleColorMode = () =>
setColorMode(colorMode === 'light' ? 'dark' : 'light');

useEffect(() => {
useLayoutEffect(() => {
if (loaded) {
return;
}

const theme = localStorage.getItem('theme');
if (theme && (theme === 'light' || theme === 'dark')) {
setColorMode(theme);
setLoaded(true);
} else {
setColorMode("light") //if no value in localstorage present
}
setLoaded(true);
}, []);

useEffect(() => {
Expand Down

0 comments on commit df7d12a

Please sign in to comment.