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..30d946875b 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -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. + +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. @@ -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', diff --git a/src/core/PaperProvider.tsx b/src/core/PaperProvider.tsx index be34463b74..6c2394bd7c 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,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; 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); + }); +});