@zag-js/toast 1.46.0 → 1.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/index.d.mts +10 -17
  2. package/dist/index.d.ts +10 -17
  3. package/dist/index.js +18 -45
  4. package/dist/index.mjs +5 -11
  5. package/dist/toast-group.connect.d.mts +4 -8
  6. package/dist/toast-group.connect.d.ts +4 -8
  7. package/dist/toast-group.connect.js +73 -97
  8. package/dist/toast-group.connect.mjs +68 -61
  9. package/dist/toast-group.machine.d.mts +2 -8
  10. package/dist/toast-group.machine.d.ts +2 -8
  11. package/dist/toast-group.machine.js +294 -302
  12. package/dist/toast-group.machine.mjs +289 -266
  13. package/dist/toast.anatomy.d.mts +2 -6
  14. package/dist/toast.anatomy.d.ts +2 -6
  15. package/dist/toast.anatomy.js +8 -40
  16. package/dist/toast.anatomy.mjs +4 -14
  17. package/dist/toast.connect.d.mts +4 -8
  18. package/dist/toast.connect.d.ts +4 -8
  19. package/dist/toast.connect.js +133 -158
  20. package/dist/toast.connect.mjs +128 -122
  21. package/dist/toast.dom.d.mts +21 -16
  22. package/dist/toast.dom.d.ts +21 -16
  23. package/dist/toast.dom.js +56 -53
  24. package/dist/toast.dom.mjs +42 -20
  25. package/dist/toast.machine.d.mts +2 -8
  26. package/dist/toast.machine.d.ts +2 -8
  27. package/dist/toast.machine.js +272 -311
  28. package/dist/toast.machine.mjs +268 -276
  29. package/dist/toast.store.d.mts +2 -8
  30. package/dist/toast.store.d.ts +2 -8
  31. package/dist/toast.store.js +344 -299
  32. package/dist/toast.store.mjs +340 -274
  33. package/dist/toast.types.d.mts +372 -374
  34. package/dist/toast.types.d.ts +372 -374
  35. package/dist/toast.types.js +4 -18
  36. package/dist/toast.types.mjs +1 -0
  37. package/dist/toast.utils.d.mts +10 -13
  38. package/dist/toast.utils.d.ts +10 -13
  39. package/dist/toast.utils.js +169 -198
  40. package/dist/toast.utils.mjs +159 -168
  41. package/package.json +9 -9
