-
Notifications
You must be signed in to change notification settings - Fork 692
/
index.js
179 lines (158 loc) · 4.77 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import PropTypes from 'prop-types';
import React from 'react';
import {StyleSheet} from 'react-native';
import {View as AnimatableView} from 'react-native-animatable';
import {Constants, Colors, Spacings, BaseComponent, View, Image, Button} from 'react-native-ui-lib';
const SHOW_ANIMATION_DELAY = 350;
const SHOW_ANIMATION_DURATION = 180;
const HIDE_ANIMATION_DURATION = 150;
const gradientImage = () => require('./gradient.png');
/**
* @description: Hovering button with gradient background
* @modifiers: margin, background, color
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/FloatingButtonScreen.js
* @extends: Button
* @extendsLink: https://github.com/wix/react-native-ui-lib/blob/master/src/components/button/index.js
*/
class FloatingButton extends BaseComponent {
static displayName = 'FloatingButton';
static propTypes = {
/**
* Whether the button is visible
*/
visible: PropTypes.bool,
/**
* Button element (all Button's component's props)
*/
button: PropTypes.shape(Button.propTypes),
/**
* Secondary button element (all Button's component's props)
*/
secondaryButton: PropTypes.shape(Button.propTypes),
/**
* The bottom margin of the button, or secondary button if passed
*/
bottomMargin: PropTypes.number,
/**
* The duration of the button's animations (show/hide)
*/
duration: PropTypes.number,
/**
* Whether to show/hide the button without animation
*/
withoutAnimation: PropTypes.bool,
/**
* Whether to show background overlay
*/
hideBackgroundOverlay: PropTypes.bool
};
constructor(props) {
super(props);
this.state = {
shouldAnimateHide: false,
isVisible: props.visible,
animating: false
};
}
UNSAFE_componentWillReceiveProps(nextProps) {
const {withoutAnimation} = this.props;
const propsVisible = this.props.visible;
const nextVisible = nextProps.visible;
if (!withoutAnimation) {
const shouldStartAnimation = !this.state.isVisible && nextVisible && !this.state.animating;
if (shouldStartAnimation) {
this.setState({animating: true});
if (nextProps.duration) {
setTimeout(() => {
this.setState({isVisible: false, shouldAnimateHide: true});
}, nextProps.duration);
}
}
}
this.setState({
shouldAnimateHide: withoutAnimation ? false : !nextVisible && propsVisible,
isVisible: nextVisible
});
}
onAnimationEnd = () => {
this.setState({animating: false});
};
renderButton() {
const {bottomMargin, button, secondaryButton} = this.getThemeProps();
const bottom = secondaryButton ? Spacings.s4 : bottomMargin || Spacings.s8;
return (
<Button
size={'large'}
style={[styles.shadow, {marginTop: 16, marginBottom: bottom}]}
{...button}
/>
);
}
renderOverlay = () => {
if (!this.props.hideBackgroundOverlay) {
return (
<View pointerEvents={'none'} style={styles.image}>
<Image style={styles.image} source={gradientImage()} resizeMode={'stretch'}/>
</View>
);
}
}
renderSecondaryButton() {
const {secondaryButton, bottomMargin} = this.getThemeProps();
return (
<Button
link
size={'large'}
{...secondaryButton}
style={{marginBottom: bottomMargin || Spacings.s7}}
enableShadow={false}
/>
);
}
render() {
const {withoutAnimation, secondaryButton} = this.props;
const {isVisible, shouldAnimateHide} = this.state;
const Container = !withoutAnimation ? AnimatableView : View;
// NOTE: Don't show if it should not be visible and it was already animated
if (!isVisible && !shouldAnimateHide) {
return false;
}
return (
<Container
pointerEvents="box-none"
style={[styles.animatedContainer, Constants.isAndroid && {zIndex: 99}]}
animation={!isVisible ? 'fadeOutDown' : 'fadeInUp'}
duration={SHOW_ANIMATION_DURATION}
delay={!isVisible ? HIDE_ANIMATION_DURATION : SHOW_ANIMATION_DELAY}
easing={'ease-out'}
onAnimationEnd={this.onAnimationEnd}
>
{this.renderOverlay()}
{this.renderButton()}
{secondaryButton && this.renderSecondaryButton()}
</Container>
);
}
}
const styles = StyleSheet.create({
animatedContainer: {
position: 'absolute',
left: 0,
right: 0,
alignItems: 'center',
bottom: 0
},
image: {
...StyleSheet.absoluteFillObject,
width: '100%',
height: '100%'
},
shadow: {
shadowColor: Colors.dark40,
shadowOffset: {height: 5, width: 0},
shadowOpacity: 0.35,
shadowRadius: 12,
elevation: 2
}
});
export default FloatingButton;