-
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.
Option to make theme follow OS setting (#11)
* feat: Hook to detect the current OS theme * fix: Only run media query on the client side * feat: Updated Settings state and reducer to store the user preferrences for theming * feat: Updated Settings modal with options to change the theme or make it follow the OS - Refactored each individual setting into its own component file - Removed theme icon from header
- Loading branch information
1 parent
86117a0
commit 235de3d
Showing
9 changed files
with
130 additions
and
35 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
26 changes: 26 additions & 0 deletions
26
src/features/Settings/Components/DisplayCurrencySetting.tsx
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,26 @@ | ||
import React from 'react'; | ||
import SelectCurrency from 'components/SelectCurrency'; | ||
import { FC, useCallback, useContext } from 'react'; | ||
import SettingsContext from '../context'; | ||
|
||
const DisplayCurrencySetting: FC = () => { | ||
const { | ||
values: { preferredDisplayCurrency }, | ||
dispatch, | ||
} = useContext(SettingsContext); | ||
|
||
const onChange = useCallback( | ||
(currency: string) => dispatch({ type: 'SET DISPLAY CURRENCY', currency }), | ||
[dispatch] | ||
); | ||
|
||
return ( | ||
<SelectCurrency | ||
label="Preferred display currency" | ||
defaultCurrency={preferredDisplayCurrency} | ||
onChange={onChange} | ||
/> | ||
); | ||
}; | ||
|
||
export default DisplayCurrencySetting; |
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 { DarkModeToggle, Toggle } from '@oliverflecke/components-react'; | ||
import React, { useContext } from 'react'; | ||
import SettingsContext from '../context'; | ||
|
||
const ThemeSetting = () => { | ||
const { values, dispatch } = useContext(SettingsContext); | ||
|
||
return ( | ||
<> | ||
<span>Theme follow OS</span> | ||
<div className="flex flex-row justify-end"> | ||
<Toggle | ||
checked={values.themeFollowsOS} | ||
onChange={e => | ||
dispatch({ type: 'SET THEME TO FOLLOW OS', shouldFollowOS: e.target.checked }) | ||
} | ||
/> | ||
</div> | ||
|
||
{!values.themeFollowsOS && ( | ||
<> | ||
<span>Theme</span> | ||
<div className="flex flex-row justify-end"> | ||
<DarkModeToggle | ||
onToggle={() => | ||
dispatch({ type: 'SET THEME', preferresDarkMode: !values.preferresDarkMode }) | ||
} | ||
darkMode={values.preferresDarkMode} | ||
/> | ||
</div> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default ThemeSetting; |
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,23 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useThemeDetector = () => { | ||
const [isDarkTheme, setIsDarkTheme] = useState(getCurrentThemeQuery()?.matches ?? false); | ||
const listener = (e: MediaQueryListEvent) => setIsDarkTheme(e.matches); | ||
|
||
useEffect(() => { | ||
const darkThemeMediaQuery = getCurrentThemeQuery(); | ||
darkThemeMediaQuery?.addEventListener('change', listener); | ||
|
||
return () => darkThemeMediaQuery?.removeEventListener('change', listener); | ||
}, []); | ||
|
||
return isDarkTheme; | ||
}; | ||
|
||
export default useThemeDetector; | ||
|
||
function getCurrentThemeQuery(): MediaQueryList | null { | ||
if (typeof window === 'undefined') return null; | ||
|
||
return window.matchMedia('(prefers-color-scheme: dark)'); | ||
} |