Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ A component's `theme` prop is merged deeply, so you can set only the role you ne
+</Button>
```

`PaperProvider` doesn't merge `shapes` deeply: a `shapes` key in its `theme` replaces the default one. So spread the base `shapes` and `corner`, or the roles you leave out are `undefined` and those components lose their rounded corners:
`PaperProvider` merges its `theme` deeply too, so a custom theme only needs the roles it changes:

```diff
-import { MD3LightTheme, PaperProvider } from 'react-native-paper';
Expand All @@ -262,9 +262,7 @@ A component's `theme` prop is merged deeply, so you can set only the role you ne
- roundness: 2,
+ ...LightTheme,
+ shapes: {
+ ...LightTheme.shapes,
+ corner: {
+ ...LightTheme.shapes.corner,
+ extraSmall: 2,
+ small: 4,
+ medium: 6,
Expand Down
7 changes: 3 additions & 4 deletions docs/6.x/docs/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ A theme usually contains the following properties:
- `animation` (`object`)
- `scale` - scale for all animations

When creating a custom theme, you will need to provide all of these properties.
When creating a custom theme, you only need to provide the properties you change. `PaperProvider` merges your theme deeply with the default one, so a nested object such as `shapes` or `motion` keeps every key you leave out.
Comment thread
Doberjohn marked this conversation as resolved.
Resolved

Paper merges a custom theme onto `DarkTheme` when it sets `dark: true`, and onto `LightTheme` otherwise. The examples on this page still spread `LightTheme` or `DarkTheme`: that sets `dark`, and in TypeScript it gives the theme object the full `Theme` shape. Without TypeScript, nested spreads such as `...LightTheme.colors` are optional.

If you don't use a custom theme, Paper will automatically turn animations on/off, depending on device settings.

Expand Down Expand Up @@ -351,14 +353,11 @@ import App from './src/App';
const theme = {
...LightTheme, // or DarkTheme
shapes: {
...LightTheme.shapes,
corner: {
...LightTheme.shapes.corner,
largeIncreased: 10,
},
},
colors: {
...LightTheme.colors,
primary: '#3498db',
secondary: '#f1c40f',
tertiary: '#a1b2c3',
Expand Down
11 changes: 3 additions & 8 deletions src/core/PaperProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getDefaultDirection, LocaleProvider, type Direction } from './locale';
import SafeAreaProviderCompat from './SafeAreaProviderCompat';
import { Provider as SettingsProvider } from './settings';
import type { Settings } from './settings';
import { ThemeProvider, withoutUnsafeKeys } from './theming';
import { safeMerge, ThemeProvider } from './theming';
import {
useResolvedReduceMotion,
type ReduceMotionPreference,
Expand Down Expand Up @@ -36,14 +36,9 @@ const PaperProvider = (props: Props) => {
const scale = resolvedReduceMotion
? 0
: (props.theme?.animation?.scale ?? 1);
const safeTheme = withoutUnsafeKeys(props.theme);
const merged = safeMerge(base, props.theme);

return {
...base,
...safeTheme,
colors: { ...base.colors, ...safeTheme?.colors },
animation: { ...safeTheme?.animation, scale },
};
return { ...merged, animation: { ...merged.animation, scale } };
}, [colorScheme, props.theme, resolvedReduceMotion]);

const { children, settings } = props;
Expand Down
116 changes: 116 additions & 0 deletions src/core/__tests__/PaperProviderThemeMerge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { PlatformColor, Text } from 'react-native';

import { describe, expect, it, jest } from '@jest/globals';
import { render, screen } from '@testing-library/react-native';

import Button from '../../components/Button/Button';
import Checkbox from '../../components/Checkbox';
import Chip from '../../components/Chip/Chip';
import FAB from '../../components/FAB';
import Surface from '../../components/Surface';
import Switch from '../../components/Switch/Switch';
import { LightTheme } from '../../theme/schemes';
import type { Theme, ThemeProp } from '../../theme/types';
import PaperProvider from '../PaperProvider';
import { useTheme } from '../theming';

const ThemeReader = ({ onTheme }: { onTheme: (theme: Theme) => void }) => {
onTheme(useTheme());
return null;
};

const getProvidedTheme = async (theme: ThemeProp) => {
const onTheme = jest.fn<(theme: Theme) => void>();

await render(
<PaperProvider theme={theme}>
<ThemeReader onTheme={onTheme} />
</PaperProvider>
);

return onTheme.mock.lastCall?.[0];
};

describe('PaperProvider theme merge', () => {
it('renders components when the theme overrides a single motion token', async () => {
await render(
<PaperProvider theme={{ motion: { duration: { short3: 300 } } }}>
<Surface elevation={1}>
<Text>Surface</Text>
</Surface>
<Chip onPress={() => {}}>Chip</Chip>
<Button onPress={() => {}}>Button</Button>
<Switch value onValueChange={() => {}} />
<Checkbox status="checked" onPress={() => {}} />
<FAB icon="plus" aria-label="Add" onPress={() => {}} />
</PaperProvider>
);

expect(screen.getByText('Surface')).toBeOnTheScreen();
expect(screen.getByText('Button')).toBeOnTheScreen();
});

it('keeps the motion tokens a partial motion override leaves out', async () => {
const theme = await getProvidedTheme({
motion: { duration: { short3: 300 } },
});

expect(theme?.motion).toStrictEqual({
...LightTheme.motion,
duration: { ...LightTheme.motion.duration, short3: 300 },
});
});

it('keeps the corner roles a partial shapes override leaves out', async () => {
const theme = await getProvidedTheme({
shapes: { corner: { medium: 6 } },
});

expect(theme?.shapes).toStrictEqual({
...LightTheme.shapes,
corner: { ...LightTheme.shapes.corner, medium: 6 },
});
});

it('keeps the elevation colors a partial colors override leaves out', async () => {
const theme = await getProvidedTheme({
colors: { primary: 'tomato', elevation: { level1: 'red' } },
});

expect(theme?.colors).toStrictEqual({
...LightTheme.colors,
primary: 'tomato',
elevation: { ...LightTheme.colors.elevation, level1: 'red' },
});
});

it('keeps the font properties and variants a partial fonts override leaves out', async () => {
const theme = await getProvidedTheme({
fonts: { bodyLarge: { fontSize: 18 } },
});

expect(theme?.fonts).toStrictEqual({
...LightTheme.fonts,
bodyLarge: { ...LightTheme.fonts.bodyLarge, fontSize: 18 },
});
});

it('replaces an easing curve as a whole', async () => {
const theme = await getProvidedTheme({
motion: { easing: { emphasized: [0.1, 0, 0, 1] } },
});

expect(theme?.motion.easing.emphasized).toStrictEqual([0.1, 0, 0, 1]);
expect(theme?.motion.easing.standard).toStrictEqual(
LightTheme.motion.easing.standard
);
});

it('keeps a platform color as a single value', async () => {
const primary = PlatformColor('systemBlue');
const theme = await getProvidedTheme({ colors: { primary } });

expect(theme?.colors.primary).toStrictEqual(primary);
expect(theme?.colors.secondary).toBe(LightTheme.colors.secondary);
});
});
Loading