@zag-js/toast 0.1.15 → 0.2.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/index.js +72 -20
- package/dist/index.mjs +666 -0
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,11 +1,40 @@
|
|
|
1
|
-
|
|
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/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
api: () => api,
|
|
24
|
+
connect: () => connect,
|
|
25
|
+
createMachine: () => createToastMachine,
|
|
26
|
+
group: () => group
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// ../../utilities/dom/dist/index.mjs
|
|
2
31
|
var dataAttr = (guard) => {
|
|
3
32
|
return guard ? "" : void 0;
|
|
4
33
|
};
|
|
5
34
|
var MAX_Z_INDEX = 2147483647;
|
|
6
35
|
var runIfFn = (v, ...a) => {
|
|
7
36
|
const res = typeof v === "function" ? v(...a) : v;
|
|
8
|
-
return res
|
|
37
|
+
return res != null ? res : void 0;
|
|
9
38
|
};
|
|
10
39
|
var cast = (v) => v;
|
|
11
40
|
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
@@ -17,22 +46,44 @@ function isWindow(value) {
|
|
|
17
46
|
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
18
47
|
}
|
|
19
48
|
function getDocument(el) {
|
|
49
|
+
var _a;
|
|
20
50
|
if (isWindow(el))
|
|
21
51
|
return el.document;
|
|
22
52
|
if (isDocument(el))
|
|
23
53
|
return el;
|
|
24
|
-
return (el == null ? void 0 : el.ownerDocument)
|
|
54
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
25
55
|
}
|
|
26
56
|
function defineDomHelpers(helpers) {
|
|
27
57
|
const dom2 = {
|
|
28
58
|
getRootNode: (ctx) => {
|
|
29
|
-
var _a;
|
|
30
|
-
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx))
|
|
59
|
+
var _a, _b;
|
|
60
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
31
61
|
},
|
|
32
62
|
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
33
|
-
getWin: (ctx) =>
|
|
63
|
+
getWin: (ctx) => {
|
|
64
|
+
var _a;
|
|
65
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
66
|
+
},
|
|
34
67
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
35
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
68
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
69
|
+
createEmitter: (ctx, target) => {
|
|
70
|
+
const win = dom2.getWin(ctx);
|
|
71
|
+
return function emit(evt, detail, options) {
|
|
72
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
73
|
+
const eventName = `zag:${evt}`;
|
|
74
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
75
|
+
const event = new win.CustomEvent(eventName, init);
|
|
76
|
+
target.dispatchEvent(event);
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
createListener: (target) => {
|
|
80
|
+
return function listen(evt, handler) {
|
|
81
|
+
const eventName = `zag:${evt}`;
|
|
82
|
+
const listener = (e) => handler(e);
|
|
83
|
+
target.addEventListener(eventName, listener);
|
|
84
|
+
return () => target.removeEventListener(eventName, listener);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
36
87
|
};
|
|
37
88
|
return {
|
|
38
89
|
...dom2,
|
|
@@ -55,10 +106,10 @@ function trackDocumentVisibility(_doc, callback) {
|
|
|
55
106
|
});
|
|
56
107
|
}
|
|
57
108
|
|
|
58
|
-
// ../../utilities/core/dist/index.
|
|
109
|
+
// ../../utilities/core/dist/index.mjs
|
|
59
110
|
var runIfFn2 = (v, ...a) => {
|
|
60
111
|
const res = typeof v === "function" ? v(...a) : v;
|
|
61
|
-
return res
|
|
112
|
+
return res != null ? res : void 0;
|
|
62
113
|
};
|
|
63
114
|
var uuid = /* @__PURE__ */ (() => {
|
|
64
115
|
let id = 0;
|
|
@@ -76,7 +127,7 @@ function warn(...a) {
|
|
|
76
127
|
}
|
|
77
128
|
|
|
78
129
|
// src/toast-group.connect.ts
|
|
79
|
-
|
|
130
|
+
var import_core = require("@zag-js/core");
|
|
80
131
|
|
|
81
132
|
// src/toast.dom.ts
|
|
82
133
|
var dom = defineDomHelpers({
|
|
@@ -115,7 +166,7 @@ var defaultTimeouts = {
|
|
|
115
166
|
custom: 5e3
|
|
116
167
|
};
|
|
117
168
|
function getToastDuration(duration, type) {
|
|
118
|
-
return duration
|
|
169
|
+
return duration != null ? duration : defaultTimeouts[type];
|
|
119
170
|
}
|
|
120
171
|
function getGroupPlacementStyle(ctx, placement) {
|
|
121
172
|
const offset = ctx.offsets;
|
|
@@ -271,7 +322,7 @@ function groupConnect(state, send, normalize) {
|
|
|
271
322
|
return portal;
|
|
272
323
|
},
|
|
273
324
|
subscribe(fn) {
|
|
274
|
-
return subscribe(state.context.toasts, () => fn(state.context.toasts));
|
|
325
|
+
return (0, import_core.subscribe)(state.context.toasts, () => fn(state.context.toasts));
|
|
275
326
|
}
|
|
276
327
|
};
|
|
277
328
|
if (!state.matches("unknown")) {
|
|
@@ -281,15 +332,15 @@ function groupConnect(state, send, normalize) {
|
|
|
281
332
|
}
|
|
282
333
|
|
|
283
334
|
// src/toast-group.machine.ts
|
|
284
|
-
|
|
335
|
+
var import_core3 = require("@zag-js/core");
|
|
285
336
|
|
|
286
337
|
// src/toast.machine.ts
|
|
287
|
-
|
|
288
|
-
var { not, and, or } = guards;
|
|
338
|
+
var import_core2 = require("@zag-js/core");
|
|
339
|
+
var { not, and, or } = import_core2.guards;
|
|
289
340
|
function createToastMachine(options = {}) {
|
|
290
341
|
const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...rest } = options;
|
|
291
342
|
const __duration = getToastDuration(duration, type);
|
|
292
|
-
return createMachine(
|
|
343
|
+
return (0, import_core2.createMachine)(
|
|
293
344
|
{
|
|
294
345
|
id,
|
|
295
346
|
entry: "invokeOnOpen",
|
|
@@ -436,7 +487,7 @@ function createToastMachine(options = {}) {
|
|
|
436
487
|
|
|
437
488
|
// src/toast-group.machine.ts
|
|
438
489
|
function groupMachine(ctx) {
|
|
439
|
-
return
|
|
490
|
+
return (0, import_core3.createMachine)({
|
|
440
491
|
id: "toaster",
|
|
441
492
|
initial: "active",
|
|
442
493
|
context: {
|
|
@@ -636,9 +687,10 @@ function api() {
|
|
|
636
687
|
return toaster;
|
|
637
688
|
}
|
|
638
689
|
}
|
|
639
|
-
export
|
|
690
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
691
|
+
0 && (module.exports = {
|
|
640
692
|
api,
|
|
641
693
|
connect,
|
|
642
|
-
|
|
694
|
+
createMachine,
|
|
643
695
|
group
|
|
644
|
-
};
|
|
696
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
// ../../utilities/dom/dist/index.mjs
|
|
2
|
+
var dataAttr = (guard) => {
|
|
3
|
+
return guard ? "" : void 0;
|
|
4
|
+
};
|
|
5
|
+
var MAX_Z_INDEX = 2147483647;
|
|
6
|
+
var runIfFn = (v, ...a) => {
|
|
7
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
8
|
+
return res != null ? res : void 0;
|
|
9
|
+
};
|
|
10
|
+
var cast = (v) => v;
|
|
11
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
12
|
+
var isDom = () => typeof window !== "undefined";
|
|
13
|
+
function isDocument(el) {
|
|
14
|
+
return el.nodeType === Node.DOCUMENT_NODE;
|
|
15
|
+
}
|
|
16
|
+
function isWindow(value) {
|
|
17
|
+
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
18
|
+
}
|
|
19
|
+
function getDocument(el) {
|
|
20
|
+
var _a;
|
|
21
|
+
if (isWindow(el))
|
|
22
|
+
return el.document;
|
|
23
|
+
if (isDocument(el))
|
|
24
|
+
return el;
|
|
25
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
26
|
+
}
|
|
27
|
+
function defineDomHelpers(helpers) {
|
|
28
|
+
const dom2 = {
|
|
29
|
+
getRootNode: (ctx) => {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
32
|
+
},
|
|
33
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
34
|
+
getWin: (ctx) => {
|
|
35
|
+
var _a;
|
|
36
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
37
|
+
},
|
|
38
|
+
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
39
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
40
|
+
createEmitter: (ctx, target) => {
|
|
41
|
+
const win = dom2.getWin(ctx);
|
|
42
|
+
return function emit(evt, detail, options) {
|
|
43
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
44
|
+
const eventName = `zag:${evt}`;
|
|
45
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
46
|
+
const event = new win.CustomEvent(eventName, init);
|
|
47
|
+
target.dispatchEvent(event);
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
createListener: (target) => {
|
|
51
|
+
return function listen(evt, handler) {
|
|
52
|
+
const eventName = `zag:${evt}`;
|
|
53
|
+
const listener = (e) => handler(e);
|
|
54
|
+
target.addEventListener(eventName, listener);
|
|
55
|
+
return () => target.removeEventListener(eventName, listener);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
...dom2,
|
|
61
|
+
...helpers
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
var isRef = (v) => hasProp(v, "current");
|
|
65
|
+
function addDomEvent(target, eventName, handler, options) {
|
|
66
|
+
const node = isRef(target) ? target.current : runIfFn(target);
|
|
67
|
+
node == null ? void 0 : node.addEventListener(eventName, handler, options);
|
|
68
|
+
return () => {
|
|
69
|
+
node == null ? void 0 : node.removeEventListener(eventName, handler, options);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function trackDocumentVisibility(_doc, callback) {
|
|
73
|
+
const doc = cast(_doc);
|
|
74
|
+
return addDomEvent(doc, "visibilitychange", () => {
|
|
75
|
+
const hidden = doc.hidden || doc.msHidden || doc.webkitHidden;
|
|
76
|
+
callback(!!hidden);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ../../utilities/core/dist/index.mjs
|
|
81
|
+
var runIfFn2 = (v, ...a) => {
|
|
82
|
+
const res = typeof v === "function" ? v(...a) : v;
|
|
83
|
+
return res != null ? res : void 0;
|
|
84
|
+
};
|
|
85
|
+
var uuid = /* @__PURE__ */ (() => {
|
|
86
|
+
let id = 0;
|
|
87
|
+
return () => {
|
|
88
|
+
id++;
|
|
89
|
+
return id.toString(36);
|
|
90
|
+
};
|
|
91
|
+
})();
|
|
92
|
+
function warn(...a) {
|
|
93
|
+
const m = a.length === 1 ? a[0] : a[1];
|
|
94
|
+
const c = a.length === 2 ? a[0] : true;
|
|
95
|
+
if (c && process.env.NODE_ENV !== "production") {
|
|
96
|
+
console.warn(m);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/toast-group.connect.ts
|
|
101
|
+
import { subscribe } from "@zag-js/core";
|
|
102
|
+
|
|
103
|
+
// src/toast.dom.ts
|
|
104
|
+
var dom = defineDomHelpers({
|
|
105
|
+
getGroupId: (placement) => `toast-group:${placement}`,
|
|
106
|
+
getContainerId: (ctx) => `toast:${ctx.id}`,
|
|
107
|
+
getTitleId: (ctx) => `toast-title:${ctx.id}`,
|
|
108
|
+
getDescriptionId: (ctx) => `toast-description:${ctx.id}`,
|
|
109
|
+
getCloseButtonId: (ctx) => `toast-close-button:${ctx.id}`,
|
|
110
|
+
getPortalId: (ctx) => `toast-portal:${ctx.id}`,
|
|
111
|
+
getPortalEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPortalId(ctx)),
|
|
112
|
+
createPortalEl: (ctx) => {
|
|
113
|
+
const existing = dom.getPortalEl(ctx);
|
|
114
|
+
if (existing)
|
|
115
|
+
return existing;
|
|
116
|
+
const portal = dom.getDoc(ctx).createElement("toast-portal");
|
|
117
|
+
portal.id = dom.getPortalId(ctx);
|
|
118
|
+
return portal;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// src/toast.utils.ts
|
|
123
|
+
function getToastsByPlacement(toasts) {
|
|
124
|
+
const result = {};
|
|
125
|
+
for (const toast of toasts) {
|
|
126
|
+
const placement = toast.state.context.placement;
|
|
127
|
+
result[placement] || (result[placement] = []);
|
|
128
|
+
result[placement].push(toast);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
var defaultTimeouts = {
|
|
133
|
+
info: 5e3,
|
|
134
|
+
error: 5e3,
|
|
135
|
+
success: 2e3,
|
|
136
|
+
loading: Infinity,
|
|
137
|
+
custom: 5e3
|
|
138
|
+
};
|
|
139
|
+
function getToastDuration(duration, type) {
|
|
140
|
+
return duration != null ? duration : defaultTimeouts[type];
|
|
141
|
+
}
|
|
142
|
+
function getGroupPlacementStyle(ctx, placement) {
|
|
143
|
+
const offset = ctx.offsets;
|
|
144
|
+
const __offset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
|
|
145
|
+
const rtl = ctx.dir === "rtl";
|
|
146
|
+
const __placement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
|
|
147
|
+
const isRighty = __placement.includes("right");
|
|
148
|
+
const isLefty = __placement.includes("left");
|
|
149
|
+
const styles = {
|
|
150
|
+
position: "fixed",
|
|
151
|
+
pointerEvents: ctx.count > 0 ? void 0 : "none",
|
|
152
|
+
display: "flex",
|
|
153
|
+
flexDirection: "column",
|
|
154
|
+
"--toast-gutter": ctx.gutter,
|
|
155
|
+
zIndex: ctx.zIndex
|
|
156
|
+
};
|
|
157
|
+
let alignItems = "center";
|
|
158
|
+
if (isRighty)
|
|
159
|
+
alignItems = "flex-end";
|
|
160
|
+
if (isLefty)
|
|
161
|
+
alignItems = "flex-start";
|
|
162
|
+
styles.alignItems = alignItems;
|
|
163
|
+
if (__placement.includes("top")) {
|
|
164
|
+
const offset2 = __offset.top;
|
|
165
|
+
styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
|
|
166
|
+
}
|
|
167
|
+
if (__placement.includes("bottom")) {
|
|
168
|
+
const offset2 = __offset.bottom;
|
|
169
|
+
styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
|
|
170
|
+
}
|
|
171
|
+
if (!__placement.includes("left")) {
|
|
172
|
+
const offset2 = __offset.right;
|
|
173
|
+
styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
|
|
174
|
+
}
|
|
175
|
+
if (!__placement.includes("right")) {
|
|
176
|
+
const offset2 = __offset.left;
|
|
177
|
+
styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
|
|
178
|
+
}
|
|
179
|
+
return styles;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/toast-group.connect.ts
|
|
183
|
+
var toaster = {};
|
|
184
|
+
function groupConnect(state, send, normalize) {
|
|
185
|
+
const group2 = {
|
|
186
|
+
count: state.context.count,
|
|
187
|
+
toasts: state.context.toasts,
|
|
188
|
+
toastsByPlacement: getToastsByPlacement(state.context.toasts),
|
|
189
|
+
isVisible(id) {
|
|
190
|
+
if (!state.context.toasts.length)
|
|
191
|
+
return false;
|
|
192
|
+
return !!state.context.toasts.find((toast) => toast.id == id);
|
|
193
|
+
},
|
|
194
|
+
create(options) {
|
|
195
|
+
const uid = `toast:${uuid()}`;
|
|
196
|
+
const id = options.id ? options.id : uid;
|
|
197
|
+
if (group2.isVisible(id))
|
|
198
|
+
return;
|
|
199
|
+
send({ type: "ADD_TOAST", toast: { ...options, id } });
|
|
200
|
+
return id;
|
|
201
|
+
},
|
|
202
|
+
upsert(options) {
|
|
203
|
+
const { id } = options;
|
|
204
|
+
const isVisible = id ? group2.isVisible(id) : false;
|
|
205
|
+
if (isVisible && id != null) {
|
|
206
|
+
return group2.update(id, options);
|
|
207
|
+
} else {
|
|
208
|
+
return group2.create(options);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
dismiss(id) {
|
|
212
|
+
if (id == null) {
|
|
213
|
+
send("DISMISS_ALL");
|
|
214
|
+
} else if (group2.isVisible(id)) {
|
|
215
|
+
send({ type: "DISMISS_TOAST", id });
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
remove(id) {
|
|
219
|
+
if (id == null) {
|
|
220
|
+
send("REMOVE_ALL");
|
|
221
|
+
} else if (group2.isVisible(id)) {
|
|
222
|
+
send({ type: "REMOVE_TOAST", id });
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
dismissByPlacement(placement) {
|
|
226
|
+
const toasts = group2.toastsByPlacement[placement];
|
|
227
|
+
if (toasts) {
|
|
228
|
+
toasts.forEach((toast) => group2.dismiss(toast.id));
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
update(id, options) {
|
|
232
|
+
if (!group2.isVisible(id))
|
|
233
|
+
return;
|
|
234
|
+
send({ type: "UPDATE_TOAST", id, toast: options });
|
|
235
|
+
return id;
|
|
236
|
+
},
|
|
237
|
+
loading(options) {
|
|
238
|
+
options.type = "loading";
|
|
239
|
+
return group2.upsert(options);
|
|
240
|
+
},
|
|
241
|
+
success(options) {
|
|
242
|
+
options.type = "success";
|
|
243
|
+
return group2.upsert(options);
|
|
244
|
+
},
|
|
245
|
+
error(options) {
|
|
246
|
+
options.type = "error";
|
|
247
|
+
return group2.upsert(options);
|
|
248
|
+
},
|
|
249
|
+
promise(promise, options, shared = {}) {
|
|
250
|
+
const id = group2.loading({ ...shared, ...options.loading });
|
|
251
|
+
promise.then((response) => {
|
|
252
|
+
const successOptions = runIfFn2(options.success, response);
|
|
253
|
+
group2.success({ ...shared, ...successOptions, id });
|
|
254
|
+
}).catch((error) => {
|
|
255
|
+
const errorOptions = runIfFn2(options.error, error);
|
|
256
|
+
group2.error({ ...shared, ...errorOptions, id });
|
|
257
|
+
});
|
|
258
|
+
return promise;
|
|
259
|
+
},
|
|
260
|
+
pause(id) {
|
|
261
|
+
if (id == null) {
|
|
262
|
+
send("PAUSE_ALL");
|
|
263
|
+
} else if (group2.isVisible(id)) {
|
|
264
|
+
send({ type: "PAUSE_TOAST", id });
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
resume(id) {
|
|
268
|
+
if (id == null) {
|
|
269
|
+
send("RESUME_ALL");
|
|
270
|
+
} else if (group2.isVisible(id)) {
|
|
271
|
+
send({ type: "RESUME_TOAST", id });
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
getGroupProps(options) {
|
|
275
|
+
const { placement, label = "Notifications" } = options;
|
|
276
|
+
return normalize.element({
|
|
277
|
+
tabIndex: -1,
|
|
278
|
+
"aria-label": label,
|
|
279
|
+
id: dom.getGroupId(placement),
|
|
280
|
+
"data-placement": placement,
|
|
281
|
+
"aria-live": "polite",
|
|
282
|
+
role: "region",
|
|
283
|
+
style: getGroupPlacementStyle(state.context, placement)
|
|
284
|
+
});
|
|
285
|
+
},
|
|
286
|
+
createPortal() {
|
|
287
|
+
const doc = dom.getDoc(state.context);
|
|
288
|
+
const exist = dom.getPortalEl(state.context);
|
|
289
|
+
if (exist)
|
|
290
|
+
return exist;
|
|
291
|
+
const portal = dom.createPortalEl(state.context);
|
|
292
|
+
doc.body.appendChild(portal);
|
|
293
|
+
return portal;
|
|
294
|
+
},
|
|
295
|
+
subscribe(fn) {
|
|
296
|
+
return subscribe(state.context.toasts, () => fn(state.context.toasts));
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
if (!state.matches("unknown")) {
|
|
300
|
+
Object.assign(toaster, group2);
|
|
301
|
+
}
|
|
302
|
+
return group2;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// src/toast-group.machine.ts
|
|
306
|
+
import { createMachine as createMachine2 } from "@zag-js/core";
|
|
307
|
+
|
|
308
|
+
// src/toast.machine.ts
|
|
309
|
+
import { createMachine, guards } from "@zag-js/core";
|
|
310
|
+
var { not, and, or } = guards;
|
|
311
|
+
function createToastMachine(options = {}) {
|
|
312
|
+
const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 500, ...rest } = options;
|
|
313
|
+
const __duration = getToastDuration(duration, type);
|
|
314
|
+
return createMachine(
|
|
315
|
+
{
|
|
316
|
+
id,
|
|
317
|
+
entry: "invokeOnOpen",
|
|
318
|
+
initial: type === "loading" ? "persist" : "active",
|
|
319
|
+
context: {
|
|
320
|
+
id,
|
|
321
|
+
type,
|
|
322
|
+
remaining: __duration,
|
|
323
|
+
duration: __duration,
|
|
324
|
+
removeDelay,
|
|
325
|
+
createdAt: Date.now(),
|
|
326
|
+
placement,
|
|
327
|
+
...rest
|
|
328
|
+
},
|
|
329
|
+
on: {
|
|
330
|
+
UPDATE: [
|
|
331
|
+
{
|
|
332
|
+
guard: and("hasTypeChanged", "isChangingToLoading"),
|
|
333
|
+
target: "persist",
|
|
334
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
guard: or("hasDurationChanged", "hasTypeChanged"),
|
|
338
|
+
target: "active:temp",
|
|
339
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
actions: ["setContext", "invokeOnUpdate"]
|
|
343
|
+
}
|
|
344
|
+
]
|
|
345
|
+
},
|
|
346
|
+
states: {
|
|
347
|
+
"active:temp": {
|
|
348
|
+
tags: ["visible", "updating"],
|
|
349
|
+
after: {
|
|
350
|
+
0: "active"
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
persist: {
|
|
354
|
+
tags: ["visible", "paused"],
|
|
355
|
+
activities: "trackDocumentVisibility",
|
|
356
|
+
on: {
|
|
357
|
+
RESUME: {
|
|
358
|
+
guard: not("isLoadingType"),
|
|
359
|
+
target: "active",
|
|
360
|
+
actions: ["setCreatedAt"]
|
|
361
|
+
},
|
|
362
|
+
DISMISS: "dismissing"
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
active: {
|
|
366
|
+
tags: ["visible"],
|
|
367
|
+
activities: "trackDocumentVisibility",
|
|
368
|
+
after: {
|
|
369
|
+
VISIBLE_DURATION: "dismissing"
|
|
370
|
+
},
|
|
371
|
+
on: {
|
|
372
|
+
DISMISS: "dismissing",
|
|
373
|
+
PAUSE: {
|
|
374
|
+
target: "persist",
|
|
375
|
+
actions: "setRemainingDuration"
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
dismissing: {
|
|
380
|
+
entry: "invokeOnClosing",
|
|
381
|
+
after: {
|
|
382
|
+
REMOVE_DELAY: {
|
|
383
|
+
target: "inactive",
|
|
384
|
+
actions: "notifyParentToRemove"
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
inactive: {
|
|
389
|
+
entry: "invokeOnClose",
|
|
390
|
+
type: "final"
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
activities: {
|
|
396
|
+
trackDocumentVisibility(ctx, _evt, { send }) {
|
|
397
|
+
if (!ctx.pauseOnPageIdle)
|
|
398
|
+
return;
|
|
399
|
+
return trackDocumentVisibility(dom.getDoc(ctx), function(hidden) {
|
|
400
|
+
send(hidden ? "PAUSE" : "RESUME");
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
guards: {
|
|
405
|
+
isChangingToLoading: (_, evt) => {
|
|
406
|
+
var _a;
|
|
407
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) === "loading";
|
|
408
|
+
},
|
|
409
|
+
isLoadingType: (ctx) => ctx.type === "loading",
|
|
410
|
+
hasTypeChanged: (ctx, evt) => {
|
|
411
|
+
var _a;
|
|
412
|
+
return ((_a = evt.toast) == null ? void 0 : _a.type) !== ctx.type;
|
|
413
|
+
},
|
|
414
|
+
hasDurationChanged: (ctx, evt) => {
|
|
415
|
+
var _a;
|
|
416
|
+
return ((_a = evt.toast) == null ? void 0 : _a.duration) !== ctx.duration;
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
delays: {
|
|
420
|
+
VISIBLE_DURATION: (ctx) => ctx.remaining,
|
|
421
|
+
REMOVE_DELAY: (ctx) => ctx.removeDelay
|
|
422
|
+
},
|
|
423
|
+
actions: {
|
|
424
|
+
setRemainingDuration(ctx) {
|
|
425
|
+
ctx.remaining -= Date.now() - ctx.createdAt;
|
|
426
|
+
},
|
|
427
|
+
setCreatedAt(ctx) {
|
|
428
|
+
ctx.createdAt = Date.now();
|
|
429
|
+
},
|
|
430
|
+
notifyParentToRemove(_ctx, _evt, { self }) {
|
|
431
|
+
self.sendParent({ type: "REMOVE_TOAST", id: self.id });
|
|
432
|
+
},
|
|
433
|
+
invokeOnClosing(ctx) {
|
|
434
|
+
var _a;
|
|
435
|
+
(_a = ctx.onClosing) == null ? void 0 : _a.call(ctx);
|
|
436
|
+
},
|
|
437
|
+
invokeOnClose(ctx) {
|
|
438
|
+
var _a;
|
|
439
|
+
(_a = ctx.onClose) == null ? void 0 : _a.call(ctx);
|
|
440
|
+
},
|
|
441
|
+
invokeOnOpen(ctx) {
|
|
442
|
+
var _a;
|
|
443
|
+
(_a = ctx.onOpen) == null ? void 0 : _a.call(ctx);
|
|
444
|
+
},
|
|
445
|
+
invokeOnUpdate(ctx) {
|
|
446
|
+
var _a;
|
|
447
|
+
(_a = ctx.onUpdate) == null ? void 0 : _a.call(ctx);
|
|
448
|
+
},
|
|
449
|
+
setContext(ctx, evt) {
|
|
450
|
+
const { duration: duration2, type: type2 } = evt.toast;
|
|
451
|
+
const time = getToastDuration(duration2, type2);
|
|
452
|
+
Object.assign(ctx, { ...evt.toast, duration: time, remaining: time });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/toast-group.machine.ts
|
|
460
|
+
function groupMachine(ctx) {
|
|
461
|
+
return createMachine2({
|
|
462
|
+
id: "toaster",
|
|
463
|
+
initial: "active",
|
|
464
|
+
context: {
|
|
465
|
+
dir: "ltr",
|
|
466
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
467
|
+
toasts: [],
|
|
468
|
+
gutter: "1rem",
|
|
469
|
+
zIndex: MAX_Z_INDEX,
|
|
470
|
+
pauseOnPageIdle: false,
|
|
471
|
+
pauseOnInteraction: true,
|
|
472
|
+
offsets: { left: "0px", right: "0px", top: "0px", bottom: "0px" },
|
|
473
|
+
...ctx
|
|
474
|
+
},
|
|
475
|
+
computed: {
|
|
476
|
+
count: (ctx2) => ctx2.toasts.length
|
|
477
|
+
},
|
|
478
|
+
on: {
|
|
479
|
+
SETUP: {},
|
|
480
|
+
PAUSE_TOAST: {
|
|
481
|
+
actions: (_ctx, evt, { self }) => {
|
|
482
|
+
self.sendChild("PAUSE", evt.id);
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
PAUSE_ALL: {
|
|
486
|
+
actions: (ctx2) => {
|
|
487
|
+
ctx2.toasts.forEach((toast) => toast.send("PAUSE"));
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
RESUME_TOAST: {
|
|
491
|
+
actions: (_ctx, evt, { self }) => {
|
|
492
|
+
self.sendChild("RESUME", evt.id);
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
RESUME_ALL: {
|
|
496
|
+
actions: (ctx2) => {
|
|
497
|
+
ctx2.toasts.forEach((toast) => toast.send("RESUME"));
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
ADD_TOAST: {
|
|
501
|
+
guard: (ctx2) => ctx2.toasts.length < ctx2.max,
|
|
502
|
+
actions: (ctx2, evt, { self }) => {
|
|
503
|
+
const options = {
|
|
504
|
+
...evt.toast,
|
|
505
|
+
pauseOnPageIdle: ctx2.pauseOnPageIdle,
|
|
506
|
+
pauseOnInteraction: ctx2.pauseOnInteraction,
|
|
507
|
+
dir: ctx2.dir,
|
|
508
|
+
getRootNode: ctx2.getRootNode
|
|
509
|
+
};
|
|
510
|
+
const toast = createToastMachine(options);
|
|
511
|
+
const actor = self.spawn(toast);
|
|
512
|
+
ctx2.toasts.push(actor);
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
UPDATE_TOAST: {
|
|
516
|
+
actions: (_ctx, evt, { self }) => {
|
|
517
|
+
self.sendChild({ type: "UPDATE", toast: evt.toast }, evt.id);
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
DISMISS_TOAST: {
|
|
521
|
+
actions: (_ctx, evt, { self }) => {
|
|
522
|
+
self.sendChild("DISMISS", evt.id);
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
DISMISS_ALL: {
|
|
526
|
+
actions: (ctx2) => {
|
|
527
|
+
ctx2.toasts.forEach((toast) => toast.send("DISMISS"));
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
REMOVE_TOAST: {
|
|
531
|
+
actions: (ctx2, evt, { self }) => {
|
|
532
|
+
self.stopChild(evt.id);
|
|
533
|
+
const index = ctx2.toasts.findIndex((toast) => toast.id === evt.id);
|
|
534
|
+
ctx2.toasts.splice(index, 1);
|
|
535
|
+
}
|
|
536
|
+
},
|
|
537
|
+
REMOVE_ALL: {
|
|
538
|
+
actions: (ctx2, _evt, { self }) => {
|
|
539
|
+
ctx2.toasts.forEach((toast) => self.stopChild(toast.id));
|
|
540
|
+
while (ctx2.toasts.length)
|
|
541
|
+
ctx2.toasts.pop();
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// src/toast.connect.ts
|
|
549
|
+
function connect(state, send, normalize) {
|
|
550
|
+
const isVisible = state.hasTag("visible");
|
|
551
|
+
const isPaused = state.hasTag("paused");
|
|
552
|
+
const pauseOnInteraction = state.context.pauseOnInteraction;
|
|
553
|
+
const placement = state.context.placement;
|
|
554
|
+
return {
|
|
555
|
+
type: state.context.type,
|
|
556
|
+
title: state.context.title,
|
|
557
|
+
description: state.context.description,
|
|
558
|
+
placement,
|
|
559
|
+
isVisible,
|
|
560
|
+
isPaused,
|
|
561
|
+
isRtl: state.context.dir === "rtl",
|
|
562
|
+
pause() {
|
|
563
|
+
send("PAUSE");
|
|
564
|
+
},
|
|
565
|
+
resume() {
|
|
566
|
+
send("RESUME");
|
|
567
|
+
},
|
|
568
|
+
dismiss() {
|
|
569
|
+
send("DISMISS");
|
|
570
|
+
},
|
|
571
|
+
rootProps: normalize.element({
|
|
572
|
+
"data-part": "root",
|
|
573
|
+
dir: state.context.dir,
|
|
574
|
+
id: dom.getContainerId(state.context),
|
|
575
|
+
"data-open": dataAttr(isVisible),
|
|
576
|
+
"data-type": state.context.type,
|
|
577
|
+
"data-placement": placement,
|
|
578
|
+
role: "status",
|
|
579
|
+
"aria-atomic": "true",
|
|
580
|
+
tabIndex: 0,
|
|
581
|
+
style: {
|
|
582
|
+
position: "relative",
|
|
583
|
+
pointerEvents: "auto",
|
|
584
|
+
margin: "calc(var(--toast-gutter) / 2)",
|
|
585
|
+
"--remove-delay": `${state.context.removeDelay}ms`,
|
|
586
|
+
"--duration": `${state.context.duration}ms`
|
|
587
|
+
},
|
|
588
|
+
onKeyDown(event) {
|
|
589
|
+
if (event.key == "Escape") {
|
|
590
|
+
send("DISMISS");
|
|
591
|
+
event.preventDefault();
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
onFocus() {
|
|
595
|
+
if (pauseOnInteraction) {
|
|
596
|
+
send("PAUSE");
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
onBlur() {
|
|
600
|
+
if (pauseOnInteraction) {
|
|
601
|
+
send("RESUME");
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
onPointerEnter() {
|
|
605
|
+
if (pauseOnInteraction) {
|
|
606
|
+
send("PAUSE");
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
onPointerLeave() {
|
|
610
|
+
if (pauseOnInteraction) {
|
|
611
|
+
send("RESUME");
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}),
|
|
615
|
+
titleProps: normalize.element({
|
|
616
|
+
"data-part": "title",
|
|
617
|
+
id: dom.getTitleId(state.context)
|
|
618
|
+
}),
|
|
619
|
+
descriptionProps: normalize.element({
|
|
620
|
+
"data-part": "description",
|
|
621
|
+
id: dom.getDescriptionId(state.context)
|
|
622
|
+
}),
|
|
623
|
+
closeButtonProps: normalize.button({
|
|
624
|
+
id: dom.getCloseButtonId(state.context),
|
|
625
|
+
"data-part": "close-button",
|
|
626
|
+
type: "button",
|
|
627
|
+
"aria-label": "Dismiss notification",
|
|
628
|
+
onClick() {
|
|
629
|
+
send("DISMISS");
|
|
630
|
+
}
|
|
631
|
+
}),
|
|
632
|
+
render() {
|
|
633
|
+
var _a, _b;
|
|
634
|
+
return (_b = (_a = state.context).render) == null ? void 0 : _b.call(_a, {
|
|
635
|
+
id: state.context.id,
|
|
636
|
+
type: state.context.type,
|
|
637
|
+
duration: state.context.duration,
|
|
638
|
+
title: state.context.title,
|
|
639
|
+
placement: state.context.placement,
|
|
640
|
+
description: state.context.description,
|
|
641
|
+
dismiss() {
|
|
642
|
+
send("DISMISS");
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// src/index.ts
|
|
650
|
+
var group = {
|
|
651
|
+
connect: groupConnect,
|
|
652
|
+
machine: groupMachine
|
|
653
|
+
};
|
|
654
|
+
function api() {
|
|
655
|
+
if (!isDom()) {
|
|
656
|
+
warn("toast.api() is only available in the browser");
|
|
657
|
+
} else {
|
|
658
|
+
return toaster;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
export {
|
|
662
|
+
api,
|
|
663
|
+
connect,
|
|
664
|
+
createToastMachine as createMachine,
|
|
665
|
+
group
|
|
666
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/toast",
|
|
4
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"description": "Core logic for the toast widget implemented as a state machine",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
6
8
|
"keywords": [
|
|
7
9
|
"js",
|
|
8
10
|
"machine",
|
|
@@ -15,8 +17,6 @@
|
|
|
15
17
|
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
16
18
|
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
17
19
|
"license": "MIT",
|
|
18
|
-
"main": "dist/index.js",
|
|
19
|
-
"types": "dist/index.d.ts",
|
|
20
20
|
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/toast",
|
|
21
21
|
"sideEffects": false,
|
|
22
22
|
"files": [
|
|
@@ -29,17 +29,17 @@
|
|
|
29
29
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@zag-js/core": "0.
|
|
33
|
-
"@zag-js/types": "0.
|
|
32
|
+
"@zag-js/core": "0.2.0",
|
|
33
|
+
"@zag-js/types": "0.3.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@zag-js/dom-utils": "0.
|
|
37
|
-
"@zag-js/utils": "0.
|
|
36
|
+
"@zag-js/dom-utils": "0.2.0",
|
|
37
|
+
"@zag-js/utils": "0.2.0"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build-fast": "tsup src/index.ts --format=esm",
|
|
40
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
41
41
|
"start": "pnpm build --watch",
|
|
42
|
-
"build": "tsup src/index.ts --format=esm --dts",
|
|
42
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
43
43
|
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
44
44
|
"lint": "eslint src --ext .ts,.tsx",
|
|
45
45
|
"test-ci": "pnpm test --ci --runInBand",
|