From 8a4b525b40b0617500cb396e58c4bd97eda9ec74 Mon Sep 17 00:00:00 2001 From: Yosef Date: Tue, 17 Sep 2024 17:38:44 +0300 Subject: [PATCH 1/2] fix system thmem to revert if ignoresystemmode is not enabled --- features/theme/index.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/features/theme/index.tsx b/features/theme/index.tsx index da8b1a7..c3a1b65 100644 --- a/features/theme/index.tsx +++ b/features/theme/index.tsx @@ -89,7 +89,9 @@ export function createTheme({ if (next.ignoreSystemMode) { Colors.setScheme(scheme as SchemeType); } else { - Colors.setScheme('default'); + // Revert to system default if ignoreSystemMode is false + const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; + Colors.setScheme(systemScheme || 'default'); } return next; @@ -121,7 +123,12 @@ export function createTheme({ ); React.useEffect(() => { - Colors.setScheme(scheme as SchemeType); + if (ignoreSystemMode) { + Colors.setScheme(scheme as SchemeType); + } else { + const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; + Colors.setScheme(systemScheme || 'default'); + } setNavThemeState( createReactNavigationTheme( availableSchemes[scheme], @@ -145,7 +152,8 @@ export function createTheme({ if (next.ignoreSystemMode) { Colors.setScheme(prev.scheme || defaultScheme); } else { - Colors.setScheme('default'); + const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; + Colors.setScheme(systemScheme || 'default'); } return next; From 06b4128ab566febe74d22c116bc63510de7c83af Mon Sep 17 00:00:00 2001 From: Yosef Date: Wed, 18 Sep 2024 00:54:26 +0300 Subject: [PATCH 2/2] make the code reusable --- features/theme/index.tsx | 42 ++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/features/theme/index.tsx b/features/theme/index.tsx index c3a1b65..7c1a4b0 100644 --- a/features/theme/index.tsx +++ b/features/theme/index.tsx @@ -36,6 +36,16 @@ export interface CreateThemeOptions { componentDefaults?: Record>; } +// Utility function to handle color scheme setting logic +function setColorScheme(scheme: 'light' | 'dark', ignoreSystemMode: boolean) { + if (ignoreSystemMode) { + Colors.setScheme(scheme as SchemeType); + } else { + const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; + Colors.setScheme(systemScheme || 'default'); + } +} + export function createTheme({ ignoreSystemMode, supportDarkMode, @@ -85,15 +95,7 @@ export function createTheme({ return (scheme: 'light' | 'dark') => { setTheme((prev) => { const next = { ...prev, scheme }; - - if (next.ignoreSystemMode) { - Colors.setScheme(scheme as SchemeType); - } else { - // Revert to system default if ignoreSystemMode is false - const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; - Colors.setScheme(systemScheme || 'default'); - } - + setColorScheme(scheme, next.ignoreSystemMode); return next; }); }; @@ -104,10 +106,8 @@ export function createTheme({ const [state, setState] = React.useState<'light' | 'dark'>(scheme); React.useEffect(() => { - const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; - const initialScheme = ignoreSystemMode ? scheme : systemScheme ?? scheme; - Colors.setScheme(initialScheme as SchemeType); - setState(initialScheme); + setColorScheme(scheme, ignoreSystemMode); + setState(scheme); }, [scheme, ignoreSystemMode]); return state; @@ -123,12 +123,7 @@ export function createTheme({ ); React.useEffect(() => { - if (ignoreSystemMode) { - Colors.setScheme(scheme as SchemeType); - } else { - const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; - Colors.setScheme(systemScheme || 'default'); - } + setColorScheme(scheme, ignoreSystemMode); setNavThemeState( createReactNavigationTheme( availableSchemes[scheme], @@ -148,14 +143,7 @@ export function createTheme({ const dispatch = () => { setState((prev) => { const next = { ...prev, ignoreSystemMode: !ignoreSystemMode }; - - if (next.ignoreSystemMode) { - Colors.setScheme(prev.scheme || defaultScheme); - } else { - const systemScheme = Appearance.getColorScheme() as 'light' | 'dark' | null; - Colors.setScheme(systemScheme || 'default'); - } - + setColorScheme(prev.scheme || defaultScheme, next.ignoreSystemMode); return next; }); };