|
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'; |
2 | 6 | import React, { useMemo } from 'react'; |
3 | | -import { useColorScheme } from 'react-native'; |
| 7 | +import { Platform, useColorScheme } from 'react-native'; |
4 | 8 | import { |
5 | 9 | MD3DarkTheme, |
6 | 10 | MD3LightTheme, |
7 | 11 | } from 'react-native-paper'; |
8 | 12 |
|
| 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 | + |
9 | 27 | export const PreferencesContext = React.createContext(null); |
10 | 28 |
|
| 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 | + |
11 | 51 | 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(); |
14 | 57 |
|
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; |
17 | 64 | return { |
18 | 65 | ...baseTheme, |
19 | 66 | colors: { |
20 | 67 | ...baseTheme.colors, |
21 | | - ...theme.light, |
| 68 | + ...dynamicColors, |
22 | 69 | }, |
23 | 70 | }; |
24 | | - }, [colorScheme, theme]); |
| 71 | + } |
| 72 | + |
| 73 | + // Return the base combined theme for web and other platforms |
| 74 | + return baseTheme; |
| 75 | + }, [colorScheme, materialYouTheme.theme, isDark]); |
25 | 76 |
|
26 | | - return paperTheme; |
27 | | - }; |
| 77 | + return paperTheme; |
| 78 | +}; |
0 commit comments