@zag-js/toast 2.0.0-next.2 → 2.0.0-next.4

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 (45) hide show
  1. package/dist/index.d.mts +10 -17
  2. package/dist/index.d.ts +10 -17
  3. package/dist/index.js +18 -45
  4. package/dist/index.mjs +5 -11
  5. package/dist/toast-group.connect.d.mts +4 -8
  6. package/dist/toast-group.connect.d.ts +4 -8
  7. package/dist/toast-group.connect.js +82 -103
  8. package/dist/toast-group.connect.mjs +77 -67
  9. package/dist/toast-group.machine.d.mts +2 -8
  10. package/dist/toast-group.machine.d.ts +2 -8
  11. package/dist/toast-group.machine.js +295 -290
  12. package/dist/toast-group.machine.mjs +290 -254
  13. package/dist/toast.anatomy.d.mts +2 -6
  14. package/dist/toast.anatomy.d.ts +2 -6
  15. package/dist/toast.anatomy.js +8 -40
  16. package/dist/toast.anatomy.mjs +4 -14
  17. package/dist/toast.connect.d.mts +4 -8
  18. package/dist/toast.connect.d.ts +4 -8
  19. package/dist/toast.connect.js +176 -160
  20. package/dist/toast.connect.mjs +171 -124
  21. package/dist/toast.dom.d.mts +21 -14
  22. package/dist/toast.dom.d.ts +21 -14
  23. package/dist/toast.dom.js +50 -48
  24. package/dist/toast.dom.mjs +35 -17
  25. package/dist/toast.machine.d.mts +2 -8
  26. package/dist/toast.machine.d.ts +2 -8
  27. package/dist/toast.machine.js +404 -288
  28. package/dist/toast.machine.mjs +400 -253
  29. package/dist/toast.store.d.mts +2 -8
  30. package/dist/toast.store.d.ts +2 -8
  31. package/dist/toast.store.js +345 -263
  32. package/dist/toast.store.mjs +341 -238
  33. package/dist/toast.types.d.mts +460 -427
  34. package/dist/toast.types.d.ts +460 -427
  35. package/dist/toast.types.js +4 -18
  36. package/dist/toast.types.mjs +1 -0
  37. package/dist/toast.utils.d.mts +10 -13
  38. package/dist/toast.utils.d.ts +10 -13
  39. package/dist/toast.utils.js +185 -198
  40. package/dist/toast.utils.mjs +175 -168
  41. package/dist/utils/swipe.d.mts +16 -0
  42. package/dist/utils/swipe.d.ts +16 -0
  43. package/dist/utils/swipe.js +100 -0
  44. package/dist/utils/swipe.mjs +93 -0
  45. package/package.json +8 -8
@@ -1,246 +1,349 @@
1
+ import { compact, hasProp, runIfFn, uuid, warn } from "@zag-js/utils";
1
2
  // src/toast.store.ts
