UNPKG

2.74 kBJavaScriptView Raw
1import * as React from 'react';
2import { AccessibilityInfo, Appearance } from 'react-native';
3import { ThemeProvider } from './theming';
4import { Provider as SettingsProvider } from './settings';
5import MaterialCommunityIcon from '../components/MaterialCommunityIcon';
6import PortalHost from '../components/Portal/PortalHost';
7import DefaultTheme from '../styles/DefaultTheme';
8import DarkTheme from '../styles/DarkTheme';
9
10const Provider = ({ ...props
11}) => {
12 const colorSchemeName = !props.theme && (Appearance === null || Appearance === void 0 ? void 0 : Appearance.getColorScheme()) || 'light';
13 const [reduceMotionEnabled, setReduceMotionEnabled] = React.useState(false);
14 const [colorScheme, setColorScheme] = React.useState(colorSchemeName);
15
16 const handleAppearanceChange = preferences => {
17 const {
18 colorScheme
19 } = preferences;
20 setColorScheme(colorScheme);
21 };
22
23 React.useEffect(() => {
24 let subscription;
25
26 if (!props.theme) {
27 subscription = AccessibilityInfo.addEventListener('reduceMotionChanged', setReduceMotionEnabled);
28 }
29
30 return () => {
31 if (!props.theme) {
32 var _subscription;
33
34 if ((_subscription = subscription) !== null && _subscription !== void 0 && _subscription.remove) {
35 subscription.remove();
36 } else {
37 AccessibilityInfo.removeEventListener('reduceMotionChanged', setReduceMotionEnabled);
38 }
39 }
40 };
41 }, [props.theme]);
42 React.useEffect(() => {
43 let appearanceSubscription;
44
45 if (!props.theme) {
46 appearanceSubscription = Appearance === null || Appearance === void 0 ? void 0 : Appearance.addChangeListener(handleAppearanceChange);
47 }
48
49 return () => {
50 if (!props.theme) {
51 if (appearanceSubscription) {
52 appearanceSubscription.remove();
53 } else {
54 Appearance === null || Appearance === void 0 ? void 0 : Appearance.removeChangeListener(handleAppearanceChange);
55 }
56 }
57 };
58 }, [props.theme]);
59
60 const getTheme = () => {
61 const {
62 theme: providedTheme
63 } = props;
64
65 if (providedTheme) {
66 return providedTheme;
67 } else {
68 const theme = colorScheme === 'dark' ? DarkTheme : DefaultTheme;
69 return { ...theme,
70 animation: { ...theme.animation,
71 scale: reduceMotionEnabled ? 0 : 1
72 }
73 };
74 }
75 };
76
77 const {
78 children,
79 settings
80 } = props;
81 return /*#__PURE__*/React.createElement(PortalHost, null, /*#__PURE__*/React.createElement(SettingsProvider, {
82 value: settings || {
83 icon: MaterialCommunityIcon
84 }
85 }, /*#__PURE__*/React.createElement(ThemeProvider, {
86 theme: getTheme()
87 }, children)));
88};
89
90export default Provider;
91//# sourceMappingURL=Provider.js.map
\No newline at end of file