diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index 8f5c6b5f38..6f192e4f68 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -415,14 +415,14 @@ export default function Main() { } ``` -`Theme` requires every key, so it doesn't fit a partial theme kept in a variable. Type the variable with `ProviderProps['theme']`, the type that the `theme` props of `PaperProvider`, `ThemeProvider` and Paper's components take: +`Theme` requires every key, so it doesn't fit a partial theme kept in a variable. Type the variable with `ThemeProp`, the partial theme type that the `theme` props of `PaperProvider`, `ThemeProvider` and Paper's components take: ```tsx import * as React from 'react'; -import { PaperProvider, type ProviderProps } from 'react-native-paper'; +import { PaperProvider, type ThemeProp } from 'react-native-paper'; import App from './src/App'; -const theme: ProviderProps['theme'] = { +const theme: ThemeProp = { colors: { primary: 'tomato', }, @@ -437,7 +437,7 @@ export default function Main() { } ``` -The type includes `undefined`. If the variable must always hold a theme, type it with `NonNullable`. +Every key in `ThemeProp` is optional at every level, so you can override a single color or font without restating the rest of the theme. ### Advanced theme overrides diff --git a/src/index.tsx b/src/index.tsx index 2b0614c8cc..473da4aeab 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -149,5 +149,10 @@ export type { Props as SegmentedButtonsProps } from './components/SegmentedButto export type { Props as ListImageProps } from './components/List/ListImage'; export type { Props as TooltipProps } from './components/Tooltip/Tooltip'; -export { type TypescaleKey, type Theme, type Elevation } from './theme/types'; +export { + type TypescaleKey, + type Theme, + type ThemeProp, + type Elevation, +} from './theme/types'; export type { ReduceMotionPreference } from './core/useResolvedReduceMotion'; diff --git a/src/theme/__tests__/ThemeProvider.test.tsx b/src/theme/__tests__/ThemeProvider.test.tsx index bd7bfc53aa..43e86f9129 100644 --- a/src/theme/__tests__/ThemeProvider.test.tsx +++ b/src/theme/__tests__/ThemeProvider.test.tsx @@ -7,6 +7,7 @@ import { render, screen } from '@testing-library/react-native'; import Button from '../../components/Button/Button'; import Text from '../../components/Typography/Text'; import PaperProvider from '../../core/PaperProvider'; +import type { ThemeProp as PackageThemeProp } from '../../index'; import { ThemeProvider, useTheme } from '../provider'; import { DarkTheme, LightTheme } from '../schemes'; import type { Theme, ThemeProp } from '../types'; @@ -44,6 +45,31 @@ describe('ThemeProvider', () => { }); }); + it('applies a partial theme typed with the ThemeProp the package exports', async () => { + const theme: PackageThemeProp = { + colors: { primary: 'tomato' }, + fonts: { bodyLarge: { fontSize: 18 } }, + }; + const onTheme = createOnTheme(); + + await render( + + + + + + ); + + expect(onTheme.mock.lastCall?.[0]).toStrictEqual({ + ...LightTheme, + colors: { ...LightTheme.colors, primary: 'tomato' }, + fonts: { + ...LightTheme.fonts, + bodyLarge: { ...LightTheme.fonts.bodyLarge, fontSize: 18 }, + }, + }); + }); + it('merges nested themes in order', async () => { const onTheme = createOnTheme();