@zag-js/toast 1.45.0 → 1.47.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/dist/toast-group.machine.js +80 -22
- package/dist/toast-group.machine.mjs +81 -23
- package/dist/toast.connect.js +6 -5
- package/dist/toast.connect.mjs +6 -5
- package/dist/toast.dom.d.mts +12 -1
- package/dist/toast.dom.d.ts +12 -1
- package/dist/toast.dom.js +40 -2
- package/dist/toast.dom.mjs +35 -1
- package/dist/toast.machine.js +45 -33
- package/dist/toast.machine.mjs +46 -34
- package/dist/toast.store.js +107 -64
- package/dist/toast.store.mjs +108 -65
- package/dist/toast.types.d.mts +11 -4
- package/dist/toast.types.d.ts +11 -4
- package/dist/toast.utils.d.mts +3 -2
- package/dist/toast.utils.d.ts +3 -2
- package/dist/toast.utils.js +25 -4
- package/dist/toast.utils.mjs +23 -3
- package/package.json +7 -7
package/dist/toast.store.js
CHANGED
|
@@ -36,7 +36,8 @@ var priorities = {
|
|
|
36
36
|
};
|
|
37
37
|
var DEFAULT_TYPE = "info";
|
|
38
38
|
var getPriorityForType = (type, hasAction) => {
|
|
39
|
-
const
|
|
39
|
+
const key = type ?? DEFAULT_TYPE;
|
|
40
|
+
const [actionable, nonActionable] = (0, import_utils.hasProp)(priorities, key) ? priorities[key] : priorities[DEFAULT_TYPE];
|
|
40
41
|
return hasAction ? actionable : nonActionable;
|
|
41
42
|
};
|
|
42
43
|
var sortToastsByPriority = (toastArray) => {
|
|
@@ -61,15 +62,35 @@ function createToastStore(props = {}) {
|
|
|
61
62
|
let toasts = [];
|
|
62
63
|
let dismissedToasts = /* @__PURE__ */ new Set();
|
|
63
64
|
let toastQueue = [];
|
|
65
|
+
const closingToasts = /* @__PURE__ */ new Set();
|
|
66
|
+
const promiseTokens = /* @__PURE__ */ new Map();
|
|
67
|
+
let paused = false;
|
|
68
|
+
const layoutSubscribers = /* @__PURE__ */ new Set();
|
|
64
69
|
const subscribe = (subscriber) => {
|
|
65
70
|
subscribers.push(subscriber);
|
|
71
|
+
let active = true;
|
|
66
72
|
return () => {
|
|
73
|
+
if (!active) return;
|
|
74
|
+
active = false;
|
|
67
75
|
const index = subscribers.indexOf(subscriber);
|
|
68
|
-
subscribers.splice(index, 1);
|
|
76
|
+
if (index !== -1) subscribers.splice(index, 1);
|
|
69
77
|
};
|
|
70
78
|
};
|
|
71
|
-
const
|
|
72
|
-
|
|
79
|
+
const notifications = [];
|
|
80
|
+
let publishing = false;
|
|
81
|
+
const publish = (...data) => {
|
|
82
|
+
notifications.push(...data);
|
|
83
|
+
if (publishing) return data;
|
|
84
|
+
publishing = true;
|
|
85
|
+
try {
|
|
86
|
+
while (notifications.length) {
|
|
87
|
+
const next = notifications.shift();
|
|
88
|
+
subscribers.slice().forEach((subscriber) => subscriber(next));
|
|
89
|
+
}
|
|
90
|
+
} finally {
|
|
91
|
+
publishing = false;
|
|
92
|
+
notifications.length = 0;
|
|
93
|
+
}
|
|
73
94
|
return data;
|
|
74
95
|
};
|
|
75
96
|
const addToast = (data) => {
|
|
@@ -77,30 +98,34 @@ function createToastStore(props = {}) {
|
|
|
77
98
|
toastQueue.push(data);
|
|
78
99
|
return;
|
|
79
100
|
}
|
|
80
|
-
publish(data);
|
|
81
101
|
toasts.unshift(data);
|
|
102
|
+
publish(data);
|
|
82
103
|
};
|
|
83
104
|
const processQueue = () => {
|
|
84
105
|
toastQueue = sortToastsByPriority(toastQueue);
|
|
85
106
|
while (toastQueue.length > 0 && toasts.length < attrs.max) {
|
|
86
107
|
const nextToast = toastQueue.shift();
|
|
87
108
|
if (nextToast) {
|
|
88
|
-
|
|
89
|
-
toasts.unshift(
|
|
109
|
+
const toast = { ...nextToast, paused: paused || nextToast.paused };
|
|
110
|
+
toasts.unshift(toast);
|
|
111
|
+
publish(toast);
|
|
90
112
|
}
|
|
91
113
|
}
|
|
92
114
|
};
|
|
93
115
|
const create = (data) => {
|
|
94
116
|
const id = data.id ?? `toast:${(0, import_utils.uuid)()}`;
|
|
95
|
-
const exists = toasts.find((toast) => toast.id === id);
|
|
96
|
-
if (
|
|
117
|
+
const exists = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
118
|
+
if (closingToasts.has(id)) return id;
|
|
119
|
+
if ((0, import_utils.hasProp)(data, "promise") && data.promise !== exists?.promise) promiseTokens.delete(id);
|
|
120
|
+
dismissedToasts.delete(id);
|
|
97
121
|
if (exists) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
122
|
+
const next = { ...exists, ...data, id };
|
|
123
|
+
if (toasts.some((toast) => toast.id === id)) {
|
|
124
|
+
toasts = toasts.map((toast) => toast.id === id ? next : toast);
|
|
125
|
+
publish(next);
|
|
126
|
+
} else {
|
|
127
|
+
toastQueue = toastQueue.map((toast) => toast.id === id ? next : toast);
|
|
128
|
+
}
|
|
104
129
|
} else {
|
|
105
130
|
const newToast = {
|
|
106
131
|
id,
|
|
@@ -108,8 +133,8 @@ function createToastStore(props = {}) {
|
|
|
108
133
|
removeDelay: attrs.removeDelay,
|
|
109
134
|
type: DEFAULT_TYPE,
|
|
110
135
|
...data,
|
|
111
|
-
|
|
112
|
-
|
|
136
|
+
gap: attrs.gap,
|
|
137
|
+
paused
|
|
113
138
|
};
|
|
114
139
|
const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
|
|
115
140
|
addToast({ ...newToast, priority });
|
|
@@ -117,18 +142,19 @@ function createToastStore(props = {}) {
|
|
|
117
142
|
return id;
|
|
118
143
|
};
|
|
119
144
|
const remove = (id) => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
145
|
+
const removed = id == null ? [...toasts, ...toastQueue] : [...toasts, ...toastQueue].filter((toast) => toast.id === id);
|
|
146
|
+
const visible = id == null ? toasts : toasts.filter((toast) => toast.id === id);
|
|
147
|
+
removed.forEach((toast) => {
|
|
148
|
+
dismissedToasts.add(toast.id);
|
|
149
|
+
closingToasts.delete(toast.id);
|
|
150
|
+
promiseTokens.delete(toast.id);
|
|
151
|
+
});
|
|
152
|
+
if (id != null) dismissedToasts.add(id);
|
|
153
|
+
toasts = id == null ? [] : toasts.filter((toast) => toast.id !== id);
|
|
154
|
+
toastQueue = id == null ? [] : toastQueue.filter((toast) => toast.id !== id);
|
|
155
|
+
if (id != null && !visible.length) publish({ id, dismiss: true });
|
|
156
|
+
publish(...visible.map((toast) => ({ id: toast.id, dismiss: true })));
|
|
157
|
+
processQueue();
|
|
132
158
|
return id;
|
|
133
159
|
};
|
|
134
160
|
const error = (data) => {
|
|
@@ -163,6 +189,10 @@ function createToastStore(props = {}) {
|
|
|
163
189
|
promise: promise2,
|
|
164
190
|
type: "loading"
|
|
165
191
|
});
|
|
192
|
+
const token = /* @__PURE__ */ Symbol();
|
|
193
|
+
const current = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
194
|
+
if (current?.promise === promise2 && !closingToasts.has(id)) promiseTokens.set(id, token);
|
|
195
|
+
const isCurrent = () => promiseTokens.get(id) === token && !closingToasts.has(id);
|
|
166
196
|
let removable = true;
|
|
167
197
|
let result;
|
|
168
198
|
const prom = (0, import_utils.runIfFn)(promise2).then(async (response) => {
|
|
@@ -170,23 +200,24 @@ function createToastStore(props = {}) {
|
|
|
170
200
|
if (isHttpResponse(response) && !response.ok) {
|
|
171
201
|
removable = false;
|
|
172
202
|
const errorOptions = (0, import_utils.runIfFn)(options.error, `HTTP Error! status: ${response.status}`);
|
|
173
|
-
create({ ...shared, ...errorOptions, id, type: "error" });
|
|
203
|
+
if (isCurrent()) create({ ...shared, ...errorOptions, id, type: "error" });
|
|
174
204
|
} else if (options.success !== void 0) {
|
|
175
205
|
removable = false;
|
|
176
206
|
const successOptions = (0, import_utils.runIfFn)(options.success, response);
|
|
177
|
-
create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
|
|
207
|
+
if (isCurrent()) create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
|
|
178
208
|
}
|
|
179
209
|
}).catch(async (error2) => {
|
|
180
210
|
result = ["reject", error2];
|
|
181
211
|
if (options.error !== void 0) {
|
|
182
212
|
removable = false;
|
|
183
213
|
const errorOptions = (0, import_utils.runIfFn)(options.error, error2);
|
|
184
|
-
create({ ...shared, ...errorOptions, id, type: "error" });
|
|
214
|
+
if (isCurrent()) create({ ...shared, ...errorOptions, id, type: "error" });
|
|
185
215
|
}
|
|
186
216
|
}).finally(() => {
|
|
187
|
-
if (removable) {
|
|
217
|
+
if (removable && isCurrent()) {
|
|
188
218
|
remove(id);
|
|
189
219
|
}
|
|
220
|
+
if (promiseTokens.get(id) === token) promiseTokens.delete(id);
|
|
190
221
|
options.finally?.();
|
|
191
222
|
});
|
|
192
223
|
const unwrap = () => new Promise(
|
|
@@ -195,37 +226,46 @@ function createToastStore(props = {}) {
|
|
|
195
226
|
return { id, unwrap };
|
|
196
227
|
};
|
|
197
228
|
const update = (id, data) => {
|
|
198
|
-
|
|
229
|
+
if (closingToasts.has(id)) return id;
|
|
230
|
+
if (typeof data !== "function") return create({ ...data, id });
|
|
231
|
+
const current = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
232
|
+
if (!current) return id;
|
|
233
|
+
const next = { ...data({ ...current }), id };
|
|
234
|
+
if (closingToasts.has(id)) return id;
|
|
235
|
+
if (toasts.some((toast) => toast.id === id)) return create(next);
|
|
236
|
+
if (!toastQueue.some((toast) => toast.id === id)) return id;
|
|
237
|
+
return create(next);
|
|
238
|
+
};
|
|
239
|
+
const changeToasts = (data, id) => {
|
|
240
|
+
const changed = [];
|
|
241
|
+
toasts = toasts.map((toast) => {
|
|
242
|
+
if (id != null && toast.id !== id || closingToasts.has(toast.id)) return toast;
|
|
243
|
+
const next = { ...toast, ...data };
|
|
244
|
+
changed.push(next);
|
|
245
|
+
return next;
|
|
246
|
+
});
|
|
247
|
+
toastQueue = toastQueue.map((toast) => id == null || toast.id === id ? { ...toast, ...data } : toast);
|
|
248
|
+
publish(...changed);
|
|
199
249
|
};
|
|
200
250
|
const pause = (id) => {
|
|
201
|
-
if (id
|
|
202
|
-
|
|
203
|
-
if (toast.id === id) return publish({ ...toast, message: "PAUSE" });
|
|
204
|
-
return toast;
|
|
205
|
-
});
|
|
206
|
-
} else {
|
|
207
|
-
toasts = toasts.map((toast) => publish({ ...toast, message: "PAUSE" }));
|
|
208
|
-
}
|
|
251
|
+
if (id == null) paused = true;
|
|
252
|
+
changeToasts({ paused: true }, id);
|
|
209
253
|
};
|
|
210
254
|
const resume = (id) => {
|
|
211
|
-
if (id
|
|
212
|
-
|
|
213
|
-
if (toast.id === id) return publish({ ...toast, message: "RESUME" });
|
|
214
|
-
return toast;
|
|
215
|
-
});
|
|
216
|
-
} else {
|
|
217
|
-
toasts = toasts.map((toast) => publish({ ...toast, message: "RESUME" }));
|
|
218
|
-
}
|
|
255
|
+
if (id == null) paused = false;
|
|
256
|
+
changeToasts({ paused: false }, id);
|
|
219
257
|
};
|
|
220
258
|
const dismiss = (id) => {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
259
|
+
const queued = toastQueue.filter((toast) => id == null || toast.id === id);
|
|
260
|
+
toastQueue = toastQueue.filter((toast) => id != null && toast.id !== id);
|
|
261
|
+
queued.forEach((toast) => {
|
|
262
|
+
dismissedToasts.add(toast.id);
|
|
263
|
+
promiseTokens.delete(toast.id);
|
|
264
|
+
});
|
|
265
|
+
const changed = toasts.filter((toast) => (id == null || toast.id === id) && !closingToasts.has(toast.id));
|
|
266
|
+
changed.forEach((toast) => closingToasts.add(toast.id));
|
|
267
|
+
toasts = toasts.map((toast) => closingToasts.has(toast.id) ? { ...toast, message: "DISMISS" } : toast);
|
|
268
|
+
publish(...changed.map((toast) => ({ ...toast, message: "DISMISS" })));
|
|
229
269
|
};
|
|
230
270
|
const isVisible = (id) => {
|
|
231
271
|
return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
|
|
@@ -233,12 +273,14 @@ function createToastStore(props = {}) {
|
|
|
233
273
|
const isDismissed = (id) => {
|
|
234
274
|
return dismissedToasts.has(id);
|
|
235
275
|
};
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
276
|
+
const subscribeLayout = (subscriber) => {
|
|
277
|
+
layoutSubscribers.add(subscriber);
|
|
278
|
+
return () => {
|
|
279
|
+
layoutSubscribers.delete(subscriber);
|
|
280
|
+
};
|
|
241
281
|
};
|
|
282
|
+
const expand = () => layoutSubscribers.forEach((subscriber) => subscriber(true));
|
|
283
|
+
const collapse = () => layoutSubscribers.forEach((subscriber) => subscriber(false));
|
|
242
284
|
return {
|
|
243
285
|
attrs,
|
|
244
286
|
subscribe,
|
|
@@ -259,7 +301,8 @@ function createToastStore(props = {}) {
|
|
|
259
301
|
isVisible,
|
|
260
302
|
isDismissed,
|
|
261
303
|
expand,
|
|
262
|
-
collapse
|
|
304
|
+
collapse,
|
|
305
|
+
subscribeLayout
|
|
263
306
|
};
|
|
264
307
|
}
|
|
265
308
|
var isHttpResponse = (data) => {
|
package/dist/toast.store.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/toast.store.ts
|
|
2
|
-
import { compact, runIfFn, uuid, warn } from "@zag-js/utils";
|
|
2
|
+
import { compact, hasProp, runIfFn, uuid, warn } from "@zag-js/utils";
|
|
3
3
|
var withDefaults = (options, defaults) => {
|
|
4
4
|
return { ...defaults, ...compact(options) };
|
|
5
5
|
};
|
|
@@ -12,7 +12,8 @@ var priorities = {
|
|
|
12
12
|
};
|
|
13
13
|
var DEFAULT_TYPE = "info";
|
|
14
14
|
var getPriorityForType = (type, hasAction) => {
|
|
15
|
-
const
|
|
15
|
+
const key = type ?? DEFAULT_TYPE;
|
|
16
|
+
const [actionable, nonActionable] = hasProp(priorities, key) ? priorities[key] : priorities[DEFAULT_TYPE];
|
|
16
17
|
return hasAction ? actionable : nonActionable;
|
|
17
18
|
};
|
|
18
19
|
var sortToastsByPriority = (toastArray) => {
|
|
@@ -37,15 +38,35 @@ function createToastStore(props = {}) {
|
|
|
37
38
|
let toasts = [];
|
|
38
39
|
let dismissedToasts = /* @__PURE__ */ new Set();
|
|
39
40
|
let toastQueue = [];
|
|
41
|
+
const closingToasts = /* @__PURE__ */ new Set();
|
|
42
|
+
const promiseTokens = /* @__PURE__ */ new Map();
|
|
43
|
+
let paused = false;
|
|
44
|
+
const layoutSubscribers = /* @__PURE__ */ new Set();
|
|
40
45
|
const subscribe = (subscriber) => {
|
|
41
46
|
subscribers.push(subscriber);
|
|
47
|
+
let active = true;
|
|
42
48
|
return () => {
|
|
49
|
+
if (!active) return;
|
|
50
|
+
active = false;
|
|
43
51
|
const index = subscribers.indexOf(subscriber);
|
|
44
|
-
subscribers.splice(index, 1);
|
|
52
|
+
if (index !== -1) subscribers.splice(index, 1);
|
|
45
53
|
};
|
|
46
54
|
};
|
|
47
|
-
const
|
|
48
|
-
|
|
55
|
+
const notifications = [];
|
|
56
|
+
let publishing = false;
|
|
57
|
+
const publish = (...data) => {
|
|
58
|
+
notifications.push(...data);
|
|
59
|
+
if (publishing) return data;
|
|
60
|
+
publishing = true;
|
|
61
|
+
try {
|
|
62
|
+
while (notifications.length) {
|
|
63
|
+
const next = notifications.shift();
|
|
64
|
+
subscribers.slice().forEach((subscriber) => subscriber(next));
|
|
65
|
+
}
|
|
66
|
+
} finally {
|
|
67
|
+
publishing = false;
|
|
68
|
+
notifications.length = 0;
|
|
69
|
+
}
|
|
49
70
|
return data;
|
|
50
71
|
};
|
|
51
72
|
const addToast = (data) => {
|
|
@@ -53,30 +74,34 @@ function createToastStore(props = {}) {
|
|
|
53
74
|
toastQueue.push(data);
|
|
54
75
|
return;
|
|
55
76
|
}
|
|
56
|
-
publish(data);
|
|
57
77
|
toasts.unshift(data);
|
|
78
|
+
publish(data);
|
|
58
79
|
};
|
|
59
80
|
const processQueue = () => {
|
|
60
81
|
toastQueue = sortToastsByPriority(toastQueue);
|
|
61
82
|
while (toastQueue.length > 0 && toasts.length < attrs.max) {
|
|
62
83
|
const nextToast = toastQueue.shift();
|
|
63
84
|
if (nextToast) {
|
|
64
|
-
|
|
65
|
-
toasts.unshift(
|
|
85
|
+
const toast = { ...nextToast, paused: paused || nextToast.paused };
|
|
86
|
+
toasts.unshift(toast);
|
|
87
|
+
publish(toast);
|
|
66
88
|
}
|
|
67
89
|
}
|
|
68
90
|
};
|
|
69
91
|
const create = (data) => {
|
|
70
92
|
const id = data.id ?? `toast:${uuid()}`;
|
|
71
|
-
const exists = toasts.find((toast) => toast.id === id);
|
|
72
|
-
if (
|
|
93
|
+
const exists = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
94
|
+
if (closingToasts.has(id)) return id;
|
|
95
|
+
if (hasProp(data, "promise") && data.promise !== exists?.promise) promiseTokens.delete(id);
|
|
96
|
+
dismissedToasts.delete(id);
|
|
73
97
|
if (exists) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
const next = { ...exists, ...data, id };
|
|
99
|
+
if (toasts.some((toast) => toast.id === id)) {
|
|
100
|
+
toasts = toasts.map((toast) => toast.id === id ? next : toast);
|
|
101
|
+
publish(next);
|
|
102
|
+
} else {
|
|
103
|
+
toastQueue = toastQueue.map((toast) => toast.id === id ? next : toast);
|
|
104
|
+
}
|
|
80
105
|
} else {
|
|
81
106
|
const newToast = {
|
|
82
107
|
id,
|
|
@@ -84,8 +109,8 @@ function createToastStore(props = {}) {
|
|
|
84
109
|
removeDelay: attrs.removeDelay,
|
|
85
110
|
type: DEFAULT_TYPE,
|
|
86
111
|
...data,
|
|
87
|
-
|
|
88
|
-
|
|
112
|
+
gap: attrs.gap,
|
|
113
|
+
paused
|
|
89
114
|
};
|
|
90
115
|
const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
|
|
91
116
|
addToast({ ...newToast, priority });
|
|
@@ -93,18 +118,19 @@ function createToastStore(props = {}) {
|
|
|
93
118
|
return id;
|
|
94
119
|
};
|
|
95
120
|
const remove = (id) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
121
|
+
const removed = id == null ? [...toasts, ...toastQueue] : [...toasts, ...toastQueue].filter((toast) => toast.id === id);
|
|
122
|
+
const visible = id == null ? toasts : toasts.filter((toast) => toast.id === id);
|
|
123
|
+
removed.forEach((toast) => {
|
|
124
|
+
dismissedToasts.add(toast.id);
|
|
125
|
+
closingToasts.delete(toast.id);
|
|
126
|
+
promiseTokens.delete(toast.id);
|
|
127
|
+
});
|
|
128
|
+
if (id != null) dismissedToasts.add(id);
|
|
129
|
+
toasts = id == null ? [] : toasts.filter((toast) => toast.id !== id);
|
|
130
|
+
toastQueue = id == null ? [] : toastQueue.filter((toast) => toast.id !== id);
|
|
131
|
+
if (id != null && !visible.length) publish({ id, dismiss: true });
|
|
132
|
+
publish(...visible.map((toast) => ({ id: toast.id, dismiss: true })));
|
|
133
|
+
processQueue();
|
|
108
134
|
return id;
|
|
109
135
|
};
|
|
110
136
|
const error = (data) => {
|
|
@@ -139,6 +165,10 @@ function createToastStore(props = {}) {
|
|
|
139
165
|
promise: promise2,
|
|
140
166
|
type: "loading"
|
|
141
167
|
});
|
|
168
|
+
const token = /* @__PURE__ */ Symbol();
|
|
169
|
+
const current = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
170
|
+
if (current?.promise === promise2 && !closingToasts.has(id)) promiseTokens.set(id, token);
|
|
171
|
+
const isCurrent = () => promiseTokens.get(id) === token && !closingToasts.has(id);
|
|
142
172
|
let removable = true;
|
|
143
173
|
let result;
|
|
144
174
|
const prom = runIfFn(promise2).then(async (response) => {
|
|
@@ -146,23 +176,24 @@ function createToastStore(props = {}) {
|
|
|
146
176
|
if (isHttpResponse(response) && !response.ok) {
|
|
147
177
|
removable = false;
|
|
148
178
|
const errorOptions = runIfFn(options.error, `HTTP Error! status: ${response.status}`);
|
|
149
|
-
create({ ...shared, ...errorOptions, id, type: "error" });
|
|
179
|
+
if (isCurrent()) create({ ...shared, ...errorOptions, id, type: "error" });
|
|
150
180
|
} else if (options.success !== void 0) {
|
|
151
181
|
removable = false;
|
|
152
182
|
const successOptions = runIfFn(options.success, response);
|
|
153
|
-
create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
|
|
183
|
+
if (isCurrent()) create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
|
|
154
184
|
}
|
|
155
185
|
}).catch(async (error2) => {
|
|
156
186
|
result = ["reject", error2];
|
|
157
187
|
if (options.error !== void 0) {
|
|
158
188
|
removable = false;
|
|
159
189
|
const errorOptions = runIfFn(options.error, error2);
|
|
160
|
-
create({ ...shared, ...errorOptions, id, type: "error" });
|
|
190
|
+
if (isCurrent()) create({ ...shared, ...errorOptions, id, type: "error" });
|
|
161
191
|
}
|
|
162
192
|
}).finally(() => {
|
|
163
|
-
if (removable) {
|
|
193
|
+
if (removable && isCurrent()) {
|
|
164
194
|
remove(id);
|
|
165
195
|
}
|
|
196
|
+
if (promiseTokens.get(id) === token) promiseTokens.delete(id);
|
|
166
197
|
options.finally?.();
|
|
167
198
|
});
|
|
168
199
|
const unwrap = () => new Promise(
|
|
@@ -171,37 +202,46 @@ function createToastStore(props = {}) {
|
|
|
171
202
|
return { id, unwrap };
|
|
172
203
|
};
|
|
173
204
|
const update = (id, data) => {
|
|
174
|
-
|
|
205
|
+
if (closingToasts.has(id)) return id;
|
|
206
|
+
if (typeof data !== "function") return create({ ...data, id });
|
|
207
|
+
const current = toasts.find((toast) => toast.id === id) ?? toastQueue.find((toast) => toast.id === id);
|
|
208
|
+
if (!current) return id;
|
|
209
|
+
const next = { ...data({ ...current }), id };
|
|
210
|
+
if (closingToasts.has(id)) return id;
|
|
211
|
+
if (toasts.some((toast) => toast.id === id)) return create(next);
|
|
212
|
+
if (!toastQueue.some((toast) => toast.id === id)) return id;
|
|
213
|
+
return create(next);
|
|
214
|
+
};
|
|
215
|
+
const changeToasts = (data, id) => {
|
|
216
|
+
const changed = [];
|
|
217
|
+
toasts = toasts.map((toast) => {
|
|
218
|
+
if (id != null && toast.id !== id || closingToasts.has(toast.id)) return toast;
|
|
219
|
+
const next = { ...toast, ...data };
|
|
220
|
+
changed.push(next);
|
|
221
|
+
return next;
|
|
222
|
+
});
|
|
223
|
+
toastQueue = toastQueue.map((toast) => id == null || toast.id === id ? { ...toast, ...data } : toast);
|
|
224
|
+
publish(...changed);
|
|
175
225
|
};
|
|
176
226
|
const pause = (id) => {
|
|
177
|
-
if (id
|
|
178
|
-
|
|
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
|
-
}
|
|
227
|
+
if (id == null) paused = true;
|
|
228
|
+
changeToasts({ paused: true }, id);
|
|
185
229
|
};
|
|
186
230
|
const resume = (id) => {
|
|
187
|
-
if (id
|
|
188
|
-
|
|
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
|
-
}
|
|
231
|
+
if (id == null) paused = false;
|
|
232
|
+
changeToasts({ paused: false }, id);
|
|
195
233
|
};
|
|
196
234
|
const dismiss = (id) => {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
235
|
+
const queued = toastQueue.filter((toast) => id == null || toast.id === id);
|
|
236
|
+
toastQueue = toastQueue.filter((toast) => id != null && toast.id !== id);
|
|
237
|
+
queued.forEach((toast) => {
|
|
238
|
+
dismissedToasts.add(toast.id);
|
|
239
|
+
promiseTokens.delete(toast.id);
|
|
240
|
+
});
|
|
241
|
+
const changed = toasts.filter((toast) => (id == null || toast.id === id) && !closingToasts.has(toast.id));
|
|
242
|
+
changed.forEach((toast) => closingToasts.add(toast.id));
|
|
243
|
+
toasts = toasts.map((toast) => closingToasts.has(toast.id) ? { ...toast, message: "DISMISS" } : toast);
|
|
244
|
+
publish(...changed.map((toast) => ({ ...toast, message: "DISMISS" })));
|
|
205
245
|
};
|
|
206
246
|
const isVisible = (id) => {
|
|
207
247
|
return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
|
|
@@ -209,12 +249,14 @@ function createToastStore(props = {}) {
|
|
|
209
249
|
const isDismissed = (id) => {
|
|
210
250
|
return dismissedToasts.has(id);
|
|
211
251
|
};
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
252
|
+
const subscribeLayout = (subscriber) => {
|
|
253
|
+
layoutSubscribers.add(subscriber);
|
|
254
|
+
return () => {
|
|
255
|
+
layoutSubscribers.delete(subscriber);
|
|
256
|
+
};
|
|
217
257
|
};
|
|
258
|
+
const expand = () => layoutSubscribers.forEach((subscriber) => subscriber(true));
|
|
259
|
+
const collapse = () => layoutSubscribers.forEach((subscriber) => subscriber(false));
|
|
218
260
|
return {
|
|
219
261
|
attrs,
|
|
220
262
|
subscribe,
|
|
@@ -235,7 +277,8 @@ function createToastStore(props = {}) {
|
|
|
235
277
|
isVisible,
|
|
236
278
|
isDismissed,
|
|
237
279
|
expand,
|
|
238
|
-
collapse
|
|
280
|
+
collapse,
|
|
281
|
+
subscribeLayout
|
|
239
282
|
};
|
|
240
283
|
}
|
|
241
284
|
var isHttpResponse = (data) => {
|
package/dist/toast.types.d.mts
CHANGED
|
@@ -115,6 +115,10 @@ type ToastSchema<O = any> = {
|
|
|
115
115
|
mounted: boolean;
|
|
116
116
|
initialHeight: number;
|
|
117
117
|
remainingTime: number;
|
|
118
|
+
rendered: {
|
|
119
|
+
title: boolean;
|
|
120
|
+
description: boolean;
|
|
121
|
+
};
|
|
118
122
|
};
|
|
119
123
|
computed: {
|
|
120
124
|
height: number;
|
|
@@ -181,7 +185,7 @@ interface ToastStoreProps {
|
|
|
181
185
|
removeDelay?: number | undefined;
|
|
182
186
|
/**
|
|
183
187
|
* Whether to pause toast when the user leaves the browser tab
|
|
184
|
-
* @default
|
|
188
|
+
* @default true
|
|
185
189
|
*/
|
|
186
190
|
pauseOnPageIdle?: boolean | undefined;
|
|
187
191
|
}
|
|
@@ -227,13 +231,16 @@ interface ToastStore<V = any> {
|
|
|
227
231
|
*/
|
|
228
232
|
subscribe: (subscriber: (...args: any[]) => void) => VoidFunction;
|
|
229
233
|
/**
|
|
230
|
-
* Create a
|
|
234
|
+
* Create a toast or update an existing toast with the same ID.
|
|
235
|
+
* Calls targeting closing toasts are ignored.
|
|
231
236
|
*/
|
|
232
237
|
create: (data: Options<V>) => string;
|
|
233
238
|
/**
|
|
234
|
-
* Update
|
|
239
|
+
* Update a toast with new properties or a function of its current properties.
|
|
240
|
+
* Updates to closing toasts are ignored.
|
|
241
|
+
* Functional updates are also ignored when the toast does not exist.
|
|
235
242
|
*/
|
|
236
|
-
update: (id: string, data: Partial<ToastProps<V
|
|
243
|
+
update: (id: string, data: MaybeFunction<Partial<ToastProps<V>>, Partial<ToastProps<V>>>) => string;
|
|
237
244
|
/**
|
|
238
245
|
* Remove a toast by its ID
|
|
239
246
|
*/
|
package/dist/toast.types.d.ts
CHANGED
|
@@ -115,6 +115,10 @@ type ToastSchema<O = any> = {
|
|
|
115
115
|
mounted: boolean;
|
|
116
116
|
initialHeight: number;
|
|
117
117
|
remainingTime: number;
|
|
118
|
+
rendered: {
|
|
119
|
+
title: boolean;
|
|
120
|
+
description: boolean;
|
|
121
|
+
};
|
|
118
122
|
};
|
|
119
123
|
computed: {
|
|
120
124
|
height: number;
|
|
@@ -181,7 +185,7 @@ interface ToastStoreProps {
|
|
|
181
185
|
removeDelay?: number | undefined;
|
|
182
186
|
/**
|
|
183
187
|
* Whether to pause toast when the user leaves the browser tab
|
|
184
|
-
* @default
|
|
188
|
+
* @default true
|
|
185
189
|
*/
|
|
186
190
|
pauseOnPageIdle?: boolean | undefined;
|
|
187
191
|
}
|
|
@@ -227,13 +231,16 @@ interface ToastStore<V = any> {
|
|
|
227
231
|
*/
|
|
228
232
|
subscribe: (subscriber: (...args: any[]) => void) => VoidFunction;
|
|
229
233
|
/**
|
|
230
|
-
* Create a
|
|
234
|
+
* Create a toast or update an existing toast with the same ID.
|
|
235
|
+
* Calls targeting closing toasts are ignored.
|
|
231
236
|
*/
|
|
232
237
|
create: (data: Options<V>) => string;
|
|
233
238
|
/**
|
|
234
|
-
* Update
|
|
239
|
+
* Update a toast with new properties or a function of its current properties.
|
|
240
|
+
* Updates to closing toasts are ignored.
|
|
241
|
+
* Functional updates are also ignored when the toast does not exist.
|
|
235
242
|
*/
|
|
236
|
-
update: (id: string, data: Partial<ToastProps<V
|
|
243
|
+
update: (id: string, data: MaybeFunction<Partial<ToastProps<V>>, Partial<ToastProps<V>>>) => string;
|
|
237
244
|
/**
|
|
238
245
|
* Remove a toast by its ID
|
|
239
246
|
*/
|
package/dist/toast.utils.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Service } from '@zag-js/core';
|
|
2
2
|
import { Style } from '@zag-js/types';
|
|
3
|
-
import { Type, ToastSchema, ToastGroupService, Placement } from './toast.types.mjs';
|
|
3
|
+
import { Type, ToastSchema, ToastGroupService, Placement, ToastGroupSchema, ToastHeight } from './toast.types.mjs';
|
|
4
4
|
import '@zag-js/dom-query';
|
|
5
5
|
|
|
6
6
|
declare const defaultTimeouts: Record<Type, number>;
|
|
@@ -9,5 +9,6 @@ declare function getGroupPlacementStyle(service: ToastGroupService, placement: P
|
|
|
9
9
|
declare function getPlacementStyle(service: Service<ToastSchema>, visible: boolean): Style;
|
|
10
10
|
declare function getGhostBeforeStyle(service: Service<ToastSchema>, visible: boolean): Style;
|
|
11
11
|
declare function getGhostAfterStyle(): Style;
|
|
12
|
+
declare function setHeight(parent: Service<ToastGroupSchema>, item: ToastHeight): void;
|
|
12
13
|
|
|
13
|
-
export { defaultTimeouts, getGhostAfterStyle, getGhostBeforeStyle, getGroupPlacementStyle, getPlacementStyle, getToastDuration };
|
|
14
|
+
export { defaultTimeouts, getGhostAfterStyle, getGhostBeforeStyle, getGroupPlacementStyle, getPlacementStyle, getToastDuration, setHeight };
|