@mehul214/react-native-flash-toast 1.0.0 → 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/dist/index.mjs CHANGED
@@ -55,43 +55,147 @@ var flash = showMessage;
55
55
  var dismissFlash = hideMessage;
56
56
  var BuiltInIcon = ({
57
57
  type,
58
- size = 22,
58
+ size = 18,
59
59
  color = "#FFFFFF"
60
60
  }) => {
61
- const containerStyle = [
62
- styles.circle,
63
- {
64
- width: size,
65
- height: size,
66
- borderRadius: size / 2,
67
- borderColor: color
68
- }
69
- ];
70
61
  switch (type) {
71
- case "success":
72
- return /* @__PURE__ */ jsx(View, { style: containerStyle, children: /* @__PURE__ */ jsx(Text, { style: [styles.symbol, { color, fontSize: size * 0.65 }], children: "\u2713" }) });
73
- case "danger":
74
- return /* @__PURE__ */ jsx(View, { style: containerStyle, children: /* @__PURE__ */ jsx(Text, { style: [styles.symbol, { color, fontSize: size * 0.65 }], children: "\u2715" }) });
75
- case "warning":
76
- return /* @__PURE__ */ jsx(View, { style: containerStyle, children: /* @__PURE__ */ jsx(Text, { style: [styles.symbol, { color, fontSize: size * 0.65 }], children: "!" }) });
77
- case "info":
78
- return /* @__PURE__ */ jsx(View, { style: containerStyle, children: /* @__PURE__ */ jsx(Text, { style: [styles.symbol, { color, fontSize: size * 0.65 }], children: "\u2139" }) });
62
+ case "success": {
63
+ const checkWidth = size * 0.36;
64
+ const checkHeight = size * 0.65;
65
+ const stroke = Math.max(2, size * 0.12);
66
+ return /* @__PURE__ */ jsx(View, { style: [styles.center, { width: size, height: size }], children: /* @__PURE__ */ jsx(
67
+ View,
68
+ {
69
+ style: {
70
+ width: checkWidth,
71
+ height: checkHeight,
72
+ borderColor: color,
73
+ borderBottomWidth: stroke,
74
+ borderRightWidth: stroke,
75
+ borderRadius: 1,
76
+ transform: [{ rotate: "45deg" }, { translateY: -size * 0.08 }]
77
+ }
78
+ }
79
+ ) });
80
+ }
81
+ case "danger": {
82
+ const crossThickness = Math.max(2, size * 0.12);
83
+ const crossLength = size * 0.72;
84
+ return /* @__PURE__ */ jsxs(View, { style: [styles.center, { width: size, height: size }], children: [
85
+ /* @__PURE__ */ jsx(
86
+ View,
87
+ {
88
+ style: [
89
+ styles.crossBar,
90
+ {
91
+ width: crossLength,
92
+ height: crossThickness,
93
+ backgroundColor: color,
94
+ borderRadius: crossThickness / 2,
95
+ transform: [{ rotate: "45deg" }]
96
+ }
97
+ ]
98
+ }
99
+ ),
100
+ /* @__PURE__ */ jsx(
101
+ View,
102
+ {
103
+ style: [
104
+ styles.crossBar,
105
+ {
106
+ width: crossLength,
107
+ height: crossThickness,
108
+ backgroundColor: color,
109
+ borderRadius: crossThickness / 2,
110
+ transform: [{ rotate: "-45deg" }]
111
+ }
112
+ ]
113
+ }
114
+ )
115
+ ] });
116
+ }
117
+ case "warning": {
118
+ const warnThickness = Math.max(2, size * 0.13);
119
+ const dotSize = warnThickness * 1.1;
120
+ return /* @__PURE__ */ jsxs(View, { style: [styles.center, { width: size, height: size }], children: [
121
+ /* @__PURE__ */ jsx(
122
+ View,
123
+ {
124
+ style: {
125
+ width: warnThickness,
126
+ height: size * 0.46,
127
+ backgroundColor: color,
128
+ borderRadius: warnThickness / 2,
129
+ marginBottom: size * 0.14
130
+ }
131
+ }
132
+ ),
133
+ /* @__PURE__ */ jsx(
134
+ View,
135
+ {
136
+ style: {
137
+ width: dotSize,
138
+ height: dotSize,
139
+ borderRadius: dotSize / 2,
140
+ backgroundColor: color
141
+ }
142
+ }
143
+ )
144
+ ] });
145
+ }
146
+ case "info": {
147
+ const infoThickness = Math.max(2, size * 0.13);
148
+ const infoDotSize = infoThickness * 1.1;
149
+ return /* @__PURE__ */ jsxs(View, { style: [styles.center, { width: size, height: size }], children: [
150
+ /* @__PURE__ */ jsx(
151
+ View,
152
+ {
153
+ style: {
154
+ width: infoDotSize,
155
+ height: infoDotSize,
156
+ borderRadius: infoDotSize / 2,
157
+ backgroundColor: color,
158
+ marginBottom: size * 0.14
159
+ }
160
+ }
161
+ ),
162
+ /* @__PURE__ */ jsx(
163
+ View,
164
+ {
165
+ style: {
166
+ width: infoThickness,
167
+ height: size * 0.46,
168
+ backgroundColor: color,
169
+ borderRadius: infoThickness / 2
170
+ }
171
+ }
172
+ )
173
+ ] });
174
+ }
79
175
  case "default":
80
- default:
81
- return /* @__PURE__ */ jsx(View, { style: containerStyle, children: /* @__PURE__ */ jsx(Text, { style: [styles.symbol, { color, fontSize: size * 0.6 }], children: "\u25CF" }) });
176
+ default: {
177
+ const defaultDot = size * 0.44;
178
+ return /* @__PURE__ */ jsx(View, { style: [styles.center, { width: size, height: size }], children: /* @__PURE__ */ jsx(
179
+ View,
180
+ {
181
+ style: {
182
+ width: defaultDot,
183
+ height: defaultDot,
184
+ borderRadius: defaultDot / 2,
185
+ backgroundColor: color
186
+ }
187
+ }
188
+ ) });
189
+ }
82
190
  }
83
191
  };
