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