Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
[:bug:] fixed an issue that prevented theme to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Mostafa Shamsitabar committed Apr 3, 2021
1 parent 042736f commit 53f3368
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/sonnat-ui/src/styles/useDarkMode/useDarkMode.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import React from "react";
import { dark, light } from "../createColors";
import usePreviousValue from "../../utils/usePreviousValue";
import defaultTheme from "../defaultTheme";

export default function useDarkMode(isDarkMode = false, theme = defaultTheme) {
let currentTheme = theme;

const prevState = usePreviousValue(isDarkMode);
const cachedTheme = React.useRef(theme);

const newTheme = React.useMemo(() => {
if (prevState != null && isDarkMode !== prevState) {
return {
...cachedTheme.current,
colors: {
...cachedTheme.current.colors,
...(isDarkMode ? dark : light)
},
darkMode: isDarkMode
};
} else return cachedTheme.current;
}, [prevState, isDarkMode]);

if (prevState != null && isDarkMode !== prevState) {
return {
...theme,
colors: { ...theme.colors, ...(isDarkMode ? dark : light) },
darkMode: isDarkMode
};
}
cachedTheme.current = newTheme;

return currentTheme;
return newTheme;
}

0 comments on commit 53f3368

Please sign in to comment.