@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,280 +1,272 @@
1
- // src/toast.machine.ts
2
- import { createGuards, createMachine } from "@zag-js/core";
3
- import { raf } from "@zag-js/dom-query";
1
+ import { getDescriptionEl, getRootEl, getTitleEl, observeContent } from "./toast.dom.mjs";
2
+ import { getToastDuration, setHeight } from "./toast.utils.mjs";
3
+ import { contains, raf } from "@zag-js/dom-query";
4
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, and } = 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
- if (prop("message") === "DISMISS") return "dismissing";
19
- const persist = prop("type") === "loading" || prop("duration") === Infinity || prop("paused");
20
- return persist ? "visible:persist" : "visible";
21
- },
22
- context({ prop, bindable }) {
23
- return {
24
- remainingTime: bindable(() => ({
25
- defaultValue: getToastDuration(prop("duration"), prop("type"))
26
- })),
27
- createdAt: bindable(() => ({
28
- defaultValue: Date.now()
29
- })),
30
- mounted: bindable(() => ({
31
- defaultValue: false
32
- })),
33
- initialHeight: bindable(() => ({
34
- defaultValue: 0
35
- })),
36
- rendered: bindable(() => ({
37
- defaultValue: { title: true, description: true }
38
- }))
39
- };
40
- },
41
- refs() {
42
- return {
43
- closeTimerStartTime: Date.now(),
44
- lastCloseStartTimerStartTime: 0
45
- };
46
- },
47
- computed: {
48
- zIndex: ({ prop }) => {
49
- const toasts = prop("parent").context.get("toasts");
50
- const index = toasts.findIndex((toast) => toast.id === prop("id"));
51
- return toasts.length - index;
52
- },
53
- height: ({ prop }) => {
54
- const heights = prop("parent").context.get("heights");
55
- const height = heights.find((height2) => height2.id === prop("id"));
56
- return height?.height ?? 0;
57
- },
58
- heightIndex: ({ prop }) => {
59
- const heights = prop("parent").context.get("heights");
60
- return heights.findIndex((height) => height.id === prop("id"));
61
- },
62
- frontmost: ({ prop }) => prop("index") === 0,
63
- heightBefore: ({ prop }) => {
64
- const heights = prop("parent").context.get("heights");
65
- const heightIndex = heights.findIndex((height) => height.id === prop("id"));
66
- return heights.reduce((prev, curr, reducerIndex) => {
67
- if (reducerIndex >= heightIndex) return prev;
68
- return prev + curr.height;
69
- }, 0);
70
- },
71
- shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === Infinity
72
- },
73
- watch({ track, prop, send }) {
74
- track([() => prop("message")], () => {
75
- const message = prop("message");
76
- if (message) send({ type: message, src: "programmatic" });
77
- });
78
- track([() => prop("paused")], () => {
79
- send({ type: prop("paused") ? "PAUSE" : "RESUME" });
80
- });
81
- track([() => prop("type"), () => prop("duration")], () => {
82
- send({ type: "UPDATE" });
83
- });
84
- },
85
- on: {
86
- UPDATE: [
87
- {
88
- guard: and("isVisible", "shouldPause"),
89
- target: "visible:persist",
90
- actions: ["resetCloseTimer"]
91
- },
92
- {
93
- guard: "isVisible",
94
- target: "visible:updating",
95
- actions: ["resetCloseTimer"]
96
- }
97
- ],
98
- MEASURE: {
99
- actions: ["measureHeight"]
100
- }
101
- },
102
- entry: ["setMounted", "measureHeight", "invokeOnVisible"],
103
- effects: ["trackContent"],
104
- states: {
105
- "visible:updating": {
106
- tags: ["visible", "updating"],
107
- effects: ["waitForNextTick"],
108
- on: {
109
- DISMISS: {
110
- target: "dismissing"
111
- },
112
- PAUSE: {
113
- target: "visible:persist"
114
- },
115
- SHOW: {
116
- target: "visible"
117
- }
118
- }
119
- },
120
- "visible:persist": {
121
- tags: ["visible", "paused"],
122
- on: {
123
- RESUME: {
124
- guard: not("shouldPause"),
125
- target: "visible",
126
- actions: ["setCloseTimer"]
127
- },
128
- DISMISS: {
129
- target: "dismissing"
130
- }
131
- }
132
- },
133
- visible: {
134
- tags: ["visible"],
135
- effects: ["waitForDuration"],
136
- on: {
137
- DISMISS: {
138
- target: "dismissing"
139
- },
140
- PAUSE: {
141
- target: "visible:persist",
142
- actions: ["syncRemainingTime"]
143
- }
144
- }
145
- },
146
- dismissing: {
147
- entry: ["invokeOnDismiss"],
148
- effects: ["waitForRemoveDelay"],
149
- on: {
150
- REMOVE: {
151
- target: "unmounted",
152
- actions: ["notifyParentToRemove"]
153
- }
154
- }
155
- },
156
- unmounted: {
157
- entry: ["invokeOnUnmount"]
158
- }
159
- },
160
- implementations: {
161
- effects: {
162
- waitForRemoveDelay({ prop, send }) {
163
- return setRafTimeout(() => {
164
- send({ type: "REMOVE", src: "timer" });
165
- }, prop("removeDelay"));
166
- },
167
- waitForDuration({ send, context, computed }) {
168
- if (computed("shouldPersist")) return;
169
- return setRafTimeout(() => {
170
- send({ type: "DISMISS", src: "timer" });
171
- }, context.get("remainingTime"));
172
- },
173
- waitForNextTick({ send }) {
174
- return setRafTimeout(() => {
175
- send({ type: "SHOW", src: "timer" });
176
- }, 0);
177
- },
178
- trackContent({ scope, prop, context }) {
179
- return raf(() => {
180
- const rootEl = dom.getRootEl(scope);
181
- if (!rootEl) return;
182
- const syncHeight = () => {
183
- const height = measureLayoutHeight(rootEl);
184
- const item = { id: prop("id"), height };
185
- setHeight(prop("parent"), item);
186
- };
187
- const syncRenderedElements = () => {
188
- context.set("rendered", {
189
- title: !!dom.getTitleEl(scope),
190
- description: !!dom.getDescriptionEl(scope)
191
- });
192
- };
193
- syncRenderedElements();
194
- const win = scope.getWin();
195
- const observer = new win.MutationObserver(() => {
196
- syncHeight();
197
- syncRenderedElements();
198
- });
199
- observer.observe(rootEl, {
200
- childList: true,
201
- subtree: true,
202
- characterData: true
203
- });
204
- return () => observer.disconnect();
205
- });
206
- }
207
- },
208
- guards: {
209
- isVisible: ({ state }) => state.hasTag("visible"),
210
- shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
211
- },
212
- actions: {
213
- setMounted({ context }) {
214
- raf(() => {
215
- context.set("mounted", true);
216
- });
217
- },
218
- measureHeight({ scope, prop, context }) {
219
- queueMicrotask(() => {
220
- const rootEl = dom.getRootEl(scope);
221
- if (!rootEl) return;
222
- const height = measureLayoutHeight(rootEl);
223
- context.set("initialHeight", height);
224
- const item = { id: prop("id"), height };
225
- setHeight(prop("parent"), item);
226
- });
227
- },
228
- setCloseTimer({ refs }) {
229
- refs.set("closeTimerStartTime", Date.now());
230
- },
231
- resetCloseTimer({ context, refs, prop }) {
232
- refs.set("closeTimerStartTime", Date.now());
233
- context.set("remainingTime", getToastDuration(prop("duration"), prop("type")));
234
- },
235
- syncRemainingTime({ context, refs }) {
236
- context.set("remainingTime", (prev) => {
237
- const closeTimerStartTime = refs.get("closeTimerStartTime");
238
- const elapsedTime = Date.now() - closeTimerStartTime;
239
- refs.set("lastCloseStartTimerStartTime", Date.now());
240
- return prev - elapsedTime;
241
- });
242
- },
243
- notifyParentToRemove({ prop }) {
244
- const parent = prop("parent");
245
- parent.send({ type: "TOAST.REMOVE", id: prop("id") });
246
- },
247
- invokeOnDismiss({ prop, event }) {
248
- prop("parent").prop("store").dismiss(prop("id"));
249
- prop("onStatusChange")?.({ status: "dismissing", src: event.src });
250
- },
251
- invokeOnUnmount({ prop }) {
252
- prop("onStatusChange")?.({ status: "unmounted" });
253
- },
254
- invokeOnVisible({ prop }) {
255
- prop("onStatusChange")?.({ status: "visible" });
256
- }
257
- }
258
- }
5
+ import { createGuards, createMachine } from "@zag-js/core";
6
+ // src/toast.machine.ts
7
+ const { not, and } = createGuards();
8
+ const machine = createMachine({
9
+ props({ props }) {
10
+ ensureProps(props, [
11
+ "id",
12
+ "type",
13
+ "parent",
14
+ "removeDelay"
15
+ ], "toast");
16
+ return {
17
+ closable: true,
18
+ ...props,
19
+ duration: getToastDuration(props.duration, props.type)
20
+ };
21
+ },
22
+ initialState({ prop }) {
23
+ if (prop("message") === "DISMISS") return "dismissing";
24
+ return prop("type") === "loading" || prop("duration") === 1e400 || prop("paused") ? "visible:persist" : "visible";
25
+ },
26
+ context({ prop, bindable }) {
27
+ return {
28
+ remainingTime: bindable(() => ({ defaultValue: getToastDuration(prop("duration"), prop("type")) })),
29
+ createdAt: bindable(() => ({ defaultValue: Date.now() })),
30
+ mounted: bindable(() => ({ defaultValue: false })),
31
+ initialHeight: bindable(() => ({ defaultValue: 0 })),
32
+ rendered: bindable(() => ({ defaultValue: {
33
+ title: true,
34
+ description: true
35
+ } }))
36
+ };
37
+ },
38
+ refs() {
39
+ return {
40
+ closeTimerStartTime: Date.now(),
41
+ lastCloseStartTimerStartTime: 0
42
+ };
43
+ },
44
+ computed: {
45
+ zIndex: ({ prop }) => {
46
+ const toasts = prop("parent").context.get("toasts");
47
+ const index = toasts.findIndex((toast) => toast.id === prop("id"));
48
+ return toasts.length - index;
49
+ },
50
+ height: ({ prop }) => {
51
+ return prop("parent").context.get("heights").find((height) => height.id === prop("id"))?.height ?? 0;
52
+ },
53
+ heightIndex: ({ prop }) => {
54
+ return prop("parent").context.get("heights").findIndex((height) => height.id === prop("id"));
55
+ },
56
+ frontmost: ({ prop }) => prop("index") === 0,
57
+ heightBefore: ({ prop }) => {
58
+ const heights = prop("parent").context.get("heights");
59
+ const heightIndex = heights.findIndex((height) => height.id === prop("id"));
60
+ return heights.reduce((prev, curr, reducerIndex) => {
61
+ if (reducerIndex >= heightIndex) return prev;
62
+ return prev + curr.height;
63
+ }, 0);
64
+ },
65
+ shouldPersist: ({ prop }) => prop("type") === "loading" || prop("duration") === 1e400
66
+ },
67
+ watch({ track, prop, send }) {
68
+ track([() => prop("message")], () => {
69
+ const message = prop("message");
70
+ if (message) send({
71
+ type: message,
72
+ src: "programmatic"
73
+ });
74
+ });
75
+ track([() => prop("paused")], () => {
76
+ send({ type: prop("paused") ? "PAUSE" : "RESUME" });
77
+ });
78
+ track([() => prop("type"), () => prop("duration")], () => {
79
+ send({ type: "UPDATE" });
80
+ });
81
+ },
82
+ on: {
83
+ UPDATE: [{
84
+ guard: and("isVisible", "shouldPause"),
85
+ target: "visible:persist",
86
+ actions: ["resetCloseTimer"]
87
+ }, {
88
+ guard: "isVisible",
89
+ target: "visible:updating",
90
+ actions: ["resetCloseTimer"]
91
+ }],
92
+ MEASURE: { actions: ["measureHeight"] }
93
+ },
94
+ entry: [
95
+ "setMounted",
96
+ "measureHeight",
97
+ "invokeOnVisible"
98
+ ],
99
+ effects: ["trackContent"],
100
+ states: {
101
+ "visible:updating": {
102
+ tags: ["visible", "updating"],
103
+ effects: ["waitForNextTick"],
104
+ on: {
105
+ DISMISS: { target: "dismissing" },
106
+ PAUSE: { target: "visible:persist" },
107
+ SHOW: { target: "visible" }
108
+ }
109
+ },
110
+ "visible:persist": {
111
+ tags: ["visible", "paused"],
112
+ on: {
113
+ RESUME: {
114
+ guard: not("shouldPause"),
115
+ target: "visible",
116
+ actions: ["setCloseTimer"]
117
+ },
118
+ DISMISS: { target: "dismissing" }
119
+ }
120
+ },
121
+ visible: {
122
+ tags: ["visible"],
123
+ effects: ["waitForDuration"],
124
+ on: {
125
+ DISMISS: { target: "dismissing" },
126
+ PAUSE: {
127
+ target: "visible:persist",
128
+ actions: ["syncRemainingTime"]
129
+ }
130
+ }
131
+ },
132
+ dismissing: {
133
+ entry: ["notifyParentOfDismiss", "invokeOnDismiss"],
134
+ effects: ["waitForRemoveDelay"],
135
+ on: { REMOVE: {
136
+ target: "unmounted",
137
+ actions: ["notifyParentToRemove"]
138
+ } }
139
+ },
140
+ unmounted: { entry: ["invokeOnUnmount"] }
141
+ },
142
+ implementations: {
143
+ effects: {
144
+ waitForRemoveDelay({ prop, send }) {
145
+ return setRafTimeout(() => {
146
+ send({
147
+ type: "REMOVE",
148
+ src: "timer"
149
+ });
150
+ }, prop("removeDelay"));
151
+ },
152
+ waitForDuration({ send, context, computed }) {
153
+ if (computed("shouldPersist")) return;
154
+ return setRafTimeout(() => {
155
+ send({
156
+ type: "DISMISS",
157
+ src: "timer"
158
+ });
159
+ }, context.get("remainingTime"));
160
+ },
161
+ waitForNextTick({ send }) {
162
+ return setRafTimeout(() => {
163
+ send({
164
+ type: "SHOW",
165
+ src: "timer"
166
+ });
167
+ }, 0);
168
+ },
169
+ trackContent({ scope, prop, context, state }) {
170
+ return raf(() => {
171
+ const rootEl = getRootEl(scope);
172
+ if (!rootEl) return;
173
+ const syncHeight = () => {
174
+ if (!state.hasTag("visible") || !rootEl.isConnected) return;
175
+ const height = measureLayoutHeight(rootEl);
176
+ setHeight(prop("parent"), {
177
+ id: prop("id"),
178
+ height
179
+ });
180
+ };
181
+ const syncRenderedElements = () => {
182
+ context.set("rendered", {
183
+ title: !!getTitleEl(scope),
184
+ description: !!getDescriptionEl(scope)
185
+ });
186
+ };
187
+ syncRenderedElements();
188
+ return observeContent(rootEl, {
189
+ filter: (el) => !el.hasAttribute("data-ghost"),
190
+ onChange: syncHeight,
191
+ onMutate: syncRenderedElements
192
+ });
193
+ });
194
+ }
195
+ },
196
+ guards: {
197
+ isVisible: ({ state }) => state.hasTag("visible"),
198
+ shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
199
+ },
200
+ actions: {
201
+ setMounted({ context }) {
202
+ raf(() => {
203
+ context.set("mounted", true);
204
+ });
205
+ },
206
+ measureHeight({ scope, prop, context }) {
207
+ queueMicrotask(() => {
208
+ const rootEl = getRootEl(scope);
209
+ if (!rootEl) return;
210
+ const height = measureLayoutHeight(rootEl);
211
+ context.set("initialHeight", height);
212
+ const item = {
213
+ id: prop("id"),
214
+ height
215
+ };
216
+ setHeight(prop("parent"), item);
217
+ });
218
+ },
219
+ setCloseTimer({ refs }) {
220
+ refs.set("closeTimerStartTime", Date.now());
221
+ },
222
+ resetCloseTimer({ context, refs, prop }) {
223
+ refs.set("closeTimerStartTime", Date.now());
224
+ context.set("remainingTime", getToastDuration(prop("duration"), prop("type")));
225
+ },
226
+ syncRemainingTime({ context, refs }) {
227
+ context.set("remainingTime", (prev) => {
228
+ const closeTimerStartTime = refs.get("closeTimerStartTime");
229
+ const elapsedTime = Date.now() - closeTimerStartTime;
230
+ refs.set("lastCloseStartTimerStartTime", Date.now());
231
+ return prev - elapsedTime;
232
+ });
233
+ },
234
+ notifyParentToRemove({ prop }) {
235
+ prop("parent").send({
236
+ type: "TOAST.REMOVE",
237
+ id: prop("id")
238
+ });
239
+ },
240
+ notifyParentOfDismiss({ prop, scope }) {
241
+ const activeEl = scope.getActiveElement();
242
+ if (!contains(getRootEl(scope), activeEl)) return;
243
+ const focusVisible = !!activeEl?.matches(":focus-visible");
244
+ prop("parent").send({
245
+ type: "TOAST.DISMISS",
246
+ focusVisible
247
+ });
248
+ },
249
+ invokeOnDismiss({ prop, event }) {
250
+ prop("parent").prop("store").dismiss(prop("id"));
251
+ prop("onStatusChange")?.({
252
+ status: "dismissing",
253
+ src: event.src
254
+ });
255
+ },
256
+ invokeOnUnmount({ prop }) {
257
+ prop("onStatusChange")?.({ status: "unmounted" });
258
+ },
259
+ invokeOnVisible({ prop }) {
260
+ prop("onStatusChange")?.({ status: "visible" });
261
+ }
262
+ }
263
+ }
259
264
  });
