Skip to content

Commit 699bb06

Browse files
authored
feat: adjust adaptNavigationTheme to the latest navigation theme changes (#4690)
1 parent d0ad27c commit 699bb06

File tree

6 files changed

+261
-169
lines changed

6 files changed

+261
-169
lines changed

docs/docs/guides/02-theming.mdx

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Theming
33
---
44

5-
import DynamicColorTheme from '@site/src/components/DynamicColorTheme.tsx'
5+
import DynamicColorTheme from '@site/src/components/DynamicColorTheme.tsx';
66

77
# Theming
88

@@ -188,18 +188,18 @@ export default function Main() {
188188

189189
## Creating dynamic theme colors
190190

191-
Dynamic Color Themes allows for generating two color schemes lightScheme and darkScheme, based on the provided source color.
191+
Dynamic Color Themes allows for generating two color schemes lightScheme and darkScheme, based on the provided source color.
192192
Created schemes are following the Material Design 3 color system and covering colors structure from the Paper theme. User may generate these schemes using the following tool:
193193

194194
<DynamicColorTheme />
195195

196196
<br />
197197

198-
Passed source color into the util is translated into tones to automatically provide the range of tones that map to color roles.
198+
Passed source color into the util is translated into tones to automatically provide the range of tones that map to color roles.
199199

200200
![customColors](../../static/screenshots/custom-colors.png)
201201

202-
*Source: [Material You Color System](https://m3.material.io/styles/color/the-color-system/custom-colors)*
202+
_Source: [Material You Color System](https://m3.material.io/styles/color/the-color-system/custom-colors)_
203203

204204
### Using schemes
205205

@@ -240,11 +240,7 @@ To get started, follow the [installation instructions](https://github.com/pchmn/
240240
```tsx
241241
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
242242
import { useColorScheme } from 'react-native';
243-
import {
244-
MD3DarkTheme,
245-
MD3LightTheme,
246-
PaperProvider,
247-
} from 'react-native-paper';
243+
import { MD3DarkTheme, MD3LightTheme, PaperProvider } from 'react-native-paper';
248244
import App from './src/App';
249245

250246
export default function Main() {
@@ -269,29 +265,65 @@ export default function Main() {
269265
The `adaptNavigationTheme` function takes an existing React Navigation theme and returns a React Navigation theme using the colors from Material Design 3. This theme can be passed to `NavigationContainer` so that React Navigation's UI elements have the same color scheme as Paper.
270266

271267
```ts
272-
adaptNavigationTheme(themes)
268+
adaptNavigationTheme(themes);
269+
```
270+
271+
:::info
272+
For users of `react-navigation` version `7.0.0` and above, `adaptNavigationTheme` overrides the **fonts** from the navigation theme as follows:
273+
274+
```ts
275+
fonts: {
276+
regular: {
277+
fontFamily: materialTheme.fonts.bodyMedium.fontFamily,
278+
fontWeight: materialTheme.fonts.bodyMedium.fontWeight,
279+
letterSpacing: materialTheme.fonts.bodyMedium.letterSpacing,
280+
},
281+
medium: {
282+
fontFamily: materialTheme.fonts.titleMedium.fontFamily,
283+
fontWeight: materialTheme.fonts.titleMedium.fontWeight,
284+
letterSpacing: materialTheme.fonts.titleMedium.letterSpacing,
285+
},
286+
bold: {
287+
fontFamily: materialTheme.fonts.headlineSmall.fontFamily,
288+
fontWeight: materialTheme.fonts.headlineSmall.fontWeight,
289+
letterSpacing: materialTheme.fonts.headlineSmall.letterSpacing,
290+
},
291+
heavy: {
292+
fontFamily: materialTheme.fonts.headlineLarge.fontFamily,
293+
fontWeight: materialTheme.fonts.headlineLarge.fontWeight,
294+
letterSpacing: materialTheme.fonts.headlineLarge.letterSpacing,
295+
},
296+
}
273297
```
274298

299+
:::
300+
275301
<b>Parameters:</b>
276302

277-
| NAME | TYPE |
278-
| ----------- | ----------- |
279-
| themes | object |
303+
| NAME | TYPE |
304+
| ------ | ------ |
305+
| themes | object |
280306

281307
Valid `themes` keys are:
282308

283-
* `reactNavigationLight` () - React Navigation compliant light theme.
284-
* `reactNavigationDark` () - React Navigation compliant dark theme.
285-
* `materialLight` () - React Native Paper compliant light theme.
286-
* `materialDark` () - React Native Paper compliant dark theme.
309+
- `reactNavigationLight` () - React Navigation compliant light theme.
310+
- `reactNavigationDark` () - React Navigation compliant dark theme.
311+
- `materialLight` () - React Native Paper compliant light theme.
312+
- `materialDark` () - React Native Paper compliant dark theme.
287313

288314
```ts
289315
// App.tsx
290316
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';
291317
import { createStackNavigator } from '@react-navigation/stack';
292-
import { PaperProvider, MD3LightTheme, adaptNavigationTheme } from 'react-native-paper';
318+
import {
319+
PaperProvider,
320+
MD3LightTheme,
321+
adaptNavigationTheme,
322+
} from 'react-native-paper';
293323
const Stack = createStackNavigator();
294-
const { LightTheme } = adaptNavigationTheme({ reactNavigationLight: DefaultTheme });
324+
const { LightTheme } = adaptNavigationTheme({
325+
reactNavigationLight: DefaultTheme,
326+
});
295327
export default function App() {
296328
return (
297329
<PaperProvider theme={MD3LightTheme}>
@@ -312,12 +344,19 @@ By default, TypeScript works well whenever you change the value of the built-in
312344

313345
There are two supported ways of overriding the theme:
314346

315-
1. <b>Simple built-in theme overrides</b> - when you only customize the values and the whole theme schema remains the same
316-
2. <b>Advanced theme overrides</b> - when you <i>add new properties</i> or <i>change the built-in schema shape</i>
347+
1. <b>Simple built-in theme overrides</b> - when you only customize the values and
348+
the whole theme schema remains the same
349+
2. <b>Advanced theme overrides</b> - when you <i>add new properties</i> or <i>
350+
change the built-in schema shape
351+
</i>
317352

318353
:::caution
319-
TypeScript support for `withTheme` is currently limited to <b>Material You (MD3)</b> theme only.
320-
<i>We are planning to provide a better support of handling custom theme overrides in future releases.</i>
354+
TypeScript support for `withTheme` is currently limited to <b>Material You (MD3)</b> theme only.
355+
356+
<i>
357+
We are planning to provide a better support of handling custom theme overrides
358+
in future releases.
359+
</i>
321360
:::
322361

323362
### Simple built-in theme overrides
@@ -387,11 +426,7 @@ export default function Main() {
387426

388427
```ts
389428
import * as React from 'react';
390-
import {
391-
MD3LightTheme,
392-
PaperProvider,
393-
useTheme,
394-
} from 'react-native-paper';
429+
import { MD3LightTheme, PaperProvider, useTheme } from 'react-native-paper';
395430
import App from './src/App';
396431

397432
const theme = {
@@ -499,11 +534,7 @@ The final example for Material Design 2 would look like this:
499534

500535
```ts
501536
import * as React from 'react';
502-
import {
503-
MD2LightTheme,
504-
PaperProvider,
505-
useTheme,
506-
} from 'react-native-paper';
537+
import { MD2LightTheme, PaperProvider, useTheme } from 'react-native-paper';
507538
import App from './src/App';
508539

509540
const theme = {

example/src/index.native.tsx

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@ import { I18nManager } from 'react-native';
44
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
55
import AsyncStorage from '@react-native-async-storage/async-storage';
66
import { createDrawerNavigator } from '@react-navigation/drawer';
7-
import {
8-
InitialState,
9-
NavigationContainer,
10-
DarkTheme as NavigationDarkTheme,
11-
DefaultTheme as NavigationDefaultTheme,
12-
} from '@react-navigation/native';
7+
import { InitialState, NavigationContainer } from '@react-navigation/native';
138
import { useFonts } from 'expo-font';
149
import { useKeepAwake } from 'expo-keep-awake';
1510
import { StatusBar } from 'expo-status-bar';
1611
import * as Updates from 'expo-updates';
1712
import {
18-
Provider as PaperProvider,
19-
MD3DarkTheme,
20-
MD3LightTheme,
13+
PaperProvider,
2114
MD2DarkTheme,
2215
MD2LightTheme,
16+
MD3DarkTheme,
17+
MD3LightTheme,
2318
MD2Theme,
2419
MD3Theme,
2520
useTheme,
26-
adaptNavigationTheme,
27-
configureFonts,
2821
} from 'react-native-paper';
2922
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
3023

3124
import DrawerItems from './DrawerItems';
3225
import App from './RootNavigator';
3326
import { deviceColorsSupported } from '../utils';
27+
import {
28+
CombinedDefaultTheme,
29+
CombinedDarkTheme,
30+
createConfiguredFontTheme,
31+
} from '../utils/themes';
3432

3533
const PERSISTENCE_KEY = 'NAVIGATION_STATE';
3634
const PREFERENCES_KEY = 'APP_PREFERENCES';
@@ -192,38 +190,8 @@ export default function PaperExample() {
192190
return null;
193191
}
194192

195-
const { LightTheme, DarkTheme } = adaptNavigationTheme({
196-
reactNavigationLight: NavigationDefaultTheme,
197-
reactNavigationDark: NavigationDarkTheme,
198-
});
199-
200-
const CombinedDefaultTheme = {
201-
...MD3LightTheme,
202-
...LightTheme,
203-
colors: {
204-
...MD3LightTheme.colors,
205-
...LightTheme.colors,
206-
},
207-
};
208-
209-
const CombinedDarkTheme = {
210-
...MD3DarkTheme,
211-
...DarkTheme,
212-
colors: {
213-
...MD3DarkTheme.colors,
214-
...DarkTheme.colors,
215-
},
216-
};
217-
218193
const combinedTheme = isDarkMode ? CombinedDarkTheme : CombinedDefaultTheme;
219-
const configuredFontTheme = {
220-
...combinedTheme,
221-
fonts: configureFonts({
222-
config: {
223-
fontFamily: 'Abel',
224-
},
225-
}),
226-
};
194+
const configuredFontTheme = createConfiguredFontTheme(combinedTheme);
227195

228196
return (
229197
<PaperProvider

example/src/index.tsx

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import * as React from 'react';
22

33
import AsyncStorage from '@react-native-async-storage/async-storage';
44
import { createDrawerNavigator } from '@react-navigation/drawer';
5-
import {
6-
InitialState,
7-
NavigationContainer,
8-
DarkTheme as NavigationDarkTheme,
9-
DefaultTheme as NavigationDefaultTheme,
10-
} from '@react-navigation/native';
5+
import { InitialState, NavigationContainer } from '@react-navigation/native';
116
import { useFonts } from 'expo-font';
127
import { useKeepAwake } from 'expo-keep-awake';
138
import {
@@ -19,13 +14,16 @@ import {
1914
MD2Theme,
2015
MD3Theme,
2116
useTheme,
22-
adaptNavigationTheme,
23-
configureFonts,
2417
} from 'react-native-paper';
2518
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
2619

2720
import DrawerItems from './DrawerItems';
2821
import App from './RootNavigator';
22+
import {
23+
CombinedDarkTheme,
24+
CombinedDefaultTheme,
25+
createConfiguredFontTheme,
26+
} from '../utils/themes';
2927

3028
const PERSISTENCE_KEY = 'NAVIGATION_STATE';
3129
const PREFERENCES_KEY = 'APP_PREFERENCES';
@@ -149,38 +147,8 @@ export default function PaperExample() {
149147
return null;
150148
}
151149

152-
const { LightTheme, DarkTheme } = adaptNavigationTheme({
153-
reactNavigationLight: NavigationDefaultTheme,
154-
reactNavigationDark: NavigationDarkTheme,
155-
});
156-
157-
const CombinedDefaultTheme = {
158-
...MD3LightTheme,
159-
...LightTheme,
160-
colors: {
161-
...MD3LightTheme.colors,
162-
...LightTheme.colors,
163-
},
164-
};
165-
166-
const CombinedDarkTheme = {
167-
...MD3DarkTheme,
168-
...DarkTheme,
169-
colors: {
170-
...MD3DarkTheme.colors,
171-
...DarkTheme.colors,
172-
},
173-
};
174-
175150
const combinedTheme = isDarkMode ? CombinedDarkTheme : CombinedDefaultTheme;
176-
const configuredFontTheme = {
177-
...combinedTheme,
178-
fonts: configureFonts({
179-
config: {
180-
fontFamily: 'Abel',
181-
},
182-
}),
183-
};
151+
const configuredFontTheme = createConfiguredFontTheme(combinedTheme);
184152

185153
return (
186154
<PaperProvider

example/utils/themes.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
DarkTheme as NavigationDarkTheme,
3+
DefaultTheme as NavigationDefaultTheme,
4+
} from '@react-navigation/native';
5+
import {
6+
adaptNavigationTheme,
7+
MD3DarkTheme,
8+
MD3LightTheme,
9+
configureFonts,
10+
} from 'react-native-paper';
11+
12+
const { LightTheme, DarkTheme } = adaptNavigationTheme({
13+
reactNavigationLight: NavigationDefaultTheme,
14+
reactNavigationDark: NavigationDarkTheme,
15+
});
16+
17+
export const CombinedDefaultTheme = {
18+
...MD3LightTheme,
19+
...LightTheme,
20+
colors: {
21+
...MD3LightTheme.colors,
22+
...LightTheme.colors,
23+
},
24+
};
25+
26+
export const CombinedDarkTheme = {
27+
...MD3DarkTheme,
28+
...DarkTheme,
29+
colors: {
30+
...MD3DarkTheme.colors,
31+
...DarkTheme.colors,
32+
},
33+
};
34+
35+
type CombinedTheme = typeof CombinedDefaultTheme;
36+
37+
export const createConfiguredFontTheme = (theme: CombinedTheme) => ({
38+
...theme,
39+
fonts: configureFonts({
40+
config: {
41+
fontFamily: 'Abel',
42+
},
43+
}),
44+
});

0 commit comments

Comments
 (0)