Skip to content

Commit

Permalink
fix: SSR on switch theme (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
desoindx authored Nov 29, 2022
1 parent 9183086 commit fa212c1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/interface/SwitchTheme/SwitchTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SwitchTheme = ({
systemLabel,
systemHint,
}) => {
const [storedValue, setStoredValue] = useState(window.localStorage.getItem('prefers-color-scheme') || SYSTEM);
const [storedValue, setStoredValue] = useState('');

const themes = [
{ label: lightLabel, value: LIGHT, svg: <Light /> },
Expand All @@ -33,8 +33,16 @@ const SwitchTheme = ({
];

useEffect(() => {
setStoredValue(window.localStorage.getItem('prefers-color-scheme') || SYSTEM);
}, []);

useEffect(() => {
if (!storedValue) {
return;
}

let tempTheme = storedValue;
if (!storedValue || storedValue === SYSTEM) {
if (storedValue === SYSTEM) {
const preferedTheme = window.matchMedia('(prefers-color-scheme: dark)');
tempTheme = (preferedTheme && preferedTheme.matches) ? DARK : LIGHT;
}
Expand Down

0 comments on commit fa212c1

Please sign in to comment.