@zag-js/toast 1.34.1 → 1.35.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 +11 -365
- package/dist/index.d.ts +11 -365
- package/dist/index.js +40 -1084
- package/dist/index.mjs +13 -1080
- package/dist/toast-group.connect.d.mts +8 -0
- package/dist/toast-group.connect.d.ts +8 -0
- package/dist/toast-group.connect.js +98 -0
- package/dist/toast-group.connect.mjs +63 -0
- package/dist/toast-group.machine.d.mts +8 -0
- package/dist/toast-group.machine.d.ts +8 -0
- package/dist/toast-group.machine.js +292 -0
- package/dist/toast-group.machine.mjs +257 -0
- package/dist/toast.anatomy.d.mts +6 -0
- package/dist/toast.anatomy.d.ts +6 -0
- package/dist/toast.anatomy.js +41 -0
- package/dist/toast.anatomy.mjs +15 -0
- package/dist/toast.connect.d.mts +8 -0
- package/dist/toast.connect.d.ts +8 -0
- package/dist/toast.connect.js +155 -0
- package/dist/toast.connect.mjs +120 -0
- package/dist/toast.dom.d.mts +14 -0
- package/dist/toast.dom.d.ts +14 -0
- package/dist/toast.dom.js +48 -0
- package/dist/toast.dom.mjs +17 -0
- package/dist/toast.machine.d.mts +8 -0
- package/dist/toast.machine.d.ts +8 -0
- package/dist/toast.machine.js +291 -0
- package/dist/toast.machine.mjs +256 -0
- package/dist/toast.store.d.mts +8 -0
- package/dist/toast.store.d.ts +8 -0
- package/dist/toast.store.js +249 -0
- package/dist/toast.store.mjs +224 -0
- package/dist/toast.types.d.mts +371 -0
- package/dist/toast.types.d.ts +371 -0
- package/dist/toast.types.js +18 -0
- package/dist/toast.types.mjs +0 -0
- package/dist/toast.utils.d.mts +13 -0
- package/dist/toast.utils.d.ts +13 -0
- package/dist/toast.utils.js +209 -0
- package/dist/toast.utils.mjs +179 -0
- package/package.json +18 -8
|
@@ -0,0 +1,48 @@
|
|
|
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.dom.ts
|
|
21
|
+
var toast_dom_exports = {};
|
|
22
|
+
__export(toast_dom_exports, {
|
|
23
|
+
getCloseTriggerId: () => getCloseTriggerId,
|
|
24
|
+
getDescriptionId: () => getDescriptionId,
|
|
25
|
+
getRegionEl: () => getRegionEl,
|
|
26
|
+
getRegionId: () => getRegionId,
|
|
27
|
+
getRootEl: () => getRootEl,
|
|
28
|
+
getRootId: () => getRootId,
|
|
29
|
+
getTitleId: () => getTitleId
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(toast_dom_exports);
|
|
32
|
+
var getRegionId = (placement) => `toast-group:${placement}`;
|
|
33
|
+
var getRegionEl = (ctx, placement) => ctx.getById(`toast-group:${placement}`);
|
|
34
|
+
var getRootId = (ctx) => `toast:${ctx.id}`;
|
|
35
|
+
var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
|
|
36
|
+
var getTitleId = (ctx) => `toast:${ctx.id}:title`;
|
|
37
|
+
var getDescriptionId = (ctx) => `toast:${ctx.id}:description`;
|
|
38
|
+
var getCloseTriggerId = (ctx) => `toast${ctx.id}:close`;
|
|
39
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
40
|
+
0 && (module.exports = {
|
|
41
|
+
getCloseTriggerId,
|
|
42
|
+
getDescriptionId,
|
|
43
|
+
getRegionEl,
|
|
44
|
+
getRegionId,
|
|
45
|
+
getRootEl,
|
|
46
|
+
getRootId,
|
|
47
|
+
getTitleId
|
|
48
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/toast.dom.ts
|
|
2
|
+
var getRegionId = (placement) => `toast-group:${placement}`;
|
|
3
|
+
var getRegionEl = (ctx, placement) => ctx.getById(`toast-group:${placement}`);
|
|
4
|
+
var getRootId = (ctx) => `toast:${ctx.id}`;
|
|
5
|
+
var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
|
|
6
|
+
var getTitleId = (ctx) => `toast:${ctx.id}:title`;
|
|
7
|
+
var getDescriptionId = (ctx) => `toast:${ctx.id}:description`;
|
|
8
|
+
var getCloseTriggerId = (ctx) => `toast${ctx.id}:close`;
|
|
9
|
+
export {
|
|
10
|
+
getCloseTriggerId,
|
|
11
|
+
getDescriptionId,
|
|
12
|
+
getRegionEl,
|
|
13
|
+
getRegionId,
|
|
14
|
+
getRootEl,
|
|
15
|
+
getRootId,
|
|
16
|
+
getTitleId
|
|
17
|
+
};
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/toast.machine.ts
|
|
31
|
+
var toast_machine_exports = {};
|
|
32
|
+
__export(toast_machine_exports, {
|
|
33
|
+
machine: () => machine
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(toast_machine_exports);
|
|
36
|
+
var import_core = require("@zag-js/core");
|
|
37
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
38
|
+
var import_utils = require("@zag-js/utils");
|
|
39
|
+
var dom = __toESM(require("./toast.dom.cjs"));
|
|
40
|
+
var import_toast = require("./toast.utils.cjs");
|
|
41
|
+
var { not } = (0, import_core.createGuards)();
|
|
42
|
+
var machine = (0, import_core.createMachine)({
|
|
43
|
+
props({ props }) {
|
|
44
|
+
(0, import_utils.ensureProps)(props, ["id", "type", "parent", "removeDelay"], "toast");
|
|
45
|
+
return {
|
|
46
|
+
closable: true,
|
|
47
|
+
...props,
|
|
48
|
+
duration: (0, import_toast.getToastDuration)(props.duration, props.type)
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
initialState({ prop }) {
|
|
52
|
+
const persist = prop("type") === "loading" || prop("duration") === Infinity;
|
|
53
|
+
return persist ? "visible:persist" : "visible";
|
|
54
|
+
},
|
|
55
|
+
context({ prop, bindable }) {
|
|
56
|
+
return {
|
|
57
|
+
remainingTime: bindable(() => ({
|
|
58
|
+
defaultValue: (0, import_toast.getToastDuration)(prop("duration"), prop("type"))
|
|
59
|
+
})),
|
|
60
|
+
createdAt: bindable(() => ({
|
|
61
|
+
defaultValue: Date.now()
|
|
62
|
+
})),
|
|
63
|
+
mounted: bindable(() => ({
|
|
64
|
+
defaultValue: false
|
|
65
|
+
})),
|
|
66
|
+
initialHeight: bindable(() => ({
|
|
67
|
+
defaultValue: 0
|
|
68
|
+
}))
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
refs() {
|
|
72
|
+
return {
|
|
73
|
+
closeTimerStartTime: Date.now(),
|
|
74
|
+
lastCloseStartTimerStartTime: 0
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
computed: {
|
|
78
|
+
zIndex: ({ prop }) => {
|
|
79
|
+
const toasts = prop("parent").context.get("toasts");
|
|
80
|
+
const index = toasts.findIndex((toast) => toast.id === prop("id"));
|
|
81
|
+
return toasts.length - index;
|
|
82
|
+
},
|
|
83
|
+
height: ({ prop }) => {
|
|
84
|
+
const heights = prop("parent").context.get("heights");
|
|
85
|
+
const height = heights.find((height2) => height2.id === prop("id"));
|
|
86
|
+
return height?.height ?? 0;
|
|
87
|
+
},
|
|
88
|
+
heightIndex: ({ prop }) => {
|
|
89
|
+
const heights = prop("parent").context.get("heights");
|
|
90
|
+
return heights.findIndex((height) => height.id === prop("id"));
|
|
91
|
+
},
|
|
92
|
+
frontmost: ({ prop }) => prop("index") === 0,
|
|
93
|
+
heightBefore: ({ prop }) => {
|
|
94
|
+
const heights = prop("parent").context.get("heights");
|
|
95
|
+
const heightIndex = heights.findIndex((height) => height.id === prop("id"));
|
|
96
|
+
return heights.reduce((prev, curr, reducerIndex) => {
|
|
97
|
+
if (reducerIndex >= heightIndex) return prev;
|
|
98
|
+
return prev + curr.height;
|
|
99
|
+
}, 0);
|
|
100
|
+
},
|
|
101
|
+
shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === Infinity
|
|
102
|
+
},
|
|
103
|
+
watch({ track, prop, send }) {
|
|
104
|
+
track([() => prop("message")], () => {
|
|
105
|
+
const message = prop("message");
|
|
106
|
+
if (message) send({ type: message, src: "programmatic" });
|
|
107
|
+
});
|
|
108
|
+
track([() => prop("type"), () => prop("duration")], () => {
|
|
109
|
+
send({ type: "UPDATE" });
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
on: {
|
|
113
|
+
UPDATE: [
|
|
114
|
+
{
|
|
115
|
+
guard: "shouldPersist",
|
|
116
|
+
target: "visible:persist",
|
|
117
|
+
actions: ["resetCloseTimer"]
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
target: "visible:updating",
|
|
121
|
+
actions: ["resetCloseTimer"]
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
MEASURE: {
|
|
125
|
+
actions: ["measureHeight"]
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
entry: ["setMounted", "measureHeight", "invokeOnVisible"],
|
|
129
|
+
effects: ["trackHeight"],
|
|
130
|
+
states: {
|
|
131
|
+
"visible:updating": {
|
|
132
|
+
tags: ["visible", "updating"],
|
|
133
|
+
effects: ["waitForNextTick"],
|
|
134
|
+
on: {
|
|
135
|
+
SHOW: {
|
|
136
|
+
target: "visible"
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
"visible:persist": {
|
|
141
|
+
tags: ["visible", "paused"],
|
|
142
|
+
on: {
|
|
143
|
+
RESUME: {
|
|
144
|
+
guard: not("isLoadingType"),
|
|
145
|
+
target: "visible",
|
|
146
|
+
actions: ["setCloseTimer"]
|
|
147
|
+
},
|
|
148
|
+
DISMISS: {
|
|
149
|
+
target: "dismissing"
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
visible: {
|
|
154
|
+
tags: ["visible"],
|
|
155
|
+
effects: ["waitForDuration"],
|
|
156
|
+
on: {
|
|
157
|
+
DISMISS: {
|
|
158
|
+
target: "dismissing"
|
|
159
|
+
},
|
|
160
|
+
PAUSE: {
|
|
161
|
+
target: "visible:persist",
|
|
162
|
+
actions: ["syncRemainingTime"]
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
dismissing: {
|
|
167
|
+
entry: ["invokeOnDismiss"],
|
|
168
|
+
effects: ["waitForRemoveDelay"],
|
|
169
|
+
on: {
|
|
170
|
+
REMOVE: {
|
|
171
|
+
target: "unmounted",
|
|
172
|
+
actions: ["notifyParentToRemove"]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
unmounted: {
|
|
177
|
+
entry: ["invokeOnUnmount"]
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
implementations: {
|
|
181
|
+
effects: {
|
|
182
|
+
waitForRemoveDelay({ prop, send }) {
|
|
183
|
+
return (0, import_utils.setRafTimeout)(() => {
|
|
184
|
+
send({ type: "REMOVE", src: "timer" });
|
|
185
|
+
}, prop("removeDelay"));
|
|
186
|
+
},
|
|
187
|
+
waitForDuration({ send, context, computed }) {
|
|
188
|
+
if (computed("shouldPersist")) return;
|
|
189
|
+
return (0, import_utils.setRafTimeout)(() => {
|
|
190
|
+
send({ type: "DISMISS", src: "timer" });
|
|
191
|
+
}, context.get("remainingTime"));
|
|
192
|
+
},
|
|
193
|
+
waitForNextTick({ send }) {
|
|
194
|
+
return (0, import_utils.setRafTimeout)(() => {
|
|
195
|
+
send({ type: "SHOW", src: "timer" });
|
|
196
|
+
}, 0);
|
|
197
|
+
},
|
|
198
|
+
trackHeight({ scope, prop }) {
|
|
199
|
+
let cleanup;
|
|
200
|
+
(0, import_dom_query.raf)(() => {
|
|
201
|
+
const rootEl = dom.getRootEl(scope);
|
|
202
|
+
if (!rootEl) return;
|
|
203
|
+
const syncHeight = () => {
|
|
204
|
+
const originalHeight = rootEl.style.height;
|
|
205
|
+
rootEl.style.height = "auto";
|
|
206
|
+
const height = rootEl.getBoundingClientRect().height;
|
|
207
|
+
rootEl.style.height = originalHeight;
|
|
208
|
+
const item = { id: prop("id"), height };
|
|
209
|
+
setHeight(prop("parent"), item);
|
|
210
|
+
};
|
|
211
|
+
const win = scope.getWin();
|
|
212
|
+
const observer = new win.MutationObserver(syncHeight);
|
|
213
|
+
observer.observe(rootEl, {
|
|
214
|
+
childList: true,
|
|
215
|
+
subtree: true,
|
|
216
|
+
characterData: true
|
|
217
|
+
});
|
|
218
|
+
cleanup = () => observer.disconnect();
|
|
219
|
+
});
|
|
220
|
+
return () => cleanup?.();
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
guards: {
|
|
224
|
+
isLoadingType: ({ prop }) => prop("type") === "loading",
|
|
225
|
+
shouldPersist: ({ computed }) => computed("shouldPersist")
|
|
226
|
+
},
|
|
227
|
+
actions: {
|
|
228
|
+
setMounted({ context }) {
|
|
229
|
+
(0, import_dom_query.raf)(() => {
|
|
230
|
+
context.set("mounted", true);
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
measureHeight({ scope, prop, context }) {
|
|
234
|
+
queueMicrotask(() => {
|
|
235
|
+
const rootEl = dom.getRootEl(scope);
|
|
236
|
+
if (!rootEl) return;
|
|
237
|
+
const originalHeight = rootEl.style.height;
|
|
238
|
+
rootEl.style.height = "auto";
|
|
239
|
+
const height = rootEl.getBoundingClientRect().height;
|
|
240
|
+
rootEl.style.height = originalHeight;
|
|
241
|
+
context.set("initialHeight", height);
|
|
242
|
+
const item = { id: prop("id"), height };
|
|
243
|
+
setHeight(prop("parent"), item);
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
setCloseTimer({ refs }) {
|
|
247
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
248
|
+
},
|
|
249
|
+
resetCloseTimer({ context, refs, prop }) {
|
|
250
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
251
|
+
context.set("remainingTime", (0, import_toast.getToastDuration)(prop("duration"), prop("type")));
|
|
252
|
+
},
|
|
253
|
+
syncRemainingTime({ context, refs }) {
|
|
254
|
+
context.set("remainingTime", (prev) => {
|
|
255
|
+
const closeTimerStartTime = refs.get("closeTimerStartTime");
|
|
256
|
+
const elapsedTime = Date.now() - closeTimerStartTime;
|
|
257
|
+
refs.set("lastCloseStartTimerStartTime", Date.now());
|
|
258
|
+
return prev - elapsedTime;
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
notifyParentToRemove({ prop }) {
|
|
262
|
+
const parent = prop("parent");
|
|
263
|
+
parent.send({ type: "TOAST.REMOVE", id: prop("id") });
|
|
264
|
+
},
|
|
265
|
+
invokeOnDismiss({ prop, event }) {
|
|
266
|
+
prop("onStatusChange")?.({ status: "dismissing", src: event.src });
|
|
267
|
+
},
|
|
268
|
+
invokeOnUnmount({ prop }) {
|
|
269
|
+
prop("onStatusChange")?.({ status: "unmounted" });
|
|
270
|
+
},
|
|
271
|
+
invokeOnVisible({ prop }) {
|
|
272
|
+
prop("onStatusChange")?.({ status: "visible" });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
function setHeight(parent, item) {
|
|
278
|
+
const { id, height } = item;
|
|
279
|
+
parent.context.set("heights", (prev) => {
|
|
280
|
+
const alreadyExists = prev.find((i) => i.id === id);
|
|
281
|
+
if (!alreadyExists) {
|
|
282
|
+
return [{ id, height }, ...prev];
|
|
283
|
+
} else {
|
|
284
|
+
return prev.map((i) => i.id === id ? { ...i, height } : i);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
289
|
+
0 && (module.exports = {
|
|
290
|
+
machine
|
|
291
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// src/toast.machine.ts
|
|
2
|
+
import { createGuards, createMachine } from "@zag-js/core";
|
|
3
|
+
import { raf } from "@zag-js/dom-query";
|
|
4
|
+
import { ensureProps, setRafTimeout } from "@zag-js/utils";
|
|
5
|
+
import * as dom from "./toast.dom.mjs";
|
|
6
|
+
import { getToastDuration } from "./toast.utils.mjs";
|
|
7
|
+
var { not } = createGuards();
|
|
8
|
+
var machine = createMachine({
|
|
9
|
+
props({ props }) {
|
|
10
|
+
ensureProps(props, ["id", "type", "parent", "removeDelay"], "toast");
|
|
11
|
+
return {
|
|
12
|
+
closable: true,
|
|
13
|
+
...props,
|
|
14
|
+
duration: getToastDuration(props.duration, props.type)
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
initialState({ prop }) {
|
|
18
|
+
const persist = prop("type") === "loading" || prop("duration") === Infinity;
|
|
19
|
+
return persist ? "visible:persist" : "visible";
|
|
20
|
+
},
|
|
21
|
+
context({ prop, bindable }) {
|
|
22
|
+
return {
|
|
23
|
+
remainingTime: bindable(() => ({
|
|
24
|
+
defaultValue: getToastDuration(prop("duration"), prop("type"))
|
|
25
|
+
})),
|
|
26
|
+
createdAt: bindable(() => ({
|
|
27
|
+
defaultValue: Date.now()
|
|
28
|
+
})),
|
|
29
|
+
mounted: bindable(() => ({
|
|
30
|
+
defaultValue: false
|
|
31
|
+
})),
|
|
32
|
+
initialHeight: bindable(() => ({
|
|
33
|
+
defaultValue: 0
|
|
34
|
+
}))
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
refs() {
|
|
38
|
+
return {
|
|
39
|
+
closeTimerStartTime: Date.now(),
|
|
40
|
+
lastCloseStartTimerStartTime: 0
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
computed: {
|
|
44
|
+
zIndex: ({ prop }) => {
|
|
45
|
+
const toasts = prop("parent").context.get("toasts");
|
|
46
|
+
const index = toasts.findIndex((toast) => toast.id === prop("id"));
|
|
47
|
+
return toasts.length - index;
|
|
48
|
+
},
|
|
49
|
+
height: ({ prop }) => {
|
|
50
|
+
const heights = prop("parent").context.get("heights");
|
|
51
|
+
const height = heights.find((height2) => height2.id === prop("id"));
|
|
52
|
+
return height?.height ?? 0;
|
|
53
|
+
},
|
|
54
|
+
heightIndex: ({ prop }) => {
|
|
55
|
+
const heights = prop("parent").context.get("heights");
|
|
56
|
+
return heights.findIndex((height) => height.id === prop("id"));
|
|
57
|
+
},
|
|
58
|
+
frontmost: ({ prop }) => prop("index") === 0,
|
|
59
|
+
heightBefore: ({ prop }) => {
|
|
60
|
+
const heights = prop("parent").context.get("heights");
|
|
61
|
+
const heightIndex = heights.findIndex((height) => height.id === prop("id"));
|
|
62
|
+
return heights.reduce((prev, curr, reducerIndex) => {
|
|
63
|
+
if (reducerIndex >= heightIndex) return prev;
|
|
64
|
+
return prev + curr.height;
|
|
65
|
+
}, 0);
|
|
66
|
+
},
|
|
67
|
+
shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === Infinity
|
|
68
|
+
},
|
|
69
|
+
watch({ track, prop, send }) {
|
|
70
|
+
track([() => prop("message")], () => {
|
|
71
|
+
const message = prop("message");
|
|
72
|
+
if (message) send({ type: message, src: "programmatic" });
|
|
73
|
+
});
|
|
74
|
+
track([() => prop("type"), () => prop("duration")], () => {
|
|
75
|
+
send({ type: "UPDATE" });
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
on: {
|
|
79
|
+
UPDATE: [
|
|
80
|
+
{
|
|
81
|
+
guard: "shouldPersist",
|
|
82
|
+
target: "visible:persist",
|
|
83
|
+
actions: ["resetCloseTimer"]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
target: "visible:updating",
|
|
87
|
+
actions: ["resetCloseTimer"]
|
|
88
|
+
}
|
|
89
|
+
],
|
|
90
|
+
MEASURE: {
|
|
91
|
+
actions: ["measureHeight"]
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
entry: ["setMounted", "measureHeight", "invokeOnVisible"],
|
|
95
|
+
effects: ["trackHeight"],
|
|
96
|
+
states: {
|
|
97
|
+
"visible:updating": {
|
|
98
|
+
tags: ["visible", "updating"],
|
|
99
|
+
effects: ["waitForNextTick"],
|
|
100
|
+
on: {
|
|
101
|
+
SHOW: {
|
|
102
|
+
target: "visible"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"visible:persist": {
|
|
107
|
+
tags: ["visible", "paused"],
|
|
108
|
+
on: {
|
|
109
|
+
RESUME: {
|
|
110
|
+
guard: not("isLoadingType"),
|
|
111
|
+
target: "visible",
|
|
112
|
+
actions: ["setCloseTimer"]
|
|
113
|
+
},
|
|
114
|
+
DISMISS: {
|
|
115
|
+
target: "dismissing"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
visible: {
|
|
120
|
+
tags: ["visible"],
|
|
121
|
+
effects: ["waitForDuration"],
|
|
122
|
+
on: {
|
|
123
|
+
DISMISS: {
|
|
124
|
+
target: "dismissing"
|
|
125
|
+
},
|
|
126
|
+
PAUSE: {
|
|
127
|
+
target: "visible:persist",
|
|
128
|
+
actions: ["syncRemainingTime"]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
dismissing: {
|
|
133
|
+
entry: ["invokeOnDismiss"],
|
|
134
|
+
effects: ["waitForRemoveDelay"],
|
|
135
|
+
on: {
|
|
136
|
+
REMOVE: {
|
|
137
|
+
target: "unmounted",
|
|
138
|
+
actions: ["notifyParentToRemove"]
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
unmounted: {
|
|
143
|
+
entry: ["invokeOnUnmount"]
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
implementations: {
|
|
147
|
+
effects: {
|
|
148
|
+
waitForRemoveDelay({ prop, send }) {
|
|
149
|
+
return setRafTimeout(() => {
|
|
150
|
+
send({ type: "REMOVE", src: "timer" });
|
|
151
|
+
}, prop("removeDelay"));
|
|
152
|
+
},
|
|
153
|
+
waitForDuration({ send, context, computed }) {
|
|
154
|
+
if (computed("shouldPersist")) return;
|
|
155
|
+
return setRafTimeout(() => {
|
|
156
|
+
send({ type: "DISMISS", src: "timer" });
|
|
157
|
+
}, context.get("remainingTime"));
|
|
158
|
+
},
|
|
159
|
+
waitForNextTick({ send }) {
|
|
160
|
+
return setRafTimeout(() => {
|
|
161
|
+
send({ type: "SHOW", src: "timer" });
|
|
162
|
+
}, 0);
|
|
163
|
+
},
|
|
164
|
+
trackHeight({ scope, prop }) {
|
|
165
|
+
let cleanup;
|
|
166
|
+
raf(() => {
|
|
167
|
+
const rootEl = dom.getRootEl(scope);
|
|
168
|
+
if (!rootEl) return;
|
|
169
|
+
const syncHeight = () => {
|
|
170
|
+
const originalHeight = rootEl.style.height;
|
|
171
|
+
rootEl.style.height = "auto";
|
|
172
|
+
const height = rootEl.getBoundingClientRect().height;
|
|
173
|
+
rootEl.style.height = originalHeight;
|
|
174
|
+
const item = { id: prop("id"), height };
|
|
175
|
+
setHeight(prop("parent"), item);
|
|
176
|
+
};
|
|
177
|
+
const win = scope.getWin();
|
|
178
|
+
const observer = new win.MutationObserver(syncHeight);
|
|
179
|
+
observer.observe(rootEl, {
|
|
180
|
+
childList: true,
|
|
181
|
+
subtree: true,
|
|
182
|
+
characterData: true
|
|
183
|
+
});
|
|
184
|
+
cleanup = () => observer.disconnect();
|
|
185
|
+
});
|
|
186
|
+
return () => cleanup?.();
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
guards: {
|
|
190
|
+
isLoadingType: ({ prop }) => prop("type") === "loading",
|
|
191
|
+
shouldPersist: ({ computed }) => computed("shouldPersist")
|
|
192
|
+
},
|
|
193
|
+
actions: {
|
|
194
|
+
setMounted({ context }) {
|
|
195
|
+
raf(() => {
|
|
196
|
+
context.set("mounted", true);
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
measureHeight({ scope, prop, context }) {
|
|
200
|
+
queueMicrotask(() => {
|
|
201
|
+
const rootEl = dom.getRootEl(scope);
|
|
202
|
+
if (!rootEl) return;
|
|
203
|
+
const originalHeight = rootEl.style.height;
|
|
204
|
+
rootEl.style.height = "auto";
|
|
205
|
+
const height = rootEl.getBoundingClientRect().height;
|
|
206
|
+
rootEl.style.height = originalHeight;
|
|
207
|
+
context.set("initialHeight", height);
|
|
208
|
+
const item = { id: prop("id"), height };
|
|
209
|
+
setHeight(prop("parent"), item);
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
setCloseTimer({ refs }) {
|
|
213
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
214
|
+
},
|
|
215
|
+
resetCloseTimer({ context, refs, prop }) {
|
|
216
|
+
refs.set("closeTimerStartTime", Date.now());
|
|
217
|
+
context.set("remainingTime", getToastDuration(prop("duration"), prop("type")));
|
|
218
|
+
},
|
|
219
|
+
syncRemainingTime({ context, refs }) {
|
|
220
|
+
context.set("remainingTime", (prev) => {
|
|
221
|
+
const closeTimerStartTime = refs.get("closeTimerStartTime");
|
|
222
|
+
const elapsedTime = Date.now() - closeTimerStartTime;
|
|
223
|
+
refs.set("lastCloseStartTimerStartTime", Date.now());
|
|
224
|
+
return prev - elapsedTime;
|
|
225
|
+
});
|
|
226
|
+
},
|
|
227
|
+
notifyParentToRemove({ prop }) {
|
|
228
|
+
const parent = prop("parent");
|
|
229
|
+
parent.send({ type: "TOAST.REMOVE", id: prop("id") });
|
|
230
|
+
},
|
|
231
|
+
invokeOnDismiss({ prop, event }) {
|
|
232
|
+
prop("onStatusChange")?.({ status: "dismissing", src: event.src });
|
|
233
|
+
},
|
|
234
|
+
invokeOnUnmount({ prop }) {
|
|
235
|
+
prop("onStatusChange")?.({ status: "unmounted" });
|
|
236
|
+
},
|
|
237
|
+
invokeOnVisible({ prop }) {
|
|
238
|
+
prop("onStatusChange")?.({ status: "visible" });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
function setHeight(parent, item) {
|
|
244
|
+
const { id, height } = item;
|
|
245
|
+
parent.context.set("heights", (prev) => {
|
|
246
|
+
const alreadyExists = prev.find((i) => i.id === id);
|
|
247
|
+
if (!alreadyExists) {
|
|
248
|
+
return [{ id, height }, ...prev];
|
|
249
|
+
} else {
|
|
250
|
+
return prev.map((i) => i.id === id ? { ...i, height } : i);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
export {
|
|
255
|
+
machine
|
|
256
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ToastStoreProps, ToastStore } from './toast.types.mjs';
|
|
2
|
+
import '@zag-js/types';
|
|
3
|
+
import '@zag-js/core';
|
|
4
|
+
import '@zag-js/dom-query';
|
|
5
|
+
|
|
6
|
+
declare function createToastStore<V = any>(props?: ToastStoreProps): ToastStore<V>;
|
|
7
|
+
|
|
8
|
+
export { createToastStore };
|