84
192
  var styles = StyleSheet.create({
85
- circle: {
86
- borderWidth: 1.5,
193
+ center: {
87
194
  justifyContent: "center",
88
195
  alignItems: "center"
89
196
  },
90
- symbol: {
91
- fontWeight: "800",
92
- textAlign: "center",
93
- includeFontPadding: false,
94
- lineHeight: 22
197
+ crossBar: {
198
+ position: "absolute"
95
199
  }
96
200
  });
97
201
  var COLORS = {
@@ -154,22 +258,22 @@ var styles2 = StyleSheet.create({
154
258
  ios: {
155
259
  shadowColor: "#000000",
156
260
  shadowOffset: { width: 0, height: 6 },
157
- shadowOpacity: 0.18,
158
- shadowRadius: 12
261
+ shadowOpacity: 0.2,
262
+ shadowRadius: 14
159
263
  },
160
264
  android: {
161
265
  elevation: 8
162
266
  },
163
267
  web: {
164
- boxShadow: "0 10px 25px -5px rgba(0, 0, 0, 0.2), 0 8px 10px -6px rgba(0, 0, 0, 0.1)"
268
+ boxShadow: "0 10px 25px -5px rgba(0, 0, 0, 0.25), 0 8px 10px -6px rgba(0, 0, 0, 0.15)"
165
269
  }
166
270
  })
167
271
  },
168
272
  floatingContainer: {
169
273
  marginHorizontal: 16,
170
- borderRadius: 14,
171
- borderWidth: StyleSheet.hairlineWidth,
172
- borderColor: "rgba(255, 255, 255, 0.15)"
274
+ borderRadius: 16,
275
+ borderWidth: 1,
276
+ borderColor: "rgba(255, 255, 255, 0.16)"
173
277
  },
174
278
  edgeToEdgeContainer: {
175
279
  marginHorizontal: 0,
@@ -179,7 +283,7 @@ var styles2 = StyleSheet.create({
179
283
  flex: 1,
180
284
  flexDirection: "row",
181
285
  alignItems: "center",
182
- paddingVertical: 14,
286
+ paddingVertical: 13,
183
287
  paddingHorizontal: 16
184
288
  },
185
289
  iconContainer: {
@@ -187,6 +291,14 @@ var styles2 = StyleSheet.create({
187
291
  justifyContent: "center",
188
292
  alignItems: "center"
189
293
  },
294
+ iconBadge: {
295
+ width: 32,
296
+ height: 32,
297
+ borderRadius: 16,
298
+ backgroundColor: "rgba(255, 255, 255, 0.22)",
299
+ justifyContent: "center",
300
+ alignItems: "center"
301
+ },
190
302
  textContainer: {
191
303
  flex: 1,
192
304
  justifyContent: "center"
@@ -195,25 +307,28 @@ var styles2 = StyleSheet.create({
195
307
  fontSize: 15,
196
308
  fontWeight: "600",
197
309
  letterSpacing: -0.2,
310
+ lineHeight: 20,
198
311
  color: "#FFFFFF"
199
312
  },
200
313
  descriptionText: {
201
314
  fontSize: 13,
202
315
  fontWeight: "400",
203
- color: "rgba(255, 255, 255, 0.88)",
316
+ color: "rgba(255, 255, 255, 0.9)",
204
317
  marginTop: 2,
205
318
  lineHeight: 18
206
319
  },
207
320
  actionButton: {
208
- marginLeft: 10,
321
+ marginLeft: 12,
209
322
  paddingHorizontal: 12,
210
323
  paddingVertical: 6,
211
324
  borderRadius: 8,
212
- backgroundColor: "rgba(255, 255, 255, 0.2)"
325
+ backgroundColor: "rgba(255, 255, 255, 0.22)",
326
+ borderWidth: 1,
327
+ borderColor: "rgba(255, 255, 255, 0.2)"
213
328
  },
214
329
  actionButtonText: {
215
330
  fontSize: 13,
216
- fontWeight: "700",
331
+ fontWeight: "600",
217
332
  color: "#FFFFFF"
218
333
  },
219
334
  progressBarContainer: {
@@ -222,11 +337,11 @@ var styles2 = StyleSheet.create({
222
337
  left: 0,
223
338
  right: 0,
224
339
  height: 3,
225
- backgroundColor: "rgba(255, 255, 255, 0.25)"
340
+ backgroundColor: "rgba(255, 255, 255, 0.2)"
226
341
  },
227
342
  progressBarFill: {
228
343
  height: "100%",
229
- backgroundColor: "#FFFFFF"
344
+ backgroundColor: "rgba(255, 255, 255, 0.9)"
230
345
  }
231
346
  });
232
347
  function getDefaultSafeAreaInsets() {
@@ -268,6 +383,7 @@ var FlashMessage = React.forwardRef(
268
383
  } = props;
269
384
  const [currentMessage, setCurrentMessage] = useState(null);
270
385
  const [isVisible, setIsVisible] = useState(false);
386
+ const currentMessageRef = useRef(null);
271
387
  const translateY = useRef(new Animated.Value(-150)).current;
272
388
  const opacity = useRef(new Animated.Value(0)).current;
273
389
  const progress = useRef(new Animated.Value(1)).current;
@@ -276,9 +392,6 @@ var FlashMessage = React.forwardRef(
276
392
  const remainingTimeRef = useRef(0);
277
393
  const timerStartedAtRef = useRef(0);
278
394
  const safeInsets = getDefaultSafeAreaInsets();
279
- const position = currentMessage?.position ?? defaultPosition;
280
- const isTop = position === "top";
281
- const isBottom = position === "bottom";
282
395
  const clearTimers = useCallback(() => {
283
396
  if (hideTimeoutRef.current) {
284
397
  clearTimeout(hideTimeoutRef.current);
@@ -292,7 +405,8 @@ var FlashMessage = React.forwardRef(
292
405
  const animateHide = useCallback(
293
406
  (callback) => {
294
407
  clearTimers();
295
- const targetY = isTop ? -180 : isBottom ? 180 : 0;
408
+ const pos = currentMessageRef.current?.position ?? defaultPosition;
409
+ const targetY = pos === "top" ? -180 : pos === "bottom" ? 180 : 0;
296
410
  Animated.parallel([
297
411
  Animated.timing(translateY, {
298
412
  toValue: targetY,
@@ -306,14 +420,16 @@ var FlashMessage = React.forwardRef(
306
420
  })
307
421
  ]).start(() => {
308
422
  setIsVisible(false);
309
- if (currentMessage?.onHide) {
310
- currentMessage.onHide();
311
- }
423
+ const onHide = currentMessageRef.current?.onHide;
424
+ currentMessageRef.current = null;
312
425
  setCurrentMessage(null);
426
+ if (onHide) {
427
+ onHide();
428
+ }
313
429
  callback?.();
314
430
  });
315
431
  },
316
- [clearTimers, isTop, isBottom, opacity, translateY, currentMessage]
432
+ [clearTimers, defaultPosition, opacity, translateY]
317
433
  );
318
434
  const startAutoHideTimer = useCallback(
319
435
  (durationMs) => {
@@ -326,7 +442,6 @@ var FlashMessage = React.forwardRef(
326
442
  toValue: 0,
327
443
  duration: durationMs,
328
444
  useNativeDriver: false
329
- // width needs layout or scaleX
330
445
  });
331
446
  progressAnimationRef.current.start();
332
447
  hideTimeoutRef.current = setTimeout(() => {
@@ -354,6 +469,7 @@ var FlashMessage = React.forwardRef(
354
469
  const show = useCallback(
355
470
  (options) => {
356
471
  clearTimers();
472
+ currentMessageRef.current = options;
357
473
  setCurrentMessage(options);
358
474
  setIsVisible(true);
359
475
  const pos = options.position ?? defaultPosition;
@@ -379,55 +495,62 @@ var FlashMessage = React.forwardRef(
379
495
  }
380
496
  });
381
497
  const shouldAutoHide = options.autoHide ?? true;
382
- const dur = shouldAutoHide ? options.duration ?? defaultDuration : 0;
498
+ const dur = shouldAutoHide ? options.duration !== void 0 ? options.duration : defaultDuration : 0;
383
499
  if (dur > 0) {
384
500
  startAutoHideTimer(dur);
385
501
  }
386
502
  },
387
503
  [clearTimers, defaultDuration, defaultPosition, opacity, progress, startAutoHideTimer, translateY]
388
504
  );
505
+ const showRef = useRef(show);
506
+ const animateHideRef = useRef(animateHide);
507
+ showRef.current = show;
508
+ animateHideRef.current = animateHide;
389
509
  useImperativeHandle(
390
510
  ref,
391
511
  () => ({
392
- showMessage: show,
393
- hideMessage: () => animateHide()
512
+ showMessage: (options) => showRef.current(options),
513
+ hideMessage: () => animateHideRef.current()
394
514
  }),
395
- [show, animateHide]
515
+ []
396
516
  );
397
517
  useEffect(() => {
398
518
  const instanceRef = {
399
- showMessage: show,
400
- hideMessage: () => animateHide()
519
+ showMessage: (options) => showRef.current(options),
520
+ hideMessage: () => animateHideRef.current()
401
521
  };
402
522
  flashManager.register(instanceRef);
403
523
  return () => {
404
524
  flashManager.unregister();
405
525
  clearTimers();
406
526
  };
407
- }, [show, animateHide, clearTimers]);
527
+ }, [clearTimers]);
408
528
  const panResponder = useRef(
409
529
  PanResponder.create({
410
- onStartShouldSetPanResponder: () => true,
530
+ onStartShouldSetPanResponder: () => false,
411
531
  onMoveShouldSetPanResponder: (_, gestureState) => {
412
- return Math.abs(gestureState.dy) > 5;
532
+ const swipeEnabled = currentMessageRef.current?.swipeToDismiss ?? true;
533
+ return swipeEnabled && Math.abs(gestureState.dy) > 8;
413
534
  },
414
535
  onPanResponderGrant: () => {
415
- if (currentMessage?.pauseOnPress ?? true) {
536
+ if (currentMessageRef.current?.pauseOnPress ?? true) {
416
537
  pauseTimer();
417
538
  }
418
539
  },
419
540
  onPanResponderMove: (_, gestureState) => {
420
- if (!(currentMessage?.swipeToDismiss ?? true)) return;
421
- if (isTop && gestureState.dy < 0) {
541
+ if (!(currentMessageRef.current?.swipeToDismiss ?? true)) return;
542
+ const pos = currentMessageRef.current?.position ?? defaultPosition;
543
+ if (pos === "top" && gestureState.dy < 0) {
422
544
  translateY.setValue(gestureState.dy);
423
- } else if (isBottom && gestureState.dy > 0) {
545
+ } else if (pos === "bottom" && gestureState.dy > 0) {
424
546
  translateY.setValue(gestureState.dy);
425
547
  }
426
548
  },
427
549
  onPanResponderRelease: (_, gestureState) => {
550
+ const pos = currentMessageRef.current?.position ?? defaultPosition;
428
551
  const swipeThreshold = 30;
429
- const isDismissGesture = isTop && gestureState.dy < -swipeThreshold || isBottom && gestureState.dy > swipeThreshold;
430
- if (isDismissGesture && (currentMessage?.swipeToDismiss ?? true)) {
552
+ const isDismissGesture = pos === "top" && gestureState.dy < -swipeThreshold || pos === "bottom" && gestureState.dy > swipeThreshold;
553
+ if (isDismissGesture && (currentMessageRef.current?.swipeToDismiss ?? true)) {
431
554
  animateHide();
432
555
  } else {
433
556
  Animated.spring(translateY, {
@@ -436,7 +559,7 @@ var FlashMessage = React.forwardRef(
436
559
  friction: 8,
437
560
  useNativeDriver: true
438
561
  }).start();
439
- if (currentMessage?.pauseOnPress ?? true) {
562
+ if (currentMessageRef.current?.pauseOnPress ?? true) {
440
563
  resumeTimer();
441
564
  }
442
565
  }
@@ -446,6 +569,9 @@ var FlashMessage = React.forwardRef(
446
569
  if (!isVisible || !currentMessage) {
447
570
  return null;
448
571
  }
572
+ const position = currentMessage.position ?? defaultPosition;
573
+ const isTop = position === "top";
574
+ const isBottom = position === "bottom";
449
575
  const type = currentMessage.type ?? "default";
450
576
  const typeColors = COLORS[type];
451
577
  const backgroundColor = currentMessage.backgroundColor ?? typeColors.background;
@@ -457,13 +583,13 @@ var FlashMessage = React.forwardRef(
457
583
  const renderIcon = () => {
458
584
  if (currentMessage.icon === "none") return null;
459
585
  if (typeof currentMessage.icon === "function") {
460
- return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: currentMessage.icon({ size: 22, color: textColor }) });
586
+ return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsx(View, { style: styles2.iconBadge, children: currentMessage.icon({ size: 18, color: textColor }) }) });
461
587
  }
462
588
  if (React.isValidElement(currentMessage.icon)) {
463
- return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: currentMessage.icon });
589
+ return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsx(View, { style: styles2.iconBadge, children: currentMessage.icon }) });
464
590
  }
465
591
  const iconType = typeof currentMessage.icon === "string" ? currentMessage.icon : type;
466
- return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsx(BuiltInIcon, { type: iconType, color: textColor, size: 22 }) });
592
+ return /* @__PURE__ */ jsx(View, { style: styles2.iconContainer, children: /* @__PURE__ */ jsx(View, { style: styles2.iconBadge, children: /* @__PURE__ */ jsx(BuiltInIcon, { type: iconType, color: textColor, size: 18 }) }) });
467
593
  };
468
594
  const handlePress = () => {
469
595
  if (currentMessage.onPress) {
@@ -501,6 +627,16 @@ var FlashMessage = React.forwardRef(
501
627
  Pressable,
502
628
  {
503
629
  onPress: handlePress,
630
+ onPressIn: () => {
631
+ if (currentMessage.pauseOnPress ?? true) {
632
+ pauseTimer();
633
+ }
634
+ },
635
+ onPressOut: () => {
636
+ if (currentMessage.pauseOnPress ?? true) {
637
+ resumeTimer();
638
+ }
639
+ },
504
640
  onLongPress: currentMessage.onLongPress,
505
641
  style: styles2.contentWrapper,
506
642
  children: [