-
Notifications
You must be signed in to change notification settings - Fork 36
fix: improved theme logic #504
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,49 +1,58 @@ | ||||||
| import { Appearance } from 'react-native'; | ||||||
| import { proxy, subscribe as sub } from 'valtio'; | ||||||
| import type { ThemeMode, ThemeVariables } from '@reown/appkit-common-react-native'; | ||||||
| import { derive } from 'derive-valtio'; | ||||||
|
|
||||||
| // -- Types --------------------------------------------- // | ||||||
| export interface ThemeControllerState { | ||||||
| themeMode?: ThemeMode; | ||||||
| defaultThemeMode?: ThemeMode; | ||||||
| systemThemeMode?: ThemeMode | null; | ||||||
| defaultThemeMode?: ThemeMode | null; | ||||||
| themeVariables: ThemeVariables; | ||||||
| } | ||||||
|
|
||||||
| // -- State --------------------------------------------- // | ||||||
| const state = proxy<ThemeControllerState>({ | ||||||
| themeMode: undefined, | ||||||
| const baseState = proxy<ThemeControllerState>({ | ||||||
| systemThemeMode: undefined, | ||||||
| defaultThemeMode: undefined, | ||||||
| themeVariables: {} | ||||||
| }); | ||||||
|
|
||||||
| // -- Derived State ------------------------------------- // | ||||||
| const derivedState = derive( | ||||||
| { | ||||||
| themeMode: (get): ThemeMode => { | ||||||
| const snap = get(baseState); | ||||||
|
|
||||||
| return snap.defaultThemeMode ?? snap.systemThemeMode ?? 'light'; | ||||||
| } | ||||||
| }, | ||||||
| { | ||||||
| proxy: baseState | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| // -- Controller ---------------------------------------- // | ||||||
| export const ThemeController = { | ||||||
| state, | ||||||
| state: derivedState, | ||||||
|
|
||||||
| subscribe(callback: (newState: ThemeControllerState) => void) { | ||||||
| return sub(state, () => callback(state)); | ||||||
| return sub(derivedState, () => callback(derivedState)); | ||||||
| }, | ||||||
|
|
||||||
| setThemeMode(themeMode?: ThemeControllerState['themeMode']) { | ||||||
| if (!themeMode) { | ||||||
| state.themeMode = (Appearance.getColorScheme() ?? 'light') as ThemeMode; | ||||||
| } else { | ||||||
| state.themeMode = themeMode; | ||||||
| } | ||||||
| setSystemThemeMode(systemThemeMode?: ThemeControllerState['systemThemeMode']) { | ||||||
| baseState.systemThemeMode = systemThemeMode ?? 'light'; | ||||||
|
||||||
| baseState.systemThemeMode = systemThemeMode ?? 'light'; | |
| baseState.systemThemeMode = systemThemeMode === undefined ? 'light' : systemThemeMode; |
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.
Type inconsistency:
systemThemeModeanddefaultThemeModeare typed asThemeMode | nullbut are initialized withundefined. This creates a mismatch between the type definition and the actual runtime values. Consider either:ThemeMode | null | undefinedorThemeMode | undefinednullinstead ofundefinedon lines 14-15This inconsistency could lead to type-safety issues when accessing these properties.