Skip to content

Commit 1eaf916

Browse files
committed
refactor: override completely navigation fonts
1 parent 0bbcac6 commit 1eaf916

File tree

4 files changed

+88
-76
lines changed

4 files changed

+88
-76
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 = {

src/core/__tests__/theming.test.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,25 @@ describe('adaptNavigationTheme', () => {
238238
notification: MD3LightTheme.colors.error,
239239
},
240240
fonts: {
241-
...NavigationThemeWithFonts.fonts,
242241
regular: {
243-
...NavigationThemeWithFonts.fonts.regular,
244-
fontFamily: MD3LightTheme.fonts.default.fontFamily,
242+
fontFamily: MD3LightTheme.fonts.bodyMedium.fontFamily,
243+
fontWeight: MD3LightTheme.fonts.bodyMedium.fontWeight,
244+
letterSpacing: MD3LightTheme.fonts.bodyMedium.letterSpacing,
245245
},
246246
medium: {
247-
...NavigationThemeWithFonts.fonts.medium,
248-
fontFamily: MD3LightTheme.fonts.default.fontFamily,
247+
fontFamily: MD3LightTheme.fonts.titleMedium.fontFamily,
248+
fontWeight: MD3LightTheme.fonts.titleMedium.fontWeight,
249+
letterSpacing: MD3LightTheme.fonts.titleMedium.letterSpacing,
249250
},
250251
bold: {
251-
...NavigationThemeWithFonts.fonts.bold,
252-
fontFamily: MD3LightTheme.fonts.default.fontFamily,
252+
fontFamily: MD3LightTheme.fonts.headlineSmall.fontFamily,
253+
fontWeight: MD3LightTheme.fonts.headlineSmall.fontWeight,
254+
letterSpacing: MD3LightTheme.fonts.headlineSmall.letterSpacing,
253255
},
254256
heavy: {
255-
...NavigationThemeWithFonts.fonts.heavy,
256-
fontFamily: MD3LightTheme.fonts.default.fontFamily,
257+
fontFamily: MD3LightTheme.fonts.headlineLarge.fontFamily,
258+
fontWeight: MD3LightTheme.fonts.headlineLarge.fontWeight,
259+
letterSpacing: MD3LightTheme.fonts.headlineLarge.letterSpacing,
257260
},
258261
},
259262
});

src/core/theming.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
MD3Theme,
1515
MD3AndroidColors,
1616
NavigationTheme,
17-
NavigationFonts,
1817
} from '../types';
1918

2019
export const DefaultTheme = MD3LightTheme;
@@ -132,26 +131,28 @@ const getAdaptedTheme = <T extends NavigationTheme>(
132131
};
133132

134133
if ('fonts' in theme) {
135-
const navigationFonts = theme.fonts as NavigationFonts;
136134
return {
137135
...base,
138136
fonts: {
139-
...navigationFonts,
140137
regular: {
141-
...navigationFonts.regular,
142-
fontFamily: materialTheme.fonts.default.fontFamily,
138+
fontFamily: materialTheme.fonts.bodyMedium.fontFamily,
139+
fontWeight: materialTheme.fonts.bodyMedium.fontWeight,
140+
letterSpacing: materialTheme.fonts.bodyMedium.letterSpacing,
143141
},
144142
medium: {
145-
...navigationFonts.medium,
146-
fontFamily: materialTheme.fonts.default.fontFamily,
143+
fontFamily: materialTheme.fonts.titleMedium.fontFamily,
144+
fontWeight: materialTheme.fonts.titleMedium.fontWeight,
145+
letterSpacing: materialTheme.fonts.titleMedium.letterSpacing,
147146
},
148147
bold: {
149-
...navigationFonts.bold,
150-
fontFamily: materialTheme.fonts.default.fontFamily,
148+
fontFamily: materialTheme.fonts.headlineSmall.fontFamily,
149+
fontWeight: materialTheme.fonts.headlineSmall.fontWeight,
150+
letterSpacing: materialTheme.fonts.headlineSmall.letterSpacing,
151151
},
152152
heavy: {
153-
...navigationFonts.heavy,
154-
fontFamily: materialTheme.fonts.default.fontFamily,
153+
fontFamily: materialTheme.fonts.headlineLarge.fontFamily,
154+
fontWeight: materialTheme.fonts.headlineLarge.fontWeight,
155+
letterSpacing: materialTheme.fonts.headlineLarge.letterSpacing,
155156
},
156157
},
157158
};

src/types.tsx

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,3 @@ export type NavigationTheme = {
214214
notification: string;
215215
};
216216
};
217-
218-
type NavigationFontStyle = {
219-
fontFamily: string;
220-
fontWeight:
221-
| 'normal'
222-
| 'bold'
223-
| '100'
224-
| '200'
225-
| '300'
226-
| '400'
227-
| '500'
228-
| '600'
229-
| '700'
230-
| '800'
231-
| '900';
232-
};
233-
234-
export type NavigationFonts = {
235-
regular: NavigationFontStyle;
236-
medium: NavigationFontStyle;
237-
bold: NavigationFontStyle;
238-
heavy: NavigationFontStyle;
239-
};

0 commit comments

Comments
 (0)