Skip to content
Open
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
8 changes: 4 additions & 4 deletions docs/6.x/docs/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand All @@ -437,7 +437,7 @@ export default function Main() {
}
```

The type includes `undefined`. If the variable must always hold a theme, type it with `NonNullable<ProviderProps['theme']>`.
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

Expand Down
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
26 changes: 26 additions & 0 deletions src/theme/__tests__/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
<PaperProvider theme={LightTheme} reduceMotion="off">
<ThemeProvider theme={theme}>
<ThemeReader onTheme={onTheme} />
</ThemeProvider>
</PaperProvider>
);

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();

Expand Down
Loading