@@ -1,315 +1,276 @@
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
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
34
4
  });
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.js"));
40
- var import_toast = require("./toast.utils.js");
41
- var { not, and } = (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
- if (prop("message") === "DISMISS") return "dismissing";
53
- const persist = prop("type") === "loading" || prop("duration") === Infinity || prop("paused");
54
- return persist ? "visible:persist" : "visible";
55
- },
56
- context({ prop, bindable }) {
57
- return {
58
- remainingTime: bindable(() => ({
59
- defaultValue: (0, import_toast.getToastDuration)(prop("duration"), prop("type"))
60
- })),
61
- createdAt: bindable(() => ({
62
- defaultValue: Date.now()
63
- })),
64
- mounted: bindable(() => ({
65
- defaultValue: false
66
- })),
67
- initialHeight: bindable(() => ({
68
- defaultValue: 0
69
- })),
70
- rendered: bindable(() => ({
71
- defaultValue: { title: true, description: true }
72
- }))
73
- };
74
- },
75
- refs() {
76
- return {
77
- closeTimerStartTime: Date.now(),
78
- lastCloseStartTimerStartTime: 0
79
- };
80
- },
81
- computed: {
82
- zIndex: ({ prop }) => {
83
- const toasts = prop("parent").context.get("toasts");
84
- const index = toasts.findIndex((toast) => toast.id === prop("id"));
85
- return toasts.length - index;
86
- },
87
- height: ({ prop }) => {
88
- const heights = prop("parent").context.get("heights");
89
- const height = heights.find((height2) => height2.id === prop("id"));
90
- return height?.height ?? 0;
91
- },
92
- heightIndex: ({ prop }) => {
93
- const heights = prop("parent").context.get("heights");
94
- return heights.findIndex((height) => height.id === prop("id"));
95
- },
96
- frontmost: ({ prop }) => prop("index") === 0,
97
- heightBefore: ({ prop }) => {
98
- const heights = prop("parent").context.get("heights");
99
- const heightIndex = heights.findIndex((height) => height.id === prop("id"));
100
- return heights.reduce((prev, curr, reducerIndex) => {
101
- if (reducerIndex >= heightIndex) return prev;
102
- return prev + curr.height;
103
- }, 0);
104
- },
105
- shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === Infinity
106
- },
107
- watch({ track, prop, send }) {
108
- track([() => prop("message")], () => {
109
- const message = prop("message");
110
- if (message) send({ type: message, src: "programmatic" });
111
- });
112
- track([() => prop("paused")], () => {
113
- send({ type: prop("paused") ? "PAUSE" : "RESUME" });
114
- });
115
- track([() => prop("type"), () => prop("duration")], () => {
116
- send({ type: "UPDATE" });
117
- });
118
- },
119
- on: {
120
- UPDATE: [
121
- {
122
- guard: and("isVisible", "shouldPause"),
123
- target: "visible:persist",
124
- actions: ["resetCloseTimer"]
125
- },
126
- {
127
- guard: "isVisible",
128
- target: "visible:updating",
129
- actions: ["resetCloseTimer"]
130
- }
131
- ],
132
- MEASURE: {
133
- actions: ["measureHeight"]
134
- }
135
- },
136
- entry: ["setMounted", "measureHeight", "invokeOnVisible"],
137
- effects: ["trackContent"],
138
- states: {
139
- "visible:updating": {
140
- tags: ["visible", "updating"],
141
- effects: ["waitForNextTick"],
142
- on: {
143
- DISMISS: {
144
- target: "dismissing"
145
- },
146
- PAUSE: {
147
- target: "visible:persist"
148
- },
149
- SHOW: {
150
- target: "visible"
151
- }
152
- }
153
- },
154
- "visible:persist": {
155
- tags: ["visible", "paused"],
156
- on: {
157
- RESUME: {
158
- guard: not("shouldPause"),
159
- target: "visible",
160
- actions: ["setCloseTimer"]
161
- },
162
- DISMISS: {
163
- target: "dismissing"
164
- }
165
- }
166
- },
167
- visible: {
168
- tags: ["visible"],
169
- effects: ["waitForDuration"],
170
- on: {
171
- DISMISS: {
172
- target: "dismissing"
173
- },
174
- PAUSE: {
175
- target: "visible:persist",
176
- actions: ["syncRemainingTime"]
177
- }
178
- }
179
- },
180
- dismissing: {
181
- entry: ["invokeOnDismiss"],
182
- effects: ["waitForRemoveDelay"],
183
- on: {
184
- REMOVE: {
185
- target: "unmounted",
186
- actions: ["notifyParentToRemove"]
187
- }
188
- }
189
- },
190
- unmounted: {
191
- entry: ["invokeOnUnmount"]
192
- }
193
- },
194
- implementations: {
195
- effects: {
196
- waitForRemoveDelay({ prop, send }) {
197
- return (0, import_utils.setRafTimeout)(() => {
198
- send({ type: "REMOVE", src: "timer" });
199
- }, prop("removeDelay"));
200
- },
201
- waitForDuration({ send, context, computed }) {
202
- if (computed("shouldPersist")) return;
203
- return (0, import_utils.setRafTimeout)(() => {
204
- send({ type: "DISMISS", src: "timer" });
205
- }, context.get("remainingTime"));
206
- },
207
- waitForNextTick({ send }) {
208
- return (0, import_utils.setRafTimeout)(() => {
209
- send({ type: "SHOW", src: "timer" });
210
- }, 0);
211
- },
212
- trackContent({ scope, prop, context }) {
213
- return (0, import_dom_query.raf)(() => {
214
- const rootEl = dom.getRootEl(scope);
215
- if (!rootEl) return;
216
- const syncHeight = () => {
217
- const height = measureLayoutHeight(rootEl);
218
- const item = { id: prop("id"), height };
219
- setHeight(prop("parent"), item);
220
- };
221
- const syncRenderedElements = () => {
222
- context.set("rendered", {
223
- title: !!dom.getTitleEl(scope),
224
- description: !!dom.getDescriptionEl(scope)
225
- });
226
- };
227
- syncRenderedElements();
228
- const win = scope.getWin();
229
- const observer = new win.MutationObserver(() => {
230
- syncHeight();
231
- syncRenderedElements();
232
- });
233
- observer.observe(rootEl, {
234
- childList: true,
235
- subtree: true,
236
- characterData: true
237
- });
238
- return () => observer.disconnect();
239
- });
240
- }
241
- },
242
- guards: {
243
- isVisible: ({ state }) => state.hasTag("visible"),
244
- shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
245
- },
246
- actions: {
247
- setMounted({ context }) {
248
- (0, import_dom_query.raf)(() => {
249
- context.set("mounted", true);
250
- });
251
- },
252
- measureHeight({ scope, prop, context }) {
253
- queueMicrotask(() => {
254
- const rootEl = dom.getRootEl(scope);
255
- if (!rootEl) return;
256
- const height = measureLayoutHeight(rootEl);
257
- context.set("initialHeight", height);
258
- const item = { id: prop("id"), height };
259
- setHeight(prop("parent"), item);
260
- });
261
- },
262
- setCloseTimer({ refs }) {
263
- refs.set("closeTimerStartTime", Date.now());
264
- },
265
- resetCloseTimer({ context, refs, prop }) {
266
- refs.set("closeTimerStartTime", Date.now());
267
- context.set("remainingTime", (0, import_toast.getToastDuration)(prop("duration"), prop("type")));
268
- },
269
- syncRemainingTime({ context, refs }) {
270
- context.set("remainingTime", (prev) => {
271
- const closeTimerStartTime = refs.get("closeTimerStartTime");
272
- const elapsedTime = Date.now() - closeTimerStartTime;
273
- refs.set("lastCloseStartTimerStartTime", Date.now());
274
- return prev - elapsedTime;
275
- });
276
- },
277
- notifyParentToRemove({ prop }) {
278
- const parent = prop("parent");
279
- parent.send({ type: "TOAST.REMOVE", id: prop("id") });
280
- },
281
- invokeOnDismiss({ prop, event }) {
282
- prop("parent").prop("store").dismiss(prop("id"));
283
- prop("onStatusChange")?.({ status: "dismissing", src: event.src });
284
- },
285
- invokeOnUnmount({ prop }) {
286
- prop("onStatusChange")?.({ status: "unmounted" });
287
- },
288
- invokeOnVisible({ prop }) {
289
- prop("onStatusChange")?.({ status: "visible" });
290
- }
291
- }
292
- }
5
+ const require_toast_dom = require("./toast.dom.js");
6
+ const require_toast_utils = require("./toast.utils.js");
7
+ let _zag_js_dom_query = require("@zag-js/dom-query");
8
+ let _zag_js_utils = require("@zag-js/utils");
9
+ let _zag_js_core = require("@zag-js/core");
10
+ // src/toast.machine.ts
11
+ const { not, and } = (0, _zag_js_core.createGuards)();
12
+ const machine = (0, _zag_js_core.createMachine)({
13
+ props({ props }) {
14
+ (0, _zag_js_utils.ensureProps)(props, [
15
+ "id",
16
+ "type",
17
+ "parent",
18
+ "removeDelay"
19
+ ], "toast");
20
+ return {
21
+ closable: true,
22
+ ...props,
23
+ duration: require_toast_utils.getToastDuration(props.duration, props.type)
24
+ };
25
+ },
26
+ initialState({ prop }) {
27
+ if (prop("message") === "DISMISS") return "dismissing";
28
+ return prop("type") === "loading" || prop("duration") === 1e400 || prop("paused") ? "visible:persist" : "visible";
29
+ },
30
+ context({ prop, bindable }) {
31
+ return {
32
+ remainingTime: bindable(() => ({ defaultValue: require_toast_utils.getToastDuration(prop("duration"), prop("type")) })),
33
+ createdAt: bindable(() => ({ defaultValue: Date.now() })),
34
+ mounted: bindable(() => ({ defaultValue: false })),
35
+ initialHeight: bindable(() => ({ defaultValue: 0 })),
36
+ rendered: bindable(() => ({ defaultValue: {
37
+ title: true,
38
+ description: true
39
+ } }))
40
+ };
41
+ },
42
+ refs() {
43
+ return {
44
+ closeTimerStartTime: Date.now(),
45
+ lastCloseStartTimerStartTime: 0
46
+ };
47
+ },
48
+ computed: {
49
+ zIndex: ({ prop }) => {
50
+ const toasts = prop("parent").context.get("toasts");
51
+ const index = toasts.findIndex((toast) => toast.id === prop("id"));
52
+ return toasts.length - index;
53
+ },
54
+ height: ({ prop }) => {
55
+ return prop("parent").context.get("heights").find((height) => height.id === prop("id"))?.height ?? 0;
56
+ },
57
+ heightIndex: ({ prop }) => {
58
+ return prop("parent").context.get("heights").findIndex((height) => height.id === prop("id"));
59
+ },
60
+ frontmost: ({ prop }) => prop("index") === 0,
61
+ heightBefore: ({ prop }) => {
62
+ const heights = prop("parent").context.get("heights");
63
+ const heightIndex = heights.findIndex((height) => height.id === prop("id"));
64
+ return heights.reduce((prev, curr, reducerIndex) => {
65
+ if (reducerIndex >= heightIndex) return prev;
66
+ return prev + curr.height;
67
+ }, 0);
68
+ },
69
+ shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === 1e400
70
+ },
71
+ watch({ track, prop, send }) {
72
+ track([() => prop("message")], () => {
73
+ const message = prop("message");
74
+ if (message) send({
75
+ type: message,
76
+ src: "programmatic"
77
+ });
78
+ });
79
+ track([() => prop("paused")], () => {
80
+ send({ type: prop("paused") ? "PAUSE" : "RESUME" });
81
+ });
82
+ track([() => prop("type"), () => prop("duration")], () => {
83
+ send({ type: "UPDATE" });
84
+ });
85
+ },
86
+ on: {
87
+ UPDATE: [{
88
+ guard: and("isVisible", "shouldPause"),
89
+ target: "visible:persist",
90
+ actions: ["resetCloseTimer"]
91
+ }, {
92
+ guard: "isVisible",
93
+ target: "visible:updating",
94
+ actions: ["resetCloseTimer"]
95
+ }],
96
+ MEASURE: { actions: ["measureHeight"] }
97
+ },
98
+ entry: [
99
+ "setMounted",
100
+ "measureHeight",
101
+ "invokeOnVisible"
102
+ ],
103
+ effects: ["trackContent"],
104
+ states: {
105
+ "visible:updating": {
106
+ tags: ["visible", "updating"],
107
+ effects: ["waitForNextTick"],
108
+ on: {
109
+ DISMISS: { target: "dismissing" },
110
+ PAUSE: { target: "visible:persist" },
111
+ SHOW: { target: "visible" }
112
+ }
113
+ },
114
+ "visible:persist": {
115
+ tags: ["visible", "paused"],
116
+ on: {
117
+ RESUME: {
118
+ guard: not("shouldPause"),
119
+ target: "visible",
120
+ actions: ["setCloseTimer"]
121
+ },
122
+ DISMISS: { target: "dismissing" }
123
+ }
124
+ },
125
+ visible: {
126
+ tags: ["visible"],
127
+ effects: ["waitForDuration"],
128
+ on: {
129
+ DISMISS: { target: "dismissing" },
130
+ PAUSE: {
131
+ target: "visible:persist",
132
+ actions: ["syncRemainingTime"]
133
+ }
134
+ }
135
+ },
136
+ dismissing: {
137
+ entry: ["notifyParentOfDismiss", "invokeOnDismiss"],
138
+ effects: ["waitForRemoveDelay"],
139
+ on: { REMOVE: {
140
+ target: "unmounted",
141
+ actions: ["notifyParentToRemove"]
142
+ } }
143
+ },
144
+ unmounted: { entry: ["invokeOnUnmount"] }
145
+ },
146
+ implementations: {
147
+ effects: {
148
+ waitForRemoveDelay({ prop, send }) {
149
+ return (0, _zag_js_utils.setRafTimeout)(() => {
150
+ send({
151
+ type: "REMOVE",
152
+ src: "timer"
153
+ });
154
+ }, prop("removeDelay"));
155
+ },
156
+ waitForDuration({ send, context, computed }) {
157
+ if (computed("shouldPersist")) return;
158
+ return (0, _zag_js_utils.setRafTimeout)(() => {
159
+ send({
160
+ type: "DISMISS",
161
+ src: "timer"
162
+ });
163
+ }, context.get("remainingTime"));
164
+ },
165
+ waitForNextTick({ send }) {
166
+ return (0, _zag_js_utils.setRafTimeout)(() => {
167
+ send({
168
+ type: "SHOW",
169
+ src: "timer"
170
+ });
171
+ }, 0);
172
+ },
173
+ trackContent({ scope, prop, context, state }) {
174
+ return (0, _zag_js_dom_query.raf)(() => {
175
+ const rootEl = require_toast_dom.getRootEl(scope);
176
+ if (!rootEl) return;
177
+ const syncHeight = () => {
178
+ if (!state.hasTag("visible") || !rootEl.isConnected) return;
179
+ const height = measureLayoutHeight(rootEl);
180
+ require_toast_utils.setHeight(prop("parent"), {
181
+ id: prop("id"),
182
+ height
183
+ });
184
+ };
185
+ const syncRenderedElements = () => {
186
+ context.set("rendered", {
187
+ title: !!require_toast_dom.getTitleEl(scope),
188
+ description: !!require_toast_dom.getDescriptionEl(scope)
189
+ });
190
+ };
191
+ syncRenderedElements();
192
+ return require_toast_dom.observeContent(rootEl, {
193
+ filter: (el) => !el.hasAttribute("data-ghost"),
194
+ onChange: syncHeight,
195
+ onMutate: syncRenderedElements
196
+ });
197
+ });
198
+ }
199
+ },
200
+ guards: {
201
+ isVisible: ({ state }) => state.hasTag("visible"),
202
+ shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
203
+ },
204
+ actions: {
205
+ setMounted({ context }) {
206
+ (0, _zag_js_dom_query.raf)(() => {
207
+ context.set("mounted", true);
208
+ });
209
+ },
210
+ measureHeight({ scope, prop, context }) {
211
+ queueMicrotask(() => {
212
+ const rootEl = require_toast_dom.getRootEl(scope);
213
+ if (!rootEl) return;
214
+ const height = measureLayoutHeight(rootEl);
215
+ context.set("initialHeight", height);
216
+ const item = {
217
+ id: prop("id"),
218
+ height
219
+ };
220
+ require_toast_utils.setHeight(prop("parent"), item);
221
+ });
222
+ },
223
+ setCloseTimer({ refs }) {
224
+ refs.set("closeTimerStartTime", Date.now());
225
+ },
226
+ resetCloseTimer({ context, refs, prop }) {
227
+ refs.set("closeTimerStartTime", Date.now());
228
+ context.set("remainingTime", require_toast_utils.getToastDuration(prop("duration"), prop("type")));
229
+ },
230
+ syncRemainingTime({ context, refs }) {
231
+ context.set("remainingTime", (prev) => {
232
+ const closeTimerStartTime = refs.get("closeTimerStartTime");
233
+ const elapsedTime = Date.now() - closeTimerStartTime;
234
+ refs.set("lastCloseStartTimerStartTime", Date.now());
235
+ return prev - elapsedTime;
236
+ });
237
+ },
238
+ notifyParentToRemove({ prop }) {
239
+ prop("parent").send({
240
+ type: "TOAST.REMOVE",
241
+ id: prop("id")
242
+ });
243
+ },
244
+ notifyParentOfDismiss({ prop, scope }) {
245
+ const activeEl = scope.getActiveElement();
246
+ if (!(0, _zag_js_dom_query.contains)(require_toast_dom.getRootEl(scope), activeEl)) return;
247
+ const focusVisible = !!activeEl?.matches(":focus-visible");
248
+ prop("parent").send({
249
+ type: "TOAST.DISMISS",
250
+ focusVisible
251
+ });
252
+ },
253
+ invokeOnDismiss({ prop, event }) {
254
+ prop("parent").prop("store").dismiss(prop("id"));
255
+ prop("onStatusChange")?.({
256
+ status: "dismissing",
257
+ src: event.src
258
+ });
259
+ },
260
+ invokeOnUnmount({ prop }) {
261
+ prop("onStatusChange")?.({ status: "unmounted" });
262
+ },
263
+ invokeOnVisible({ prop }) {
264
+ prop("onStatusChange")?.({ status: "visible" });
265
+ }
266
+ }
267
+ }
293
268
  });
294
269
  function measureLayoutHeight(el) {
295
- const prevHeight = el.style.height;
296
- el.style.height = "auto";
297
- const height = el.offsetHeight;
298
- el.style.height = prevHeight;
299
- return height;
270
+ const prevHeight = el.style.height;
271
+ el.style.height = "auto";
272
+ const height = el.offsetHeight;
273
+ el.style.height = prevHeight;
274
+ return height;
300
275
  }
301
- function setHeight(parent, item) {
302
- const { id, height } = item;
303
- parent.context.set("heights", (prev) => {
304
- const alreadyExists = prev.find((i) => i.id === id);
305
- if (!alreadyExists) {
306
- return [{ id, height }, ...prev];
307
- } else {
308
- return prev.map((i) => i.id === id ? { ...i, height } : i);
309
- }
310
- });
311
- }
312
- // Annotate the CommonJS export names for ESM import in node:
313
- 0 && (module.exports = {
314
- machine
315
- });
276
+ exports.machine = machine;