-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add Light & Dark theme support #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import React, {createContext, useContext, useMemo} from 'react'; | ||
| import {useSelector} from 'react-redux'; | ||
| import {useColorScheme} from 'react-native'; | ||
| import {lightColors, darkColors} from './colors'; | ||
|
|
||
| /** | ||
| * @typedef {'light' | 'dark' | 'system'} ThemeMode | ||
| */ | ||
|
|
||
| /** | ||
| * Theme context that provides the current theme colors and mode information. | ||
| */ | ||
| const ThemeContext = createContext({ | ||
| colors: darkColors, | ||
| isDark: true, | ||
| themeMode: 'system', | ||
| }); | ||
|
|
||
| /** | ||
| * ThemeProvider component that wraps the application and provides theme context. | ||
| * It reads the theme preference from Redux store and the system color scheme | ||
| * to determine the current theme. | ||
| * | ||
| * @param {Object} props - Component props | ||
| * @param {React.ReactNode} props.children - Child components | ||
| * @returns {JSX.Element} The provider component wrapping children | ||
| */ | ||
| export function ThemeProvider({children}) { | ||
| const systemColorScheme = useColorScheme(); | ||
| const themeMode = useSelector(state => state.data.themeMode) || 'system'; | ||
|
|
||
| const {colors, isDark} = useMemo(() => { | ||
| let isDarkTheme; | ||
|
|
||
| if (themeMode === 'system') { | ||
| isDarkTheme = systemColorScheme === 'dark'; | ||
| } else { | ||
| isDarkTheme = themeMode === 'dark'; | ||
| } | ||
|
|
||
| return { | ||
| colors: isDarkTheme ? darkColors : lightColors, | ||
| isDark: isDarkTheme, | ||
| }; | ||
| }, [themeMode, systemColorScheme]); | ||
|
|
||
| const value = useMemo( | ||
| () => ({ | ||
| colors, | ||
| isDark, | ||
| themeMode, | ||
| }), | ||
| [colors, isDark, themeMode], | ||
| ); | ||
|
|
||
| return ( | ||
| <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook to access the current theme context. | ||
| * Must be used within a ThemeProvider. | ||
| * | ||
| * @returns {{ colors: typeof darkColors, isDark: boolean, themeMode: string }} | ||
| * The current theme colors, dark mode flag, and theme mode setting | ||
| */ | ||
| export function useTheme() { | ||
| const context = useContext(ThemeContext); | ||
| if (!context) { | ||
|
||
| throw new Error('useTheme must be used within a ThemeProvider'); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| export {lightColors, darkColors}; | ||
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.
The theme toggle TouchableOpacity is missing accessibility properties. Consider adding
accessibilityRole="button",accessibilityLabel, andaccessibilityHintto improve screen reader support. For example: