From 71a5dfc6b2450ed61a79996b27650488c4bf69bd Mon Sep 17 00:00:00 2001 From: Doberjohn Date: Wed, 18 Nov 2026 00:20:08 +0200 Subject: [PATCH 1/3] fix(provider): merge the theme prop deeply PaperProvider spread the theme's top-level keys and merged only colors one level deep, so a partial nested object replaced the default whole. A theme with only `motion: { duration: { short3: 300 } }` type-checked but crashed every component at render, and a partial `shapes.corner` left the other corner roles undefined. Merge with safeMerge, as a component's theme prop already does, so a partial override keeps every key it leaves out. --- docs/6.x/docs/guides/migration.md | 4 +- docs/6.x/docs/guides/theming.mdx | 5 +- src/core/PaperProvider.tsx | 13 +- .../PaperProviderThemeMerge.test.tsx | 116 ++++++++++++++++++ 4 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 src/core/__tests__/PaperProviderThemeMerge.test.tsx diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 97e50db1ca..bb5821f1bb 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -251,7 +251,7 @@ A component's `theme` prop is merged deeply, so you can set only the role you ne + ``` -`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'; @@ -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, diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index c54c1d1294..8e6f1a9ed1 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -137,7 +137,7 @@ 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. If you don't use a custom theme, Paper will automatically turn animations on/off, depending on device settings. @@ -351,14 +351,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', diff --git a/src/core/PaperProvider.tsx b/src/core/PaperProvider.tsx index be34463b74..fc72b95b15 100644 --- a/src/core/PaperProvider.tsx +++ b/src/core/PaperProvider.tsx @@ -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, @@ -36,14 +36,11 @@ const PaperProvider = (props: Props) => { const scale = resolvedReduceMotion ? 0 : (props.theme?.animation?.scale ?? 1); - const safeTheme = withoutUnsafeKeys(props.theme); + // Merged deeply, like a component's `theme` prop: a nested key such as + // `motion` or `shapes` fills what it leaves out from the default 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; diff --git a/src/core/__tests__/PaperProviderThemeMerge.test.tsx b/src/core/__tests__/PaperProviderThemeMerge.test.tsx new file mode 100644 index 0000000000..e6e3c851b4 --- /dev/null +++ b/src/core/__tests__/PaperProviderThemeMerge.test.tsx @@ -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( + + + + ); + + return onTheme.mock.lastCall?.[0]; +}; + +describe('PaperProvider theme merge', () => { + it('renders components when the theme overrides a single motion token', async () => { + await render( + + + Surface + + {}}>Chip + + {}} /> + {}} /> + {}} /> + + ); + + 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); + }); +}); From e8acd401a300e1ecccbee8c6d623eac537beb915 Mon Sep 17 00:00:00 2001 From: Doberjohn Date: Wed, 18 Nov 2026 01:43:12 +0200 Subject: [PATCH 2/3] docs(theming): say why the examples still spread the base theme The guide now says a custom theme only needs the properties it changes, while its examples still spread LightTheme or DarkTheme. Explain that the spread sets dark and gives TypeScript the full Theme shape, and that nested spreads are optional otherwise. --- docs/6.x/docs/guides/theming.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index 8e6f1a9ed1..30d946875b 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -139,6 +139,8 @@ A theme usually contains the following 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. +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. Otherwise, your custom theme will need to handle it manually, using React Native's [AccessibilityInfo API](https://reactnative.dev/docs/accessibilityinfo). From f963da2831801256ff97db2294576420bed6fc35 Mon Sep 17 00:00:00 2001 From: John Fanidis Date: Wed, 18 Nov 2026 02:33:53 +0200 Subject: [PATCH 3/3] Remove comments from theme merging logic Removed comments explaining theme merging process. --- src/core/PaperProvider.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/PaperProvider.tsx b/src/core/PaperProvider.tsx index fc72b95b15..6c2394bd7c 100644 --- a/src/core/PaperProvider.tsx +++ b/src/core/PaperProvider.tsx @@ -36,8 +36,6 @@ const PaperProvider = (props: Props) => { const scale = resolvedReduceMotion ? 0 : (props.theme?.animation?.scale ?? 1); - // Merged deeply, like a component's `theme` prop: a nested key such as - // `motion` or `shapes` fills what it leaves out from the default theme. const merged = safeMerge(base, props.theme); return { ...merged, animation: { ...merged.animation, scale } };