2
- import { compact, runIfFn, uuid, warn } from "@zag-js/utils";
3
- var withDefaults = (options, defaults) => {
4
- return { ...defaults, ...compact(options) };
3
+ const withDefaults = (options, defaults) => {
4
+ return {
5
+ ...defaults,
6
+ ...compact(options)
7
+ };
5
8
  };
6
- var priorities = {
7
- error: [1, 2],
8
- warning: [3, 6],
9
- loading: [4, 5],
10
- success: [5, 7],
11
- info: [6, 8]
9
+ const priorities = {
10
+ error: [1, 2],
11
+ warning: [3, 6],
12
+ loading: [4, 5],
13
+ success: [5, 7],
14
+ info: [6, 8]
12
15
  };
13
- var DEFAULT_TYPE = "info";
14
- var getPriorityForType = (type, hasAction) => {
15
- const [actionable, nonActionable] = priorities[type ?? DEFAULT_TYPE];
16
- return hasAction ? actionable : nonActionable;
16
+ const DEFAULT_TYPE = "info";
17
+ const getPriorityForType = (type, hasAction) => {
18
+ const key = type ?? DEFAULT_TYPE;
19
+ const [actionable, nonActionable] = hasProp(priorities, key) ? priorities[key] : priorities[DEFAULT_TYPE];
20
+ return hasAction ? actionable : nonActionable;
17
21
  };
18
- var sortToastsByPriority = (toastArray) => {
19
- return toastArray.sort((a, b) => {
20
- const priorityA = a.priority ?? getPriorityForType(a.type, !!a.action);
21
- const priorityB = b.priority ?? getPriorityForType(b.type, !!b.action);
22
- return priorityA - priorityB;
23
- });
22
+ const sortToastsByPriority = (toastArray) => {
23
+ return toastArray.sort((a, b) => {
24
+ return (a.priority ?? getPriorityForType(a.type, !!a.action)) - (b.priority ?? getPriorityForType(b.type, !!b.action));
25
+ });
24
26
  };
25
27
  function createToastStore(props = {}) {
26
- const attrs = withDefaults(props, {
27
- placement: "bottom",
28
- overlap: false,
29
- max: 24,
30
- gap: 16,
31
- offsets: "1rem",
32
- hotkey: ["altKey", "KeyT"],
33
- removeDelay: 200,
34
- pauseOnPageIdle: true
35
- });
36
- let subscribers = [];
37
- let toasts = [];
38
- let dismissedToasts = /* @__PURE__ */ new Set();
39
- let toastQueue = [];
40
- const subscribe = (subscriber) => {
41
- subscribers.push(subscriber);
42
- return () => {
43
- const index = subscribers.indexOf(subscriber);
44
- subscribers.splice(index, 1);
45
- };
46
- };
47
- const publish = (data) => {
48
- subscribers.forEach((subscriber) => subscriber(data));
49
- return data;
50
- };
51
- const addToast = (data) => {
52
- if (toasts.length >= attrs.max) {
53
- toastQueue.push(data);
54
- return;
55
- }
56
- publish(data);
57
- toasts.unshift(data);
58
- };
59
- const processQueue = () => {
60
- toastQueue = sortToastsByPriority(toastQueue);
61
- while (toastQueue.length > 0 && toasts.length < attrs.max) {
62
- const nextToast = toastQueue.shift();
63
- if (nextToast) {
64
- publish(nextToast);
65
- toasts.unshift(nextToast);
66
- }
67
- }
68
- };
69
- const create = (data) => {
70
- const id = data.id ?? `toast:${uuid()}`;
71
- const exists = toasts.find((toast) => toast.id === id);
72
- if (dismissedToasts.has(id)) dismissedToasts.delete(id);
73
- if (exists) {
74
- toasts = toasts.map((toast) => {
75
- if (toast.id === id) {
76
- return publish({ ...toast, ...data, id });
77
- }
78
- return toast;
79
- });
80
- } else {
81
- const newToast = {
82
- id,
83
- duration: attrs.duration,
84
- removeDelay: attrs.removeDelay,
85
- type: DEFAULT_TYPE,
86
- ...data,
87
- stacked: !attrs.overlap,
88
- gap: attrs.gap
89
- };
90
- const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
91
- addToast({ ...newToast, priority });
92
- }
93
- return id;
94
- };
95
- const remove = (id) => {
96
- dismissedToasts.add(id);
97
- if (!id) {
98
- toasts.forEach((toast) => {
99
- subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
100
- });
101
- toasts = [];
102
- toastQueue = [];
103
- } else {
104
- subscribers.forEach((subscriber) => subscriber({ id, dismiss: true }));
105
- toasts = toasts.filter((toast) => toast.id !== id);
106
- processQueue();
107
- }
108
- return id;
109
- };
110
- const error = (data) => {
111
- return create({ ...data, type: "error" });
112
- };
113
- const success = (data) => {
114
- return create({ ...data, type: "success" });
115
- };
116
- const info = (data) => {
117
- return create({ ...data, type: "info" });
118
- };
119
- const warning = (data) => {
120
- return create({ ...data, type: "warning" });
121
- };
122
- const loading = (data) => {
123
- return create({ ...data, type: "loading" });
124
- };
125
- const getVisibleToasts = () => {
126
- return toasts.filter((toast) => !dismissedToasts.has(toast.id));
127
- };
128
- const getCount = () => {
129
- return toasts.length;
130
- };
131
- const promise = (promise2, options, shared = {}) => {
132
- if (!options || !options.loading) {
133
- warn("[zag-js > toast] toaster.promise() requires at least a 'loading' option to be specified");
134
- return;
135
- }
136
- const id = create({
137
- ...shared,
138
- ...options.loading,
139
- promise: promise2,
140
- type: "loading"
141
- });
142
- let removable = true;
143
- let result;
144
- const prom = runIfFn(promise2).then(async (response) => {
145
- result = ["resolve", response];
146
- if (isHttpResponse(response) && !response.ok) {
147
- removable = false;
148
- const errorOptions = runIfFn(options.error, `HTTP Error! status: ${response.status}`);
149
- create({ ...shared, ...errorOptions, id, type: "error" });
150
- } else if (options.success !== void 0) {
151
- removable = false;
152
- const successOptions = runIfFn(options.success, response);
153
- create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
154
- }
155
- }).catch(async (error2) => {
156
- result = ["reject", error2];
157
- if (options.error !== void 0) {
158
- removable = false;
159
- const errorOptions = runIfFn(options.error, error2);
160
- create({ ...shared, ...errorOptions, id, type: "error" });
161
- }
162
- }).finally(() => {
163
- if (removable) {
164
- remove(id);
165
- }
166
- options.finally?.();
167
- });
168
- const unwrap = () => new Promise(
169
- (resolve, reject) => prom.then(() => result[0] === "reject" ? reject(result[1]) : resolve(result[1])).catch(reject)
170
- );
171
- return { id, unwrap };
172
- };
173
- const update = (id, data) => {
174
- return create({ id, ...data });
175
- };
176
- const pause = (id) => {
177
- if (id != null) {
178
- toasts = toasts.map((toast) => {
179
- if (toast.id === id) return publish({ ...toast, message: "PAUSE" });
180
- return toast;
181
- });
182
- } else {
183
- toasts = toasts.map((toast) => publish({ ...toast, message: "PAUSE" }));
184
- }
185
- };
186
- const resume = (id) => {
187
- if (id != null) {
188
- toasts = toasts.map((toast) => {
189
- if (toast.id === id) return publish({ ...toast, message: "RESUME" });
190
- return toast;
191
- });
192
- } else {
193
- toasts = toasts.map((toast) => publish({ ...toast, message: "RESUME" }));
194
- }
195
- };
196
- const dismiss = (id) => {
197
- if (id != null) {
198
- toasts = toasts.map((toast) => {
199
- if (toast.id === id) return publish({ ...toast, message: "DISMISS" });
200
- return toast;
201
- });
202
- } else {
203
- toasts = toasts.map((toast) => publish({ ...toast, message: "DISMISS" }));
204
- }
205
- };
206
- const isVisible = (id) => {
207
- return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
208
- };
209
- const isDismissed = (id) => {
210
- return dismissedToasts.has(id);
211
- };
212
- const expand = () => {
213
- toasts = toasts.map((toast) => publish({ ...toast, stacked: true }));
214
- };
215
- const collapse = () => {
216
- toasts = toasts.map((toast) => publish({ ...toast, stacked: false }));
217
- };
218
- return {
219
- attrs,
220
- subscribe,
221
- create,
222
- update,
223
- remove,
224
- dismiss,
225
- error,
226
- success,
227
- info,
228
- warning,
229
- loading,
230
- getVisibleToasts,
231
- getCount,
232
- promise,
233
- pause,
234
- resume,
235
- isVisible,
236
- isDismissed,
237
- expand,
238
- collapse
239
- };
28
+ const attrs = withDefaults(props, {
29
+ placement: "bottom",
30
+ overlap: false,
31
+ max: 24,
32
+ gap: 16,
33
+ offsets: "1rem",
34
+ hotkey: ["altKey", "KeyT"],
35
+ removeDelay: 200,
36
+ pauseOnPageIdle: true
37
+ });
38
+ let subscribers = [];
39
+ let toasts = [];
40
+ let dismissedToasts = /* @__PURE__ */ new Set();
41
+ let toastQueue = [];
42
+ const closingToasts = /* @__PURE__ */ new Set();
43
+ const promiseTokens = /* @__PURE__ */ new Map();
44
+ let paused = false;
45
+ const layoutSubscribers = /* @__PURE__ */ new Set();
46
+ const subscribe = (subscriber) => {
47
+ subscribers.push(subscriber);
48
+ let active = true;
49
+ return () => {
50
+ if (!active) return;
51
+ active = false;
52
+ const index = subscribers.indexOf(subscriber);
53
+ if (index !== -1) subscribers.splice(index, 1);
54
+ };
55
+ };
56
+ const notifications = [];
57
+ let publishing = false;
58
+ const publish = (...data) => {
59
+ notifications.push(...data);
60
+ if (publishing) return data;
61
+ publishing = true;
62
+ try {
63
+ while (notifications.length) {
64
+ const next = notifications.shift();
65
+ subscribers.slice().forEach((subscriber) => subscriber(next));
66
+ }
67
+ } finally {
68
+ publishing = false;
69
+ notifications.length = 0;
70
+ }
71
+ return data;
72
+ };
73
+ const addToast = (data) => {
74
+ if (toasts.length >= attrs.max) {
75
+ toastQueue.push(data);
76
+ return;
77
+ }
78
+ toasts.unshift(data);
79
+ publish(data);
80
+ };
81
+ const processQueue = () => {
82
+ toastQueue = sortToastsByPriority(toastQueue);
83
+ while (toastQueue.length > 0 && toasts.length < attrs.max) {
84
+ const nextToast = toastQueue.shift();
85
+ if (nextToast) {
86
+ const toast = {
87
+ ...nextToast,
88
+ paused: paused || nextToast.paused
89
+ };
90
+ toasts.unshift(toast);
91
+ publish(toast);
92
+ }
93
+ }
94
+ };
95
+ const create = (data) => {
96
+ const id = data.id ?? `toast:${uuid()}`;
97
+ const exists = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
98
+ if (closingToasts.has(id)) return id;
99
+ if (hasProp(data, "promise") && data.promise !== exists?.promise) promiseTokens.delete(id);
100
+ dismissedToasts.delete(id);
101
+ if (exists) {
102
+ const next = {
103
+ ...exists,
104
+ ...data,
105
+ id
106
+ };
107
+ if (toasts.some((toast) => toast.id === id)) {
108
+ toasts = toasts.map((toast) => toast.id === id ? next : toast);
109
+ publish(next);
110
+ } else toastQueue = toastQueue.map((toast) => toast.id === id ? next : toast);
111
+ } else {
112
+ const newToast = {
113
+ id,
114
+ duration: attrs.duration,
115
+ removeDelay: attrs.removeDelay,
116
+ swipeDirections: attrs.swipeDirections,
117
+ type: DEFAULT_TYPE,
118
+ ...data,
119
+ gap: attrs.gap,
120
+ paused
121
+ };
122
+ const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
123
+ addToast({
124
+ ...newToast,
125
+ priority
126
+ });
127
+ }
128
+ return id;
129
+ };
130
+ const remove = (id) => {
131
+ const removed = id == null ? [...toasts, ...toastQueue] : [...toasts, ...toastQueue].filter((toast) => toast.id === id);
132
+ const visible = id == null ? toasts : toasts.filter((toast) => toast.id === id);
133
+ removed.forEach((toast) => {
134
+ dismissedToasts.add(toast.id);
135
+ closingToasts.delete(toast.id);
136
+ promiseTokens.delete(toast.id);
137
+ });
138
+ if (id != null) dismissedToasts.add(id);
139
+ toasts = id == null ? [] : toasts.filter((toast) => toast.id !== id);
140
+ toastQueue = id == null ? [] : toastQueue.filter((toast) => toast.id !== id);
141
+ if (id != null && !visible.length) publish({
142
+ id,
143
+ dismiss: true
144
+ });
145
+ publish(...visible.map((toast) => ({
146
+ id: toast.id,
147
+ dismiss: true
148
+ })));
149
+ processQueue();
150
+ return id;
151
+ };
152
+ const error = (data) => {
153
+ return create({
154
+ ...data,
155
+ type: "error"
156
+ });
157
+ };
158
+ const success = (data) => {
159
+ return create({
160
+ ...data,
161
+ type: "success"
162
+ });
163
+ };
164
+ const info = (data) => {
165
+ return create({
166
+ ...data,
167
+ type: "info"
168
+ });
169
+ };
170
+ const warning = (data) => {
171
+ return create({
172
+ ...data,
173
+ type: "warning"
174
+ });
175
+ };
176
+ const loading = (data) => {
177
+ return create({
178
+ ...data,
179
+ type: "loading"
180
+ });
181
+ };
182
+ const getVisibleToasts = () => {
183
+ return toasts.filter((toast) => !dismissedToasts.has(toast.id));
184
+ };
185
+ const getCount = () => {
186
+ return toasts.length;
187
+ };
188
+ const promise = (promise, options, shared = {}) => {
189
+ if (!options || !options.loading) {
190
+ warn("[zag-js > toast] toaster.promise() requires at least a 'loading' option to be specified");
191
+ return;
192
+ }
193
+ const id = create({
194
+ ...shared,
195
+ ...options.loading,
196
+ promise,
197
+ type: "loading"
198
+ });
199
+ const token = Symbol();
200
+ if ((toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id))?.promise === promise && !closingToasts.has(id)) promiseTokens.set(id, token);
201
+ const isCurrent = () => promiseTokens.get(id) === token && !closingToasts.has(id);
202
+ let removable = true;
203
+ let result;
204
+ const prom = runIfFn(promise).then(async (response) => {
205
+ result = ["resolve", response];
206
+ if (isHttpResponse(response) && !response.ok) {
207
+ removable = false;
208
+ const errorOptions = runIfFn(options.error, `HTTP Error! status: ${response.status}`);
209
+ if (isCurrent()) create({
210
+ ...shared,
211
+ ...errorOptions,
212
+ id,
213
+ type: "error"
214
+ });
215
+ } else if (options.success !== void 0) {
216
+ removable = false;
217
+ const successOptions = runIfFn(options.success, response);
218
+ if (isCurrent()) create({
219
+ ...shared,
220
+ ...successOptions,
221
+ id,
222
+ type: successOptions.type ?? "success"
223
+ });
224
+ }
225
+ }).catch(async (error) => {
226
+ result = ["reject", error];
227
+ if (options.error !== void 0) {
228
+ removable = false;
229
+ const errorOptions = runIfFn(options.error, error);
230
+ if (isCurrent()) create({
231
+ ...shared,
232
+ ...errorOptions,
233
+ id,
234
+ type: "error"
235
+ });
236
+ }
237
+ }).finally(() => {
238
+ if (removable && isCurrent()) remove(id);
239
+ if (promiseTokens.get(id) === token) promiseTokens.delete(id);
240
+ options.finally?.();
241
+ });
242
+ const unwrap = () => new Promise((resolve, reject) => prom.then(() => result[0] === "reject" ? reject(result[1]) : resolve(result[1])).catch(reject));
243
+ return {
244
+ id,
245
+ unwrap
246
+ };
247
+ };
248
+ const update = (id, data) => {
249
+ if (closingToasts.has(id)) return id;
250
+ if (typeof data !== "function") return create({
251
+ ...data,
252
+ id
253
+ });
254
+ const current = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
255
+ if (!current) return id;
256
+ const next = {
257
+ ...data({ ...current }),
258
+ id
259
+ };
260
+ if (closingToasts.has(id)) return id;
261
+ if (toasts.some((toast) => toast.id === id)) return create(next);
262
+ if (!toastQueue.some((toast) => toast.id === id)) return id;
263
+ return create(next);
264
+ };
265
+ const changeToasts = (data, id) => {
266
+ const changed = [];
267
+ toasts = toasts.map((toast) => {
268
+ if (id != null && toast.id !== id || closingToasts.has(toast.id)) return toast;
269
+ const next = {
270
+ ...toast,
271
+ ...data
272
+ };
273
+ changed.push(next);
274
+ return next;
275
+ });
276
+ toastQueue = toastQueue.map((toast) => id == null || toast.id === id ? {
277
+ ...toast,
278
+ ...data
279
+ } : toast);
280
+ publish(...changed);
281
+ };
282
+ const pause = (id) => {
283
+ if (id == null) paused = true;
284
+ changeToasts({ paused: true }, id);
285
+ };
286
+ const resume = (id) => {
287
+ if (id == null) paused = false;
288
+ changeToasts({ paused: false }, id);
289
+ };
290
+ const dismiss = (id) => {
291
+ const queued = toastQueue.filter((toast) => id == null || toast.id === id);
292
+ toastQueue = toastQueue.filter((toast) => id != null && toast.id !== id);
293
+ queued.forEach((toast) => {
294
+ dismissedToasts.add(toast.id);
295
+ promiseTokens.delete(toast.id);
296
+ });
297
+ const changed = toasts.filter((toast) => (id == null || toast.id === id) && !closingToasts.has(toast.id));
298
+ changed.forEach((toast) => closingToasts.add(toast.id));
299
+ toasts = toasts.map((toast) => closingToasts.has(toast.id) ? {
300
+ ...toast,
301
+ message: "DISMISS"
302
+ } : toast);
303
+ publish(...changed.map((toast) => ({
304
+ ...toast,
305
+ message: "DISMISS"
306
+ })));
307
+ };
308
+ const isVisible = (id) => {
309
+ return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
310
+ };
311
+ const isDismissed = (id) => {
312
+ return dismissedToasts.has(id);
313
+ };
314
+ const subscribeLayout = (subscriber) => {
315
+ layoutSubscribers.add(subscriber);
316
+ return () => {
317
+ layoutSubscribers.delete(subscriber);
318
+ };
319
+ };
320
+ const expand = () => layoutSubscribers.forEach((subscriber) => subscriber(true));
321
+ const collapse = () => layoutSubscribers.forEach((subscriber) => subscriber(false));
322
+ return {
323
+ attrs,
324
+ subscribe,
325
+ create,
326
+ update,
327
+ remove,
328
+ dismiss,
329
+ error,
330
+ success,
331
+ info,
332
+ warning,
333
+ loading,
334
+ getVisibleToasts,
335
+ getCount,
336
+ promise,
337
+ pause,
338
+ resume,
339
+ isVisible,
340
+ isDismissed,
341
+ expand,
342
+ collapse,
343
+ subscribeLayout
344
+ };
240
345
  }
241
- var isHttpResponse = (data) => {
242
- return data && typeof data === "object" && "ok" in data && typeof data.ok === "boolean" && "status" in data && typeof data.status === "number";
243
- };
244
- export {
245
- createToastStore
346
+ const isHttpResponse = (data) => {
347
+ return data && typeof data === "object" && "ok" in data && typeof data.ok === "boolean" && "status" in data && typeof data.status === "number";
246
348
  };
349
+ export { createToastStore };