-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathOpacityView.js
62 lines (53 loc) · 1.64 KB
/
OpacityView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from 'react';
import {View} from 'react-native';
import Animated, {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import PropTypes from 'prop-types';
import * as StyleUtils from '../styles/StyleUtils';
import variables from '../styles/variables';
const propTypes = {
/**
* Should we dim the view
*/
shouldDim: PropTypes.bool.isRequired,
/**
* Content to render
*/
children: PropTypes.node.isRequired,
/**
* Array of style objects
* @default []
*/
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/**
* The value to use for the opacity when the view is dimmed
* @default 0.5
*/
dimmingValue: PropTypes.number,
};
const defaultProps = {
style: [],
dimmingValue: variables.hoverDimValue,
};
const OpacityView = (props) => {
const opacity = useSharedValue(1);
const opacityStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));
React.useEffect(() => {
if (props.shouldDim) {
opacity.value = withTiming(props.dimmingValue, {duration: 50});
} else {
opacity.value = withTiming(1, {duration: 50});
}
}, [props.shouldDim, props.dimmingValue, opacity]);
return (
<Animated.View style={[opacityStyle]}>
<View style={StyleUtils.parseStyleAsArray(props.style)}>{props.children}</View>
</Animated.View>
);
};
OpacityView.displayName = 'OpacityView';
OpacityView.propTypes = propTypes;
OpacityView.defaultProps = defaultProps;
export default OpacityView;