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