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
36 changes: 33 additions & 3 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ const MyComponent = () => {
};
```

### Reduce motion

The `animation` theme key and its `scale` were removed. The new `reduceMotion` prop of `PaperProvider` turns Paper's animations on or off instead:

- `'auto'` (default): follows the reduce motion setting of the device.
- `'on'`: turns Paper's animations off.
- `'off'`: keeps Paper's animations on, even when the device setting is on.

In 5.x, `PaperProvider` set the `scale` itself and replaced any `scale` in the theme passed to it, so a `scale` of `0` only turned animations off in a nested `ThemeProvider` or a component's `theme` prop. To turn animations off, for example in tests, remove that `scale` and pass `reduceMotion="on"` to `PaperProvider`:

```diff
-<PaperProvider>
- <ThemeProvider theme={{ ...LightTheme, animation: { scale: 0 } }}>
- <App />
- </ThemeProvider>
+<PaperProvider reduceMotion="on">
+ <App />
</PaperProvider>
```

If your app was written for a 6.0 alpha build, which also read a `scale` set on `PaperProvider`, remove it from that theme too.

Other `scale` values, which made animations slower or faster, have no replacement in 6.0. A `scale` in a component's `theme` prop, such as `theme={{ animation: { scale: 2 } }}`, does nothing now, and it can no longer turn animations back on while reduce motion is on. Paper ignores an `animation` left in your theme. TypeScript reports it in a theme object written inline in the `theme` prop of `PaperProvider` or a component, or typed as `Theme`, but not in the `theme` prop of `ThemeProvider`, which accepts any value, or in an untyped variable that also holds other theme keys.

In 5.x, `PaperProvider` also followed the device setting only when you passed no `theme`. In 6.x, `'auto'` follows the device setting with every theme, so if your app passes a custom theme, Paper's animations now stop when the user turns on reduce motion. 6.x also reads the setting when the app starts, while 5.x only reacted when it changed.

To follow the same preference in your own components, use the `useReduceMotion()` hook, which returns `true` when reduce motion is on. See [Reduce motion](./theming#reduce-motion) in the theming guide.

### Elevation

The `elevation` prop no longer accepts a React Native `Animated.Value` in the following components:
Expand Down Expand Up @@ -226,7 +254,7 @@ If you import it from Paper's type declarations instead, the path changed:

#### `roundness`

The `roundness` theme property was removed. Corner radii are now set in `theme.shapes.corner`, a scale of roles from `extraSmall` (4) to `extraExtraLarge` (48), and each component uses one fixed role. Paper ignores a `roundness` left in your theme, and TypeScript only reports it in a theme object written inline in a `theme` prop or typed as `Theme`.
The `roundness` theme property was removed. Corner radii are now set in `theme.shapes.corner`, a scale of roles from `extraSmall` (4) to `extraExtraLarge` (48), and each component uses one fixed role. Paper ignores a `roundness` left in your theme. TypeScript reports it in a theme object written inline in the `theme` prop of `PaperProvider` or a component, or typed as `Theme`, but not in the `theme` prop of `ThemeProvider`, which accepts any value, or in an untyped variable that also holds other theme keys.

In 5.x, each of these components multiplied `roundness` by a fixed number, and the 6.x defaults match the 5.x corners with the default `roundness` of 4. To port a custom `roundness`, set each role to your `roundness` times its multiplier:

Expand Down Expand Up @@ -278,7 +306,7 @@ A component's `theme` prop is merged deeply, so you can set only the role you ne

#### Removed keys

The 6.x theme has no `version`, `isV3`, `mode` or `animation.defaultAnimationDuration` keys. Paper 6 only supports Material Design 3, so `version` and `isV3` have nothing to select, and `mode` and `defaultAnimationDuration` have no replacement. Remove `version: 3` and `isV3` from your custom theme objects:
The 6.x theme has no `version`, `isV3`, `mode` or `animation` keys. Paper 6 only supports Material Design 3, so `version` and `isV3` have nothing to select. `mode` and `animation.defaultAnimationDuration` have no replacement, and `animation.scale` is replaced by the `reduceMotion` prop of `PaperProvider`, see [Reduce motion](#reduce-motion). Remove `version: 3` and `isV3` from your custom theme objects:

```diff
const theme = {
Expand All @@ -292,7 +320,7 @@ The 6.x theme has no `version`, `isV3`, `mode` or `animation.defaultAnimationDur
};
```

As with `roundness`, Paper ignores these keys, and TypeScript only reports them in a theme object written inline in a `theme` prop or typed as `Theme`. Code that reads `theme.isV3` gets `undefined` once the key is gone, so a check like `theme.isV3 ? a : b` takes the Material Design 2 branch. Keep only the Material Design 3 branch.
As with `roundness`, Paper ignores these keys, and TypeScript reports them in the same cases. Code that reads `theme.isV3` gets `undefined` once the key is gone, so a check like `theme.isV3 ? a : b` takes the Material Design 2 branch. Keep only the Material Design 3 branch.

#### `configureFonts`

Expand Down Expand Up @@ -390,6 +418,8 @@ On web, `Button`, `Card`, the `Chip` body, `FAB`, `FAB.Extended` and the `FAB.Me

Libraries built on Paper may not support 6.x yet. For example, `react-native-paper-dates` 0.24.1 still uses `HelperText`, the 5.x theme exports and `theme.roundness`, so its `DatePickerInput` crashes when it shows a validation error.

A library that reads `theme.animation.scale` to turn its own animations off should read `useReduceMotion()` instead. The 6.x theme has no `animation` key, so `theme.animation.scale` throws.

## Components

### Appbar
Expand Down
50 changes: 46 additions & 4 deletions docs/6.x/docs/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,58 @@ A theme usually contains the following properties:
- `fontWeight`
- `lineHeight`
- `fontSize`
- `animation` (`object`)
- `scale` - scale for all animations

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.
## Reduce motion

Otherwise, your custom theme will need to handle it manually, using React Native's [AccessibilityInfo API](https://reactnative.dev/docs/accessibilityinfo).
`PaperProvider` takes a `reduceMotion` prop, typed `ReduceMotionPreference`, that turns Paper's animations on or off:

- `'auto'` (default): follows the reduce motion setting of the device, read with React Native's [AccessibilityInfo API](https://reactnative.dev/docs/accessibilityinfo).
- `'on'`: turns Paper's animations off.
- `'off'`: keeps Paper's animations on, even when the device setting is on.

It works the same whether or not you pass a custom `theme`. With `'auto'`, Paper starts from the setting the device had when the app launched and follows every change after that.

When reduce motion is on, Paper's components skip their animations. A few still move: the `ActivityIndicator` rotation, the indeterminate `ProgressBar` and the `TouchableRipple` ripple on web. The `TextInput` label and active indicator follow only the device setting: they don't read the `reduceMotion` prop.

To turn Paper's animations off in tests and screenshots, whatever the device setting, pass `reduceMotion="on"`:

```js
import * as React from 'react';
import { PaperProvider } from 'react-native-paper';
import App from './src/App';

export default function Main() {
return (
<PaperProvider reduceMotion="on">
<App />
</PaperProvider>
);
}
```

To follow the same preference in your own components, use the `useReduceMotion()` hook. It returns `true` when reduce motion is on. Reanimated's animations follow the device setting on their own too, so give them `ReduceMotion.Never` to leave the choice to the hook:

```js
import * as React from 'react';
import { useReduceMotion } from 'react-native-paper';
import Animated, { ReduceMotion, SlideInDown } from 'react-native-reanimated';

const slideIn = SlideInDown.reduceMotion(ReduceMotion.Never);

export default function Toast({ children }) {
const reduceMotion = useReduceMotion();

return (
<Animated.View entering={reduceMotion ? undefined : slideIn}>
{children}
</Animated.View>
);
}
```
Comment thread
Doberjohn marked this conversation as resolved.
Resolved

## Extending the theme

Expand Down
13 changes: 6 additions & 7 deletions src/components/ActivityIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Animated, Easing, Platform, StyleSheet, View } from 'react-native';
import type { ColorValue, StyleProp, ViewProps, ViewStyle } from 'react-native';

import { useInternalTheme } from '../core/theming';
import { useMotionDuration } from '../theme/accessibility/ReduceMotionContext';
import type { ThemeProp } from '../theme/types';

export type Props = ViewProps & {
Expand Down Expand Up @@ -69,14 +70,12 @@ const ActivityIndicator = ({
undefined
);

const {
animation: { scale },
} = theme;
const getDuration = useMotionDuration();

const startRotation = React.useCallback(() => {
// Show indicator
Animated.timing(fade, {
duration: 200 * scale,
duration: getDuration(200),
toValue: 1,
isInteraction: false,
useNativeDriver: true,
Expand All @@ -88,7 +87,7 @@ const ActivityIndicator = ({
// $FlowFixMe
Animated.loop(rotation.current).start();
}
}, [scale, fade, timer]);
}, [getDuration, fade, timer]);

const stopRotation = () => {
if (rotation.current) {
Expand All @@ -114,15 +113,15 @@ const ActivityIndicator = ({
} else if (hidesWhenStopped) {
// Hide indicator first and then stop rotation
Animated.timing(fade, {
duration: 200 * scale,
duration: getDuration(200),
toValue: 0,
useNativeDriver: true,
isInteraction: false,
}).start(stopRotation);
} else {
stopRotation();
}
}, [animating, fade, hidesWhenStopped, startRotation, scale, timer]);
}, [animating, fade, hidesWhenStopped, startRotation, getDuration, timer]);

const color = indicatorColor || theme.colors?.primary;
const size =
Expand Down
8 changes: 3 additions & 5 deletions src/components/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StyleSheet } from 'react-native';
import Animated, { type AnimatedStyle } from 'react-native-reanimated';

import { useInternalTheme } from '../core/theming';
import { useReduceMotion } from '../theme/accessibility/ReduceMotionContext';
import { cornerFull } from '../theme/tokens/sys/shape';
import type { ThemeProp } from '../theme/types';

Expand Down Expand Up @@ -56,10 +57,7 @@ const Badge = ({
...rest
}: Props) => {
const theme = useInternalTheme(themeOverrides);

const {
animation: { scale },
} = theme;
const reduceMotion = useReduceMotion();

const textColor = theme.colors.onError;

Expand All @@ -69,7 +67,7 @@ const Badge = ({

const transitionStyle = {
opacity: visible ? 1 : 0,
transitionDuration: 150 * scale,
transitionDuration: reduceMotion ? 0 : 150,
transitionProperty: 'opacity',
};

Expand Down
8 changes: 4 additions & 4 deletions src/components/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Surface from './Surface';
import type { SurfaceStyle } from './Surface';
import Text from './Typography/Text';
import { useInternalTheme } from '../core/theming';
import { useMotionDuration } from '../theme/accessibility/ReduceMotionContext';
import type { Elevation, ThemeProp } from '../theme/types';

const DEFAULT_MAX_WIDTH = 960;
Expand Down Expand Up @@ -165,22 +166,21 @@ const Banner = ({
const showCallback = useLatestCallback(onShowAnimationFinished);
const hideCallback = useLatestCallback(onHideAnimationFinished);

const { scale } = theme.animation;
const animationDuration = (visible ? 250 : 200) * scale;
const getDuration = useMotionDuration();

React.useEffect(() => {
const callback = visible ? showCallback : hideCallback;

position.value = withTiming(
visible ? 1 : 0,
{
duration: animationDuration,
duration: getDuration(visible ? 250 : 200),
easing: Easing.inOut(Easing.ease),
reduceMotion: ReduceMotion.Never,
},
(finished) => scheduleOnRN(callback, { finished: finished ?? false })
);
}, [animationDuration, hideCallback, position, showCallback, visible]);
}, [getDuration, hideCallback, position, showCallback, visible]);

const surfaceStyle = useAnimatedStyle(() => ({
opacity: interpolate(position.value, [0, 0.1, 1], [0, 1, 1]),
Expand Down
7 changes: 4 additions & 3 deletions src/components/BottomNavigation/BottomNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useLatestCallback from 'use-latest-callback';
import BottomNavigationBar from './BottomNavigationBar';
import BottomNavigationRouteScreen from './BottomNavigationRouteScreen';
import { useInternalTheme } from '../../core/theming';
import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext';
import type { ThemeProp } from '../../theme/types';
import useAnimatedValueArray from '../../utils/useAnimatedValueArray';
import type { IconSource } from '../Icon';
Expand Down Expand Up @@ -342,7 +343,7 @@ const BottomNavigation = <Route extends BaseRoute>({
getLazy = ({ route }: { route: Route }) => route.lazy,
}: Props<Route>) => {
const theme = useInternalTheme(themeOverrides);
const { scale } = theme.animation;
const getDuration = useMotionDuration();
const compact = compactProp ?? false;
let shifting = shiftingProp ?? false;

Expand Down Expand Up @@ -393,7 +394,7 @@ const BottomNavigation = <Route extends BaseRoute>({
...navigationState.routes.map((_, i) =>
Animated.timing(tabsPositionAnims[i], {
toValue: i === index ? 0 : i >= index ? 1 : -1,
duration: 150 * scale,
duration: getDuration(150),
useNativeDriver: true,
easing: sceneAnimationEasing,
})
Expand All @@ -415,7 +416,7 @@ const BottomNavigation = <Route extends BaseRoute>({
[
navigationState.routes,
offsetsAnims,
scale,
getDuration,
tabsPositionAnims,
sceneAnimationEasing,
]
Expand Down
15 changes: 8 additions & 7 deletions src/components/BottomNavigation/BottomNavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getLabelColor,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext';
import type { ThemeProp } from '../../theme/types';
import useAnimatedValue from '../../utils/useAnimatedValue';
import useAnimatedValueArray from '../../utils/useAnimatedValueArray';
Expand Down Expand Up @@ -334,7 +335,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
const theme = useInternalTheme(themeOverrides);
const { colors } = theme;
const { bottom, left, right } = useSafeAreaInsets();
const { scale } = theme.animation;
const getDuration = useMotionDuration();
const compact = compactProp ?? false;
let shifting = shiftingProp ?? false;

Expand Down Expand Up @@ -374,28 +375,28 @@ const BottomNavigationBar = <Route extends BaseRoute>({
setKeyboardVisible(true);
Animated.timing(visibleAnim, {
toValue: 0,
duration: 150 * scale,
duration: getDuration(150),
useNativeDriver: true,
}).start();
}, [scale, visibleAnim]);
}, [getDuration, visibleAnim]);

const handleKeyboardHide = React.useCallback(() => {
Animated.timing(visibleAnim, {
toValue: 1,
duration: 100 * scale,
duration: getDuration(100),
useNativeDriver: true,
}).start(() => {
setKeyboardVisible(false);
});
}, [scale, visibleAnim]);
}, [getDuration, visibleAnim]);

const animateToIndex = React.useCallback(
(index: number) => {
Animated.parallel(
navigationState.routes.map((_, i) =>
Animated.timing(tabsAnims[i], {
toValue: i === index ? 1 : 0,
duration: 150 * scale,
duration: getDuration(150),
useNativeDriver: true,
easing: animationEasing,
})
Expand All @@ -405,7 +406,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
tabsAnims.map((tab, i) => tab.setValue(i === index ? 1 : 0));
});
},
[scale, navigationState.routes, tabsAnims, animationEasing]
[getDuration, navigationState.routes, tabsAnims, animationEasing]
);

React.useEffect(() => {
Expand Down
9 changes: 5 additions & 4 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export type Props = Omit<ViewProps, 'style'> & {
* `false` the shape still changes, it just snaps instead of animating, and
* there is no press morph at all.
*
* The press morph is skipped when the OS reduce-motion setting is on.
* Under reduce motion the shape behaves as with `false`. Reduce motion
* comes from the `reduceMotion` prop of `PaperProvider`, which follows the
* OS setting by default.
*/
animateShape?: boolean;
/**
Expand Down Expand Up @@ -348,9 +350,7 @@ const Button = ({
() => toSpringCurve(theme.motion.spring.fast.spatial),
[theme.motion.spring.fast.spatial]
);
const shapeTransitionDuration = animatesShape
? shapeSpring.duration * theme.animation.scale
: 0;
const shapeTransitionDuration = animatesShape ? shapeSpring.duration : 0;

const tracksPress = animatesShape || isElevationEntitled;

Expand Down Expand Up @@ -422,6 +422,7 @@ const Button = ({
const surfaceTransitionDuration = getButtonTransitionDuration({
theme,
pressed,
reduceMotion,
containerColor,
previousContainerColor: committedContainerColor,
});
Expand Down
Loading
Loading