Skip to content

Commit b354fa9

Browse files
authored
feat(theme)!: remove animation.scale and export useReduceMotion (#5235)
The 16 components that multiplied durations by `theme.animation.scale` now read `useReduceMotion()`, which is exported with the `ReduceMotionPreference` type. A component's `theme` prop can no longer turn animations back on under reduce motion, `'auto'` starts from the device setting at launch, and an OS setting change no longer replays Banner, CrossFadeIcon or ActivityIndicator animations. BREAKING CHANGE: `theme.animation.scale` is removed; pass `reduceMotion="on"` to `PaperProvider` to turn animations off, and read the preference with `useReduceMotion()`.
1 parent a525c28 commit b354fa9

43 files changed

Lines changed: 1331 additions & 188 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/6.x/docs/guides/migration.md‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ const MyComponent = () => {
4242
};
4343
```
4444

45+
### Reduce motion
46+
47+
The `animation` theme key and its `scale` were removed. The new `reduceMotion` prop of `PaperProvider` turns Paper's animations on or off instead:
48+
49+
- `'auto'` (default): follows the reduce motion setting of the device.
50+
- `'on'`: turns Paper's animations off.
51+
- `'off'`: keeps Paper's animations on, even when the device setting is on.
52+
53+
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`:
54+
55+
```diff
56+
-<PaperProvider>
57+
- <ThemeProvider theme={{ ...LightTheme, animation: { scale: 0 } }}>
58+
- <App />
59+
- </ThemeProvider>
60+
+<PaperProvider reduceMotion="on">
61+
+ <App />
62+
</PaperProvider>
63+
```
64+
65+
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.
66+
67+
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.
68+
69+
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.
70+
71+
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.
72+
4573
### Elevation
4674

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

227255
#### `roundness`
228256

229-
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`.
257+
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.
230258

231259
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:
232260

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

279307
#### Removed keys
280308

281-
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:
309+
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:
282310

283311
```diff
284312
const theme = {
@@ -292,7 +320,7 @@ The 6.x theme has no `version`, `isV3`, `mode` or `animation.defaultAnimationDur
292320
};
293321
```
294322

295-
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.
323+
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.
296324

297325
#### `configureFonts`
298326

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

391419
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.
392420

421+
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.
422+
393423
## Components
394424

395425
### Appbar

‎docs/6.x/docs/guides/theming.mdx‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,58 @@ A theme usually contains the following properties:
134134
- `fontWeight`
135135
- `lineHeight`
136136
- `fontSize`
137-
- `animation` (`object`)
138-
- `scale` - scale for all animations
139137

140138
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.
141139

142140
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.
143141

144-
If you don't use a custom theme, Paper will automatically turn animations on/off, depending on device settings.
142+
## Reduce motion
145143

146-
Otherwise, your custom theme will need to handle it manually, using React Native's [AccessibilityInfo API](https://reactnative.dev/docs/accessibilityinfo).
144+
`PaperProvider` takes a `reduceMotion` prop, typed `ReduceMotionPreference`, that turns Paper's animations on or off:
145+
146+
- `'auto'` (default): follows the reduce motion setting of the device, read with React Native's [AccessibilityInfo API](https://reactnative.dev/docs/accessibilityinfo).
147+
- `'on'`: turns Paper's animations off.
148+
- `'off'`: keeps Paper's animations on, even when the device setting is on.
149+
150+
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.
151+
152+
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.
153+
154+
To turn Paper's animations off in tests and screenshots, whatever the device setting, pass `reduceMotion="on"`:
155+
156+
```js
157+
import * as React from 'react';
158+
import { PaperProvider } from 'react-native-paper';
159+
import App from './src/App';
160+
161+
export default function Main() {
162+
return (
163+
<PaperProvider reduceMotion="on">
164+
<App />
165+
</PaperProvider>
166+
);
167+
}
168+
```
169+
170+
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:
171+
172+
```js
173+
import * as React from 'react';
174+
import { useReduceMotion } from 'react-native-paper';
175+
import Animated, { ReduceMotion, SlideInDown } from 'react-native-reanimated';
176+
177+
const slideIn = SlideInDown.reduceMotion(ReduceMotion.Never);
178+
179+
export default function Toast({ children }) {
180+
const reduceMotion = useReduceMotion();
181+
182+
return (
183+
<Animated.View entering={reduceMotion ? undefined : slideIn}>
184+
{children}
185+
</Animated.View>
186+
);
187+
}
188+
```
147189

148190
## Extending the theme
149191

‎src/components/ActivityIndicator.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Animated, Easing, Platform, StyleSheet, View } from 'react-native';
44
import type { ColorValue, StyleProp, ViewProps, ViewStyle } from 'react-native';
55

66
import { useInternalTheme } from '../core/theming';
7+
import { useMotionDuration } from '../theme/accessibility/ReduceMotionContext';
78
import type { ThemeProp } from '../theme/types';
89

910
export type Props = ViewProps & {
@@ -69,14 +70,12 @@ const ActivityIndicator = ({
6970
undefined
7071
);
7172

72-
const {
73-
animation: { scale },
74-
} = theme;
73+
const getDuration = useMotionDuration();
7574

7675
const startRotation = React.useCallback(() => {
7776
// Show indicator
7877
Animated.timing(fade, {
79-
duration: 200 * scale,
78+
duration: getDuration(200),
8079
toValue: 1,
8180
isInteraction: false,
8281
useNativeDriver: true,
@@ -88,7 +87,7 @@ const ActivityIndicator = ({
8887
// $FlowFixMe
8988
Animated.loop(rotation.current).start();
9089
}
91-
}, [scale, fade, timer]);
90+
}, [getDuration, fade, timer]);
9291

9392
const stopRotation = () => {
9493
if (rotation.current) {
@@ -114,15 +113,15 @@ const ActivityIndicator = ({
114113
} else if (hidesWhenStopped) {
115114
// Hide indicator first and then stop rotation
116115
Animated.timing(fade, {
117-
duration: 200 * scale,
116+
duration: getDuration(200),
118117
toValue: 0,
119118
useNativeDriver: true,
120119
isInteraction: false,
121120
}).start(stopRotation);
122121
} else {
123122
stopRotation();
124123
}
125-
}, [animating, fade, hidesWhenStopped, startRotation, scale, timer]);
124+
}, [animating, fade, hidesWhenStopped, startRotation, getDuration, timer]);
126125

127126
const color = indicatorColor || theme.colors?.primary;
128127
const size =

‎src/components/Badge.tsx‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StyleSheet } from 'react-native';
44
import Animated, { type AnimatedStyle } from 'react-native-reanimated';
55

66
import { useInternalTheme } from '../core/theming';
7+
import { useReduceMotion } from '../theme/accessibility/ReduceMotionContext';
78
import { cornerFull } from '../theme/tokens/sys/shape';
89
import type { ThemeProp } from '../theme/types';
910

@@ -56,10 +57,7 @@ const Badge = ({
5657
...rest
5758
}: Props) => {
5859
const theme = useInternalTheme(themeOverrides);
59-
60-
const {
61-
animation: { scale },
62-
} = theme;
60+
const reduceMotion = useReduceMotion();
6361

6462
const textColor = theme.colors.onError;
6563

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

7068
const transitionStyle = {
7169
opacity: visible ? 1 : 0,
72-
transitionDuration: 150 * scale,
70+
transitionDuration: reduceMotion ? 0 : 150,
7371
transitionProperty: 'opacity',
7472
};
7573

‎src/components/Banner.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Surface from './Surface';
2626
import type { SurfaceStyle } from './Surface';
2727
import Text from './Typography/Text';
2828
import { useInternalTheme } from '../core/theming';
29+
import { useMotionDuration } from '../theme/accessibility/ReduceMotionContext';
2930
import type { Elevation, ThemeProp } from '../theme/types';
3031

3132
const DEFAULT_MAX_WIDTH = 960;
@@ -165,22 +166,21 @@ const Banner = ({
165166
const showCallback = useLatestCallback(onShowAnimationFinished);
166167
const hideCallback = useLatestCallback(onHideAnimationFinished);
167168

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

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

174174
position.value = withTiming(
175175
visible ? 1 : 0,
176176
{
177-
duration: animationDuration,
177+
duration: getDuration(visible ? 250 : 200),
178178
easing: Easing.inOut(Easing.ease),
179179
reduceMotion: ReduceMotion.Never,
180180
},
181181
(finished) => scheduleOnRN(callback, { finished: finished ?? false })
182182
);
183-
}, [animationDuration, hideCallback, position, showCallback, visible]);
183+
}, [getDuration, hideCallback, position, showCallback, visible]);
184184

185185
const surfaceStyle = useAnimatedStyle(() => ({
186186
opacity: interpolate(position.value, [0, 0.1, 1], [0, 1, 1]),

‎src/components/BottomNavigation/BottomNavigation.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import useLatestCallback from 'use-latest-callback';
1313
import BottomNavigationBar from './BottomNavigationBar';
1414
import BottomNavigationRouteScreen from './BottomNavigationRouteScreen';
1515
import { useInternalTheme } from '../../core/theming';
16+
import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext';
1617
import type { ThemeProp } from '../../theme/types';
1718
import useAnimatedValueArray from '../../utils/useAnimatedValueArray';
1819
import type { IconSource } from '../Icon';
@@ -342,7 +343,7 @@ const BottomNavigation = <Route extends BaseRoute>({
342343
getLazy = ({ route }: { route: Route }) => route.lazy,
343344
}: Props<Route>) => {
344345
const theme = useInternalTheme(themeOverrides);
345-
const { scale } = theme.animation;
346+
const getDuration = useMotionDuration();
346347
const compact = compactProp ?? false;
347348
let shifting = shiftingProp ?? false;
348349

@@ -393,7 +394,7 @@ const BottomNavigation = <Route extends BaseRoute>({
393394
...navigationState.routes.map((_, i) =>
394395
Animated.timing(tabsPositionAnims[i], {
395396
toValue: i === index ? 0 : i >= index ? 1 : -1,
396-
duration: 150 * scale,
397+
duration: getDuration(150),
397398
useNativeDriver: true,
398399
easing: sceneAnimationEasing,
399400
})
@@ -415,7 +416,7 @@ const BottomNavigation = <Route extends BaseRoute>({
415416
[
416417
navigationState.routes,
417418
offsetsAnims,
418-
scale,
419+
getDuration,
419420
tabsPositionAnims,
420421
sceneAnimationEasing,
421422
]

‎src/components/BottomNavigation/BottomNavigationBar.tsx‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
getLabelColor,
1717
} from './utils';
1818
import { useInternalTheme } from '../../core/theming';
19+
import { useMotionDuration } from '../../theme/accessibility/ReduceMotionContext';
1920
import type { ThemeProp } from '../../theme/types';
2021
import useAnimatedValue from '../../utils/useAnimatedValue';
2122
import useAnimatedValueArray from '../../utils/useAnimatedValueArray';
@@ -334,7 +335,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
334335
const theme = useInternalTheme(themeOverrides);
335336
const { colors } = theme;
336337
const { bottom, left, right } = useSafeAreaInsets();
337-
const { scale } = theme.animation;
338+
const getDuration = useMotionDuration();
338339
const compact = compactProp ?? false;
339340
let shifting = shiftingProp ?? false;
340341

@@ -374,28 +375,28 @@ const BottomNavigationBar = <Route extends BaseRoute>({
374375
setKeyboardVisible(true);
375376
Animated.timing(visibleAnim, {
376377
toValue: 0,
377-
duration: 150 * scale,
378+
duration: getDuration(150),
378379
useNativeDriver: true,
379380
}).start();
380-
}, [scale, visibleAnim]);
381+
}, [getDuration, visibleAnim]);
381382

382383
const handleKeyboardHide = React.useCallback(() => {
383384
Animated.timing(visibleAnim, {
384385
toValue: 1,
385-
duration: 100 * scale,
386+
duration: getDuration(100),
386387
useNativeDriver: true,
387388
}).start(() => {
388389
setKeyboardVisible(false);
389390
});
390-
}, [scale, visibleAnim]);
391+
}, [getDuration, visibleAnim]);
391392

392393
const animateToIndex = React.useCallback(
393394
(index: number) => {
394395
Animated.parallel(
395396
navigationState.routes.map((_, i) =>
396397
Animated.timing(tabsAnims[i], {
397398
toValue: i === index ? 1 : 0,
398-
duration: 150 * scale,
399+
duration: getDuration(150),
399400
useNativeDriver: true,
400401
easing: animationEasing,
401402
})
@@ -405,7 +406,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
405406
tabsAnims.map((tab, i) => tab.setValue(i === index ? 1 : 0));
406407
});
407408
},
408-
[scale, navigationState.routes, tabsAnims, animationEasing]
409+
[getDuration, navigationState.routes, tabsAnims, animationEasing]
409410
);
410411

411412
React.useEffect(() => {

‎src/components/Button/Button.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ export type Props = Omit<ViewProps, 'style'> & {
8686
* `false` the shape still changes, it just snaps instead of animating, and
8787
* there is no press morph at all.
8888
*
89-
* The press morph is skipped when the OS reduce-motion setting is on.
89+
* Under reduce motion the shape behaves as with `false`. Reduce motion
90+
* comes from the `reduceMotion` prop of `PaperProvider`, which follows the
91+
* OS setting by default.
9092
*/
9193
animateShape?: boolean;
9294
/**
@@ -348,9 +350,7 @@ const Button = ({
348350
() => toSpringCurve(theme.motion.spring.fast.spatial),
349351
[theme.motion.spring.fast.spatial]
350352
);
351-
const shapeTransitionDuration = animatesShape
352-
? shapeSpring.duration * theme.animation.scale
353-
: 0;
353+
const shapeTransitionDuration = animatesShape ? shapeSpring.duration : 0;
354354

355355
const tracksPress = animatesShape || isElevationEntitled;
356356

@@ -422,6 +422,7 @@ const Button = ({
422422
const surfaceTransitionDuration = getButtonTransitionDuration({
423423
theme,
424424
pressed,
425+
reduceMotion,
425426
containerColor,
426427
previousContainerColor: committedContainerColor,
427428
});

0 commit comments

Comments
 (0)