@mehul214/react-native-flash-toast 0.0.0-stage → 1.0.1
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.
- package/LICENSE +22 -0
- package/README.md +251 -2
- package/dist/index.d.mts +240 -0
- package/dist/index.d.ts +242 -0
- package/dist/index.js +749 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +730 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +87 -4
- package/src/FlashMessage.tsx +436 -0
- package/src/FlashMessageManager.ts +71 -0
- package/src/FlashMessageProvider.tsx +48 -0
- package/src/hooks/useFlashMessage.ts +2 -0
- package/src/icons.tsx +154 -0
- package/src/index.ts +31 -0
- package/src/styles.ts +145 -0
- package/src/types.ts +101 -0
- package/src/utils/safeArea.ts +45 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var reactNative = require('react-native');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
12
|
+
|
|
13
|
+
// src/FlashMessage.tsx
|
|
14
|
+
|
|
15
|
+
// src/FlashMessageManager.ts
|
|
16
|
+
var FlashMessageManager = class {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.defaultInstance = null;
|
|
19
|
+
this.instances = /* @__PURE__ */ new Map();
|
|
20
|
+
this.pendingQueue = [];
|
|
21
|
+
}
|
|
22
|
+
register(ref, id = "default") {
|
|
23
|
+
this.instances.set(id, ref);
|
|
24
|
+
if (!this.defaultInstance || id === "default") {
|
|
25
|
+
this.defaultInstance = ref;
|
|
26
|
+
}
|
|
27
|
+
if (this.pendingQueue.length > 0) {
|
|
28
|
+
const next = this.pendingQueue.shift();
|
|
29
|
+
if (next) {
|
|
30
|
+
ref.showMessage(next);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
unregister(id = "default") {
|
|
35
|
+
this.instances.delete(id);
|
|
36
|
+
if (this.defaultInstance === this.instances.get(id)) {
|
|
37
|
+
this.defaultInstance = this.instances.values().next().value || null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
showMessage(options, targetId) {
|
|
41
|
+
const target = targetId ? this.instances.get(targetId) : this.defaultInstance;
|
|
42
|
+
if (target) {
|
|
43
|
+
target.showMessage(options);
|
|
44
|
+
} else {
|
|
45
|
+
this.pendingQueue.push(options);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
hideMessage(targetId) {
|
|
49
|
+
const target = targetId ? this.instances.get(targetId) : this.defaultInstance;
|
|
50
|
+
if (target) {
|
|
51
|
+
target.hideMessage();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var flashManager = new FlashMessageManager();
|
|
56
|
+
function showMessage(options) {
|
|
57
|
+
flashManager.showMessage(options);
|
|
58
|
+
}
|
|
59
|
+
function hideMessage() {
|
|
60
|
+
flashManager.hideMessage();
|
|
61
|
+
}
|
|
62
|
+
var flash = showMessage;
|
|
63
|
+
var dismissFlash = hideMessage;
|
|
64
|
+
var BuiltInIcon = ({
|
|
65
|
+
type,
|
|
66
|
+
size = 18,
|
|
67
|
+
color = "#FFFFFF"
|
|
68
|
+
}) => {
|
|
69
|
+
switch (type) {
|
|
70
|
+
case "success": {
|
|
71
|
+
const checkWidth = size * 0.36;
|
|
72
|
+
const checkHeight = size * 0.65;
|
|
73
|
+
const stroke = Math.max(2, size * 0.12);
|
|
74
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: [styles.center, { width: size, height: size }], children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
75
|
+
reactNative.View,
|
|
76
|
+
{
|
|
77
|
+
style: {
|
|
78
|
+
width: checkWidth,
|
|
79
|
+
height: checkHeight,
|
|
80
|
+
borderColor: color,
|
|
81
|
+
borderBottomWidth: stroke,
|
|
82
|
+
borderRightWidth: stroke,
|
|
83
|
+
borderRadius: 1,
|
|
84
|
+
transform: [{ rotate: "45deg" }, { translateY: -size * 0.08 }]
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
) });
|
|
88
|
+
}
|
|
89
|
+
case "danger": {
|
|
90
|
+
const crossThickness = Math.max(2, size * 0.12);
|
|
91
|
+
const crossLength = size * 0.72;
|
|
92
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: [styles.center, { width: size, height: size }], children: [
|
|
93
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
94
|
+
reactNative.View,
|
|
95
|
+
{
|
|
96
|
+
style: [
|
|
97
|
+
styles.crossBar,
|
|
98
|
+
{
|
|
99
|
+
width: crossLength,
|
|
100
|
+
height: crossThickness,
|
|
101
|
+
backgroundColor: color,
|
|
102
|
+
borderRadius: crossThickness / 2,
|
|
103
|
+
transform: [{ rotate: "45deg" }]
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
),
|
|
108
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
109
|
+
reactNative.View,
|
|
110
|
+
{
|
|
111
|
+
style: [
|
|
112
|
+
styles.crossBar,
|
|
113
|
+
{
|
|
114
|
+
width: crossLength,
|
|
115
|
+
height: crossThickness,
|
|
116
|
+
backgroundColor: color,
|
|
117
|
+
borderRadius: crossThickness / 2,
|
|
118
|
+
transform: [{ rotate: "-45deg" }]
|
|
119
|
+
}
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
] });
|
|
124
|
+
}
|
|
125
|
+
case "warning": {
|
|
126
|
+
const warnThickness = Math.max(2, size * 0.13);
|
|
127
|
+
const dotSize = warnThickness * 1.1;
|
|
128
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: [styles.center, { width: size, height: size }], children: [
|
|
129
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
130
|
+
reactNative.View,
|
|
131
|
+
{
|
|
132
|
+
style: {
|
|
133
|
+
width: warnThickness,
|
|
134
|
+
height: size * 0.46,
|
|
135
|
+
backgroundColor: color,
|
|
136
|
+
borderRadius: warnThickness / 2,
|
|
137
|
+
marginBottom: size * 0.14
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
),
|
|
141
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
142
|
+
reactNative.View,
|
|
143
|
+
{
|
|
144
|
+
style: {
|
|
145
|
+
width: dotSize,
|
|
146
|
+
height: dotSize,
|
|
147
|
+
borderRadius: dotSize / 2,
|
|
148
|
+
backgroundColor: color
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
] });
|
|
153
|
+
}
|
|
154
|
+
case "info": {
|
|
155
|
+
const infoThickness = Math.max(2, size * 0.13);
|
|
156
|
+
const infoDotSize = infoThickness * 1.1;
|
|
157
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: [styles.center, { width: size, height: size }], children: [
|
|
158
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
159
|
+
reactNative.View,
|
|
160
|
+
{
|
|
161
|
+
style: {
|
|
162
|
+
width: infoDotSize,
|
|
163
|
+
height: infoDotSize,
|
|
164
|
+
borderRadius: infoDotSize / 2,
|
|
165
|
+
backgroundColor: color,
|
|
166
|
+
marginBottom: size * 0.14
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
),
|
|
170
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
171
|
+
reactNative.View,
|
|
172
|
+
{
|
|
173
|
+
style: {
|
|
174
|
+
width: infoThickness,
|
|
175
|
+
height: size * 0.46,
|
|
176
|
+
backgroundColor: color,
|
|
177
|
+
borderRadius: infoThickness / 2
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
)
|
|
181
|
+
] });
|
|
182
|
+
}
|
|
183
|
+
case "default":
|
|
184
|
+
default: {
|
|
185
|
+
const defaultDot = size * 0.44;
|
|
186
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: [styles.center, { width: size, height: size }], children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
187
|
+
reactNative.View,
|
|
188
|
+
{
|
|
189
|
+
style: {
|
|
190
|
+
width: defaultDot,
|
|
191
|
+
height: defaultDot,
|
|
192
|
+
borderRadius: defaultDot / 2,
|
|
193
|
+
backgroundColor: color
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
) });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
var styles = reactNative.StyleSheet.create({
|
|
201
|
+
center: {
|
|
202
|
+
justifyContent: "center",
|
|
203
|
+
alignItems: "center"
|
|
204
|
+
},
|
|
205
|
+
crossBar: {
|
|
206
|
+
position: "absolute"
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
var COLORS = {
|
|
210
|
+
default: {
|
|
211
|
+
background: "#1F2937",
|
|
212
|
+
// Slate 800
|
|
213
|
+
text: "#FFFFFF",
|
|
214
|
+
icon: "#9CA3AF"
|
|
215
|
+
},
|
|
216
|
+
success: {
|
|
217
|
+
background: "#059669",
|
|
218
|
+
// Emerald 600
|
|
219
|
+
text: "#FFFFFF",
|
|
220
|
+
icon: "#D1FAE5"
|
|
221
|
+
},
|
|
222
|
+
danger: {
|
|
223
|
+
background: "#DC2626",
|
|
224
|
+
// Red 600
|
|
225
|
+
text: "#FFFFFF",
|
|
226
|
+
icon: "#FEE2E2"
|
|
227
|
+
},
|
|
228
|
+
warning: {
|
|
229
|
+
background: "#D97706",
|
|
230
|
+
// Amber 600
|
|
231
|
+
text: "#FFFFFF",
|
|
232
|
+
icon: "#FEF3C7"
|
|
233
|
+
},
|
|
234
|
+
info: {
|
|
235
|
+
background: "#2563EB",
|
|
236
|
+
// Blue 600
|
|
237
|
+
text: "#FFFFFF",
|
|
238
|
+
icon: "#DBEAFE"
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var styles2 = reactNative.StyleSheet.create({
|
|
242
|
+
rootOverlay: {
|
|
243
|
+
position: "absolute",
|
|
244
|
+
left: 0,
|
|
245
|
+
right: 0,
|
|
246
|
+
zIndex: 99999,
|
|
247
|
+
elevation: 99999
|
|
248
|
+
},
|
|
249
|
+
rootTop: {
|
|
250
|
+
top: 0
|
|
251
|
+
},
|
|
252
|
+
rootBottom: {
|
|
253
|
+
bottom: 0
|
|
254
|
+
},
|
|
255
|
+
rootCenter: {
|
|
256
|
+
top: 0,
|
|
257
|
+
bottom: 0,
|
|
258
|
+
justifyContent: "center",
|
|
259
|
+
alignItems: "center"
|
|
260
|
+
},
|
|
261
|
+
toastContainer: {
|
|
262
|
+
flexDirection: "row",
|
|
263
|
+
alignItems: "center",
|
|
264
|
+
overflow: "hidden",
|
|
265
|
+
...reactNative.Platform.select({
|
|
266
|
+
ios: {
|
|
267
|
+
shadowColor: "#000000",
|
|
268
|
+
shadowOffset: { width: 0, height: 6 },
|
|
269
|
+
shadowOpacity: 0.2,
|
|
270
|
+
shadowRadius: 14
|
|
271
|
+
},
|
|
272
|
+
android: {
|
|
273
|
+
elevation: 8
|
|
274
|
+
},
|
|
275
|
+
web: {
|
|
276
|
+
boxShadow: "0 10px 25px -5px rgba(0, 0, 0, 0.25), 0 8px 10px -6px rgba(0, 0, 0, 0.15)"
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
},
|
|
280
|
+
floatingContainer: {
|
|
281
|
+
marginHorizontal: 16,
|
|
282
|
+
borderRadius: 16,
|
|
283
|
+
borderWidth: 1,
|
|
284
|
+
borderColor: "rgba(255, 255, 255, 0.16)"
|
|
285
|
+
},
|
|
286
|
+
edgeToEdgeContainer: {
|
|
287
|
+
marginHorizontal: 0,
|
|
288
|
+
borderRadius: 0
|
|
289
|
+
},
|
|
290
|
+
contentWrapper: {
|
|
291
|
+
flex: 1,
|
|
292
|
+
flexDirection: "row",
|
|
293
|
+
alignItems: "center",
|
|
294
|
+
paddingVertical: 13,
|
|
295
|
+
paddingHorizontal: 16
|
|
296
|
+
},
|
|
297
|
+
iconContainer: {
|
|
298
|
+
marginRight: 12,
|
|
299
|
+
justifyContent: "center",
|
|
300
|
+
alignItems: "center"
|
|
301
|
+
},
|
|
302
|
+
iconBadge: {
|
|
303
|
+
width: 32,
|
|
304
|
+
height: 32,
|
|
305
|
+
borderRadius: 16,
|
|
306
|
+
backgroundColor: "rgba(255, 255, 255, 0.22)",
|
|
307
|
+
justifyContent: "center",
|
|
308
|
+
alignItems: "center"
|
|
309
|
+
},
|
|
310
|
+
textContainer: {
|
|
311
|
+
flex: 1,
|
|
312
|
+
justifyContent: "center"
|
|
313
|
+
},
|
|
314
|
+
titleText: {
|
|
315
|
+
fontSize: 15,
|
|
316
|
+
fontWeight: "600",
|
|
317
|
+
letterSpacing: -0.2,
|
|
318
|
+
lineHeight: 20,
|
|
319
|
+
color: "#FFFFFF"
|
|
320
|
+
},
|
|
321
|
+
descriptionText: {
|
|
322
|
+
fontSize: 13,
|
|
323
|
+
fontWeight: "400",
|
|
324
|
+
color: "rgba(255, 255, 255, 0.9)",
|
|
325
|
+
marginTop: 2,
|
|
326
|
+
lineHeight: 18
|
|
327
|
+
},
|
|
328
|
+
actionButton: {
|
|
329
|
+
marginLeft: 12,
|
|
330
|
+
paddingHorizontal: 12,
|
|
331
|
+
paddingVertical: 6,
|
|
332
|
+
borderRadius: 8,
|
|
333
|
+
backgroundColor: "rgba(255, 255, 255, 0.22)",
|
|
334
|
+
borderWidth: 1,
|
|
335
|
+
borderColor: "rgba(255, 255, 255, 0.2)"
|
|
336
|
+
},
|
|
337
|
+
actionButtonText: {
|
|
338
|
+
fontSize: 13,
|
|
339
|
+
fontWeight: "600",
|
|
340
|
+
color: "#FFFFFF"
|
|
341
|
+
},
|
|
342
|
+
progressBarContainer: {
|
|
343
|
+
position: "absolute",
|
|
344
|
+
bottom: 0,
|
|
345
|
+
left: 0,
|
|
346
|
+
right: 0,
|
|
347
|
+
height: 3,
|
|
348
|
+
backgroundColor: "rgba(255, 255, 255, 0.2)"
|
|
349
|
+
},
|
|
350
|
+
progressBarFill: {
|
|
351
|
+
height: "100%",
|
|
352
|
+
backgroundColor: "rgba(255, 255, 255, 0.9)"
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
function getDefaultSafeAreaInsets() {
|
|
356
|
+
const { width, height } = reactNative.Dimensions.get("window");
|
|
357
|
+
const isLandscape = width > height;
|
|
358
|
+
if (reactNative.Platform.OS === "android") {
|
|
359
|
+
return {
|
|
360
|
+
top: reactNative.StatusBar.currentHeight ?? 24,
|
|
361
|
+
bottom: 16,
|
|
362
|
+
left: 0,
|
|
363
|
+
right: 0
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
if (reactNative.Platform.OS === "ios") {
|
|
367
|
+
const isModernIphone = !reactNative.Platform.isPad && !reactNative.Platform.isTV && (height >= 812 || width >= 812);
|
|
368
|
+
return {
|
|
369
|
+
top: isModernIphone ? isLandscape ? 0 : 50 : 20,
|
|
370
|
+
bottom: isModernIphone ? 34 : 0,
|
|
371
|
+
left: 0,
|
|
372
|
+
right: 0
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
top: 16,
|
|
377
|
+
bottom: 16,
|
|
378
|
+
left: 0,
|
|
379
|
+
right: 0
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
var FlashMessage = React__default.default.forwardRef(
|
|
383
|
+
(props, ref) => {
|
|
384
|
+
const {
|
|
385
|
+
position: defaultPosition = "top",
|
|
386
|
+
duration: defaultDuration = 3e3,
|
|
387
|
+
floating: defaultFloating = true,
|
|
388
|
+
statusBarOffset,
|
|
389
|
+
bottomOffset,
|
|
390
|
+
style: containerCustomStyle
|
|
391
|
+
} = props;
|
|
392
|
+
const [currentMessage, setCurrentMessage] = React.useState(null);
|
|
393
|
+
const [isVisible, setIsVisible] = React.useState(false);
|
|
394
|
+
const currentMessageRef = React.useRef(null);
|
|
395
|
+
const translateY = React.useRef(new reactNative.Animated.Value(-150)).current;
|
|
396
|
+
const opacity = React.useRef(new reactNative.Animated.Value(0)).current;
|
|
397
|
+
const progress = React.useRef(new reactNative.Animated.Value(1)).current;
|
|
398
|
+
const hideTimeoutRef = React.useRef(null);
|
|
399
|
+
const progressAnimationRef = React.useRef(null);
|
|
400
|
+
const remainingTimeRef = React.useRef(0);
|
|
401
|
+
const timerStartedAtRef = React.useRef(0);
|
|
402
|
+
const safeInsets = getDefaultSafeAreaInsets();
|
|
403
|
+
const clearTimers = React.useCallback(() => {
|
|
404
|
+
if (hideTimeoutRef.current) {
|
|
405
|
+
clearTimeout(hideTimeoutRef.current);
|
|
406
|
+
hideTimeoutRef.current = null;
|
|
407
|
+
}
|
|
408
|
+
if (progressAnimationRef.current) {
|
|
409
|
+
progressAnimationRef.current.stop();
|
|
410
|
+
progressAnimationRef.current = null;
|
|
411
|
+
}
|
|
412
|
+
}, []);
|
|
413
|
+
const animateHide = React.useCallback(
|
|
414
|
+
(callback) => {
|
|
415
|
+
clearTimers();
|
|
416
|
+
const pos = currentMessageRef.current?.position ?? defaultPosition;
|
|
417
|
+
const targetY = pos === "top" ? -180 : pos === "bottom" ? 180 : 0;
|
|
418
|
+
reactNative.Animated.parallel([
|
|
419
|
+
reactNative.Animated.timing(translateY, {
|
|
420
|
+
toValue: targetY,
|
|
421
|
+
duration: 220,
|
|
422
|
+
useNativeDriver: true
|
|
423
|
+
}),
|
|
424
|
+
reactNative.Animated.timing(opacity, {
|
|
425
|
+
toValue: 0,
|
|
426
|
+
duration: 200,
|
|
427
|
+
useNativeDriver: true
|
|
428
|
+
})
|
|
429
|
+
]).start(() => {
|
|
430
|
+
setIsVisible(false);
|
|
431
|
+
const onHide = currentMessageRef.current?.onHide;
|
|
432
|
+
currentMessageRef.current = null;
|
|
433
|
+
setCurrentMessage(null);
|
|
434
|
+
if (onHide) {
|
|
435
|
+
onHide();
|
|
436
|
+
}
|
|
437
|
+
callback?.();
|
|
438
|
+
});
|
|
439
|
+
},
|
|
440
|
+
[clearTimers, defaultPosition, opacity, translateY]
|
|
441
|
+
);
|
|
442
|
+
const startAutoHideTimer = React.useCallback(
|
|
443
|
+
(durationMs) => {
|
|
444
|
+
clearTimers();
|
|
445
|
+
if (durationMs <= 0) return;
|
|
446
|
+
remainingTimeRef.current = durationMs;
|
|
447
|
+
timerStartedAtRef.current = Date.now();
|
|
448
|
+
progress.setValue(1);
|
|
449
|
+
progressAnimationRef.current = reactNative.Animated.timing(progress, {
|
|
450
|
+
toValue: 0,
|
|
451
|
+
duration: durationMs,
|
|
452
|
+
useNativeDriver: false
|
|
453
|
+
});
|
|
454
|
+
progressAnimationRef.current.start();
|
|
455
|
+
hideTimeoutRef.current = setTimeout(() => {
|
|
456
|
+
animateHide();
|
|
457
|
+
}, durationMs);
|
|
458
|
+
},
|
|
459
|
+
[animateHide, clearTimers, progress]
|
|
460
|
+
);
|
|
461
|
+
const pauseTimer = React.useCallback(() => {
|
|
462
|
+
if (hideTimeoutRef.current && timerStartedAtRef.current > 0) {
|
|
463
|
+
clearTimeout(hideTimeoutRef.current);
|
|
464
|
+
hideTimeoutRef.current = null;
|
|
465
|
+
const elapsed = Date.now() - timerStartedAtRef.current;
|
|
466
|
+
remainingTimeRef.current = Math.max(0, remainingTimeRef.current - elapsed);
|
|
467
|
+
if (progressAnimationRef.current) {
|
|
468
|
+
progressAnimationRef.current.stop();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}, []);
|
|
472
|
+
const resumeTimer = React.useCallback(() => {
|
|
473
|
+
if (remainingTimeRef.current > 0) {
|
|
474
|
+
startAutoHideTimer(remainingTimeRef.current);
|
|
475
|
+
}
|
|
476
|
+
}, [startAutoHideTimer]);
|
|
477
|
+
const show = React.useCallback(
|
|
478
|
+
(options) => {
|
|
479
|
+
clearTimers();
|
|
480
|
+
currentMessageRef.current = options;
|
|
481
|
+
setCurrentMessage(options);
|
|
482
|
+
setIsVisible(true);
|
|
483
|
+
const pos = options.position ?? defaultPosition;
|
|
484
|
+
const initialY = pos === "top" ? -150 : pos === "bottom" ? 150 : 0;
|
|
485
|
+
translateY.setValue(initialY);
|
|
486
|
+
opacity.setValue(0);
|
|
487
|
+
progress.setValue(1);
|
|
488
|
+
reactNative.Animated.parallel([
|
|
489
|
+
reactNative.Animated.spring(translateY, {
|
|
490
|
+
toValue: 0,
|
|
491
|
+
tension: 70,
|
|
492
|
+
friction: 9,
|
|
493
|
+
useNativeDriver: true
|
|
494
|
+
}),
|
|
495
|
+
reactNative.Animated.timing(opacity, {
|
|
496
|
+
toValue: 1,
|
|
497
|
+
duration: 200,
|
|
498
|
+
useNativeDriver: true
|
|
499
|
+
})
|
|
500
|
+
]).start(() => {
|
|
501
|
+
if (options.onShow) {
|
|
502
|
+
options.onShow();
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
const shouldAutoHide = options.autoHide ?? true;
|
|
506
|
+
const dur = shouldAutoHide ? options.duration !== void 0 ? options.duration : defaultDuration : 0;
|
|
507
|
+
if (dur > 0) {
|
|
508
|
+
startAutoHideTimer(dur);
|
|
509
|
+
}
|
|
510
|
+
},
|
|
511
|
+
[clearTimers, defaultDuration, defaultPosition, opacity, progress, startAutoHideTimer, translateY]
|
|
512
|
+
);
|
|
513
|
+
const showRef = React.useRef(show);
|
|
514
|
+
const animateHideRef = React.useRef(animateHide);
|
|
515
|
+
showRef.current = show;
|
|
516
|
+
animateHideRef.current = animateHide;
|
|
517
|
+
React.useImperativeHandle(
|
|
518
|
+
ref,
|
|
519
|
+
() => ({
|
|
520
|
+
showMessage: (options) => showRef.current(options),
|
|
521
|
+
hideMessage: () => animateHideRef.current()
|
|
522
|
+
}),
|
|
523
|
+
[]
|
|
524
|
+
);
|
|
525
|
+
React.useEffect(() => {
|
|
526
|
+
const instanceRef = {
|
|
527
|
+
showMessage: (options) => showRef.current(options),
|
|
528
|
+
hideMessage: () => animateHideRef.current()
|
|
529
|
+
};
|
|
530
|
+
flashManager.register(instanceRef);
|
|
531
|
+
return () => {
|
|
532
|
+
flashManager.unregister();
|
|
533
|
+
clearTimers();
|
|
534
|
+
};
|
|
535
|
+
}, [clearTimers]);
|
|
536
|
+
const panResponder = React.useRef(
|
|
537
|
+
reactNative.PanResponder.create({
|
|
538
|
+
onStartShouldSetPanResponder: () => false,
|
|
539
|
+
onMoveShouldSetPanResponder: (_, gestureState) => {
|
|
540
|
+
const swipeEnabled = currentMessageRef.current?.swipeToDismiss ?? true;
|
|
541
|
+
return swipeEnabled && Math.abs(gestureState.dy) > 8;
|
|
542
|
+
},
|
|
543
|
+
onPanResponderGrant: () => {
|
|
544
|
+
if (currentMessageRef.current?.pauseOnPress ?? true) {
|
|
545
|
+
pauseTimer();
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
onPanResponderMove: (_, gestureState) => {
|
|
549
|
+
if (!(currentMessageRef.current?.swipeToDismiss ?? true)) return;
|
|
550
|
+
const pos = currentMessageRef.current?.position ?? defaultPosition;
|
|
551
|
+
if (pos === "top" && gestureState.dy < 0) {
|
|
552
|
+
translateY.setValue(gestureState.dy);
|
|
553
|
+
} else if (pos === "bottom" && gestureState.dy > 0) {
|
|
554
|
+
translateY.setValue(gestureState.dy);
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
onPanResponderRelease: (_, gestureState) => {
|
|
558
|
+
const pos = currentMessageRef.current?.position ?? defaultPosition;
|
|
559
|
+
const swipeThreshold = 30;
|
|
560
|
+
const isDismissGesture = pos === "top" && gestureState.dy < -swipeThreshold || pos === "bottom" && gestureState.dy > swipeThreshold;
|
|
561
|
+
if (isDismissGesture && (currentMessageRef.current?.swipeToDismiss ?? true)) {
|
|
562
|
+
animateHide();
|
|
563
|
+
} else {
|
|
564
|
+
reactNative.Animated.spring(translateY, {
|
|
565
|
+
toValue: 0,
|
|
566
|
+
tension: 80,
|
|
567
|
+
friction: 8,
|
|
568
|
+
useNativeDriver: true
|
|
569
|
+
}).start();
|
|
570
|
+
if (currentMessageRef.current?.pauseOnPress ?? true) {
|
|
571
|
+
resumeTimer();
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
).current;
|
|
577
|
+
if (!isVisible || !currentMessage) {
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
const position = currentMessage.position ?? defaultPosition;
|
|
581
|
+
const isTop = position === "top";
|
|
582
|
+
const isBottom = position === "bottom";
|
|
583
|
+
const type = currentMessage.type ?? "default";
|
|
584
|
+
const typeColors = COLORS[type];
|
|
585
|
+
const backgroundColor = currentMessage.backgroundColor ?? typeColors.background;
|
|
586
|
+
const textColor = currentMessage.color ?? typeColors.text;
|
|
587
|
+
const floating = currentMessage.floating ?? defaultFloating;
|
|
588
|
+
const topInset = currentMessage.statusBarHeight ?? statusBarOffset ?? (isTop ? safeInsets.top + (floating ? 8 : 0) : 0);
|
|
589
|
+
const bottomInset = bottomOffset ?? (isBottom ? safeInsets.bottom + (floating ? 8 : 0) : 0);
|
|
590
|
+
const rootPositionStyle = isTop ? [styles2.rootTop, { paddingTop: topInset }] : isBottom ? [styles2.rootBottom, { paddingBottom: bottomInset }] : styles2.rootCenter;
|
|
591
|
+
const renderIcon = () => {
|
|
592
|
+
if (currentMessage.icon === "none") return null;
|
|
593
|
+
if (typeof currentMessage.icon === "function") {
|
|
594
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconBadge, children: currentMessage.icon({ size: 18, color: textColor }) }) });
|
|
595
|
+
}
|
|
596
|
+
if (React__default.default.isValidElement(currentMessage.icon)) {
|
|
597
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconBadge, children: currentMessage.icon }) });
|
|
598
|
+
}
|
|
599
|
+
const iconType = typeof currentMessage.icon === "string" ? currentMessage.icon : type;
|
|
600
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.iconBadge, children: /* @__PURE__ */ jsxRuntime.jsx(BuiltInIcon, { type: iconType, color: textColor, size: 18 }) }) });
|
|
601
|
+
};
|
|
602
|
+
const handlePress = () => {
|
|
603
|
+
if (currentMessage.onPress) {
|
|
604
|
+
currentMessage.onPress();
|
|
605
|
+
}
|
|
606
|
+
if (currentMessage.hideOnPress ?? true) {
|
|
607
|
+
animateHide();
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
const progressWidthInterpolation = progress.interpolate({
|
|
611
|
+
inputRange: [0, 1],
|
|
612
|
+
outputRange: ["0%", "100%"]
|
|
613
|
+
});
|
|
614
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
615
|
+
reactNative.View,
|
|
616
|
+
{
|
|
617
|
+
pointerEvents: "box-none",
|
|
618
|
+
style: [styles2.rootOverlay, rootPositionStyle, containerCustomStyle],
|
|
619
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
620
|
+
reactNative.Animated.View,
|
|
621
|
+
{
|
|
622
|
+
...panResponder.panHandlers,
|
|
623
|
+
style: [
|
|
624
|
+
styles2.toastContainer,
|
|
625
|
+
floating ? styles2.floatingContainer : styles2.edgeToEdgeContainer,
|
|
626
|
+
{ backgroundColor },
|
|
627
|
+
currentMessage.style,
|
|
628
|
+
{
|
|
629
|
+
transform: [{ translateY }],
|
|
630
|
+
opacity
|
|
631
|
+
}
|
|
632
|
+
],
|
|
633
|
+
children: [
|
|
634
|
+
currentMessage.customComponent ? currentMessage.customComponent(currentMessage, () => animateHide()) : /* @__PURE__ */ jsxRuntime.jsxs(
|
|
635
|
+
reactNative.Pressable,
|
|
636
|
+
{
|
|
637
|
+
onPress: handlePress,
|
|
638
|
+
onPressIn: () => {
|
|
639
|
+
if (currentMessage.pauseOnPress ?? true) {
|
|
640
|
+
pauseTimer();
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
onPressOut: () => {
|
|
644
|
+
if (currentMessage.pauseOnPress ?? true) {
|
|
645
|
+
resumeTimer();
|
|
646
|
+
}
|
|
647
|
+
},
|
|
648
|
+
onLongPress: currentMessage.onLongPress,
|
|
649
|
+
style: styles2.contentWrapper,
|
|
650
|
+
children: [
|
|
651
|
+
renderIcon(),
|
|
652
|
+
/* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { style: styles2.textContainer, children: [
|
|
653
|
+
typeof currentMessage.message === "string" ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
654
|
+
reactNative.Text,
|
|
655
|
+
{
|
|
656
|
+
style: [styles2.titleText, { color: textColor }, currentMessage.titleStyle],
|
|
657
|
+
numberOfLines: 2,
|
|
658
|
+
children: currentMessage.message
|
|
659
|
+
}
|
|
660
|
+
) : currentMessage.message,
|
|
661
|
+
currentMessage.description ? typeof currentMessage.description === "string" ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
662
|
+
reactNative.Text,
|
|
663
|
+
{
|
|
664
|
+
style: [
|
|
665
|
+
styles2.descriptionText,
|
|
666
|
+
{ color: textColor },
|
|
667
|
+
currentMessage.textStyle
|
|
668
|
+
],
|
|
669
|
+
numberOfLines: 3,
|
|
670
|
+
children: currentMessage.description
|
|
671
|
+
}
|
|
672
|
+
) : currentMessage.description : null
|
|
673
|
+
] }),
|
|
674
|
+
currentMessage.action ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
675
|
+
reactNative.Pressable,
|
|
676
|
+
{
|
|
677
|
+
onPress: () => {
|
|
678
|
+
currentMessage.action?.onPress();
|
|
679
|
+
animateHide();
|
|
680
|
+
},
|
|
681
|
+
style: [styles2.actionButton, currentMessage.action.style],
|
|
682
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles2.actionButtonText, currentMessage.action.textStyle], children: currentMessage.action.label })
|
|
683
|
+
}
|
|
684
|
+
) : null
|
|
685
|
+
]
|
|
686
|
+
}
|
|
687
|
+
),
|
|
688
|
+
currentMessage.showProgressBar && /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles2.progressBarContainer, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
689
|
+
reactNative.Animated.View,
|
|
690
|
+
{
|
|
691
|
+
style: [
|
|
692
|
+
styles2.progressBarFill,
|
|
693
|
+
{ width: progressWidthInterpolation }
|
|
694
|
+
]
|
|
695
|
+
}
|
|
696
|
+
) })
|
|
697
|
+
]
|
|
698
|
+
}
|
|
699
|
+
)
|
|
700
|
+
}
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
);
|
|
704
|
+
FlashMessage.displayName = "FlashMessage";
|
|
705
|
+
var FlashMessageContext = React.createContext(null);
|
|
706
|
+
var FlashMessageProvider = ({
|
|
707
|
+
children,
|
|
708
|
+
...containerProps
|
|
709
|
+
}) => {
|
|
710
|
+
const contextValue = React.useMemo(
|
|
711
|
+
() => ({
|
|
712
|
+
show: (options) => flashManager.showMessage(options),
|
|
713
|
+
hide: () => flashManager.hideMessage()
|
|
714
|
+
}),
|
|
715
|
+
[]
|
|
716
|
+
);
|
|
717
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(FlashMessageContext.Provider, { value: contextValue, children: [
|
|
718
|
+
children,
|
|
719
|
+
/* @__PURE__ */ jsxRuntime.jsx(FlashMessage, { ...containerProps })
|
|
720
|
+
] });
|
|
721
|
+
};
|
|
722
|
+
function useFlashMessage() {
|
|
723
|
+
const context = React.useContext(FlashMessageContext);
|
|
724
|
+
if (!context) {
|
|
725
|
+
return {
|
|
726
|
+
show: (options) => flashManager.showMessage(options),
|
|
727
|
+
hide: () => flashManager.hideMessage()
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
return context;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/index.ts
|
|
734
|
+
var index_default = FlashMessage;
|
|
735
|
+
|
|
736
|
+
exports.BuiltInIcon = BuiltInIcon;
|
|
737
|
+
exports.COLORS = COLORS;
|
|
738
|
+
exports.FlashMessage = FlashMessage;
|
|
739
|
+
exports.FlashMessageProvider = FlashMessageProvider;
|
|
740
|
+
exports.default = index_default;
|
|
741
|
+
exports.dismissFlash = dismissFlash;
|
|
742
|
+
exports.flash = flash;
|
|
743
|
+
exports.flashManager = flashManager;
|
|
744
|
+
exports.hideMessage = hideMessage;
|
|
745
|
+
exports.showMessage = showMessage;
|
|
746
|
+
exports.styles = styles2;
|
|
747
|
+
exports.useFlashMessage = useFlashMessage;
|
|
748
|
+
//# sourceMappingURL=index.js.map
|
|
749
|
+
//# sourceMappingURL=index.js.map
|