This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[:bug:] fixed an issue that prevented theme to be updated
- Loading branch information
Mostafa Shamsitabar
committed
Apr 3, 2021
1 parent
042736f
commit 53f3368
Showing
1 changed file
with
17 additions
and
10 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
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; | ||
} |