This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
Toast.js
121 lines (108 loc) · 2.71 KB
/
Toast.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
// @flow strict
import * as React from 'react';
import { Animated, View } from 'react-native';
import { defaultTokens } from '@kiwicom/mobile-orbit';
import {
FlingGestureHandler,
Directions,
State as GestureState,
} from 'react-native-gesture-handler';
import type { TranslationType } from '../../types/Translation';
import AdaptableBadge from '../AdaptableBadge';
import StyleSheet from '../PlatformStyleSheet';
import type { StylePropType } from '../../types/Styles';
type Props = {|
+text: TranslationType,
+duration: number,
+style?: StylePropType,
+onHide?: () => void,
|};
type State = {|
+isVisible: boolean,
|};
type HandlerChangeEvent = {
+nativeEvent: {
+state: number,
...
},
...
};
export default class Toast extends React.Component<Props, State> {
translateY = new Animated.Value(-250);
state = {
isVisible: false,
};
timeout: ?TimeoutID;
static defaultProps = {
duration: 3000,
};
show = () => {
this.setState({ isVisible: true }, () => {
Animated.timing(this.translateY, {
toValue: 0,
duration: 250,
useNativeDriver: true,
}).start(() => {
this.timeout = setTimeout(this.hide, this.props.duration);
});
});
};
hide = () => {
Animated.timing(this.translateY, {
toValue: -250,
duration: 250,
useNativeDriver: true,
}).start(() => {
this.setState({ isVisible: false }, () => {
if (this.props.onHide != null) {
this.props.onHide();
}
if (this.timeout != null) {
clearTimeout(this.timeout);
this.timeout = null;
}
});
});
};
onHandlerStateChange = ({ nativeEvent }: HandlerChangeEvent) => {
if (nativeEvent.state === GestureState.ACTIVE) {
this.hide();
}
};
render() {
if (this.state.isVisible === false) {
return null;
}
return (
<FlingGestureHandler
onHandlerStateChange={this.onHandlerStateChange}
direction={Directions.UP}
>
{/* $FlowFixMe Errors after moving rn modules from untyped to
* declarations */}
<View style={[styles.toast, this.props.style]} testID="toast">
<Animated.View
style={{
transform: [{ translateY: this.translateY }],
}}
>
<AdaptableBadge type="info" style={styles.badge} translation={this.props.text} />
</Animated.View>
</View>
</FlingGestureHandler>
);
}
}
const styles = StyleSheet.create({
toast: {
position: 'absolute',
top: 0,
start: 0,
display: 'flex',
zIndex: parseInt(defaultTokens.zIndexOnTheTop, 10),
},
badge: {
borderTopStartRadius: 0,
borderTopEndRadius: 0,
},
});