react-native-bread 0.1.1 → 0.1.2

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 (68) hide show
  1. package/README.md +2 -1
  2. package/lib/commonjs/icons/CloseIcon.js +1 -22
  3. package/lib/commonjs/icons/GreenCheck.js +1 -27
  4. package/lib/commonjs/icons/InfoIcon.js +1 -24
  5. package/lib/commonjs/icons/RedX.js +1 -27
  6. package/lib/commonjs/icons/index.js +1 -34
  7. package/lib/commonjs/index.js +1 -59
  8. package/lib/commonjs/toast-api.js +1 -127
  9. package/lib/commonjs/toast-provider.js +1 -66
  10. package/lib/commonjs/toast-store.js +1 -278
  11. package/lib/commonjs/toast.js +1 -481
  12. package/lib/commonjs/types.js +1 -6
  13. package/lib/module/icons/CloseIcon.js +1 -16
  14. package/lib/module/icons/GreenCheck.js +1 -21
  15. package/lib/module/icons/InfoIcon.js +1 -18
  16. package/lib/module/icons/RedX.js +1 -21
  17. package/lib/module/icons/index.js +1 -7
  18. package/lib/module/index.js +1 -14
  19. package/lib/module/toast-api.js +1 -124
  20. package/lib/module/toast-provider.js +1 -62
  21. package/lib/module/toast-store.js +1 -274
  22. package/lib/module/toast.js +1 -475
  23. package/lib/module/types.js +1 -4
  24. package/package.json +7 -6
  25. package/lib/commonjs/icons/CloseIcon.js.map +0 -1
  26. package/lib/commonjs/icons/GreenCheck.js.map +0 -1
  27. package/lib/commonjs/icons/InfoIcon.js.map +0 -1
  28. package/lib/commonjs/icons/RedX.js.map +0 -1
  29. package/lib/commonjs/icons/index.js.map +0 -1
  30. package/lib/commonjs/index.js.map +0 -1
  31. package/lib/commonjs/toast-api.js.map +0 -1
  32. package/lib/commonjs/toast-provider.js.map +0 -1
  33. package/lib/commonjs/toast-store.js.map +0 -1
  34. package/lib/commonjs/toast.js.map +0 -1
  35. package/lib/commonjs/types.js.map +0 -1
  36. package/lib/module/icons/CloseIcon.js.map +0 -1
  37. package/lib/module/icons/GreenCheck.js.map +0 -1
  38. package/lib/module/icons/InfoIcon.js.map +0 -1
  39. package/lib/module/icons/RedX.js.map +0 -1
  40. package/lib/module/icons/index.js.map +0 -1
  41. package/lib/module/index.js.map +0 -1
  42. package/lib/module/toast-api.js.map +0 -1
  43. package/lib/module/toast-provider.js.map +0 -1
  44. package/lib/module/toast-store.js.map +0 -1
  45. package/lib/module/toast.js.map +0 -1
  46. package/lib/module/types.js.map +0 -1
  47. package/lib/typescript/icons/CloseIcon.d.ts.map +0 -1
  48. package/lib/typescript/icons/GreenCheck.d.ts.map +0 -1
  49. package/lib/typescript/icons/InfoIcon.d.ts.map +0 -1
  50. package/lib/typescript/icons/RedX.d.ts.map +0 -1
  51. package/lib/typescript/icons/index.d.ts.map +0 -1
  52. package/lib/typescript/index.d.ts.map +0 -1
  53. package/lib/typescript/toast-api.d.ts.map +0 -1
  54. package/lib/typescript/toast-provider.d.ts.map +0 -1
  55. package/lib/typescript/toast-store.d.ts.map +0 -1
  56. package/lib/typescript/toast.d.ts.map +0 -1
  57. package/lib/typescript/types.d.ts.map +0 -1
  58. package/src/icons/CloseIcon.tsx +0 -10
  59. package/src/icons/GreenCheck.tsx +0 -16
  60. package/src/icons/InfoIcon.tsx +0 -12
  61. package/src/icons/RedX.tsx +0 -16
  62. package/src/icons/index.ts +0 -4
  63. package/src/index.ts +0 -26
  64. package/src/toast-api.ts +0 -213
  65. package/src/toast-provider.tsx +0 -77
  66. package/src/toast-store.ts +0 -270
  67. package/src/toast.tsx +0 -466
  68. package/src/types.ts +0 -121
