-
Notifications
You must be signed in to change notification settings - Fork 451
feat: Add dark mode support with system theme detection #983
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
Open
dilutedev
wants to merge
1
commit into
chatwoot:develop
Choose a base branch
from
dilutedev:feature/dark-mode
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
| import React from 'react'; | ||
| import { Pressable, Text, Animated } from 'react-native'; | ||
|
|
||
| import { TickIcon } from '@/svg-icons'; | ||
| import { tailwind } from '@/theme'; | ||
| import { Theme } from '@/types/common/Theme'; | ||
| import { useHaptic } from '@/utils'; | ||
| import { Icon } from '@/components-next/common/icon'; | ||
| import i18n from '@/i18n'; | ||
|
|
||
| type ThemeListItemType = { | ||
| value: Theme; | ||
| label: string; | ||
| description: string; | ||
| }; | ||
|
|
||
| const THEME_OPTIONS: ThemeListItemType[] = [ | ||
| { | ||
| value: 'light', | ||
| label: i18n.t('SETTINGS.THEME.LIGHT'), | ||
| description: i18n.t('SETTINGS.THEME.LIGHT_DESCRIPTION'), | ||
| }, | ||
| { | ||
| value: 'dark', | ||
| label: i18n.t('SETTINGS.THEME.DARK'), | ||
| description: i18n.t('SETTINGS.THEME.DARK_DESCRIPTION'), | ||
| }, | ||
| { | ||
| value: 'system', | ||
| label: i18n.t('SETTINGS.THEME.SYSTEM'), | ||
| description: i18n.t('SETTINGS.THEME.SYSTEM_DESCRIPTION'), | ||
| }, | ||
| ]; | ||
|
|
||
| type ThemeCellProps = { | ||
| item: ThemeListItemType; | ||
| index: number; | ||
| currentTheme: Theme; | ||
| changeTheme: (theme: Theme) => void; | ||
| }; | ||
|
|
||
| const ThemeCell = ({ item, index, currentTheme, changeTheme }: ThemeCellProps) => { | ||
| const hapticSelection = useHaptic(); | ||
|
|
||
| const handlePress = () => { | ||
| hapticSelection?.(); | ||
| changeTheme(item.value); | ||
| }; | ||
|
|
||
| const isLastItem = index === THEME_OPTIONS.length - 1; | ||
| const isSelected = currentTheme === item.value; | ||
|
|
||
| return ( | ||
| <Pressable onPress={handlePress}> | ||
| <Animated.View | ||
| style={tailwind.style( | ||
| 'flex flex-row items-center justify-between py-3 px-4', | ||
| !isLastItem && 'border-b-[1px] border-blackA-A3', | ||
| )}> | ||
| <Animated.View style={tailwind.style('flex-1 flex-col')}> | ||
| <Text | ||
| style={tailwind.style( | ||
| 'text-base text-gray-950 font-inter-420-20 leading-[21px] tracking-[0.16px]', | ||
| )}> | ||
| {item.label} | ||
| </Text> | ||
| <Text style={tailwind.style('text-sm text-gray-700 font-inter-normal-20 mt-0.5')}> | ||
| {item.description} | ||
| </Text> | ||
| </Animated.View> | ||
| {isSelected && ( | ||
| <Animated.View style={tailwind.style('ml-3')}> | ||
| <Icon icon={<TickIcon />} size={20} /> | ||
| </Animated.View> | ||
| )} | ||
| </Animated.View> | ||
| </Pressable> | ||
| ); | ||
| }; | ||
|
|
||
| export const ThemeList = ({ | ||
| currentTheme, | ||
| changeTheme, | ||
| }: { | ||
| currentTheme: Theme; | ||
| changeTheme: (theme: Theme) => void; | ||
| }) => { | ||
| return ( | ||
| <Animated.View style={tailwind.style('flex flex-col')}> | ||
| {THEME_OPTIONS.map((item, index) => ( | ||
| <ThemeCell | ||
| key={item.value} | ||
| item={item} | ||
| index={index} | ||
| currentTheme={currentTheme} | ||
| changeTheme={changeTheme} | ||
| /> | ||
| ))} | ||
| </Animated.View> | ||
| ); | ||
| }; |
This file contains hidden or 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,3 +1,4 @@ | ||
| export * from './AvailabilityStatusList'; | ||
| export * from './NotificationPreferences'; | ||
| export * from './SwitchAccount'; | ||
| export * from './ThemeList'; |
This file contains hidden or 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 hidden or 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,60 @@ | ||
| import React, { createContext, useContext, useEffect, ReactNode } from 'react'; | ||
| import { Appearance, ColorSchemeName } from 'react-native'; | ||
| import { useAppDispatch, useAppSelector } from '@/hooks'; | ||
| import { setTheme } from '@/store/settings/settingsSlice'; | ||
| import { selectTheme } from '@/store/settings/settingsSelectors'; | ||
| import { tailwind } from '@/theme'; | ||
| import { Theme } from '@/types/common/Theme'; | ||
|
|
||
| type ThemeContextType = { | ||
| theme: Theme; | ||
| colorScheme: ColorSchemeName; | ||
| isDark: boolean; | ||
| changeTheme: (newTheme: Theme) => void; | ||
| }; | ||
|
|
||
| const ThemeContext = createContext<ThemeContextType | undefined>(undefined); | ||
|
|
||
| type ThemeProviderProps = { | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export const ThemeProvider = ({ children }: ThemeProviderProps) => { | ||
| const dispatch = useAppDispatch(); | ||
| const theme = useAppSelector(selectTheme) || 'system'; | ||
| const systemColorScheme = Appearance.getColorScheme(); | ||
|
|
||
| const colorScheme: ColorSchemeName = | ||
| theme === 'system' ? systemColorScheme : theme === 'dark' ? 'dark' : 'light'; | ||
|
|
||
| const isDark = colorScheme === 'dark'; | ||
|
|
||
| useEffect(() => { | ||
| if (theme === 'system') { | ||
| const subscription = Appearance.addChangeListener(({ colorScheme: newColorScheme }) => { | ||
| tailwind.setColorScheme(newColorScheme); | ||
| }); | ||
| return () => subscription.remove(); | ||
| } else { | ||
| tailwind.setColorScheme(colorScheme); | ||
| } | ||
| }, [theme, colorScheme]); | ||
|
|
||
| const changeTheme = (newTheme: Theme) => { | ||
| dispatch(setTheme(newTheme)); | ||
| }; | ||
|
|
||
| return ( | ||
| <ThemeContext.Provider value={{ theme, colorScheme, isDark, changeTheme }}> | ||
| {children} | ||
| </ThemeContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| export const useTheme = () => { | ||
| const context = useContext(ThemeContext); | ||
| if (context === undefined) { | ||
| throw new Error('useTheme must be used within a ThemeProvider'); | ||
| } | ||
| return context; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When the user selects
system(the default) the ThemeProvider branch at lines 32-36 only registers an Appearance listener and never callstailwind.setColorSchemewith the current system scheme, nor does it update context when the system theme changes. With dark mode enabled at the OS level, the app stays on the prior light scheme until a future appearance change triggers a re-render, so the “Follow system” option and default boot on dark devices show incorrect colors.Useful? React with 👍 / 👎.