-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement listener to detect dark mode in the browser
- Loading branch information
1 parent
d0762ea
commit 5fce2aa
Showing
14 changed files
with
111 additions
and
100 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 |
---|---|---|
@@ -1,20 +1,17 @@ | ||
import { LocaleSwitcher } from 'components/LocaleSwitcher'; | ||
import { ToggleTheme } from 'components/ToggleTheme'; | ||
import { | ||
useStylesDispatch, | ||
useStylesState, | ||
} from 'contexts/styles/StylesContext'; | ||
import { useThemeDispatch, useThemeState } from 'contexts/theme/ThemeContext'; | ||
|
||
import * as S from 'styles/components/Layout/Header'; | ||
|
||
export function Header() { | ||
const { theme } = useStylesState(); | ||
const { switchTheme } = useStylesDispatch(); | ||
const { mode } = useThemeState(); | ||
const { onSelectMode } = useThemeDispatch(); | ||
|
||
return ( | ||
<S.Wrapper> | ||
<LocaleSwitcher /> | ||
<ToggleTheme toggleTheme={() => switchTheme(theme)} /> | ||
<ToggleTheme toggleTheme={() => onSelectMode(mode)} /> | ||
</S.Wrapper> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import Switch from 'react-switch'; | ||
import { shade } from 'polished'; | ||
|
||
import { colors } from 'styles/theme'; | ||
import { useStylesState } from 'contexts/styles/StylesContext'; | ||
|
||
import { theme } from 'styles/theme'; | ||
import { useThemeState } from 'contexts/theme/ThemeContext'; | ||
import { ToggleThemeProps } from './types'; | ||
|
||
export function ToggleTheme({ toggleTheme }: ToggleThemeProps) { | ||
const { theme } = useStylesState(); | ||
const { mode } = useThemeState(); | ||
|
||
return ( | ||
<Switch | ||
onChange={toggleTheme} | ||
checked={theme === 'dark'} | ||
checked={mode === 'dark'} | ||
checkedIcon={false} | ||
uncheckedIcon={false} | ||
height={10} | ||
width={40} | ||
handleDiameter={20} | ||
offColor={colors.light.alto} | ||
onColor={colors.light.alto} | ||
offColor={theme.light.alto} | ||
onColor={theme.light.alto} | ||
/> | ||
); | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 { useContext, createContext } from 'react'; | ||
import { ThemeDispatchContextData, ThemeStateContextData } from './types'; | ||
|
||
export const ThemeStateContext = createContext< | ||
ThemeStateContextData | undefined | ||
>(undefined); | ||
|
||
export const ThemeDispatchContext = createContext< | ||
ThemeDispatchContextData | undefined | ||
>(undefined); | ||
|
||
export const ThemeStateProvider = ThemeStateContext.Provider; | ||
export const ThemeDispatchProvider = ThemeDispatchContext.Provider; | ||
|
||
export function useThemeState() { | ||
const context = useContext(ThemeStateContext); | ||
|
||
if (!context) { | ||
throw new Error('useThemeState must be used within a ThemeProvider'); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
export function useThemeDispatch() { | ||
const context = useContext(ThemeDispatchContext); | ||
|
||
if (!context) { | ||
throw new Error('useThemeDispatch must be used within ThemeProvider'); | ||
} | ||
|
||
return context; | ||
} |
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,40 @@ | ||
import { PropsWithChildren, useCallback, useEffect, useMemo } from 'react'; | ||
|
||
import { THEME_STORAGE_KEY } from 'constants/localStorage'; | ||
import { usePersistedState } from 'hooks/usePersistedState'; | ||
import { ThemeDispatchProvider, ThemeStateProvider } from './ThemeContext'; | ||
import { Theme } from './types'; | ||
|
||
export function ThemeContainer({ children }: PropsWithChildren<unknown>) { | ||
const [mode, setMode] = usePersistedState<Theme>(THEME_STORAGE_KEY, 'light'); | ||
|
||
const onSelectMode = useCallback((mode: Theme) => { | ||
setMode(mode === 'light' ? 'dark' : 'light'); | ||
}, []); | ||
|
||
useEffect(() => { | ||
window | ||
.matchMedia('(prefers-color-scheme: light)') | ||
.addEventListener('change', e => | ||
onSelectMode(e.matches ? 'dark' : 'light'), | ||
); | ||
|
||
onSelectMode( | ||
window.matchMedia('(prefers-color-scheme: light)').matches | ||
? 'dark' | ||
: 'light', | ||
); | ||
}, []); | ||
|
||
const themeState = useMemo(() => ({ mode }), [mode]); | ||
|
||
const themeDispatch = useMemo(() => ({ onSelectMode }), [onSelectMode]); | ||
|
||
return ( | ||
<ThemeStateProvider value={themeState}> | ||
<ThemeDispatchProvider value={themeDispatch}> | ||
{children} | ||
</ThemeDispatchProvider> | ||
</ThemeStateProvider> | ||
); | ||
} |
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,11 @@ | ||
import { theme } from 'styles/theme'; | ||
|
||
export type Theme = keyof typeof theme; | ||
|
||
export type ThemeStateContextData = { | ||
mode: Theme; | ||
}; | ||
|
||
export type ThemeDispatchContextData = { | ||
onSelectMode: (mode: Theme) => void; | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { light, dark } from './colors'; | ||
|
||
export const colors = { | ||
export const theme = { | ||
light, | ||
dark, | ||
}; |
5fce2aa
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: