Skip to content

Commit 3eb998c

Browse files
committed
fix: Enhance dynamic theming support with Material You integration and improve theme handling
1 parent 110ee2f commit 3eb998c

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

frontend/App.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { NavigationContainer } from '@react-navigation/native';
2-
import React from 'react';
2+
import { StatusBar } from 'expo-status-bar';
33
import { PaperProvider } from 'react-native-paper';
44
import { AuthProvider } from './context/AuthContext';
55
import AppNavigator from './navigation/AppNavigator';
66
import { useAppTheme } from './styles/theme';
77

88
const Main = () => {
99
const theme = useAppTheme();
10+
1011
return (
1112
<PaperProvider theme={theme}>
1213
<NavigationContainer theme={theme}>
14+
<StatusBar style={theme.dark ? 'light' : 'dark'} />
1315
<AppNavigator />
1416
</NavigationContainer>
1517
</PaperProvider>
16-
)
17-
}
18+
);
19+
};
1820

1921
export default function App() {
2022
return (

frontend/styles/theme.js

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,78 @@
1-
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
1+
import {
2+
DarkTheme as NavigationDarkTheme,
3+
DefaultTheme as NavigationDefaultTheme,
4+
} from '@react-navigation/native';
5+
import merge from 'deepmerge';
26
import React, { useMemo } from 'react';
3-
import { useColorScheme } from 'react-native';
7+
import { Platform, useColorScheme } from 'react-native';
48
import {
59
MD3DarkTheme,
610
MD3LightTheme,
711
} from 'react-native-paper';
812

13+
// Only import Material You theming on Android platform
14+
let useMaterial3Theme;
15+
if (Platform.OS === 'android') {
16+
try {
17+
({ useMaterial3Theme } = require('@pchmn/expo-material3-theme'));
18+
} catch (error) {
19+
console.warn('Material You theming not available:', error);
20+
useMaterial3Theme = () => ({ theme: null });
21+
}
22+
} else {
23+
// Mock the hook for web and other platforms
24+
useMaterial3Theme = () => ({ theme: null });
25+
}
26+
927
export const PreferencesContext = React.createContext(null);
1028

29+
// Create a manual theme adapter for React Navigation to avoid import issues
30+
const createAdaptedTheme = (paperTheme, navigationTheme) => ({
31+
...navigationTheme,
32+
colors: {
33+
...navigationTheme.colors,
34+
primary: paperTheme.colors.primary,
35+
background: paperTheme.colors.background,
36+
card: paperTheme.colors.surface,
37+
text: paperTheme.colors.onSurface,
38+
border: paperTheme.colors.outline,
39+
notification: paperTheme.colors.error,
40+
},
41+
});
42+
43+
// Create adapted navigation themes
44+
const LightNavigationTheme = createAdaptedTheme(MD3LightTheme, NavigationDefaultTheme);
45+
const DarkNavigationTheme = createAdaptedTheme(MD3DarkTheme, NavigationDarkTheme);
46+
47+
// Create combined themes by merging Paper and Navigation themes
48+
const CombinedDefaultTheme = merge(MD3LightTheme, LightNavigationTheme);
49+
const CombinedDarkTheme = merge(MD3DarkTheme, DarkNavigationTheme);
50+
1151
export const useAppTheme = () => {
12-
const colorScheme = useColorScheme();
13-
const { theme } = useMaterial3Theme();
52+
const colorScheme = useColorScheme();
53+
const isDark = colorScheme === 'dark';
54+
55+
// Only use Material You dynamic theming on supported platforms (Android 12+)
56+
const materialYouTheme = useMaterial3Theme();
1457

15-
const paperTheme = useMemo(() => {
16-
const baseTheme = colorScheme === 'dark' ? MD3DarkTheme : MD3LightTheme;
58+
const paperTheme = useMemo(() => {
59+
const baseTheme = isDark ? CombinedDarkTheme : CombinedDefaultTheme;
60+
61+
// Only apply Material You colors if available and we're on a supported platform
62+
if (materialYouTheme.theme && Platform.OS === 'android') {
63+
const dynamicColors = isDark ? materialYouTheme.theme.dark : materialYouTheme.theme.light;
1764
return {
1865
...baseTheme,
1966
colors: {
2067
...baseTheme.colors,
21-
...theme.light,
68+
...dynamicColors,
2269
},
2370
};
24-
}, [colorScheme, theme]);
71+
}
72+
73+
// Return the base combined theme for web and other platforms
74+
return baseTheme;
75+
}, [colorScheme, materialYouTheme.theme, isDark]);
2576

26-
return paperTheme;
27-
};
77+
return paperTheme;
78+
};

0 commit comments

Comments
 (0)