forked from sidevesh/react-native-snackbar-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
231 lines (219 loc) · 6.02 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
Text,
Animated,
Easing,
ViewPropTypes,
} from 'react-native';
import { Touchable } from './src';
/* Values are from https://material.io/guidelines/motion/duration-easing.html#duration-easing-dynamic-durations */
const easingValues = {
entry: Easing.bezier(0.0, 0.0, 0.2, 1),
exit: Easing.bezier(0.4, 0.0, 1, 1),
};
const durationValues = {
entry: 225,
exit: 195,
};
class SnackbarComponent extends Component {
constructor(props) {
super(props);
this.state = {
translateValue: new Animated.Value(0),
hideDistance: 9999,
};
}
render() {
return (
<Animated.View
style={[
styles.limitContainer,
{
height: this.state.translateValue.interpolate({
inputRange: [0, 1],
outputRange: [0, this.state.hideDistance],
}),
},
this.props.position === 'top'
? { top: this.props.top }
: { bottom: this.props.bottom },
]}
>
<Animated.View
style={[
this.props.containerStyle,
styles.container,
{
backgroundColor: this.props.backgroundColor,
left: this.props.left,
right: this.props.right,
},
{
[this.props.position]: this.state.translateValue.interpolate({
inputRange: [0, 1],
outputRange: [this.state.hideDistance * -1, 0],
}),
},
]}
onLayout={event => this.setState({ hideDistance: event.nativeEvent.layout.height })}
>
{typeof this.props.textMessage === 'function'
? this.props.textMessage()
: (
<Text
style={[
this.props.messageStyle,
styles.textMessage,
{ color: this.props.messageColor },
]}
>
{this.props.textMessage}
</Text>
)
}
{this.props.actionHandler !== null && !!this.props.actionText
? (
<Touchable onPress={this.props.actionHandler}>
<Text
style={[
this.props.actionStyle,
styles.actionText,
{ color: this.props.accentColor },
]}
>
{this.props.actionText.toUpperCase()}
</Text>
</Touchable>
)
: null
}
</Animated.View>
</Animated.View>
);
}
componentDidMount() {
if (this.props.visible) {
this.state.translateValue.setValue(1);
} else {
this.state.translateValue.setValue(0);
}
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.visible && !this.props.visible) {
Animated.timing(this.state.translateValue, {
duration: durationValues.entry,
toValue: 1,
easing: easingValues.entry,
useNativeDriver: false
}).start();
if (nextProps.autoHidingTime) {
const hideFunc = this.hideSnackbar.bind(this);
setTimeout(hideFunc, nextProps.autoHidingTime);
}
} else if (!nextProps.visible && this.props.visible) {
this.hideSnackbar();
}
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillUpdate(nextProps, nextState) {
if (
this.props.distanceCallback !== null
&& (
nextProps.visible !== this.props.visible
|| nextState.hideDistance !== this.state.hideDistance
)
) {
if (nextProps.visible) {
this.props.distanceCallback(nextState.hideDistance + this.props[this.props.position]);
} else {
this.props.distanceCallback(this.props[this.props.position]);
}
}
}
/**
* Starting the animation to hide the snackbar.
* @return {null} No return.
*/
hideSnackbar() {
Animated.timing(this.state.translateValue, {
duration: durationValues.exit,
toValue: 0,
easing: easingValues.exit,
useNativeDriver: false
}).start();
}
}
SnackbarComponent.defaultProps = {
accentColor: 'orange',
messageColor: '#FFFFFF',
backgroundColor: '#484848',
distanceCallback: null,
actionHandler: null,
left: 0,
right: 0,
top: 0,
bottom: 0,
visible: false,
position: 'bottom',
actionText: '',
textMessage: '',
autoHidingTime: 0, // Default value will not auto hide the snack bar as the old version.
containerStyle: {},
messageStyle: {},
actionStyle: {},
};
SnackbarComponent.propTypes = {
accentColor: PropTypes.string,
messageColor: PropTypes.string,
backgroundColor: PropTypes.string,
distanceCallback: PropTypes.func,
actionHandler: PropTypes.func,
left: PropTypes.number,
right: PropTypes.number,
top: PropTypes.number,
bottom: PropTypes.number,
visible: PropTypes.bool,
actionText: PropTypes.string,
textMessage: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
position: PropTypes.oneOf(['bottom', 'top']), // bottom (default), top
// eslint-disable-next-line react/no-unused-prop-types
autoHidingTime: PropTypes.number, // How long (in milliseconds) the snack bar will be hidden.
containerStyle: ViewPropTypes.style,
messageStyle: Text.propTypes.style,
actionStyle: Text.propTypes.style,
};
const styles = StyleSheet.create({
limitContainer: {
position: 'absolute',
overflow: 'hidden',
left: 0,
right: 0,
zIndex: 9999,
backgroundColor: 'rgba(0, 0, 0, 0)',
},
container: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
position: 'absolute',
},
textMessage: {
fontSize: 14,
flex: 1,
textAlign: 'left',
paddingStart: 20,
paddingTop: 14,
paddingBottom: 14,
},
actionText: {
fontSize: 14,
fontWeight: '600',
paddingEnd: 20,
paddingTop: 14,
paddingBottom: 14,
},
});
export default SnackbarComponent;