react-native-notitoast 0.0.0-stage → 2.0.0

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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -2
  3. package/lib/commonjs/ToastItem.js +504 -0
  4. package/lib/commonjs/ToastItem.js.map +1 -0
  5. package/lib/commonjs/ToastProvider.js +84 -0
  6. package/lib/commonjs/ToastProvider.js.map +1 -0
  7. package/lib/commonjs/index.js +26 -0
  8. package/lib/commonjs/index.js.map +1 -0
  9. package/lib/commonjs/package.json +1 -0
  10. package/lib/commonjs/theme.js +41 -0
  11. package/lib/commonjs/theme.js.map +1 -0
  12. package/lib/commonjs/toast.js +39 -0
  13. package/lib/commonjs/toast.js.map +1 -0
  14. package/lib/commonjs/types.js +6 -0
  15. package/lib/commonjs/types.js.map +1 -0
  16. package/lib/module/ToastItem.js +500 -0
  17. package/lib/module/ToastItem.js.map +1 -0
  18. package/lib/module/ToastProvider.js +80 -0
  19. package/lib/module/ToastProvider.js.map +1 -0
  20. package/lib/module/index.js +5 -0
  21. package/lib/module/index.js.map +1 -0
  22. package/lib/module/theme.js +37 -0
  23. package/lib/module/theme.js.map +1 -0
  24. package/lib/module/toast.js +33 -0
  25. package/lib/module/toast.js.map +1 -0
  26. package/lib/module/types.js +4 -0
  27. package/lib/module/types.js.map +1 -0
  28. package/lib/typescript/src/ToastItem.d.ts +12 -0
  29. package/lib/typescript/src/ToastItem.d.ts.map +1 -0
  30. package/lib/typescript/src/ToastProvider.d.ts +7 -0
  31. package/lib/typescript/src/ToastProvider.d.ts.map +1 -0
  32. package/lib/typescript/src/index.d.ts +6 -0
  33. package/lib/typescript/src/index.d.ts.map +1 -0
  34. package/lib/typescript/src/theme.d.ts +21 -0
  35. package/lib/typescript/src/theme.d.ts.map +1 -0
  36. package/lib/typescript/src/toast.d.ts +23 -0
  37. package/lib/typescript/src/toast.d.ts.map +1 -0
  38. package/lib/typescript/src/types.d.ts +93 -0
  39. package/lib/typescript/src/types.d.ts.map +1 -0
  40. package/package.json +77 -6
  41. package/src/ToastItem.tsx +597 -0
  42. package/src/ToastProvider.tsx +93 -0
  43. package/src/index.ts +14 -0
  44. package/src/theme.ts +39 -0
  45. package/src/toast.ts +51 -0
  46. package/src/types.ts +109 -0
