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