Skip to content

Commit faf6a53

Browse files
authored
feat: automatically handle reduce motion device settings (#1937)
1 parent bf2d7f3 commit faf6a53

File tree

8 files changed

+71
-29
lines changed

8 files changed

+71
-29
lines changed

docs/pages/2.theming.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ A theme usually contains the following properties:
6969
- `medium`
7070
- `light`
7171
- `thin`
72+
- `animation` (`object`)
73+
- `scale` - scale for all animations
7274

7375
When creating a custom theme, you will need to provide all of these properties.
7476

77+
If you don't use custom theme, Paper will automatically turn animations on/off, depending on device settings.
78+
79+
Otherwise, your custom theme will need to handle it manually, using React Native's [AccessibilityInfo API](https://reactnative.dev/docs/0.54/accessibilityinfo);
80+
7581
## Applying a theme to a paper component
7682

7783
If you want to change the theme for a certain component from the library, you can directly pass the `theme` prop to the component. The theme passed as the prop is merged with the theme from the `Provider`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@types/jest": "^24.0.13",
6161
"@types/node": "^13.1.0",
6262
"@types/react-dom": "^16.8.4",
63-
"@types/react-native": "^0.57.61",
63+
"@types/react-native": "^0.61.4",
6464
"@types/react-native-vector-icons": "^6.4.1",
6565
"@typescript-eslint/eslint-plugin": "^2.12.0",
6666
"@typescript-eslint/parser": "^2.12.0",

src/components/Appbar/AppbarHeader.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class AppbarHeader extends React.Component<Props> {
119119
}
120120
>
121121
<Appbar
122+
//@ts-ignore
122123
style={[
123124
//@ts-ignore
124125
{ height, backgroundColor, marginTop: statusBarHeight },

src/components/Chip.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,16 @@ class Chip extends React.Component<Props, State> {
209209
: selectedBackgroundColor;
210210

211211
const accessibilityTraits: AccessibilityTrait[] = ['button'];
212-
const accessibilityStates: AccessibilityState[] = [];
212+
const accessibilityState: AccessibilityState = {};
213213

214214
if (selected) {
215215
accessibilityTraits.push('selected');
216-
accessibilityStates.push('selected');
216+
accessibilityState.selected = true;
217217
}
218218

219219
if (disabled) {
220220
accessibilityTraits.push('disabled');
221-
accessibilityStates.push('disabled');
221+
accessibilityState.disabled = true;
222222
}
223223

224224
return (
@@ -253,7 +253,7 @@ class Chip extends React.Component<Props, State> {
253253
accessibilityTraits={accessibilityTraits}
254254
accessibilityComponentType="button"
255255
accessibilityRole="button"
256-
accessibilityStates={accessibilityStates}
256+
accessibilityState={accessibilityState}
257257
testID={testID}
258258
>
259259
<View style={styles.content}>

src/components/__tests__/__snapshots__/Chip.test.js.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`renders chip with close button 1`] = `
1515
>
1616
<View
1717
accessibilityRole="button"
18-
accessibilityStates={Array []}
18+
accessibilityState={Object {}}
1919
accessible={true}
2020
focusable={false}
2121
isTVSelectable={true}
@@ -193,7 +193,7 @@ exports[`renders chip with icon 1`] = `
193193
>
194194
<View
195195
accessibilityRole="button"
196-
accessibilityStates={Array []}
196+
accessibilityState={Object {}}
197197
accessible={true}
198198
focusable={false}
199199
isTVSelectable={true}
@@ -320,7 +320,7 @@ exports[`renders chip with onPress 1`] = `
320320
>
321321
<View
322322
accessibilityRole="button"
323-
accessibilityStates={Array []}
323+
accessibilityState={Object {}}
324324
accessible={true}
325325
focusable={true}
326326
isTVSelectable={true}
@@ -402,10 +402,10 @@ exports[`renders outlined disabled chip 1`] = `
402402
>
403403
<View
404404
accessibilityRole="button"
405-
accessibilityStates={
406-
Array [
407-
"disabled",
408-
]
405+
accessibilityState={
406+
Object {
407+
"disabled": true,
408+
}
409409
}
410410
accessible={true}
411411
focusable={false}
@@ -488,10 +488,10 @@ exports[`renders selected chip 1`] = `
488488
>
489489
<View
490490
accessibilityRole="button"
491-
accessibilityStates={
492-
Array [
493-
"selected",
494-
]
491+
accessibilityState={
492+
Object {
493+
"selected": true,
494+
}
495495
}
496496
accessible={true}
497497
focusable={false}

src/components/__tests__/__snapshots__/ListItem.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ exports[`renders list item with custom description 1`] = `
9696
>
9797
<View
9898
accessibilityRole="button"
99-
accessibilityStates={Array []}
99+
accessibilityState={Object {}}
100100
accessible={true}
101101
focusable={true}
102102
isTVSelectable={true}

src/core/Provider.tsx

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
11
import * as React from 'react';
2+
import { AccessibilityInfo } from 'react-native';
23
import { ThemeProvider } from './theming';
34
import { Provider as SettingsProvider, Settings } from './settings';
45
import MaterialCommunityIcon from '../components/MaterialCommunityIcon';
56
import PortalHost from '../components/Portal/PortalHost';
67
import { Theme } from '../types';
8+
import DefaultTheme from '../styles/DefaultTheme';
79

810
type Props = {
911
children: React.ReactNode;
1012
theme?: Theme;
1113
settings?: Settings;
1214
};
13-
1415
export default class Provider extends React.Component<Props> {
16+
state = {
17+
reduceMotionEnabled: false,
18+
};
19+
20+
async componentDidMount() {
21+
AccessibilityInfo.addEventListener(
22+
'reduceMotionChanged',
23+
this.updateReduceMotionSettingsInfo
24+
);
25+
this.updateReduceMotionSettingsInfo();
26+
}
27+
28+
componentWillUnmount() {
29+
AccessibilityInfo.removeEventListener(
30+
'reduceMotionChanged',
31+
this.updateReduceMotionSettingsInfo
32+
);
33+
}
34+
35+
private updateReduceMotionSettingsInfo = async () => {
36+
try {
37+
const reduceMotionEnabled = await AccessibilityInfo.isReduceMotionEnabled();
38+
39+
this.setState({ reduceMotionEnabled });
40+
} catch (err) {
41+
console.warn(err);
42+
}
43+
};
44+
1545
render() {
46+
const { children, settings, theme: providedTheme } = this.props;
47+
const { reduceMotionEnabled } = this.state;
48+
const theme = !providedTheme
49+
? Object.assign(DefaultTheme, {
50+
animation: {
51+
scale: reduceMotionEnabled ? 0 : 1,
52+
},
53+
})
54+
: providedTheme;
55+
1656
return (
1757
<PortalHost>
18-
<SettingsProvider
19-
value={this.props.settings || { icon: MaterialCommunityIcon }}
20-
>
21-
<ThemeProvider theme={this.props.theme}>
22-
{this.props.children}
23-
</ThemeProvider>
58+
<SettingsProvider value={settings || { icon: MaterialCommunityIcon }}>
59+
<ThemeProvider theme={theme}>{children}</ThemeProvider>
2460
</SettingsProvider>
2561
</PortalHost>
2662
);

yarn.lock

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,12 +2566,11 @@
25662566
"@types/prop-types" "*"
25672567
"@types/react" "*"
25682568

2569-
"@types/react-native@^0.57.61":
2570-
version "0.57.65"
2571-
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.57.65.tgz#9da4773aaa95924bce42a54a5c19cfd8ffd5022b"
2572-
integrity sha512-7P5ulTb+/cnwbABWaAjzKmSYkRWeK7UCTfUwHhDpnwxdiL2X/KbdN1sPgo0B2E4zxfYE3MEoHv7FhB8Acfvf8A==
2569+
"@types/react-native@^0.61.4":
2570+
version "0.61.23"
2571+
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.61.23.tgz#bff4e0311c229a5203eb37aacd4febf59f3e2980"
2572+
integrity sha512-upHmySsrVBDBokWWhYIKkKnpvadsHdioSjbBTu4xl7fjN0yb94KR5ngUOBXsyqAYqQzF+hP6qpvobG9M7Jr6hw==
25732573
dependencies:
2574-
"@types/prop-types" "*"
25752574
"@types/react" "*"
25762575

25772576
"@types/react@*":

0 commit comments

Comments
 (0)