@bacon-pos/ui 0.2.1 → 0.4.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 (80) hide show
  1. package/lib/commonjs/components/Dialog/Dialog.js +234 -0
  2. package/lib/commonjs/components/Dialog/Dialog.js.map +1 -0
  3. package/lib/commonjs/components/Dialog/Dialog.stories.js +217 -0
  4. package/lib/commonjs/components/Dialog/Dialog.stories.js.map +1 -0
  5. package/lib/commonjs/components/Dialog/Dialog.test.js +71 -0
  6. package/lib/commonjs/components/Dialog/Dialog.test.js.map +1 -0
  7. package/lib/commonjs/components/Dialog/index.js +13 -0
  8. package/lib/commonjs/components/Dialog/index.js.map +1 -0
  9. package/lib/commonjs/components/Toast/Toast.js +184 -0
  10. package/lib/commonjs/components/Toast/Toast.js.map +1 -0
  11. package/lib/commonjs/components/Toast/Toast.stories.js +109 -0
  12. package/lib/commonjs/components/Toast/Toast.stories.js.map +1 -0
  13. package/lib/commonjs/components/Toast/Toast.test.js +70 -0
  14. package/lib/commonjs/components/Toast/Toast.test.js.map +1 -0
  15. package/lib/commonjs/components/Toast/ToastProvider.js +95 -0
  16. package/lib/commonjs/components/Toast/ToastProvider.js.map +1 -0
  17. package/lib/commonjs/components/Toast/index.js +26 -0
  18. package/lib/commonjs/components/Toast/index.js.map +1 -0
  19. package/lib/commonjs/components/_internal/ScrollArea.js +96 -0
  20. package/lib/commonjs/components/_internal/ScrollArea.js.map +1 -0
  21. package/lib/commonjs/components/index.js +33 -11
  22. package/lib/commonjs/components/index.js.map +1 -1
  23. package/lib/module/components/Dialog/Dialog.js +230 -0
  24. package/lib/module/components/Dialog/Dialog.js.map +1 -0
  25. package/lib/module/components/Dialog/Dialog.stories.js +213 -0
  26. package/lib/module/components/Dialog/Dialog.stories.js.map +1 -0
  27. package/lib/module/components/Dialog/Dialog.test.js +70 -0
  28. package/lib/module/components/Dialog/Dialog.test.js.map +1 -0
  29. package/lib/module/components/Dialog/index.js +4 -0
  30. package/lib/module/components/Dialog/index.js.map +1 -0
  31. package/lib/module/components/Toast/Toast.js +180 -0
  32. package/lib/module/components/Toast/Toast.js.map +1 -0
  33. package/lib/module/components/Toast/Toast.stories.js +105 -0
  34. package/lib/module/components/Toast/Toast.stories.js.map +1 -0
  35. package/lib/module/components/Toast/Toast.test.js +69 -0
  36. package/lib/module/components/Toast/Toast.test.js.map +1 -0
  37. package/lib/module/components/Toast/ToastProvider.js +90 -0
  38. package/lib/module/components/Toast/ToastProvider.js.map +1 -0
  39. package/lib/module/components/Toast/index.js +5 -0
  40. package/lib/module/components/Toast/index.js.map +1 -0
  41. package/lib/module/components/_internal/ScrollArea.js +92 -0
  42. package/lib/module/components/_internal/ScrollArea.js.map +1 -0
  43. package/lib/module/components/index.js +2 -0
  44. package/lib/module/components/index.js.map +1 -1
  45. package/lib/typescript/commonjs/components/Dialog/Dialog.d.ts +79 -0
  46. package/lib/typescript/commonjs/components/Dialog/Dialog.d.ts.map +1 -0
  47. package/lib/typescript/commonjs/components/Dialog/index.d.ts +2 -0
  48. package/lib/typescript/commonjs/components/Dialog/index.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/components/Toast/Toast.d.ts +40 -0
  50. package/lib/typescript/commonjs/components/Toast/Toast.d.ts.map +1 -0
  51. package/lib/typescript/commonjs/components/Toast/ToastProvider.d.ts +34 -0
  52. package/lib/typescript/commonjs/components/Toast/ToastProvider.d.ts.map +1 -0
  53. package/lib/typescript/commonjs/components/Toast/index.d.ts +3 -0
  54. package/lib/typescript/commonjs/components/Toast/index.d.ts.map +1 -0
  55. package/lib/typescript/commonjs/components/_internal/ScrollArea.d.ts +20 -0
  56. package/lib/typescript/commonjs/components/_internal/ScrollArea.d.ts.map +1 -0
  57. package/lib/typescript/commonjs/components/index.d.ts +2 -0
  58. package/lib/typescript/commonjs/components/index.d.ts.map +1 -1
  59. package/lib/typescript/module/components/Dialog/Dialog.d.ts +79 -0
  60. package/lib/typescript/module/components/Dialog/Dialog.d.ts.map +1 -0
  61. package/lib/typescript/module/components/Dialog/index.d.ts +2 -0
  62. package/lib/typescript/module/components/Dialog/index.d.ts.map +1 -0
  63. package/lib/typescript/module/components/Toast/Toast.d.ts +40 -0
  64. package/lib/typescript/module/components/Toast/Toast.d.ts.map +1 -0
  65. package/lib/typescript/module/components/Toast/ToastProvider.d.ts +34 -0
  66. package/lib/typescript/module/components/Toast/ToastProvider.d.ts.map +1 -0
  67. package/lib/typescript/module/components/Toast/index.d.ts +3 -0
  68. package/lib/typescript/module/components/Toast/index.d.ts.map +1 -0
  69. package/lib/typescript/module/components/_internal/ScrollArea.d.ts +20 -0
  70. package/lib/typescript/module/components/_internal/ScrollArea.d.ts.map +1 -0
  71. package/lib/typescript/module/components/index.d.ts +2 -0
  72. package/lib/typescript/module/components/index.d.ts.map +1 -1
  73. package/package.json +1 -1
  74. package/src/components/Dialog/Dialog.tsx +299 -0
  75. package/src/components/Dialog/index.ts +6 -0
  76. package/src/components/Toast/Toast.tsx +173 -0
  77. package/src/components/Toast/ToastProvider.tsx +126 -0
  78. package/src/components/Toast/index.ts +7 -0
  79. package/src/components/_internal/ScrollArea.tsx +95 -0
  80. package/src/components/index.ts +2 -0
