@zag-js/toast 1.34.0 → 1.35.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 (41) hide show
  1. package/dist/index.d.mts +11 -365
  2. package/dist/index.d.ts +11 -365
  3. package/dist/index.js +40 -1084
  4. package/dist/index.mjs +13 -1080
  5. package/dist/toast-group.connect.d.mts +8 -0
  6. package/dist/toast-group.connect.d.ts +8 -0
  7. package/dist/toast-group.connect.js +98 -0
  8. package/dist/toast-group.connect.mjs +63 -0
  9. package/dist/toast-group.machine.d.mts +8 -0
  10. package/dist/toast-group.machine.d.ts +8 -0
  11. package/dist/toast-group.machine.js +292 -0
  12. package/dist/toast-group.machine.mjs +257 -0
  13. package/dist/toast.anatomy.d.mts +6 -0
  14. package/dist/toast.anatomy.d.ts +6 -0
  15. package/dist/toast.anatomy.js +41 -0
  16. package/dist/toast.anatomy.mjs +15 -0
  17. package/dist/toast.connect.d.mts +8 -0
  18. package/dist/toast.connect.d.ts +8 -0
  19. package/dist/toast.connect.js +155 -0
  20. package/dist/toast.connect.mjs +120 -0
  21. package/dist/toast.dom.d.mts +14 -0
  22. package/dist/toast.dom.d.ts +14 -0
  23. package/dist/toast.dom.js +48 -0
  24. package/dist/toast.dom.mjs +17 -0
  25. package/dist/toast.machine.d.mts +8 -0
  26. package/dist/toast.machine.d.ts +8 -0
  27. package/dist/toast.machine.js +291 -0
  28. package/dist/toast.machine.mjs +256 -0
  29. package/dist/toast.store.d.mts +8 -0
  30. package/dist/toast.store.d.ts +8 -0
  31. package/dist/toast.store.js +249 -0
  32. package/dist/toast.store.mjs +224 -0
  33. package/dist/toast.types.d.mts +371 -0
  34. package/dist/toast.types.d.ts +371 -0
  35. package/dist/toast.types.js +18 -0
  36. package/dist/toast.types.mjs +0 -0
  37. package/dist/toast.utils.d.mts +13 -0
  38. package/dist/toast.utils.d.ts +13 -0
  39. package/dist/toast.utils.js +209 -0
  40. package/dist/toast.utils.mjs +179 -0
  41. package/package.json +18 -8
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/toast.store.ts
21
+ var toast_store_exports = {};
22
+ __export(toast_store_exports, {
23
+ createToastStore: () => createToastStore
24
+ });
25
+ module.exports = __toCommonJS(toast_store_exports);
26
+ var import_utils = require("@zag-js/utils");
27
+ var withDefaults = (options, defaults) => {
28
+ return { ...defaults, ...(0, import_utils.compact)(options) };
29
+ };
30
+ function createToastStore(props = {}) {
31
+ const attrs = withDefaults(props, {
32
+ placement: "bottom",
33
+ overlap: false,
34
+ max: 24,
35
+ gap: 16,
36
+ offsets: "1rem",
37
+ hotkey: ["altKey", "KeyT"],
38
+ removeDelay: 200,
39
+ pauseOnPageIdle: true
40
+ });
41
+ let subscribers = [];
42
+ let toasts = [];
43
+ let dismissedToasts = /* @__PURE__ */ new Set();
44
+ let toastQueue = [];
45
+ const subscribe = (subscriber) => {
46
+ subscribers.push(subscriber);
47
+ return () => {
48
+ const index = subscribers.indexOf(subscriber);
49
+ subscribers.splice(index, 1);
50
+ };
51
+ };
52
+ const publish = (data) => {
53
+ subscribers.forEach((subscriber) => subscriber(data));
54
+ return data;
55
+ };
56
+ const addToast = (data) => {
57
+ if (toasts.length >= attrs.max) {
58
+ toastQueue.push(data);
59
+ return;
60
+ }
61
+ publish(data);
62
+ toasts.unshift(data);
63
+ };
64
+ const processQueue = () => {
65
+ while (toastQueue.length > 0 && toasts.length < attrs.max) {
66
+ const nextToast = toastQueue.shift();
67
+ if (nextToast) {
68
+ publish(nextToast);
69
+ toasts.unshift(nextToast);
70
+ }
71
+ }
72
+ };
73
+ const create = (data) => {
74
+ const id = data.id ?? `toast:${(0, import_utils.uuid)()}`;
75
+ const exists = toasts.find((toast) => toast.id === id);
76
+ if (dismissedToasts.has(id)) dismissedToasts.delete(id);
77
+ if (exists) {
78
+ toasts = toasts.map((toast) => {
79
+ if (toast.id === id) {
80
+ return publish({ ...toast, ...data, id });
81
+ }
82
+ return toast;
83
+ });
84
+ } else {
85
+ addToast({
86
+ id,
87
+ duration: attrs.duration,
88
+ removeDelay: attrs.removeDelay,
89
+ type: "info",
90
+ ...data,
91
+ stacked: !attrs.overlap,
92
+ gap: attrs.gap
93
+ });
94
+ }
95
+ return id;
96
+ };
97
+ const remove = (id) => {
98
+ dismissedToasts.add(id);
99
+ if (!id) {
100
+ toasts.forEach((toast) => {
101
+ subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
102
+ });
103
+ toasts = [];
104
+ toastQueue = [];
105
+ } else {
106
+ subscribers.forEach((subscriber) => subscriber({ id, dismiss: true }));
107
+ toasts = toasts.filter((toast) => toast.id !== id);
108
+ processQueue();
109
+ }
110
+ return id;
111
+ };
112
+ const error = (data) => {
113
+ return create({ ...data, type: "error" });
114
+ };
115
+ const success = (data) => {
116
+ return create({ ...data, type: "success" });
117
+ };
118
+ const info = (data) => {
119
+ return create({ ...data, type: "info" });
120
+ };
121
+ const warning = (data) => {
122
+ return create({ ...data, type: "warning" });
123
+ };
124
+ const loading = (data) => {
125
+ return create({ ...data, type: "loading" });
126
+ };
127
+ const getVisibleToasts = () => {
128
+ return toasts.filter((toast) => !dismissedToasts.has(toast.id));
129
+ };
130
+ const getCount = () => {
131
+ return toasts.length;
132
+ };
133
+ const promise = (promise2, options, shared = {}) => {
134
+ if (!options || !options.loading) {
135
+ (0, import_utils.warn)("[zag-js > toast] toaster.promise() requires at least a 'loading' option to be specified");
136
+ return;
137
+ }
138
+ const id = create({
139
+ ...shared,
140
+ ...options.loading,
141
+ promise: promise2,
142
+ type: "loading"
143
+ });
144
+ let removable = true;
145
+ let result;
146
+ const prom = (0, import_utils.runIfFn)(promise2).then(async (response) => {
147
+ result = ["resolve", response];
148
+ if (isHttpResponse(response) && !response.ok) {
149
+ removable = false;
150
+ const errorOptions = (0, import_utils.runIfFn)(options.error, `HTTP Error! status: ${response.status}`);
151
+ create({ ...shared, ...errorOptions, id, type: "error" });
152
+ } else if (options.success !== void 0) {
153
+ removable = false;
154
+ const successOptions = (0, import_utils.runIfFn)(options.success, response);
155
+ create({ ...shared, ...successOptions, id, type: "success" });
156
+ }
157
+ }).catch(async (error2) => {
158
+ result = ["reject", error2];
159
+ if (options.error !== void 0) {
160
+ removable = false;
161
+ const errorOptions = (0, import_utils.runIfFn)(options.error, error2);
162
+ create({ ...shared, ...errorOptions, id, type: "error" });
163
+ }
164
+ }).finally(() => {
165
+ if (removable) {
166
+ remove(id);
167
+ }
168
+ options.finally?.();
169
+ });
170
+ const unwrap = () => new Promise(
171
+ (resolve, reject) => prom.then(() => result[0] === "reject" ? reject(result[1]) : resolve(result[1])).catch(reject)
172
+ );
173
+ return { id, unwrap };
174
+ };
175
+ const update = (id, data) => {
176
+ return create({ id, ...data });
177
+ };
178
+ const pause = (id) => {
179
+ if (id != null) {
180
+ toasts = toasts.map((toast) => {
181
+ if (toast.id === id) return publish({ ...toast, message: "PAUSE" });
182
+ return toast;
183
+ });
184
+ } else {
185
+ toasts = toasts.map((toast) => publish({ ...toast, message: "PAUSE" }));
186
+ }
187
+ };
188
+ const resume = (id) => {
189
+ if (id != null) {
190
+ toasts = toasts.map((toast) => {
191
+ if (toast.id === id) return publish({ ...toast, message: "RESUME" });
192
+ return toast;
193
+ });
194
+ } else {
195
+ toasts = toasts.map((toast) => publish({ ...toast, message: "RESUME" }));
196
+ }
197
+ };
198
+ const dismiss = (id) => {
199
+ if (id != null) {
200
+ toasts = toasts.map((toast) => {
201
+ if (toast.id === id) return publish({ ...toast, message: "DISMISS" });
202
+ return toast;
203
+ });
204
+ } else {
205
+ toasts = toasts.map((toast) => publish({ ...toast, message: "DISMISS" }));
206
+ }
207
+ };
208
+ const isVisible = (id) => {
209
+ return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
210
+ };
211
+ const isDismissed = (id) => {
212
+ return dismissedToasts.has(id);
213
+ };
214
+ const expand = () => {
215
+ toasts = toasts.map((toast) => publish({ ...toast, stacked: true }));
216
+ };
217
+ const collapse = () => {
218
+ toasts = toasts.map((toast) => publish({ ...toast, stacked: false }));
219
+ };
220
+ return {
221
+ attrs,
222
+ subscribe,
223
+ create,
224
+ update,
225
+ remove,
226
+ dismiss,
227
+ error,
228
+ success,
229
+ info,
230
+ warning,
231
+ loading,
232
+ getVisibleToasts,
233
+ getCount,
234
+ promise,
235
+ pause,
236
+ resume,
237
+ isVisible,
238
+ isDismissed,
239
+ expand,
240
+ collapse
241
+ };
242
+ }
243
+ var isHttpResponse = (data) => {
244
+ return data && typeof data === "object" && "ok" in data && typeof data.ok === "boolean" && "status" in data && typeof data.status === "number";
245
+ };
246
+ // Annotate the CommonJS export names for ESM import in node:
247
+ 0 && (module.exports = {
248
+ createToastStore
249
+ });
@@ -0,0 +1,224 @@
1
+ // src/toast.store.ts
2
+ import { compact, runIfFn, uuid, warn } from "@zag-js/utils";
3
+ var withDefaults = (options, defaults) => {
4
+ return { ...defaults, ...compact(options) };
5
+ };
6
+ function createToastStore(props = {}) {
7
+ const attrs = withDefaults(props, {
8
+ placement: "bottom",
9
+ overlap: false,
10
+ max: 24,
11
+ gap: 16,
12
+ offsets: "1rem",
13
+ hotkey: ["altKey", "KeyT"],
14
+ removeDelay: 200,
15
+ pauseOnPageIdle: true
16
+ });
17
+ let subscribers = [];
18
+ let toasts = [];
19
+ let dismissedToasts = /* @__PURE__ */ new Set();
20
+ let toastQueue = [];
21
+ const subscribe = (subscriber) => {
22
+ subscribers.push(subscriber);
23
+ return () => {
24
+ const index = subscribers.indexOf(subscriber);
25
+ subscribers.splice(index, 1);
26
+ };
27
+ };
28
+ const publish = (data) => {
29
+ subscribers.forEach((subscriber) => subscriber(data));
30
+ return data;
31
+ };
32
+ const addToast = (data) => {
33
+ if (toasts.length >= attrs.max) {
34
+ toastQueue.push(data);
35
+ return;
36
+ }
37
+ publish(data);
38
+ toasts.unshift(data);
39
+ };
40
+ const processQueue = () => {
41
+ while (toastQueue.length > 0 && toasts.length < attrs.max) {
42
+ const nextToast = toastQueue.shift();
43
+ if (nextToast) {
44
+ publish(nextToast);
45
+ toasts.unshift(nextToast);
46
+ }
47
+ }
48
+ };
49
+ const create = (data) => {
50
+ const id = data.id ?? `toast:${uuid()}`;
51
+ const exists = toasts.find((toast) => toast.id === id);
52
+ if (dismissedToasts.has(id)) dismissedToasts.delete(id);
53
+ if (exists) {
54
+ toasts = toasts.map((toast) => {
55
+ if (toast.id === id) {
56
+ return publish({ ...toast, ...data, id });
57
+ }
58
+ return toast;
59
+ });
60
+ } else {
61
+ addToast({
62
+ id,
63
+ duration: attrs.duration,
64
+ removeDelay: attrs.removeDelay,
65
+ type: "info",
66
+ ...data,
67
+ stacked: !attrs.overlap,
68
+ gap: attrs.gap
69
+ });
70
+ }
71
+ return id;
72
+ };
73
+ const remove = (id) => {
74
+ dismissedToasts.add(id);
75
+ if (!id) {
76
+ toasts.forEach((toast) => {
77
+ subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
78
+ });
79
+ toasts = [];
80
+ toastQueue = [];
81
+ } else {
82
+ subscribers.forEach((subscriber) => subscriber({ id, dismiss: true }));
83
+ toasts = toasts.filter((toast) => toast.id !== id);
84
+ processQueue();
85
+ }
86
+ return id;
87
+ };
88
+ const error = (data) => {
89
+ return create({ ...data, type: "error" });
90
+ };
91
+ const success = (data) => {
92
+ return create({ ...data, type: "success" });
93
+ };
94
+ const info = (data) => {
95
+ return create({ ...data, type: "info" });
96
+ };
97
+ const warning = (data) => {
98
+ return create({ ...data, type: "warning" });
99
+ };
100
+ const loading = (data) => {
101
+ return create({ ...data, type: "loading" });
102
+ };
103
+ const getVisibleToasts = () => {
104
+ return toasts.filter((toast) => !dismissedToasts.has(toast.id));
105
+ };
106
+ const getCount = () => {
107
+ return toasts.length;
108
+ };
109
+ const promise = (promise2, options, shared = {}) => {
110
+ if (!options || !options.loading) {
111
+ warn("[zag-js > toast] toaster.promise() requires at least a 'loading' option to be specified");
112
+ return;
113
+ }
114
+ const id = create({
115
+ ...shared,
116
+ ...options.loading,
117
+ promise: promise2,
118
+ type: "loading"
119
+ });
120
+ let removable = true;
121
+ let result;
122
+ const prom = runIfFn(promise2).then(async (response) => {
123
+ result = ["resolve", response];
124
+ if (isHttpResponse(response) && !response.ok) {
125
+ removable = false;
126
+ const errorOptions = runIfFn(options.error, `HTTP Error! status: ${response.status}`);
127
+ create({ ...shared, ...errorOptions, id, type: "error" });
128
+ } else if (options.success !== void 0) {
129
+ removable = false;
130
+ const successOptions = runIfFn(options.success, response);
131
+ create({ ...shared, ...successOptions, id, type: "success" });
132
+ }
133
+ }).catch(async (error2) => {
134
+ result = ["reject", error2];
135
+ if (options.error !== void 0) {
136
+ removable = false;
137
+ const errorOptions = runIfFn(options.error, error2);
138
+ create({ ...shared, ...errorOptions, id, type: "error" });
139
+ }
140
+ }).finally(() => {
141
+ if (removable) {
142
+ remove(id);
143
+ }
144
+ options.finally?.();
145
+ });
146
+ const unwrap = () => new Promise(
147
+ (resolve, reject) => prom.then(() => result[0] === "reject" ? reject(result[1]) : resolve(result[1])).catch(reject)
148
+ );
149
+ return { id, unwrap };
150
+ };
151
+ const update = (id, data) => {
152
+ return create({ id, ...data });
153
+ };
154
+ const pause = (id) => {
155
+ if (id != null) {
156
+ toasts = toasts.map((toast) => {
157
+ if (toast.id === id) return publish({ ...toast, message: "PAUSE" });
158
+ return toast;
159
+ });
160
+ } else {
161
+ toasts = toasts.map((toast) => publish({ ...toast, message: "PAUSE" }));
162
+ }
163
+ };
164
+ const resume = (id) => {
165
+ if (id != null) {
166
+ toasts = toasts.map((toast) => {
167
+ if (toast.id === id) return publish({ ...toast, message: "RESUME" });
168
+ return toast;
169
+ });
170
+ } else {
171
+ toasts = toasts.map((toast) => publish({ ...toast, message: "RESUME" }));
172
+ }
173
+ };
174
+ const dismiss = (id) => {
175
+ if (id != null) {
176
+ toasts = toasts.map((toast) => {
177
+ if (toast.id === id) return publish({ ...toast, message: "DISMISS" });
178
+ return toast;
179
+ });
180
+ } else {
181
+ toasts = toasts.map((toast) => publish({ ...toast, message: "DISMISS" }));
182
+ }
183
+ };
184
+ const isVisible = (id) => {
185
+ return !dismissedToasts.has(id) && !!toasts.find((toast) => toast.id === id);
186
+ };
187
+ const isDismissed = (id) => {
188
+ return dismissedToasts.has(id);
189
+ };
190
+ const expand = () => {
191
+ toasts = toasts.map((toast) => publish({ ...toast, stacked: true }));
192
+ };
193
+ const collapse = () => {
194
+ toasts = toasts.map((toast) => publish({ ...toast, stacked: false }));
195
+ };
196
+ return {
197
+ attrs,
198
+ subscribe,
199
+ create,
200
+ update,
201
+ remove,
202
+ dismiss,
203
+ error,
204
+ success,
205
+ info,
206
+ warning,
207
+ loading,
208
+ getVisibleToasts,
209
+ getCount,
210
+ promise,
211
+ pause,
212
+ resume,
213
+ isVisible,
214
+ isDismissed,
215
+ expand,
216
+ collapse
217
+ };
218
+ }
219
+ var isHttpResponse = (data) => {
220
+ return data && typeof data === "object" && "ok" in data && typeof data.ok === "boolean" && "status" in data && typeof data.status === "number";
221
+ };
222
+ export {
223
+ createToastStore
224
+ };