-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #569 from chatch/toggle-color-themes
toggle color themes
- Loading branch information
Showing
17 changed files
with
494 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Theme, useTheme } from '~/context/theme.provider' | ||
import lightIcon from 'public/img/sun.svg' | ||
import darkIcon from 'public/img/moon.svg' | ||
|
||
const ThemeSwitcher = () => { | ||
const [theme, setTheme] = useTheme() | ||
const toggleTheme = () => { | ||
const newTheme = theme === Theme.Light ? Theme.Dark : Theme.Light | ||
localStorage.setItem('theme', newTheme) | ||
setTheme(newTheme) | ||
} | ||
|
||
return ( | ||
<div onClick={toggleTheme} className="theme-selector"> | ||
{theme === Theme.Light ? <LightIcon /> : <DarkIcon />} | ||
</div> | ||
) | ||
} | ||
|
||
const DarkIcon = () => { | ||
return <img src={darkIcon} alt="moon" width={28} height={28} /> | ||
} | ||
|
||
const LightIcon = () => { | ||
return <img src={lightIcon} alt="sun" width={28} height={28} /> | ||
} | ||
|
||
export default ThemeSwitcher |
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,37 @@ | ||
import type { ReactNode, Dispatch, SetStateAction } from 'react' | ||
import { createContext, useContext, useState, useEffect } from 'react' | ||
|
||
enum Theme { | ||
Dark = 'dark', | ||
Light = 'light', | ||
} | ||
|
||
type ThemeContextType = [ | ||
Theme | undefined, | ||
Dispatch<SetStateAction<Theme | undefined>>, | ||
] | ||
|
||
const ThemeContext = createContext<ThemeContextType>([undefined, () => {}]) | ||
|
||
function ThemeProvider({ children }: { children: ReactNode }) { | ||
const [theme, setTheme] = useState<Theme | undefined>(Theme.Light) | ||
|
||
useEffect(() => { | ||
const savedTheme = localStorage.getItem('theme') | ||
if (savedTheme) { | ||
setTheme(savedTheme as Theme) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<ThemeContext.Provider value={[theme, setTheme]}> | ||
{children} | ||
</ThemeContext.Provider> | ||
) | ||
} | ||
|
||
function useTheme(): ThemeContextType { | ||
return useContext(ThemeContext) | ||
} | ||
|
||
export { Theme, ThemeProvider, useTheme } |
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
Oops, something went wrong.