Skip to content
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

fix 🐛 issue where the theme should revert to the system default when ignoreSystemMode is disabled. #7

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions features/theme/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export interface CreateThemeOptions {
componentDefaults?: Record<ModifiableComponent, Record<string, any>>;
}

// 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,
Expand Down Expand Up @@ -85,13 +95,7 @@ export function createTheme({
return (scheme: 'light' | 'dark') => {
setTheme((prev) => {
const next = { ...prev, scheme };

if (next.ignoreSystemMode) {
Colors.setScheme(scheme as SchemeType);
} else {
Colors.setScheme('default');
}

setColorScheme(scheme, next.ignoreSystemMode);
return next;
});
};
Expand All @@ -102,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;
Expand All @@ -121,7 +123,7 @@ export function createTheme({
);

React.useEffect(() => {
Colors.setScheme(scheme as SchemeType);
setColorScheme(scheme, ignoreSystemMode);
setNavThemeState(
createReactNavigationTheme(
availableSchemes[scheme],
Expand All @@ -141,13 +143,7 @@ export function createTheme({
const dispatch = () => {
setState((prev) => {
const next = { ...prev, ignoreSystemMode: !ignoreSystemMode };

if (next.ignoreSystemMode) {
Colors.setScheme(prev.scheme || defaultScheme);
} else {
Colors.setScheme('default');
}

setColorScheme(prev.scheme || defaultScheme, next.ignoreSystemMode);
return next;
});
};
Expand Down