@zag-js/toast 1.45.0 → 1.47.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.
@@ -1,17 +1,51 @@
1
1
  // src/toast.dom.ts
2
+ import { AnimationFrame, getWindow, queryAll, resizeObserverBorderBox } from "@zag-js/dom-query";
3
+ import { callAll } from "@zag-js/utils";
2
4
  var getRegionId = (placement) => `toast-group:${placement}`;
3
5
  var getRegionEl = (ctx, placement) => ctx.getById(`toast-group:${placement}`);
6
+ var getToastEls = (ctx, placement) => queryAll(getRegionEl(ctx, placement), "[data-scope=toast][data-part=root]");
4
7
  var getRootId = (ctx) => `toast:${ctx.id}`;
5
8
  var getRootEl = (ctx) => ctx.getById(getRootId(ctx));
6
9
  var getTitleId = (ctx) => `toast:${ctx.id}:title`;
7
10
  var getDescriptionId = (ctx) => `toast:${ctx.id}:description`;
11
+ var getTitleEl = (ctx) => ctx.getById(getTitleId(ctx));
12
+ var getDescriptionEl = (ctx) => ctx.getById(getDescriptionId(ctx));
8
13
  var getCloseTriggerId = (ctx) => `toast${ctx.id}:close`;
14
+ function observeContent(rootEl, options) {
15
+ const { filter = () => true, onChange, onMutate } = options;
16
+ const win = getWindow(rootEl);
17
+ const frame = AnimationFrame.create();
18
+ const notify = () => frame.request(onChange);
19
+ let stopObservingChildren;
20
+ const observeChildren = () => {
21
+ stopObservingChildren?.();
22
+ const children = Array.from(rootEl.children).filter(filter);
23
+ stopObservingChildren = callAll(...children.map((child) => resizeObserverBorderBox.observe(child, notify)));
24
+ };
25
+ observeChildren();
26
+ const mutationObserver = new win.MutationObserver(() => {
27
+ frame.cancel();
28
+ onChange();
29
+ onMutate?.();
30
+ observeChildren();
31
+ });
32
+ mutationObserver.observe(rootEl, { childList: true, subtree: true, characterData: true });
33
+ return () => {
34
+ mutationObserver.disconnect();
35
+ stopObservingChildren?.();
36
+ frame.cancel();
37
+ };
38
+ }
9
39
  export {
10
40
  getCloseTriggerId,
41
+ getDescriptionEl,
11
42
  getDescriptionId,
12
43
  getRegionEl,
13
44
  getRegionId,
14
45
  getRootEl,
15
46
  getRootId,
16
- getTitleId
47
+ getTitleEl,
48
+ getTitleId,
49
+ getToastEls,
50
+ observeContent
17
51
  };
@@ -38,7 +38,7 @@ var import_dom_query = require("@zag-js/dom-query");
38
38
  var import_utils = require("@zag-js/utils");
39
39
  var dom = __toESM(require("./toast.dom.js"));
40
40
  var import_toast = require("./toast.utils.js");
