@fto-consult/expo-ui 2.18.6 → 2.19.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,249 @@
1
+ // Copyright 2022 @fto-consult/Boris Fouomene. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ import React from '$react';
6
+ import {
7
+ Animated,
8
+ Easing,
9
+ StyleSheet,
10
+ View,
11
+ } from 'react-native';
12
+ import theme from "$theme";
13
+ import {defaultObj,isObj,defaultStr} from "$utils";
14
+ import { useSafeAreaInsets } from 'react-native-safe-area-context';
15
+ import useEventCallback from 'use-event-callback';
16
+ import PropTypes from "prop-types";
17
+ import Surface from '$ecomponents/Surface';
18
+ import Label from "$ecomponents/Label"
19
+ import Button from "$components/Button";
20
+
21
+ const DURATION_SHORT = 4000;
22
+ const DURATION_MEDIUM = 7000;
23
+ const DURATION_LONG = 10000;
24
+
25
+ const Snackbar = ({
26
+ visible,
27
+ action,
28
+ icon,
29
+ duration = DURATION_MEDIUM,
30
+ onDismiss,
31
+ children,
32
+ labelProps,
33
+ elevation = 2,
34
+ containerProps,
35
+ style,
36
+ testID,
37
+ contentProps,
38
+ ...rest
39
+ }) => {
40
+ labelProps = defaultObj(labelProps);
41
+ containerProps = defaultObj(containerProps);
42
+ contentProps = defaultObj(contentProps);
43
+ labelProps = defaultObj(labelProps);
44
+ const { bottom, right, left } = useSafeAreaInsets();
45
+ testID = defaultStr(testID,"RN_SnackbarComponent")
46
+ const { current: opacity } = React.useRef(
47
+ new Animated.Value(0.0)
48
+ );
49
+ const hideTimeout = React.useRef(undefined);
50
+ const flattenStyle = StyleSheet.flatten(style) || {};
51
+ const [hidden, setHidden] = React.useState(!visible);
52
+
53
+ const scale = 1.0;
54
+
55
+ const handleOnVisible = useEventCallback(() => {
56
+ // show
57
+ if (hideTimeout.current) clearTimeout(hideTimeout.current);
58
+ setHidden(false);
59
+ Animated.timing(opacity, {
60
+ toValue: 1,
61
+ duration: 200 * scale,
62
+ easing: Easing.out(Easing.ease),
63
+ useNativeDriver: true,
64
+ }).start(({ finished }) => {
65
+ if (finished) {
66
+ const isInfinity =
67
+ duration === Number.POSITIVE_INFINITY ||
68
+ duration === Number.NEGATIVE_INFINITY;
69
+
70
+ if (!isInfinity) {
71
+ hideTimeout.current = setTimeout(
72
+ onDismiss,
73
+ duration
74
+ );
75
+ }
76
+ }
77
+ });
78
+ });
79
+
80
+ const handleOnHidden = useEventCallback(() => {
81
+ // hide
82
+ if (hideTimeout.current) {
83
+ clearTimeout(hideTimeout.current);
84
+ }
85
+
86
+ Animated.timing(opacity, {
87
+ toValue: 0,
88
+ duration: 100 * scale,
89
+ useNativeDriver: true,
90
+ }).start(({ finished }) => {
91
+ if (finished) {
92
+ setHidden(true);
93
+ }
94
+ });
95
+ });
96
+
97
+ React.useEffect(() => {
98
+ return () => {
99
+ if (hideTimeout.current) clearTimeout(hideTimeout.current);
100
+ };
101
+ }, []);
102
+
103
+ React.useLayoutEffect(() => {
104
+ if (visible) {
105
+ handleOnVisible();
106
+ } else {
107
+ handleOnHidden();
108
+ }
109
+ }, [visible, handleOnVisible, handleOnHidden]);
110
+
111
+ const roundness = theme.roundness, colors = theme.colors;
112
+ action = React.isValidElement(action)? action : isObj(action) ? <Button {...action}/> : null;
113
+ if (hidden) {
114
+ return null;
115
+ }
116
+
117
+ const containerPaddings = {
118
+ paddingBottom: bottom,
119
+ paddingHorizontal: Math.max(left, right),
120
+ };
121
+ return (
122
+ <View
123
+ testID={testID+"_Container"}
124
+ {...containerProps}
125
+ pointerEvents="box-none"
126
+ style={[styles.wrapper, containerPaddings,containerProps.style]}
127
+ >
128
+ <Surface
129
+ pointerEvents="box-none"
130
+ accessibilityLiveRegion="polite"
131
+ elevation = {elevation}
132
+ {...rest}
133
+ testID={testID}
134
+ style={[
135
+ styles.container,
136
+ {
137
+ borderRadius: roundness,
138
+ opacity: opacity,
139
+ transform: [
140
+ {
141
+ scale: visible
142
+ ? opacity.interpolate({
143
+ inputRange: [0, 1],
144
+ outputRange: [0.9, 1],
145
+ })
146
+ : 1,
147
+ },
148
+ ],
149
+ },
150
+ style
151
+ ]}
152
+ >
153
+ {<View testID={testID+"_Content"} {...contentProps} style={[styles.content,contentProps.style]}>
154
+ {typeof children =='string' || typeof children ==='string' ?
155
+ <Label {...labelProps} style={[{color:flattenStyle.color,backgroundColor:flattenStyle.backgroundColor},labelProps.style]}>
156
+ {children}
157
+ </Label> : React.isValidElement(children)? children : null}
158
+ </View>}
159
+ {React.isValidElement(action)? action : null}
160
+ </Surface>
161
+ </View>
162
+ );
163
+ };
164
+
165
+ /**
166
+ * Show the Snackbar for a short duration.
167
+ */
168
+ Snackbar.DURATION_SHORT = DURATION_SHORT;
169
+
170
+ /**
171
+ * Show the Snackbar for a medium duration.
172
+ */
173
+ Snackbar.DURATION_MEDIUM = DURATION_MEDIUM;
174
+
175
+ /**
176
+ * Show the Snackbar for a long duration.
177
+ */
178
+ Snackbar.DURATION_LONG = DURATION_LONG;
179
+
180
+ Snackbar.propTypes = {
181
+ /**
182
+ * Whether the Snackbar is currently visible.
183
+ */
184
+ visible: PropTypes.bool,
185
+ /**
186
+ * Label and press callback for the action button. It should contain the following properties:
187
+ * - `label` - Label of the action button
188
+ * - `onPress` - Callback that is called when action button is pressed.
189
+ */
190
+ action: PropTypes.oneOfType([
191
+ PropTypes.shape({
192
+ label: PropTypes.string,
193
+ }),
194
+ PropTypes.node,
195
+ PropTypes.element,
196
+ ]),
197
+ duration : PropTypes.number,
198
+ /**
199
+ * Callback called when Snackbar is dismissed. The `visible` prop needs to be updated when this is called.
200
+ */
201
+ onDismiss: PropTypes.func,
202
+ /**
203
+ * Text content of the Snackbar.
204
+ */
205
+ children: PropTypes.any,
206
+ containerProps : PropTypes.object,
207
+ contentProps : PropTypes.object,
208
+ };
209
+
210
+ const styles = StyleSheet.create({
211
+ wrapper: {
212
+ position: 'absolute',
213
+ bottom: 0,
214
+ width: '100%',
215
+ },
216
+ container: {
217
+ flexDirection: 'row',
218
+ flexWrap : "wrap",
219
+ justifyContent: 'space-between',
220
+ margin: 8,
221
+ borderRadius: 4,
222
+ minHeight: 48,
223
+ },
224
+ content: {
225
+ marginHorizontal: 16,
226
+ marginVertical: 14,
227
+ flex: 1,
228
+ },
229
+ actionsContainer: {
230
+ flexDirection: 'row',
231
+ justifyContent: 'flex-end',
232
+ alignItems: 'center',
233
+ minHeight: 48,
234
+ },
235
+ button: {
236
+ marginRight: 8,
237
+ marginLeft: 4,
238
+ },
239
+ elevation: {
240
+ elevation: 6,
241
+ },
242
+ icon: {
243
+ width: 40,
244
+ height: 40,
245
+ margin: 0,
246
+ },
247
+ });
248
+
249
+ export default theme.withStyles(Snackbar,"SnackbarComponent");