-
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.
feat: Implement dark mode in ChatHeader (#252)
* feat: Implement dark mode in ChatHeader * feat: solving warnings * feat: Update TypingIndicator * feat: implementing persistent theme * feat: Implement useEffect in useTheme * feat: Implement useCallBack
- Loading branch information
1 parent
e7f8857
commit 2fae8fc
Showing
6 changed files
with
55 additions
and
25 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
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
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,33 @@ | ||
import {useCallback, useEffect, useState} from "react" | ||
|
||
type UseThemeReturnType = { | ||
toggleTheme: () => void | ||
isDarkMode: boolean | ||
} | ||
|
||
const useTheme = (): UseThemeReturnType => { | ||
const savedTheme = localStorage.getItem("darkMode") | ||
const isDarkMode = savedTheme ? JSON.parse(savedTheme) : false | ||
|
||
const [isDarkTheme, setIsDarkTheme] = useState(isDarkMode) | ||
|
||
useEffect(() => { | ||
const html = document.documentElement | ||
|
||
if (isDarkTheme) { | ||
html.classList.add("dark") | ||
} else { | ||
html.classList.remove("dark") | ||
} | ||
|
||
localStorage.setItem("darkMode", JSON.stringify(isDarkTheme)) | ||
}, [isDarkTheme]) | ||
|
||
const toggleTheme = useCallback(() => { | ||
setIsDarkTheme(!isDarkTheme) | ||
}, [isDarkTheme]) | ||
|
||
return {toggleTheme, isDarkMode} | ||
} | ||
|
||
export default useTheme |