41
- var { not } = (0, import_core.createGuards)();
41
+ var { not, and } = (0, import_core.createGuards)();
42
42
  var machine = (0, import_core.createMachine)({
43
43
  props({ props }) {
44
44
  (0, import_utils.ensureProps)(props, ["id", "type", "parent", "removeDelay"], "toast");
@@ -49,7 +49,8 @@ var machine = (0, import_core.createMachine)({
49
49
  };
50
50
  },
51
51
  initialState({ prop }) {
52
- const persist = prop("type") === "loading" || prop("duration") === Infinity;
52
+ if (prop("message") === "DISMISS") return "dismissing";
53
+ const persist = prop("type") === "loading" || prop("duration") === Infinity || prop("paused");
53
54
  return persist ? "visible:persist" : "visible";
54
55
  },
55
56
  context({ prop, bindable }) {
@@ -65,6 +66,9 @@ var machine = (0, import_core.createMachine)({
65
66
  })),
66
67
  initialHeight: bindable(() => ({
67
68
  defaultValue: 0
69
+ })),
70
+ rendered: bindable(() => ({
71
+ defaultValue: { title: true, description: true }
68
72
  }))
69
73
  };
70
74
  },
@@ -105,6 +109,9 @@ var machine = (0, import_core.createMachine)({
105
109
  const message = prop("message");
106
110
  if (message) send({ type: message, src: "programmatic" });
107
111
  });
112
+ track([() => prop("paused")], () => {
113
+ send({ type: prop("paused") ? "PAUSE" : "RESUME" });
114
+ });
108
115
  track([() => prop("type"), () => prop("duration")], () => {
109
116
  send({ type: "UPDATE" });
110
117
  });
@@ -112,11 +119,12 @@ var machine = (0, import_core.createMachine)({
112
119
  on: {
113
120
  UPDATE: [
114
121
  {
115
- guard: "shouldPersist",
122
+ guard: and("isVisible", "shouldPause"),
116
123
  target: "visible:persist",
117
124
  actions: ["resetCloseTimer"]
118
125
  },
119
126
  {
127
+ guard: "isVisible",
120
128
  target: "visible:updating",
121
129
  actions: ["resetCloseTimer"]
122
130
  }
@@ -126,12 +134,18 @@ var machine = (0, import_core.createMachine)({
126
134
  }
127
135
  },
128
136
  entry: ["setMounted", "measureHeight", "invokeOnVisible"],
129
- effects: ["trackHeight"],
137
+ effects: ["trackContent"],
130
138
  states: {
131
139
  "visible:updating": {
132
140
  tags: ["visible", "updating"],
133
141
  effects: ["waitForNextTick"],
134
142
  on: {
143
+ DISMISS: {
144
+ target: "dismissing"
145
+ },
146
+ PAUSE: {
147
+ target: "visible:persist"
148
+ },
135
149
  SHOW: {
136
150
  target: "visible"
137
151
  }
@@ -141,7 +155,7 @@ var machine = (0, import_core.createMachine)({
141
155
  tags: ["visible", "paused"],
142
156
  on: {
143
157
  RESUME: {
144
- guard: not("isLoadingType"),
158
+ guard: not("shouldPause"),
145
159
  target: "visible",
146
160
  actions: ["setCloseTimer"]
147
161
  },
@@ -164,7 +178,7 @@ var machine = (0, import_core.createMachine)({
164
178
  }
165
179
  },
166
180
  dismissing: {
167
- entry: ["invokeOnDismiss"],
181
+ entry: ["notifyParentOfDismiss", "invokeOnDismiss"],
168
182
  effects: ["waitForRemoveDelay"],
169
183
  on: {
170
184
  REMOVE: {
@@ -195,31 +209,33 @@ var machine = (0, import_core.createMachine)({
195
209
  send({ type: "SHOW", src: "timer" });
196
210
  }, 0);
197
211
  },
198
- trackHeight({ scope, prop }) {
199
- let cleanup;
200
- (0, import_dom_query.raf)(() => {
212
+ trackContent({ scope, prop, context, state }) {
213
+ return (0, import_dom_query.raf)(() => {
201
214
  const rootEl = dom.getRootEl(scope);
202
215
  if (!rootEl) return;
203
216
  const syncHeight = () => {
217
+ if (!state.hasTag("visible") || !rootEl.isConnected) return;
204
218
  const height = measureLayoutHeight(rootEl);
205
- const item = { id: prop("id"), height };
206
- setHeight(prop("parent"), item);
219
+ (0, import_toast.setHeight)(prop("parent"), { id: prop("id"), height });
220
+ };
221
+ const syncRenderedElements = () => {
222
+ context.set("rendered", {
223
+ title: !!dom.getTitleEl(scope),
224
+ description: !!dom.getDescriptionEl(scope)
225
+ });
207
226
  };
208
- const win = scope.getWin();
209
- const observer = new win.MutationObserver(syncHeight);
210
- observer.observe(rootEl, {
211
- childList: true,
212
- subtree: true,
213
- characterData: true
227
+ syncRenderedElements();
228
+ return dom.observeContent(rootEl, {
229
+ filter: (el) => !el.hasAttribute("data-ghost"),
230
+ onChange: syncHeight,
231
+ onMutate: syncRenderedElements
214
232
  });
215
- cleanup = () => observer.disconnect();
216
233
  });
217
- return () => cleanup?.();
218
234
  }
219
235
  },
220
236
  guards: {
221
- isLoadingType: ({ prop }) => prop("type") === "loading",
222
- shouldPersist: ({ computed }) => computed("shouldPersist")
237
+ isVisible: ({ state }) => state.hasTag("visible"),
238
+ shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
223
239
  },
224
240
  actions: {
225
241
  setMounted({ context }) {
@@ -234,7 +250,7 @@ var machine = (0, import_core.createMachine)({
234
250
  const height = measureLayoutHeight(rootEl);
235
251
  context.set("initialHeight", height);
236
252
  const item = { id: prop("id"), height };
237
- setHeight(prop("parent"), item);
253
+ (0, import_toast.setHeight)(prop("parent"), item);
238
254
  });
239
255
  },
240
256
  setCloseTimer({ refs }) {
@@ -256,7 +272,14 @@ var machine = (0, import_core.createMachine)({
256
272
  const parent = prop("parent");
257
273
  parent.send({ type: "TOAST.REMOVE", id: prop("id") });
258
274
  },
275
+ notifyParentOfDismiss({ prop, scope }) {
276
+ const activeEl = scope.getActiveElement();
277
+ if (!(0, import_dom_query.contains)(dom.getRootEl(scope), activeEl)) return;
278
+ const focusVisible = !!activeEl?.matches(":focus-visible");
279
+ prop("parent").send({ type: "TOAST.DISMISS", focusVisible });
280
+ },
259
281
  invokeOnDismiss({ prop, event }) {
282
+ prop("parent").prop("store").dismiss(prop("id"));
260
283
  prop("onStatusChange")?.({ status: "dismissing", src: event.src });
261
284
  },
262
285
  invokeOnUnmount({ prop }) {
@@ -275,17 +298,6 @@ function measureLayoutHeight(el) {
275
298
  el.style.height = prevHeight;
276
299
  return height;
277
300
  }
278
- function setHeight(parent, item) {
279
- const { id, height } = item;
280
- parent.context.set("heights", (prev) => {
281
- const alreadyExists = prev.find((i) => i.id === id);
282
- if (!alreadyExists) {
283
- return [{ id, height }, ...prev];
284
- } else {
285
- return prev.map((i) => i.id === id ? { ...i, height } : i);
286
- }
287
- });
288
- }
289
301
  // Annotate the CommonJS export names for ESM import in node:
290
302
  0 && (module.exports = {
291
303
  machine
@@ -1,10 +1,10 @@
1
1
  // src/toast.machine.ts
2
2
  import { createGuards, createMachine } from "@zag-js/core";
3
- import { raf } from "@zag-js/dom-query";
3
+ import { contains, raf } from "@zag-js/dom-query";
4
4
  import { ensureProps, setRafTimeout } from "@zag-js/utils";
5
5
  import * as dom from "./toast.dom.mjs";
6
- import { getToastDuration } from "./toast.utils.mjs";
7
- var { not } = createGuards();
6
+ import { getToastDuration, setHeight } from "./toast.utils.mjs";
7
+ var { not, and } = createGuards();
8
8
  var machine = createMachine({
9
9
  props({ props }) {
10
10
  ensureProps(props, ["id", "type", "parent", "removeDelay"], "toast");
@@ -15,7 +15,8 @@ var machine = createMachine({
15
15
  };
16
16
  },
17
17
  initialState({ prop }) {
18
- const persist = prop("type") === "loading" || prop("duration") === Infinity;
18
+ if (prop("message") === "DISMISS") return "dismissing";
19
+ const persist = prop("type") === "loading" || prop("duration") === Infinity || prop("paused");
19
20
  return persist ? "visible:persist" : "visible";
20
21
  },
21
22
  context({ prop, bindable }) {
@@ -31,6 +32,9 @@ var machine = createMachine({
31
32
  })),
32
33
  initialHeight: bindable(() => ({
33
34
  defaultValue: 0
35
+ })),
36
+ rendered: bindable(() => ({
37
+ defaultValue: { title: true, description: true }
34
38
  }))
35
39
  };
36
40
  },
@@ -71,6 +75,9 @@ var machine = createMachine({
71
75
  const message = prop("message");
72
76
  if (message) send({ type: message, src: "programmatic" });
73
77
  });
78
+ track([() => prop("paused")], () => {
79
+ send({ type: prop("paused") ? "PAUSE" : "RESUME" });
80
+ });
74
81
  track([() => prop("type"), () => prop("duration")], () => {
75
82
  send({ type: "UPDATE" });
76
83
  });
@@ -78,11 +85,12 @@ var machine = createMachine({
78
85
  on: {
79
86
  UPDATE: [
80
87
  {
81
- guard: "shouldPersist",
88
+ guard: and("isVisible", "shouldPause"),
82
89
  target: "visible:persist",
83
90
  actions: ["resetCloseTimer"]
84
91
  },
85
92
  {
93
+ guard: "isVisible",
86
94
  target: "visible:updating",
87
95
  actions: ["resetCloseTimer"]
88
96
  }
@@ -92,12 +100,18 @@ var machine = createMachine({
92
100
  }
93
101
  },
94
102
  entry: ["setMounted", "measureHeight", "invokeOnVisible"],
95
- effects: ["trackHeight"],
103
+ effects: ["trackContent"],
96
104
  states: {
97
105
  "visible:updating": {
98
106
  tags: ["visible", "updating"],
99
107
  effects: ["waitForNextTick"],
100
108
  on: {
109
+ DISMISS: {
110
+ target: "dismissing"
111
+ },
112
+ PAUSE: {
113
+ target: "visible:persist"
114
+ },
101
115
  SHOW: {
102
116
  target: "visible"
103
117
  }
@@ -107,7 +121,7 @@ var machine = createMachine({
107
121
  tags: ["visible", "paused"],
108
122
  on: {
109
123
  RESUME: {
110
- guard: not("isLoadingType"),
124
+ guard: not("shouldPause"),
111
125
  target: "visible",
112
126
  actions: ["setCloseTimer"]
113
127
  },
@@ -130,7 +144,7 @@ var machine = createMachine({
130
144
  }
131
145
  },
132
146
  dismissing: {
133
- entry: ["invokeOnDismiss"],
147
+ entry: ["notifyParentOfDismiss", "invokeOnDismiss"],
134
148
  effects: ["waitForRemoveDelay"],
135
149
  on: {
136
150
  REMOVE: {
@@ -161,31 +175,33 @@ var machine = createMachine({
161
175
  send({ type: "SHOW", src: "timer" });
162
176
  }, 0);
163
177
  },
164
- trackHeight({ scope, prop }) {
165
- let cleanup;
166
- raf(() => {
178
+ trackContent({ scope, prop, context, state }) {
179
+ return raf(() => {
167
180
  const rootEl = dom.getRootEl(scope);
168
181
  if (!rootEl) return;
169
182
  const syncHeight = () => {
183
+ if (!state.hasTag("visible") || !rootEl.isConnected) return;
170
184
  const height = measureLayoutHeight(rootEl);
171
- const item = { id: prop("id"), height };
172
- setHeight(prop("parent"), item);
185
+ setHeight(prop("parent"), { id: prop("id"), height });
186
+ };
187
+ const syncRenderedElements = () => {
188
+ context.set("rendered", {
189
+ title: !!dom.getTitleEl(scope),
190
+ description: !!dom.getDescriptionEl(scope)
191
+ });
173
192
  };
174
- const win = scope.getWin();
175
- const observer = new win.MutationObserver(syncHeight);
176
- observer.observe(rootEl, {
177
- childList: true,
178
- subtree: true,
179
- characterData: true
193
+ syncRenderedElements();
194
+ return dom.observeContent(rootEl, {
195
+ filter: (el) => !el.hasAttribute("data-ghost"),
196
+ onChange: syncHeight,
197
+ onMutate: syncRenderedElements
180
198
  });
181
- cleanup = () => observer.disconnect();
182
199
  });
183
- return () => cleanup?.();
184
200
  }
185
201
  },
186
202
  guards: {
187
- isLoadingType: ({ prop }) => prop("type") === "loading",
188
- shouldPersist: ({ computed }) => computed("shouldPersist")
203
+ isVisible: ({ state }) => state.hasTag("visible"),
204
+ shouldPause: ({ computed, prop }) => computed("shouldPersist") || !!prop("paused")
189
205
  },
190
206
  actions: {
191
207
  setMounted({ context }) {
@@ -222,7 +238,14 @@ var machine = createMachine({
222
238
  const parent = prop("parent");
223
239
  parent.send({ type: "TOAST.REMOVE", id: prop("id") });
224
240
  },
241
+ notifyParentOfDismiss({ prop, scope }) {
242
+ const activeEl = scope.getActiveElement();
243
+ if (!contains(dom.getRootEl(scope), activeEl)) return;
244
+ const focusVisible = !!activeEl?.matches(":focus-visible");
245
+ prop("parent").send({ type: "TOAST.DISMISS", focusVisible });
246
+ },
225
247
  invokeOnDismiss({ prop, event }) {
248
+ prop("parent").prop("store").dismiss(prop("id"));
226
249
  prop("onStatusChange")?.({ status: "dismissing", src: event.src });
227
250
  },
228
251
  invokeOnUnmount({ prop }) {
@@ -241,17 +264,6 @@ function measureLayoutHeight(el) {
241
264
  el.style.height = prevHeight;
242
265
  return height;
243
266
  }
244
- function setHeight(parent, item) {
245
- const { id, height } = item;
246
- parent.context.set("heights", (prev) => {
247
- const alreadyExists = prev.find((i) => i.id === id);
248
- if (!alreadyExists) {
249
- return [{ id, height }, ...prev];
250
- } else {
251
- return prev.map((i) => i.id === id ? { ...i, height } : i);
252
- }
253
- });
254
- }
255
267
  export {
256
268
  machine
257
269
  };