-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michal Bartosik
committed
May 16, 2021
1 parent
ae3184e
commit 797ae12
Showing
3 changed files
with
37 additions
and
15 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import Theme from 'enums/Theme'; | ||
|
||
const useDarkMode = (): [string, () => void] => { | ||
const [theme, setTheme] = useState<string>(Theme.Light); | ||
|
||
const setMode = useCallback((mode: string) => { | ||
window.localStorage.setItem('theme', mode); | ||
setTheme(mode); | ||
document.body.className = mode; | ||
}, []); | ||
|
||
const themeToggler = (): void => { | ||
theme === Theme.Light ? setMode(Theme.Dark) : setMode(Theme.Light); | ||
}; | ||
|
||
useEffect(() => { | ||
const localTheme = window.localStorage.getItem('theme'); | ||
localTheme ? setTheme(localTheme) : setMode(Theme.Light); | ||
}, [setMode]); | ||
|
||
useEffect(() => { | ||
document.body.className = theme; | ||
}, [theme]); | ||
|
||
return [theme, themeToggler]; | ||
}; | ||
|
||
export default useDarkMode; |
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
797ae12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: