Skip to content

Commit 5714a28

Browse files
refactor: replace derive-valtio with native getter for themeMode
Use a native JavaScript getter instead of derive-valtio for computing themeMode. This ensures synchronous updates and removes the external dependency. The getter pattern is the recommended valtio approach for computed properties that reference sibling properties. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 389a336 commit 5714a28

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed
Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,48 @@
11
import { proxy, subscribe as sub } from 'valtio';
22
import type { ThemeMode, ThemeVariables } from '@reown/appkit-common-react-native';
3-
import { derive } from 'derive-valtio';
43

54
// -- Types --------------------------------------------- //
65
export interface ThemeControllerState {
6+
themeMode: ThemeMode;
77
systemThemeMode?: ThemeMode;
88
defaultThemeMode?: ThemeMode;
99
themeVariables: ThemeVariables;
1010
}
1111

1212
// -- State --------------------------------------------- //
13-
const baseState = proxy<ThemeControllerState>({
14-
systemThemeMode: undefined,
15-
defaultThemeMode: undefined,
16-
themeVariables: {}
17-
});
18-
19-
// -- Derived State ------------------------------------- //
20-
const derivedState = derive(
21-
{
22-
themeMode: (get): ThemeMode => {
23-
const snap = get(baseState);
24-
25-
return snap.defaultThemeMode ?? snap.systemThemeMode ?? 'light';
26-
}
27-
},
28-
{
29-
proxy: baseState
13+
const state = proxy({
14+
systemThemeMode: undefined as ThemeMode | undefined,
15+
defaultThemeMode: undefined as ThemeMode | undefined,
16+
themeVariables: {} as ThemeVariables,
17+
get themeMode(): ThemeMode {
18+
// eslint-disable-next-line valtio/avoid-this-in-proxy -- using `this` for sibling property access in getters is the recommended valtio pattern for computed properties
19+
return this.defaultThemeMode ?? this.systemThemeMode ?? 'light';
3020
}
31-
);
21+
});
3222

3323
// -- Controller ---------------------------------------- //
3424
export const ThemeController = {
35-
state: derivedState,
25+
state: state as ThemeControllerState,
3626

3727
subscribe(callback: (newState: ThemeControllerState) => void) {
38-
return sub(derivedState, () => callback(derivedState));
28+
return sub(state, () => callback(state));
3929
},
4030

4131
setSystemThemeMode(systemThemeMode?: ThemeControllerState['systemThemeMode']) {
42-
baseState.systemThemeMode = systemThemeMode ?? 'light';
32+
state.systemThemeMode = systemThemeMode ?? 'light';
4333
},
4434

4535
setDefaultThemeMode(themeMode?: ThemeControllerState['defaultThemeMode']) {
46-
baseState.defaultThemeMode = themeMode;
36+
state.defaultThemeMode = themeMode;
4737
},
4838

4939
setThemeVariables(themeVariables?: ThemeControllerState['themeVariables']) {
5040
if (!themeVariables) {
51-
baseState.themeVariables = {};
41+
state.themeVariables = {};
5242

5343
return;
5444
}
5545

56-
baseState.themeVariables = { ...baseState.themeVariables, ...themeVariables };
46+
state.themeVariables = { ...state.themeVariables, ...themeVariables };
5747
}
5848
};

0 commit comments

Comments
 (0)