260
265
  function measureLayoutHeight(el) {
261
- const prevHeight = el.style.height;
262
- el.style.height = "auto";
263
- const height = el.offsetHeight;
264
- el.style.height = prevHeight;
265
- return height;
266
- }
267
- function setHeight(parent, item) {
268
- const { id, height } = item;
269
- parent.context.set("heights", (prev) => {
270
- const alreadyExists = prev.find((i) => i.id === id);
271
- if (!alreadyExists) {
272
- return [{ id, height }, ...prev];
273
- } else {
274
- return prev.map((i) => i.id === id ? { ...i, height } : i);
275
- }
276
- });
266
+ const prevHeight = el.style.height;
267
+ el.style.height = "auto";
268
+ const height = el.offsetHeight;
269
+ el.style.height = prevHeight;
270
+ return height;
277
271
  }
278
- export {
279
- machine
280
- };
272
+ export { machine };
@@ -1,8 +1,2 @@
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 };
1
+ import { ToastStore, ToastStoreProps } from "./toast.types.mjs";
2
+ export declare function createToastStore<V = any>(props?: ToastStoreProps): ToastStore<V>;
@@ -1,8 +1,2 @@
1
- import { ToastStoreProps, ToastStore } from './toast.types.js';
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 };
1
+ import { ToastStore, ToastStoreProps } from "./toast.types.js";
2
+ export declare function createToastStore<V = any>(props?: ToastStoreProps): ToastStore<V>;