-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SpacerView.js
49 lines (42 loc) · 1.78 KB
/
SpacerView.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
import React from 'react';
import Animated, {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import PropTypes from 'prop-types';
import * as StyleUtils from '../styles/StyleUtils';
import stylePropTypes from '../styles/stylePropTypes';
import CONST from '../CONST';
const propTypes = {
/**
* Should we show the spacer
*/
shouldShow: PropTypes.bool.isRequired,
/**
* Array of style objects
* @default []
*/
style: stylePropTypes,
};
const defaultProps = {
style: [],
};
function SpacerView({shouldShow = true, style = []}) {
const marginVertical = useSharedValue(CONST.HORIZONTAL_SPACER.DEFAULT_MARGIN_VERTICAL);
const borderBottomWidth = useSharedValue(CONST.HORIZONTAL_SPACER.DEFAULT_BORDER_BOTTOM_WIDTH);
const animatedStyles = useAnimatedStyle(() => ({
marginVertical: marginVertical.value,
borderBottomWidth: borderBottomWidth.value,
}));
React.useEffect(() => {
const duration = CONST.ANIMATED_TRANSITION;
const values = {
marginVertical: shouldShow ? CONST.HORIZONTAL_SPACER.DEFAULT_MARGIN_VERTICAL : CONST.HORIZONTAL_SPACER.HIDDEN_MARGIN_VERTICAL,
borderBottomWidth: shouldShow ? CONST.HORIZONTAL_SPACER.DEFAULT_BORDER_BOTTOM_WIDTH : CONST.HORIZONTAL_SPACER.HIDDEN_BORDER_BOTTOM_WIDTH,
};
marginVertical.value = withTiming(values.marginVertical, {duration});
borderBottomWidth.value = withTiming(values.borderBottomWidth, {duration});
}, [shouldShow, borderBottomWidth, marginVertical]);
return <Animated.View style={[animatedStyles, ...StyleUtils.parseStyleAsArray(style)]} />;
}
SpacerView.displayName = 'SpacerView';
SpacerView.propTypes = propTypes;
SpacerView.defaultProps = defaultProps;
export default SpacerView;