@@ -0,0 +1,597 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import {
3
+ AccessibilityInfo,
4
+ Animated,
5
+ Dimensions,
6
+ Easing,
7
+ Image,
8
+ PanResponder,
9
+ Platform,
10
+ Pressable,
11
+ StyleSheet,
12
+ Text,
13
+ View,
14
+ } from 'react-native';
15
+ import type { ImageSourcePropType } from 'react-native';
16
+ import { defaultDurations, palettes, typeColors, typeGlyphs } from './theme';
17
+ import type { ToastAppearance, ToastConfig, ToastData } from './types';
18
+
19
+ type SwipeDirection = 'left' | 'right' | 'up' | 'down';
20
+
21
+ interface Props {
22
+ toast: ToastData;
23
+ config: ToastConfig;
24
+ scheme: 'light' | 'dark';
25
+ topOffset: number;
26
+ bottomOffset: number;
27
+ onExited: () => void;
28
+ }
29
+
30
+ /** Per-toast values win over provider values, but `undefined` never overrides. */
31
+ function mergeAppearance(
32
+ base: ToastAppearance,
33
+ over: ToastAppearance
34
+ ): ToastAppearance {
35
+ const out: Record<string, unknown> = { ...base };
36
+ for (const [key, value] of Object.entries(over)) {
37
+ if (value !== undefined) out[key] = value;
38
+ }
39
+ return out as ToastAppearance;
40
+ }
41
+
42
+ export function ToastItem({
43
+ toast,
44
+ config,
45
+ scheme,
46
+ topOffset,
47
+ bottomOffset,
48
+ onExited,
49
+ }: Props) {
50
+ const o = mergeAppearance(config, toast);
51
+ const type = toast.type ?? 'info';
52
+ const isTop = (o.position ?? 'top') === 'top';
53
+ const duration =
54
+ toast.duration ??
55
+ config.durations?.[type] ??
56
+ config.duration ??
57
+ defaultDurations[type];
58
+ const showProgress = o.showProgress ?? false;
59
+ const variant = o.variant ?? 'default';
60
+ const filled = variant === 'filled';
61
+ const accent = toast.accentColor ?? config.colors?.[type] ?? typeColors[type];
62
+ const palette = palettes[scheme];
63
+ const glyph = typeGlyphs[type];
64
+
65
+ // Colors
66
+ const onFilled = type === 'warning' ? '#111827' : '#FFFFFF';
67
+ const cardBg = o.backgroundColor ?? (filled ? accent : palette.card);
68
+ const borderColor = o.borderColor ?? (filled ? 'transparent' : palette.border);
69
+ const titleColor = o.titleColor ?? (filled ? onFilled : palette.title);
70
+ const messageColor = o.messageColor ?? (filled ? onFilled : palette.message);
71
+ const badgeBg = filled ? onFilled : accent;
72
+ const badgeGlyph = filled ? accent : '#FFFFFF';
73
+
74
+ // Layout pieces
75
+ const imagePosition = o.imagePosition ?? 'right';
76
+ const showIcon = o.showIcon ?? true;
77
+ const leftImage: ImageSourcePropType | undefined =
78
+ (imagePosition === 'left' ? toast.image : undefined) ??
79
+ toast.icon ??
80
+ o.appIcon;
81
+ const rightImage = imagePosition === 'right' ? toast.image : undefined;
82
+ const showTypeIcon = !leftImage && showIcon;
83
+ const showCornerBadge = !!leftImage && showIcon && (o.showTypeBadge ?? true);
84
+ const hasLeft = !!leftImage || showTypeIcon;
85
+ const showAccentBar = o.showAccentBar ?? (!hasLeft && !filled);
86
+
87
+ // Sizes
88
+ const radius = o.borderRadius ?? 18;
89
+ const iconSize = o.iconSize ?? 40;
90
+ const imageSize = o.imageSize ?? 44;
91
+ const innerBadge = Math.round(iconSize * 0.55);
92
+ const cornerBadge = Math.max(14, Math.round(iconSize * 0.42));
93
+ const titleLines = o.titleLines ?? 1;
94
+ const messageLines = o.messageLines ?? 3;
95
+
96
+ const hiddenY = isTop ? -(topOffset + 200) : bottomOffset + 200;
97
+
98
+ const translateY = useRef(new Animated.Value(hiddenY)).current;
99
+ const opacity = useRef(new Animated.Value(0)).current;
100
+ const progress = useRef(new Animated.Value(1)).current;
101
+ const exiting = useRef(false);
102
+ const onExitedRef = useRef(onExited);
103
+ onExitedRef.current = onExited;
104
+ const onHideRef = useRef(toast.onHide);
105
+ onHideRef.current = toast.onHide;
106
+
107
+ const translateX = useRef(new Animated.Value(0)).current;
108
+ const dragging = useRef(false);
109
+
110
+ // Swipe directions. `swipeable: false` turns all of them off.
111
+ const swipeable = o.swipeable ?? true;
112
+ const swipe = {
113
+ left: swipeable && (o.swipeLeft ?? true),
114
+ right: swipeable && (o.swipeRight ?? true),
115
+ up: swipeable && (o.swipeUp ?? isTop),
116
+ down: swipeable && (o.swipeDown ?? !isTop),
117
+ };
118
+ const swipeRef = useRef(swipe);
119
+ swipeRef.current = swipe;
120
+
121
+ const pauseTimer = () => progress.stopAnimation();
122
+
123
+ const startTimer = () => {
124
+ if (duration <= 0 || exiting.current || dragging.current) return;
125
+ progress.stopAnimation((value) => {
126
+ Animated.timing(progress, {
127
+ toValue: 0,
128
+ duration: value * duration,
129
+ easing: Easing.linear,
130
+ useNativeDriver: false,
131
+ }).start(({ finished }) => {
132
+ if (finished) dismiss();
133
+ });
134
+ });
135
+ };
136
+
137
+ /** `direction` = swipe direction the toast leaves in; default slides back to its edge. */
138
+ const dismiss = (direction?: SwipeDirection) => {
139
+ if (exiting.current) return;
140
+ exiting.current = true;
141
+ progress.stopAnimation();
142
+ const { width, height } = Dimensions.get('window');
143
+ const horizontal = direction === 'left' || direction === 'right';
144
+ const target =
145
+ direction === 'left'
146
+ ? -width
147
+ : direction === 'right'
148
+ ? width
149
+ : direction === 'up'
150
+ ? -height
151
+ : direction === 'down'
152
+ ? height
153
+ : hiddenY;
154
+ Animated.parallel([
155
+ Animated.timing(horizontal ? translateX : translateY, {
156
+ toValue: target,
157
+ duration: direction ? 200 : 240,
158
+ easing: direction ? Easing.out(Easing.quad) : Easing.in(Easing.cubic),
159
+ useNativeDriver: true,
160
+ }),
161
+ Animated.timing(opacity, {
162
+ toValue: 0,
163
+ duration: 200,
164
+ useNativeDriver: true,
165
+ }),
166
+ ]).start(() => {
167
+ onHideRef.current?.();
168
+ onExitedRef.current();
169
+ });
170
+ };
171
+
172
+ // Latest-closure ref so the (once-created) PanResponder never goes stale.
173
+ const actions = useRef({ dismiss, startTimer, pauseTimer });
174
+ actions.current = { dismiss, startTimer, pauseTimer };
175
+
176
+ // Axis locked when the drag starts, so a swipe moves on one axis only.
177
+ const axis = useRef<'x' | 'y'>('y');
178
+
179
+ const snapBack = () => {
180
+ dragging.current = false;
181
+ Animated.parallel([
182
+ Animated.spring(translateX, { toValue: 0, useNativeDriver: true }),
183
+ Animated.spring(translateY, { toValue: 0, useNativeDriver: true }),
184
+ ]).start();
185
+ actions.current.startTimer();
186
+ };
187
+
188
+ /** Allowed direction follows the finger; a blocked one only gives a little. */
189
+ const follow = (d: number, negOk: boolean, posOk: boolean) =>
190
+ d < 0 ? (negOk ? d : d * 0.15) : posOk ? d : d * 0.15;
191
+
192
+ const pan = useRef(
193
+ PanResponder.create({
194
+ onMoveShouldSetPanResponder: (_, g) => {
195
+ const s = swipeRef.current;
196
+ const absX = Math.abs(g.dx);
197
+ const absY = Math.abs(g.dy);
198
+ if (absX > absY) return absX > 8 && (s.left || s.right);
199
+ return absY > 6 && (s.up || s.down);
200
+ },
201
+ onPanResponderGrant: (_, g) => {
202
+ axis.current = Math.abs(g.dx) > Math.abs(g.dy) ? 'x' : 'y';
203
+ dragging.current = true;
204
+ actions.current.pauseTimer();
205
+ },
206
+ onPanResponderMove: (_, g) => {
207
+ const s = swipeRef.current;
208
+ if (axis.current === 'x') {
209
+ translateX.setValue(follow(g.dx, s.left, s.right));
210
+ } else {
211
+ translateY.setValue(follow(g.dy, s.up, s.down));
212
+ }
213
+ },
214
+ onPanResponderRelease: (_, g) => {
215
+ const s = swipeRef.current;
216
+ let direction: SwipeDirection | undefined;
217
+ if (axis.current === 'x') {
218
+ if (s.left && (g.dx < -80 || g.vx < -0.5)) direction = 'left';
219
+ else if (s.right && (g.dx > 80 || g.vx > 0.5)) direction = 'right';
220
+ } else {
221
+ if (s.up && (g.dy < -24 || g.vy < -0.6)) direction = 'up';
222
+ else if (s.down && (g.dy > 24 || g.vy > 0.6)) direction = 'down';
223
+ }
224
+ if (direction) {
225
+ dragging.current = false;
226
+ actions.current.dismiss(direction);
227
+ return;
228
+ }
229
+ snapBack();
230
+ },
231
+ onPanResponderTerminate: () => snapBack(),
232
+ onPanResponderTerminationRequest: () => false,
233
+ })
234
+ ).current;
235
+
236
+ // Enter animation + announce for screen readers.
237
+ useEffect(() => {
238
+ Animated.parallel([
239
+ Animated.spring(translateY, {
240
+ toValue: 0,
241
+ damping: 18,
242
+ stiffness: 180,
243
+ mass: 0.9,
244
+ useNativeDriver: true,
245
+ }),
246
+ Animated.timing(opacity, {
247
+ toValue: 1,
248
+ duration: 220,
249
+ useNativeDriver: true,
250
+ }),
251
+ ]).start();
252
+ startTimer();
253
+ const spoken = [toast.title, toast.message].filter(Boolean).join('. ');
254
+ if (spoken) AccessibilityInfo.announceForAccessibility(spoken);
255
+ return () => progress.stopAnimation();
256
+ // eslint-disable-next-line react-hooks/exhaustive-deps
257
+ }, []);
258
+
259
+ // Same id shown again → content updated, restart the timer.
260
+ const firstRev = useRef(toast.rev);
261
+ useEffect(() => {
262
+ if (toast.rev === firstRev.current) return;
263
+ progress.stopAnimation();
264
+ progress.setValue(1);
265
+ startTimer();
266
+ // eslint-disable-next-line react-hooks/exhaustive-deps
267
+ }, [toast.rev, duration]);
268
+
269
+ useEffect(() => {
270
+ if (toast.dismissing) dismiss();
271
+ // eslint-disable-next-line react-hooks/exhaustive-deps
272
+ }, [toast.dismissing]);
273
+
274
+ const handlePress = () => {
275
+ toast.onPress?.();
276
+ dismiss();
277
+ };
278
+
279
+ const barWidth = progress.interpolate({
280
+ inputRange: [0, 1],
281
+ outputRange: ['0%', '100%'],
282
+ });
283
+
284
+ const sizeStyle = {
285
+ ...(o.width !== undefined && { width: o.width }),
286
+ ...(o.maxWidth !== undefined && { maxWidth: o.maxWidth }),
287
+ ...(o.minHeight !== undefined && { minHeight: o.minHeight }),
288
+ ...(o.height !== undefined && { height: o.height }),
289
+ };
290
+
291
+ return (
292
+ <Animated.View
293
+ pointerEvents="box-none"
294
+ style={[
295
+ styles.container,
296
+ isTop ? { top: topOffset } : { bottom: bottomOffset },
297
+ { opacity, transform: [{ translateX }, { translateY }] },
298
+ ]}
299
+ {...pan.panHandlers}
300
+ >
301
+ <View
302
+ style={[
303
+ styles.shadow,
304
+ { backgroundColor: cardBg, borderColor, borderRadius: radius },
305
+ sizeStyle,
306
+ o.style,
307
+ ]}
308
+ >
309
+ <Pressable
310
+ onPress={handlePress}
311
+ onPressIn={pauseTimer}
312
+ onPressOut={startTimer}
313
+ accessibilityRole="alert"
314
+ accessibilityLiveRegion="polite"
315
+ style={[styles.card, { borderRadius: radius }]}
316
+ >
317
+ {variant === 'tinted' ? (
318
+ <View
319
+ pointerEvents="none"
320
+ style={[
321
+ styles.fill,
322
+ {
323
+ backgroundColor: accent,
324
+ opacity: scheme === 'dark' ? 0.16 : 0.08,
325
+ },
326
+ ]}
327
+ />
328
+ ) : null}
329
+
330
+ {showAccentBar ? (
331
+ <View style={[styles.accentBar, { backgroundColor: accent }]} />
332
+ ) : null}
333
+
334
+ <View
335
+ style={[
336
+ styles.row,
337
+ showAccentBar && styles.rowWithBar,
338
+ !hasLeft && styles.rowTextOnly,
339
+ ]}
340
+ >
341
+ {leftImage ? (
342
+ <View style={{ width: iconSize, height: iconSize }}>
343
+ <Image
344
+ source={leftImage}
345
+ style={{
346
+ width: iconSize,
347
+ height: iconSize,
348
+ borderRadius: Math.round(iconSize * 0.28),
349
+ }}
350
+ />
351
+ {showCornerBadge ? (
352
+ <View
353
+ style={[
354
+ styles.cornerBadge,
355
+ {
356
+ width: cornerBadge,
357
+ height: cornerBadge,
358
+ borderRadius: cornerBadge / 2,
359
+ backgroundColor: badgeBg,
360
+ borderColor: cardBg,
361
+ },
362
+ ]}
363
+ >
364
+ <Text
365
+ style={[
366
+ styles.glyph,
367
+ {
368
+ color: badgeGlyph,
369
+ fontSize: Math.round(cornerBadge * 0.5),
370
+ lineHeight: Math.round(cornerBadge * 0.62),
371
+ },
372
+ ]}
373
+ >
374
+ {glyph}
375
+ </Text>
376
+ </View>
377
+ ) : null}
378
+ </View>
379
+ ) : showTypeIcon ? (
380
+ <View
381
+ style={[
382
+ styles.iconWrap,
383
+ {
384
+ width: iconSize,
385
+ height: iconSize,
386
+ borderRadius: Math.round(iconSize * 0.3),
387
+ },
388
+ ]}
389
+ >
390
+ <View
391
+ style={[
392
+ styles.fill,
393
+ {
394
+ backgroundColor: filled ? '#FFFFFF' : accent,
395
+ opacity: filled ? 0.22 : 0.12,
396
+ },
397
+ ]}
398
+ />
399
+ <View
400
+ style={[
401
+ styles.innerBadge,
402
+ {
403
+ width: innerBadge,
404
+ height: innerBadge,
405
+ borderRadius: innerBadge / 2,
406
+ backgroundColor: badgeBg,
407
+ },
408
+ ]}
409
+ >
410
+ <Text
411
+ style={[
412
+ styles.glyph,
413
+ {
414
+ color: badgeGlyph,
415
+ fontSize: Math.round(innerBadge * 0.58),
416
+ lineHeight: Math.round(innerBadge * 0.72),
417
+ },
418
+ ]}
419
+ >
420
+ {glyph}
421
+ </Text>
422
+ </View>
423
+ </View>
424
+ ) : null}
425
+
426
+ <View style={styles.texts}>
427
+ {toast.title ? (
428
+ <Text
429
+ numberOfLines={titleLines}
430
+ style={[styles.title, { color: titleColor }, o.titleStyle]}
431
+ >
432
+ {toast.title}
433
+ </Text>
434
+ ) : null}
435
+ {toast.message ? (
436
+ <Text
437
+ numberOfLines={messageLines}
438
+ style={[
439
+ styles.message,
440
+ { color: messageColor },
441
+ filled && styles.softText,
442
+ o.messageStyle,
443
+ ]}
444
+ >
445
+ {toast.message}
446
+ </Text>
447
+ ) : null}
448
+ </View>
449
+
450
+ {rightImage ? (
451
+ <Image
452
+ source={rightImage}
453
+ style={{
454
+ width: imageSize,
455
+ height: imageSize,
456
+ borderRadius: Math.round(imageSize * 0.23),
457
+ }}
458
+ />
459
+ ) : null}
460
+ </View>
461
+
462
+ {showProgress && duration > 0 ? (
463
+ <View
464
+ style={[
465
+ styles.track,
466
+ {
467
+ backgroundColor: filled
468
+ ? 'rgba(255,255,255,0.25)'
469
+ : palette.track,
470
+ },
471
+ ]}
472
+ >
473
+ <Animated.View
474
+ style={[
475
+ styles.bar,
476
+ {
477
+ backgroundColor: filled ? onFilled : accent,
478
+ width: barWidth,
479
+ },
480
+ ]}
481
+ />
482
+ </View>
483
+ ) : null}
484
+ </Pressable>
485
+ </View>
486
+ </Animated.View>
487
+ );
488
+ }
489
+
490
+ const styles = StyleSheet.create({
491
+ container: {
492
+ position: 'absolute',
493
+ left: 0,
494
+ right: 0,
495
+ paddingHorizontal: 12,
496
+ alignItems: 'center',
497
+ zIndex: 9999,
498
+ elevation: 9999,
499
+ },
500
+ shadow: {
501
+ width: '100%',
502
+ maxWidth: 560,
503
+ borderWidth: StyleSheet.hairlineWidth,
504
+ ...Platform.select({
505
+ ios: {
506
+ shadowColor: '#000',
507
+ shadowOpacity: 0.12,
508
+ shadowRadius: 18,
509
+ shadowOffset: { width: 0, height: 8 },
510
+ },
511
+ android: { elevation: 8 },
512
+ default: {},
513
+ }),
514
+ },
515
+ card: {
516
+ flexGrow: 1,
517
+ justifyContent: 'center',
518
+ overflow: 'hidden',
519
+ },
520
+ fill: {
521
+ position: 'absolute',
522
+ top: 0,
523
+ left: 0,
524
+ right: 0,
525
+ bottom: 0,
526
+ },
527
+ accentBar: {
528
+ position: 'absolute',
529
+ top: 0,
530
+ bottom: 0,
531
+ left: 0,
532
+ width: 4,
533
+ },
534
+ row: {
535
+ flexDirection: 'row',
536
+ alignItems: 'center',
537
+ paddingVertical: 12,
538
+ paddingHorizontal: 12,
539
+ gap: 12,
540
+ },
541
+ rowWithBar: {
542
+ paddingLeft: 18,
543
+ },
544
+ rowTextOnly: {
545
+ paddingVertical: 14,
546
+ paddingRight: 16,
547
+ },
548
+ iconWrap: {
549
+ alignItems: 'center',
550
+ justifyContent: 'center',
551
+ overflow: 'hidden',
552
+ },
553
+ innerBadge: {
554
+ alignItems: 'center',
555
+ justifyContent: 'center',
556
+ },
557
+ cornerBadge: {
558
+ position: 'absolute',
559
+ right: -4,
560
+ bottom: -4,
561
+ borderWidth: 2,
562
+ alignItems: 'center',
563
+ justifyContent: 'center',
564
+ },
565
+ glyph: {
566
+ fontWeight: '800',
567
+ textAlign: 'center',
568
+ includeFontPadding: false,
569
+ textAlignVertical: 'center',
570
+ },
571
+ texts: {
572
+ flex: 1,
573
+ minWidth: 0,
574
+ },
575
+ title: {
576
+ fontSize: 15,
577
+ fontWeight: '700',
578
+ },
579
+ message: {
580
+ fontSize: 13.5,
581
+ lineHeight: 18,
582
+ marginTop: 2,
583
+ },
584
+ softText: {
585
+ opacity: 0.88,
586
+ },
587
+ track: {
588
+ position: 'absolute',
589
+ left: 0,
590
+ right: 0,
591
+ bottom: 0,
592
+ height: 3,
593
+ },
594
+ bar: {
595
+ height: 3,
596
+ },
597
+ });
@@ -0,0 +1,93 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import type { ReactNode } from 'react';
3
+ import {
4
+ Platform,
5
+ StatusBar,
6
+ StyleSheet,
7
+ View,
8
+ useColorScheme,
9
+ } from 'react-native';
10
+ import { ToastItem } from './ToastItem';
11
+ import { setToastHandler } from './toast';
12
+ import type { ToastConfig, ToastData, ToastOptions } from './types';
13
+
14
+ export interface ToastProviderProps extends ToastConfig {
15
+ children?: ReactNode;
16
+ }
17
+
18
+ export function ToastProvider({ children, ...config }: ToastProviderProps) {
19
+ const [queue, setQueue] = useState<ToastData[]>([]);
20
+ const counter = useRef(0);
21
+ const systemScheme = useColorScheme();
22
+
23
+ useEffect(() => {
24
+ setToastHandler({
25
+ show: (options: ToastOptions) => {
26
+ const id = options.id ?? `toast-${++counter.current}`;
27
+ setQueue((q) => {
28
+ const existing = q.find((t) => t.id === id);
29
+ if (existing) {
30
+ return q.map((t) =>
31
+ t.id === id
32
+ ? { ...t, ...options, id, rev: t.rev + 1, dismissing: false }
33
+ : t
34
+ );
35
+ }
36
+ return [...q, { ...options, id, rev: 0 }];
37
+ });
38
+ return id;
39
+ },
40
+ hide: (id?: string) => {
41
+ setQueue((q) => {
42
+ const target = id ?? q[0]?.id;
43
+ return q.flatMap((t, i) =>
44
+ t.id !== target ? [t] : i === 0 ? [{ ...t, dismissing: true }] : []
45
+ );
46
+ });
47
+ },
48
+ hideAll: () => {
49
+ setQueue((q) => (q[0] ? [{ ...q[0], dismissing: true }] : []));
50
+ },
51
+ });
52
+ return () => setToastHandler(null);
53
+ }, []);
54
+
55
+ const current = queue[0];
56
+
57
+ const handleExited = useCallback((id: string) => {
58
+ setQueue((q) => q.filter((t) => t.id !== id));
59
+ }, []);
60
+
61
+ const scheme =
62
+ config.theme === 'light' || config.theme === 'dark'
63
+ ? config.theme
64
+ : systemScheme === 'dark'
65
+ ? 'dark'
66
+ : 'light';
67
+
68
+ const topOffset =
69
+ config.topOffset ??
70
+ (Platform.OS === 'android' ? (StatusBar.currentHeight ?? 24) + 8 : 54);
71
+ const bottomOffset = config.bottomOffset ?? 40;
72
+
73
+ return (
74
+ <View style={styles.root}>
75
+ {children}
76
+ {current ? (
77
+ <ToastItem
78
+ key={current.id}
79
+ toast={current}
80
+ config={config}
81
+ scheme={scheme}
82
+ topOffset={topOffset}
83
+ bottomOffset={bottomOffset}
84
+ onExited={() => handleExited(current.id)}
85
+ />
86
+ ) : null}
87
+ </View>
88
+ );
89
+ }
90
+
91
+ const styles = StyleSheet.create({
92
+ root: { flex: 1 },
93
+ });
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export { ToastProvider } from './ToastProvider';
2
+ export type { ToastProviderProps } from './ToastProvider';
3
+ export { toast, useToast } from './toast';
4
+ export type { Toast } from './toast';
5
+ export type {
6
+ ToastAppearance,
7
+ ToastConfig,
8
+ ToastImagePosition,
9
+ ToastOptions,
10
+ ToastPosition,
11
+ ToastTheme,
12
+ ToastType,
13
+ ToastVariant,
14
+ } from './types';