@@ -1,270 +0,0 @@
1
- import type { Toast, ToastConfig, ToastOptions, ToastState, ToastTheme, ToastType, ToastTypeColors } from "./types";
2
-
3
- export type Listener = (state: ToastState) => void;
4
-
5
- const EXIT_DURATION = 350;
6
-
7
- /** Default theme values */
8
- const DEFAULT_THEME: ToastTheme = {
9
- position: "top",
10
- offset: 0,
11
- stacking: true,
12
- maxStack: 3,
13
- dismissible: true,
14
- showCloseButton: true,
15
- colors: {
16
- success: { accent: "#28B770", background: "#FFFFFF" },
17
- error: { accent: "#F05964", background: "#FFFFFF" },
18
- info: { accent: "#EDBE43", background: "#FFFFFF" },
19
- loading: { accent: "#232323", background: "#FFFFFF" },
20
- },
21
- icons: {},
22
- toastStyle: {},
23
- titleStyle: {},
24
- descriptionStyle: {},
25
- defaultDuration: 4000,
26
- };
27
-
28
- /** Deep merge user config with defaults */
29
- function mergeConfig(config: ToastConfig | undefined): ToastTheme {
30
- if (!config) return DEFAULT_THEME;
31
-
32
- const mergedColors = { ...DEFAULT_THEME.colors };
33
- if (config.colors) {
34
- for (const type of Object.keys(config.colors) as ToastType[]) {
35
- const userColors = config.colors[type];
36
- if (userColors) {
37
- mergedColors[type] = {
38
- ...DEFAULT_THEME.colors[type],
39
- ...userColors,
40
- } as ToastTypeColors;
41
- }
42
- }
43
- }
44
-
45
- return {
46
- position: config.position ?? DEFAULT_THEME.position,
47
- offset: config.offset ?? DEFAULT_THEME.offset,
48
- stacking: config.stacking ?? DEFAULT_THEME.stacking,
49
- maxStack: config.maxStack ?? DEFAULT_THEME.maxStack,
50
- dismissible: config.dismissible ?? DEFAULT_THEME.dismissible,
51
- showCloseButton: config.showCloseButton ?? DEFAULT_THEME.showCloseButton,
52
- colors: mergedColors,
53
- icons: { ...DEFAULT_THEME.icons, ...config.icons },
54
- toastStyle: { ...DEFAULT_THEME.toastStyle, ...config.toastStyle },
55
- titleStyle: { ...DEFAULT_THEME.titleStyle, ...config.titleStyle },
56
- descriptionStyle: { ...DEFAULT_THEME.descriptionStyle, ...config.descriptionStyle },
57
- defaultDuration: config.defaultDuration ?? DEFAULT_THEME.defaultDuration,
58
- };
59
- }
60
-
61
- class ToastStore {
62
- private state: ToastState = {
63
- visibleToasts: [],
64
- };
65
-
66
- private theme: ToastTheme = DEFAULT_THEME;
67
-
68
- private listeners = new Set<Listener>();
69
- private toastIdCounter = 0;
70
- private timeouts = new Map<string, ReturnType<typeof setTimeout>>();
71
-
72
- subscribe = (listener: Listener) => {
73
- this.listeners.add(listener);
74
- return () => {
75
- this.listeners.delete(listener);
76
- };
77
- };
78
-
79
- private emit() {
80
- for (const listener of this.listeners) {
81
- listener(this.state);
82
- }
83
- }
84
-
85
- private setState(partial: Partial<ToastState>) {
86
- this.state = { ...this.state, ...partial };
87
- this.emit();
88
- }
89
-
90
- getState = () => this.state;
91
-
92
- getTheme = () => this.theme;
93
-
94
- setConfig = (config: ToastConfig | undefined) => {
95
- this.theme = mergeConfig(config);
96
- };
97
-
98
- show = (
99
- title: string,
100
- description?: string,
101
- type: ToastType = "success",
102
- duration?: number,
103
- options?: ToastOptions
104
- ): string => {
105
- const actualDuration = duration ?? options?.duration ?? this.theme.defaultDuration;
106
- const maxToasts = this.theme.stacking ? this.theme.maxStack : 1;
107
-
108
- const id = `toast-${++this.toastIdCounter}`;
109
- const newToast: Toast = {
110
- id,
111
- title,
112
- description: description ?? options?.description,
113
- type,
114
- duration: actualDuration,
115
- createdAt: Date.now(),
116
- isExiting: false,
117
- options,
118
- };
119
-
120
- const { visibleToasts } = this.state;
121
-
122
- // Get only non-exiting toasts for count
123
- const activeToasts = visibleToasts.filter(t => !t.isExiting);
124
-
125
- if (activeToasts.length >= maxToasts) {
126
- const toastsToRemove = activeToasts.slice(maxToasts - 1);
127
-
128
- for (const toast of toastsToRemove) {
129
- // Clear auto-dismiss timeout
130
- const timeout = this.timeouts.get(toast.id);
131
- if (timeout) {
132
- clearTimeout(timeout);
133
- this.timeouts.delete(toast.id);
134
- }
135
- }
136
-
137
- const removeIds = new Set(toastsToRemove.map(t => t.id));
138
-
139
- if (this.theme.stacking) {
140
- // When stacking is ON: remove old toasts from state immediately (no animation for stack overflow)
141
- this.setState({
142
- visibleToasts: visibleToasts.filter(t => !removeIds.has(t.id)),
143
- });
144
- } else {
145
- // When stacking is OFF: animate out the old toast, wait, then show new one
146
- this.setState({
147
- visibleToasts: visibleToasts.map(t => (removeIds.has(t.id) ? { ...t, isExiting: true } : t)),
148
- });
149
-
150
- // Delay showing the new toast until the old one has animated out
151
- setTimeout(() => {
152
- for (const toast of toastsToRemove) {
153
- this.removeToast(toast.id);
154
- }
155
- this.addToast(newToast, actualDuration);
156
- }, EXIT_DURATION - 220);
157
-
158
- return id;
159
- }
160
- }
161
-
162
- // Add new toast immediately (stacking ON or no existing toasts)
163
- this.addToast(newToast, actualDuration);
164
-
165
- return id;
166
- };
167
-
168
- private addToast(toast: Toast, duration: number) {
169
- this.setState({
170
- visibleToasts: [toast, ...this.state.visibleToasts.filter(t => !t.isExiting)],
171
- });
172
-
173
- // Schedule auto-dismiss with duration multiplier based on position
174
- this.scheduleTimeout(toast.id, duration, 0);
175
-
176
- // Reschedule timeouts for other toasts based on their new positions
177
- this.rescheduleAllTimeouts();
178
- }
179
-
180
- private scheduleTimeout(id: string, baseDuration: number, index: number) {
181
- const existingTimeout = this.timeouts.get(id);
182
- if (existingTimeout) {
183
- clearTimeout(existingTimeout);
184
- }
185
-
186
- // Duration multiplier: index 0 = 1x, index 1 = 2x, index 2 = 3x
187
- const duration = baseDuration * (index + 1);
188
-
189
- const timeout = setTimeout(() => {
190
- this.hide(id);
191
- }, duration);
192
-
193
- this.timeouts.set(id, timeout);
194
- }
195
-
196
- private rescheduleAllTimeouts() {
197
- const { visibleToasts } = this.state;
198
-
199
- visibleToasts.forEach((toast, index) => {
200
- // Skip if already exiting or index 0 (just scheduled)
201
- if (toast.isExiting || index === 0) return;
202
-
203
- this.scheduleTimeout(toast.id, toast.duration, index);
204
- });
205
- }
206
-
207
- hide = (id: string) => {
208
- const { visibleToasts } = this.state;
209
- const toast = visibleToasts.find(t => t.id === id);
210
- if (!toast || toast.isExiting) return;
211
-
212
- // Clear the auto-dismiss timeout
213
- const timeout = this.timeouts.get(id);
214
- if (timeout) {
215
- clearTimeout(timeout);
216
- this.timeouts.delete(id);
217
- }
218
-
219
- // Mark as exiting (triggers exit animation in component)
220
- this.setState({
221
- visibleToasts: visibleToasts.map(t => (t.id === id ? { ...t, isExiting: true } : t)),
222
- });
223
-
224
- // After exit animation, actually remove the toast
225
- setTimeout(() => {
226
- this.removeToast(id);
227
- }, EXIT_DURATION);
228
- };
229
-
230
- private removeToast(id: string) {
231
- const timeout = this.timeouts.get(id);
232
- if (timeout) {
233
- clearTimeout(timeout);
234
- this.timeouts.delete(id);
235
- }
236
-
237
- this.setState({
238
- visibleToasts: this.state.visibleToasts.filter(t => t.id !== id),
239
- });
240
-
241
- // Reschedule remaining toasts with updated positions
242
- this.rescheduleAllTimeouts();
243
- }
244
-
245
- updateToast = (id: string, data: Partial<Omit<Toast, "id" | "createdAt">>) => {
246
- const { visibleToasts } = this.state;
247
- const index = visibleToasts.findIndex(t => t.id === id);
248
- if (index === -1) return;
249
-
250
- this.setState({
251
- visibleToasts: visibleToasts.map(t => (t.id === id ? { ...t, ...data } : t)),
252
- });
253
-
254
- if (data.duration !== undefined) {
255
- this.scheduleTimeout(id, data.duration, index);
256
- }
257
- };
258
-
259
- hideAll = () => {
260
- for (const timeout of this.timeouts.values()) {
261
- clearTimeout(timeout);
262
- }
263
- this.timeouts.clear();
264
- this.setState({ visibleToasts: [] });
265
- };
266
- }
267
-
268
- export const toastStore = new ToastStore();
269
-
270
- export type { Toast, ToastState, ToastType };