@@ -0,0 +1,299 @@
1
+ import { type ReactNode } from 'react';
2
+ import {
3
+ KeyboardAvoidingView,
4
+ Modal,
5
+ Platform,
6
+ Pressable,
7
+ ScrollView,
8
+ StyleSheet,
9
+ useWindowDimensions,
10
+ View,
11
+ type StyleProp,
12
+ type ViewStyle,
13
+ } from 'react-native';
14
+ import { useTheme } from '../../theme';
15
+ import { Button, type ButtonVariant } from '../Button/Button';
16
+ import { Icon } from '../Icon/Icon';
17
+ import { Text } from '../Text/Text';
18
+ import { ScrollArea } from '../_internal/ScrollArea';
19
+
20
+ export interface DialogAction {
21
+ label: string;
22
+ onPress?: () => void;
23
+ /** Button variant. Defaults to `default` (lime). Pass another for other colors. */
24
+ variant?: ButtonVariant;
25
+ loading?: boolean;
26
+ disabled?: boolean;
27
+ }
28
+
29
+ export interface DialogCancelConfig {
30
+ /** Cancel label. Defaults to "Cancel". */
31
+ label?: string;
32
+ /** Handler. Defaults to `onClose`. */
33
+ onPress?: () => void;
34
+ /** Use the destructive style (subtle red) instead of the neutral secondary. */
35
+ destructive?: boolean;
36
+ }
37
+
38
+ export interface DialogProps {
39
+ /** Whether the dialog is shown. */
40
+ visible: boolean;
41
+ /** Called on backdrop press, close button, or Android back. */
42
+ onClose: () => void;
43
+ /** Required title. */
44
+ title: string;
45
+ /** Optional subtitle. */
46
+ subtitle?: string;
47
+ /** Any content — from text to a full form. */
48
+ children?: ReactNode;
49
+ /** Primary/secondary action buttons (right side). Keep total buttons ≤ 3. */
50
+ actions?: DialogAction[];
51
+ /** Optional cancel button (left side). */
52
+ cancel?: DialogCancelConfig;
53
+ /** Show the top-right close (×). Defaults to true. */
54
+ showClose?: boolean;
55
+ /** Render the close (×) in the destructive color. */
56
+ closeDestructive?: boolean;
57
+ /** Dismiss when the backdrop (overlay) is pressed. Defaults to false. */
58
+ dismissOnBackdrop?: boolean;
59
+ /**
60
+ * `inner` (default): the card caps at the screen height and its content
61
+ * scrolls inside. `page`: the card grows to its content and the whole modal
62
+ * scrolls (nothing behind moves) — better for short screens.
63
+ */
64
+ scrollMode?: 'inner' | 'page';
65
+ /** Keep the header pinned while content scrolls (inner mode). Defaults to true. */
66
+ stickyHeader?: boolean;
67
+ /** Keep the footer pinned while content scrolls (inner mode). Defaults to true. */
68
+ stickyFooter?: boolean;
69
+ /** Inner padding. Defaults to 24. */
70
+ padding?: number;
71
+ /** Vertical space between header↔content and content↔footer. Defaults to 24. */
72
+ contentSpacing?: number;
73
+ /** Corner radius. Defaults to 24. */
74
+ radius?: number;
75
+ /** Max width of the dialog card. Defaults to 560. */
76
+ maxWidth?: number;
77
+ /** Fixed card width (overrides `maxWidth`). */
78
+ width?: number;
79
+ /** Fill the whole screen — for small devices. Overrides width/maxWidth/scrollMode. */
80
+ fullscreen?: boolean;
81
+ /** Fraction of screen height the card may occupy (inner mode). Defaults to 0.9. */
82
+ maxHeightRatio?: number;
83
+ /** Scroll indicator color (a design-system color). Defaults to a subtle neutral. */
84
+ scrollbarColor?: string;
85
+ /** Override the card style. */
86
+ style?: StyleProp<ViewStyle>;
87
+ }
88
+
89
+ /**
90
+ * Dialog — an accessible, keyboard-aware modal that can hold anything from a
91
+ * short message to a full form (selects, date pickers, etc. keep working thanks
92
+ * to `keyboardShouldPersistTaps`). Content scrolls with an elegant thin,
93
+ * auto-hiding indicator. `scrollMode="page"` scrolls the whole modal on short
94
+ * screens instead of scrolling inside the card. Footer composes the
95
+ * design-system `Button`.
96
+ */
97
+ export function Dialog({
98
+ visible,
99
+ onClose,
100
+ title,
101
+ subtitle,
102
+ children,
103
+ actions = [],
104
+ cancel,
105
+ showClose = true,
106
+ closeDestructive = false,
107
+ dismissOnBackdrop = false,
108
+ scrollMode = 'inner',
109
+ stickyHeader = true,
110
+ stickyFooter = true,
111
+ padding = 24,
112
+ contentSpacing = 24,
113
+ radius = 24,
114
+ maxWidth = 560,
115
+ width,
116
+ fullscreen = false,
117
+ maxHeightRatio = 0.9,
118
+ scrollbarColor,
119
+ style,
120
+ }: DialogProps) {
121
+ const theme = useTheme();
122
+ const { height } = useWindowDimensions();
123
+ const indicatorColor = scrollbarColor ?? theme.colors.text.tertiary;
124
+
125
+ const closeBtn = showClose ? (
126
+ <Pressable
127
+ onPress={onClose}
128
+ hitSlop={8}
129
+ accessibilityRole="button"
130
+ accessibilityLabel="Close"
131
+ style={{ position: 'absolute', top: padding, right: padding, padding: 4, zIndex: 1 }}
132
+ >
133
+ <Icon
134
+ name="close"
135
+ size={24}
136
+ color={closeDestructive ? theme.colors.feedback.error : theme.colors.text.secondary}
137
+ />
138
+ </Pressable>
139
+ ) : null;
140
+
141
+ const header = (
142
+ <View style={{ paddingHorizontal: padding, paddingTop: padding, paddingBottom: contentSpacing }}>
143
+ <View style={{ paddingRight: showClose ? 40 : 0, gap: 4 }}>
144
+ <Text variant="h3" weight="semibold" color="primary">
145
+ {title}
146
+ </Text>
147
+ {subtitle ? (
148
+ <Text variant="bodyLarge" color="secondary">
149
+ {subtitle}
150
+ </Text>
151
+ ) : null}
152
+ </View>
153
+ </View>
154
+ );
155
+
156
+ const contentBody = <View style={{ paddingHorizontal: padding }}>{children}</View>;
157
+
158
+ const hasFooter = cancel != null || actions.length > 0;
159
+ const footer = hasFooter ? (
160
+ <View
161
+ style={{
162
+ flexDirection: 'row',
163
+ gap: 12,
164
+ paddingHorizontal: padding,
165
+ paddingBottom: padding,
166
+ paddingTop: contentSpacing,
167
+ }}
168
+ >
169
+ {cancel ? (
170
+ <View style={{ flex: 1 }}>
171
+ <Button
172
+ fullWidth
173
+ variant={cancel.destructive ? 'destructive' : 'secondary'}
174
+ label={cancel.label ?? 'Cancel'}
175
+ onPress={cancel.onPress ?? onClose}
176
+ />
177
+ </View>
178
+ ) : null}
179
+ {actions.map((action, i) => (
180
+ <View key={i} style={{ flex: 1 }}>
181
+ <Button
182
+ fullWidth
183
+ variant={action.variant ?? 'default'}
184
+ label={action.label}
185
+ onPress={action.onPress}
186
+ loading={action.loading}
187
+ disabled={action.disabled}
188
+ />
189
+ </View>
190
+ ))}
191
+ </View>
192
+ ) : null;
193
+
194
+ const cardBase: ViewStyle = fullscreen
195
+ ? {
196
+ flex: 1,
197
+ width: '100%',
198
+ backgroundColor: theme.colors.background.default,
199
+ borderRadius: 0,
200
+ }
201
+ : {
202
+ width: width ?? '100%',
203
+ maxWidth: width ? undefined : maxWidth,
204
+ backgroundColor: theme.colors.background.default,
205
+ borderRadius: radius,
206
+ overflow: 'hidden',
207
+ };
208
+
209
+ const card = fullscreen ? (
210
+ <View accessibilityViewIsModal style={[cardBase, style]}>
211
+ {closeBtn}
212
+ {stickyHeader ? header : null}
213
+ <ScrollArea
214
+ indicatorColor={indicatorColor}
215
+ style={{ flex: 1 }}
216
+ keyboardShouldPersistTaps="handled"
217
+ keyboardDismissMode="interactive"
218
+ >
219
+ {!stickyHeader ? header : null}
220
+ {contentBody}
221
+ {!stickyFooter ? footer : null}
222
+ </ScrollArea>
223
+ {stickyFooter ? footer : null}
224
+ </View>
225
+ ) : scrollMode === 'page' ? (
226
+ <View accessibilityViewIsModal style={[cardBase, style]}>
227
+ {closeBtn}
228
+ {header}
229
+ {contentBody}
230
+ {footer}
231
+ </View>
232
+ ) : (
233
+ <View
234
+ accessibilityViewIsModal
235
+ style={[cardBase, { maxHeight: height * maxHeightRatio }, style]}
236
+ >
237
+ {closeBtn}
238
+ {stickyHeader ? header : null}
239
+ <ScrollArea
240
+ indicatorColor={indicatorColor}
241
+ style={{ flexShrink: 1 }}
242
+ keyboardShouldPersistTaps="handled"
243
+ keyboardDismissMode="interactive"
244
+ >
245
+ {!stickyHeader ? header : null}
246
+ {contentBody}
247
+ {!stickyFooter ? footer : null}
248
+ </ScrollArea>
249
+ {stickyFooter ? footer : null}
250
+ </View>
251
+ );
252
+
253
+ return (
254
+ <Modal
255
+ visible={visible}
256
+ transparent
257
+ animationType="fade"
258
+ statusBarTranslucent
259
+ onRequestClose={onClose}
260
+ >
261
+ <KeyboardAvoidingView
262
+ behavior={Platform.OS === 'ios' ? 'padding' : undefined}
263
+ style={{ flex: 1 }}
264
+ >
265
+ <View style={{ flex: 1, backgroundColor: 'rgba(16, 18, 20, 0.55)' }}>
266
+ {fullscreen ? (
267
+ card
268
+ ) : scrollMode === 'page' ? (
269
+ <ScrollView
270
+ style={{ flex: 1 }}
271
+ contentContainerStyle={{ flexGrow: 1, padding: 16 }}
272
+ keyboardShouldPersistTaps="handled"
273
+ showsVerticalScrollIndicator={false}
274
+ >
275
+ <Pressable
276
+ style={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}
277
+ onPress={dismissOnBackdrop ? onClose : undefined}
278
+ >
279
+ <Pressable style={{ width: '100%', alignItems: 'center' }} onPress={() => {}}>
280
+ {card}
281
+ </Pressable>
282
+ </Pressable>
283
+ </ScrollView>
284
+ ) : (
285
+ <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 }}>
286
+ <Pressable
287
+ style={StyleSheet.absoluteFill}
288
+ onPress={dismissOnBackdrop ? onClose : undefined}
289
+ accessibilityElementsHidden
290
+ importantForAccessibility="no-hide-descendants"
291
+ />
292
+ {card}
293
+ </View>
294
+ )}
295
+ </View>
296
+ </KeyboardAvoidingView>
297
+ </Modal>
298
+ );
299
+ }
@@ -0,0 +1,6 @@
1
+ export {
2
+ Dialog,
3
+ type DialogProps,
4
+ type DialogAction,
5
+ type DialogCancelConfig,
6
+ } from './Dialog';
@@ -0,0 +1,173 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import {
3
+ Animated,
4
+ Easing,
5
+ Pressable,
6
+ type LayoutChangeEvent,
7
+ type ViewStyle,
8
+ } from 'react-native';
9
+ import { useTheme, type Theme } from '../../theme';
10
+ import { palette } from '../../theme/palette';
11
+ import { Icon } from '../Icon/Icon';
12
+ import type { IconName } from '../Icon/icons.generated';
13
+ import { Text } from '../Text/Text';
14
+
15
+ /** Built-in statuses. More can be added later, or use custom colors. */
16
+ export type ToastStatus = 'success' | 'error' | 'warning' | 'info';
17
+
18
+ export interface ToastOptions {
19
+ /** Message text. Ellipsized on a single line if it overflows. */
20
+ message: string;
21
+ /** Visual status. Defaults to `info`. */
22
+ status?: ToastStatus;
23
+ /** Optional leading icon. */
24
+ icon?: IconName;
25
+ /** Time on screen in ms. `0` keeps it until dismissed. */
26
+ duration?: number;
27
+ /** Custom fixed height. */
28
+ height?: number;
29
+ /** Custom fixed width. Defaults to full width. */
30
+ width?: number;
31
+ /** Show a countdown progress bar for the remaining time (needs duration > 0). */
32
+ showProgress?: boolean;
33
+ /** Custom background (overrides status). */
34
+ backgroundColor?: string;
35
+ /** Custom text/icon color (overrides status). */
36
+ color?: string;
37
+ /** Countdown bar color. Defaults to the status foreground. */
38
+ progressColor?: string;
39
+ }
40
+
41
+ function statusColors(theme: Theme, status: ToastStatus): { bg: string; fg: string } {
42
+ switch (status) {
43
+ case 'success':
44
+ return { bg: theme.colors.feedback.successSubtle, fg: theme.colors.feedback.success };
45
+ case 'error':
46
+ return { bg: theme.colors.feedback.errorSubtle, fg: theme.colors.feedback.error };
47
+ case 'warning':
48
+ return { bg: theme.colors.feedback.warningSubtle, fg: palette.warning[600] };
49
+ case 'info':
50
+ default:
51
+ return { bg: theme.colors.background.subtle, fg: palette.neutral[500] };
52
+ }
53
+ }
54
+
55
+ export interface ToastViewProps extends ToastOptions {
56
+ id: number;
57
+ position: 'top' | 'bottom';
58
+ /** When true, the toast animates out and then calls `onRemove`. */
59
+ leaving: boolean;
60
+ onRequestClose: (id: number) => void;
61
+ onRemove: (id: number) => void;
62
+ maxWidth?: number;
63
+ }
64
+
65
+ /**
66
+ * A single toast with an elegant slide + fade in/out. Self-contained: it runs
67
+ * its own duration timer and exit animation. Managed by `ToastProvider`.
68
+ */
69
+ export function ToastView({
70
+ id,
71
+ message,
72
+ status = 'info',
73
+ icon,
74
+ duration = 3000,
75
+ height,
76
+ width,
77
+ showProgress = false,
78
+ backgroundColor,
79
+ color,
80
+ progressColor,
81
+ position,
82
+ leaving,
83
+ onRequestClose,
84
+ onRemove,
85
+ maxWidth,
86
+ }: ToastViewProps) {
87
+ const theme = useTheme();
88
+ const base = statusColors(theme, status);
89
+ const bg = backgroundColor ?? base.bg;
90
+ const fg = color ?? base.fg;
91
+
92
+ const from = position === 'top' ? -12 : 12;
93
+ const translateY = useRef(new Animated.Value(from)).current;
94
+ const opacity = useRef(new Animated.Value(0)).current;
95
+ const remaining = useRef(new Animated.Value(1)).current;
96
+ const [contentW, setContentW] = useState(0);
97
+ const withProgress = showProgress && duration > 0;
98
+
99
+ useEffect(() => {
100
+ Animated.parallel([
101
+ Animated.timing(opacity, { toValue: 1, duration: 260, easing: Easing.out(Easing.cubic), useNativeDriver: true }),
102
+ Animated.timing(translateY, { toValue: 0, duration: 260, easing: Easing.out(Easing.cubic), useNativeDriver: true }),
103
+ ]).start();
104
+ if (withProgress) {
105
+ Animated.timing(remaining, { toValue: 0, duration, easing: Easing.linear, useNativeDriver: false }).start();
106
+ }
107
+ if (duration > 0) {
108
+ const timer = setTimeout(() => onRequestClose(id), duration);
109
+ return () => clearTimeout(timer);
110
+ }
111
+ return undefined;
112
+ // Mount only.
113
+ // eslint-disable-next-line react-hooks/exhaustive-deps
114
+ }, []);
115
+
116
+ useEffect(() => {
117
+ if (!leaving) return;
118
+ Animated.parallel([
119
+ Animated.timing(opacity, { toValue: 0, duration: 200, easing: Easing.in(Easing.cubic), useNativeDriver: true }),
120
+ Animated.timing(translateY, { toValue: from, duration: 200, easing: Easing.in(Easing.cubic), useNativeDriver: true }),
121
+ ]).start(() => onRemove(id));
122
+ // eslint-disable-next-line react-hooks/exhaustive-deps
123
+ }, [leaving]);
124
+
125
+ const pressStyle: ViewStyle = {
126
+ flexDirection: 'row',
127
+ alignItems: 'center',
128
+ justifyContent: 'center',
129
+ gap: 12,
130
+ paddingHorizontal: 20,
131
+ paddingVertical: height ? 0 : 20,
132
+ borderRadius: 16,
133
+ backgroundColor: bg,
134
+ overflow: 'hidden',
135
+ ...(height ? { height } : null),
136
+ };
137
+
138
+ // Fixed width if given, otherwise full width (capped by the provider maxWidth).
139
+ const outerWidth: ViewStyle = width != null ? { width } : { width: '100%', maxWidth };
140
+
141
+ return (
142
+ <Animated.View
143
+ accessibilityRole="alert"
144
+ accessibilityLiveRegion="polite"
145
+ style={{ opacity, transform: [{ translateY }], ...outerWidth }}
146
+ >
147
+ <Pressable
148
+ onPress={() => onRequestClose(id)}
149
+ onLayout={(e: LayoutChangeEvent) => setContentW(e.nativeEvent.layout.width)}
150
+ style={pressStyle}
151
+ >
152
+ {icon ? <Icon name={icon} size={24} color={fg} /> : null}
153
+ <Text variant="bodyLarge" weight="medium" color={fg} numberOfLines={1} align="center" style={{ flexShrink: 1 }}>
154
+ {message}
155
+ </Text>
156
+ {withProgress ? (
157
+ <Animated.View
158
+ pointerEvents="none"
159
+ style={{
160
+ position: 'absolute',
161
+ left: 0,
162
+ bottom: 0,
163
+ height: 3,
164
+ borderRadius: 3,
165
+ backgroundColor: progressColor ?? fg,
166
+ width: remaining.interpolate({ inputRange: [0, 1], outputRange: [0, contentW] }),
167
+ }}
168
+ />
169
+ ) : null}
170
+ </Pressable>
171
+ </Animated.View>
172
+ );
173
+ }
@@ -0,0 +1,126 @@
1
+ import {
2
+ createContext,
3
+ useCallback,
4
+ useContext,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ type ReactNode,
9
+ } from 'react';
10
+ import { View } from 'react-native';
11
+ import { ToastView, type ToastOptions } from './Toast';
12
+
13
+ export interface ToastApi {
14
+ /** Show a toast; returns its id. */
15
+ show: (options: ToastOptions) => number;
16
+ /** Dismiss a toast by id (animates out). */
17
+ hide: (id: number) => void;
18
+ /** Dismiss all toasts. */
19
+ hideAll: () => void;
20
+ }
21
+
22
+ const ToastContext = createContext<ToastApi | null>(null);
23
+
24
+ interface ToastItem extends ToastOptions {
25
+ id: number;
26
+ leaving: boolean;
27
+ }
28
+
29
+ export interface ToastProviderProps {
30
+ children: ReactNode;
31
+ /** Where toasts appear. Defaults to `bottom`. */
32
+ position?: 'top' | 'bottom';
33
+ /** Max toasts on screen at once. Defaults to 2. */
34
+ maxVisible?: number;
35
+ /** Default time on screen in ms. Defaults to 3000. */
36
+ duration?: number;
37
+ /** Distance from the screen edge. Defaults to 24. */
38
+ offset?: number;
39
+ /** Gap between stacked toasts. Defaults to 8. */
40
+ gap?: number;
41
+ /** Max toast width. Defaults to full width (no cap). */
42
+ maxWidth?: number;
43
+ }
44
+
45
+ /**
46
+ * Provides the imperative toast API and renders the toast stack. Wrap your app
47
+ * (inside ThemeProvider). Trigger toasts with `useToast().show(...)`. Newest
48
+ * toasts appear nearest the edge; at most `maxVisible` show at once.
49
+ */
50
+ export function ToastProvider({
51
+ children,
52
+ position = 'bottom',
53
+ maxVisible = 2,
54
+ duration = 3000,
55
+ offset = 24,
56
+ gap = 8,
57
+ maxWidth,
58
+ }: ToastProviderProps) {
59
+ const [toasts, setToasts] = useState<ToastItem[]>([]);
60
+ const idRef = useRef(0);
61
+
62
+ const show = useCallback(
63
+ (options: ToastOptions) => {
64
+ const id = (idRef.current += 1);
65
+ setToasts((prev) => {
66
+ const next: ToastItem[] = [...prev, { leaving: false, duration, ...options, id }];
67
+ return next.length > maxVisible ? next.slice(next.length - maxVisible) : next;
68
+ });
69
+ return id;
70
+ },
71
+ [duration, maxVisible],
72
+ );
73
+
74
+ const hide = useCallback((id: number) => {
75
+ setToasts((prev) => prev.map((t) => (t.id === id ? { ...t, leaving: true } : t)));
76
+ }, []);
77
+
78
+ const remove = useCallback((id: number) => {
79
+ setToasts((prev) => prev.filter((t) => t.id !== id));
80
+ }, []);
81
+
82
+ const hideAll = useCallback(() => {
83
+ setToasts((prev) => prev.map((t) => ({ ...t, leaving: true })));
84
+ }, []);
85
+
86
+ const api = useMemo<ToastApi>(() => ({ show, hide, hideAll }), [show, hide, hideAll]);
87
+
88
+ // Newest nearest the edge.
89
+ const display = position === 'top' ? [...toasts].reverse() : toasts;
90
+
91
+ return (
92
+ <ToastContext.Provider value={api}>
93
+ {children}
94
+ <View
95
+ pointerEvents="box-none"
96
+ style={{
97
+ position: 'absolute',
98
+ left: 0,
99
+ right: 0,
100
+ [position]: offset,
101
+ alignItems: 'center',
102
+ paddingHorizontal: 16,
103
+ gap,
104
+ }}
105
+ >
106
+ {display.map((t) => (
107
+ <ToastView
108
+ key={t.id}
109
+ {...t}
110
+ position={position}
111
+ maxWidth={maxWidth}
112
+ onRequestClose={hide}
113
+ onRemove={remove}
114
+ />
115
+ ))}
116
+ </View>
117
+ </ToastContext.Provider>
118
+ );
119
+ }
120
+
121
+ /** Access the toast API. Must be used within a `ToastProvider`. */
122
+ export function useToast(): ToastApi {
123
+ const ctx = useContext(ToastContext);
124
+ if (!ctx) throw new Error('useToast must be used within a ToastProvider');
125
+ return ctx;
126
+ }
@@ -0,0 +1,7 @@
1
+ export { ToastView, type ToastOptions, type ToastStatus, type ToastViewProps } from './Toast';
2
+ export {
3
+ ToastProvider,
4
+ useToast,
5
+ type ToastApi,
6
+ type ToastProviderProps,
7
+ } from './ToastProvider';
@@ -0,0 +1,95 @@
1
+ import { useRef, useState, type ReactNode } from 'react';
2
+ import {
3
+ Animated,
4
+ type NativeScrollEvent,
5
+ type NativeSyntheticEvent,
6
+ type StyleProp,
7
+ type ViewStyle,
8
+ } from 'react-native';
9
+
10
+ export interface ScrollAreaProps {
11
+ children: ReactNode;
12
+ /** Thin indicator color (a design-system color). */
13
+ indicatorColor: string;
14
+ /** Indicator thickness. Defaults to 4. */
15
+ indicatorWidth?: number;
16
+ keyboardShouldPersistTaps?: 'always' | 'never' | 'handled';
17
+ keyboardDismissMode?: 'none' | 'on-drag' | 'interactive';
18
+ style?: StyleProp<ViewStyle>;
19
+ contentContainerStyle?: StyleProp<ViewStyle>;
20
+ }
21
+
22
+ /**
23
+ * ScrollView with an elegant custom vertical indicator: thin, rounded, colored,
24
+ * and only visible while scrolling (fades out when idle). The native indicator
25
+ * is hidden so it looks the same on iOS, Android and web.
26
+ */
27
+ export function ScrollArea({
28
+ children,
29
+ indicatorColor,
30
+ indicatorWidth = 4,
31
+ keyboardShouldPersistTaps,
32
+ keyboardDismissMode,
33
+ style,
34
+ contentContainerStyle,
35
+ }: ScrollAreaProps) {
36
+ const [viewportH, setViewportH] = useState(0);
37
+ const [contentH, setContentH] = useState(0);
38
+ const scrollY = useRef(new Animated.Value(0)).current;
39
+ const opacity = useRef(new Animated.Value(0)).current;
40
+ const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
41
+
42
+ const scrollable = contentH > viewportH + 1;
43
+ const thumbH = scrollable ? Math.max(24, (viewportH / contentH) * viewportH) : 0;
44
+ const maxScroll = Math.max(1, contentH - viewportH);
45
+ const translateY = scrollY.interpolate({
46
+ inputRange: [0, maxScroll],
47
+ outputRange: [0, viewportH - thumbH],
48
+ extrapolate: 'clamp',
49
+ });
50
+
51
+ const reveal = () => {
52
+ Animated.timing(opacity, { toValue: 1, duration: 120, useNativeDriver: true }).start();
53
+ if (hideTimer.current) clearTimeout(hideTimer.current);
54
+ hideTimer.current = setTimeout(() => {
55
+ Animated.timing(opacity, { toValue: 0, duration: 400, useNativeDriver: true }).start();
56
+ }, 900);
57
+ };
58
+
59
+ return (
60
+ <Animated.View style={[{ position: 'relative' }, style]}>
61
+ <Animated.ScrollView
62
+ style={{ flexShrink: 1 }}
63
+ contentContainerStyle={contentContainerStyle}
64
+ showsVerticalScrollIndicator={false}
65
+ scrollEventThrottle={16}
66
+ keyboardShouldPersistTaps={keyboardShouldPersistTaps}
67
+ keyboardDismissMode={keyboardDismissMode}
68
+ onLayout={(e) => setViewportH(e.nativeEvent.layout.height)}
69
+ onContentSizeChange={(_w: number, h: number) => setContentH(h)}
70
+ onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
71
+ useNativeDriver: true,
72
+ listener: (_e: NativeSyntheticEvent<NativeScrollEvent>) => reveal(),
73
+ })}
74
+ >
75
+ {children}
76
+ </Animated.ScrollView>
77
+ {scrollable ? (
78
+ <Animated.View
79
+ pointerEvents="none"
80
+ style={{
81
+ position: 'absolute',
82
+ top: 0,
83
+ right: 2,
84
+ width: indicatorWidth,
85
+ height: thumbH,
86
+ borderRadius: indicatorWidth / 2,
87
+ backgroundColor: indicatorColor,
88
+ opacity,
89
+ transform: [{ translateY }],
90
+ }}
91
+ />
92
+ ) : null}
93
+ </Animated.View>
94
+ );
95
+ }