@zag-js/toast 0.2.12 → 0.2.13

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.js CHANGED
@@ -27,104 +27,12 @@ __export(src_exports, {
27
27
  group: () => group
28
28
  });
29
29
  module.exports = __toCommonJS(src_exports);
30
-
31
- // ../../utilities/dom/src/attrs.ts
32
- var dataAttr = (guard) => {
33
- return guard ? "" : void 0;
34
- };
35
-
36
- // ../../utilities/dom/src/constants.ts
37
- var MAX_Z_INDEX = 2147483647;
38
-
39
- // ../../utilities/core/src/functions.ts
40
- var runIfFn = (v, ...a) => {
41
- const res = typeof v === "function" ? v(...a) : v;
42
- return res ?? void 0;
43
- };
44
- var cast = (v) => v;
45
- var uuid = /* @__PURE__ */ (() => {
46
- let id = 0;
47
- return () => {
48
- id++;
49
- return id.toString(36);
50
- };
51
- })();
52
-
53
- // ../../utilities/core/src/guard.ts
54
- var isArray = (v) => Array.isArray(v);
55
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
56
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
57
-
58
- // ../../utilities/core/src/object.ts
59
- function compact(obj) {
60
- if (obj === void 0)
61
- return obj;
62
- return Object.fromEntries(
63
- Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
64
- );
65
- }
66
-
67
- // ../../utilities/core/src/warning.ts
68
- function warn(...a) {
69
- const m = a.length === 1 ? a[0] : a[1];
70
- const c = a.length === 2 ? a[0] : true;
71
- if (c && process.env.NODE_ENV !== "production") {
72
- console.warn(m);
73
- }
74
- }
75
-
76
- // ../../utilities/dom/src/platform.ts
77
- var isDom = () => typeof window !== "undefined";
78
-
79
- // ../../utilities/dom/src/query.ts
80
- function isDocument(el) {
81
- return el.nodeType === Node.DOCUMENT_NODE;
82
- }
83
- function isWindow(value) {
84
- return value?.toString() === "[object Window]";
85
- }
86
- function getDocument(el) {
87
- if (isWindow(el))
88
- return el.document;
89
- if (isDocument(el))
90
- return el;
91
- return el?.ownerDocument ?? document;
92
- }
93
- function defineDomHelpers(helpers) {
94
- const dom2 = {
95
- getRootNode: (ctx) => ctx.getRootNode?.() ?? document,
96
- getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
97
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
98
- getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
99
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
100
- };
101
- return {
102
- ...dom2,
103
- ...helpers
104
- };
105
- }
106
-
107
- // ../../utilities/dom/src/listener.ts
108
- var isRef = (v) => hasProp(v, "current");
109
- function addDomEvent(target, eventName, handler, options) {
110
- const node = isRef(target) ? target.current : runIfFn(target);
111
- node?.addEventListener(eventName, handler, options);
112
- return () => {
113
- node?.removeEventListener(eventName, handler, options);
114
- };
115
- }
116
-
117
- // ../../utilities/dom/src/visibility-event.ts
118
- function trackDocumentVisibility(_doc, callback) {
119
- const doc = cast(_doc);
120
- return addDomEvent(doc, "visibilitychange", () => {
121
- const hidden = doc.hidden || doc.msHidden || doc.webkitHidden;
122
- callback(!!hidden);
123
- });
124
- }
30
+ var import_dom_query4 = require("@zag-js/dom-query");
31
+ var import_utils4 = require("@zag-js/utils");
125
32
 
126
33
  // src/toast-group.connect.ts
127
34
  var import_core = require("@zag-js/core");
35
+ var import_utils = require("@zag-js/utils");
128
36
 
129
37
  // src/toast.anatomy.ts
130
38
  var import_anatomy = require("@zag-js/anatomy");
@@ -132,7 +40,8 @@ var anatomy = (0, import_anatomy.createAnatomy)("toast").parts("group", "root",
132
40
  var parts = anatomy.build();
133
41
 
134
42
  // src/toast.dom.ts
135
- var dom = defineDomHelpers({
43
+ var import_dom_query = require("@zag-js/dom-query");
44
+ var dom = (0, import_dom_query.createScope)({
136
45
  getGroupId: (placement) => `toast-group:${placement}`,
137
46
  getRootId: (ctx) => `toast:${ctx.id}`,
138
47
  getTitleId: (ctx) => `toast:${ctx.id}:title`,
@@ -163,11 +72,11 @@ function getToastDuration(duration, type) {
163
72
  }
164
73
  function getGroupPlacementStyle(ctx, placement) {
165
74
  const offset = ctx.offsets;
166
- const __offset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
75
+ const computedOffset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
167
76
  const rtl = ctx.dir === "rtl";
168
- const __placement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
169
- const isRighty = __placement.includes("right");
170
- const isLefty = __placement.includes("left");
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");
171
80
  const styles = {
172
81
  position: "fixed",
173
82
  pointerEvents: ctx.count > 0 ? void 0 : "none",
@@ -182,20 +91,20 @@ function getGroupPlacementStyle(ctx, placement) {
182
91
  if (isLefty)
183
92
  alignItems = "flex-start";
184
93
  styles.alignItems = alignItems;
185
- if (__placement.includes("top")) {
186
- const offset2 = __offset.top;
94
+ if (computedPlacement.includes("top")) {
95
+ const offset2 = computedOffset.top;
187
96
  styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
188
97
  }
189
- if (__placement.includes("bottom")) {
190
- const offset2 = __offset.bottom;
98
+ if (computedPlacement.includes("bottom")) {
99
+ const offset2 = computedOffset.bottom;
191
100
  styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
192
101
  }
193
- if (!__placement.includes("left")) {
194
- const offset2 = __offset.right;
102
+ if (!computedPlacement.includes("left")) {
103
+ const offset2 = computedOffset.right;
195
104
  styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
196
105
  }
197
- if (!__placement.includes("right")) {
198
- const offset2 = __offset.left;
106
+ if (!computedPlacement.includes("right")) {
107
+ const offset2 = computedOffset.left;
199
108
  styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
200
109
  }
201
110
  return styles;
@@ -205,22 +114,40 @@ function getGroupPlacementStyle(ctx, placement) {
205
114
  var toaster = {};
206
115
  function groupConnect(state, send, normalize) {
207
116
  const group2 = {
117
+ /**
118
+ * The total number of toasts
119
+ */
208
120
  count: state.context.count,
121
+ /**
122
+ * The active toasts
123
+ */
209
124
  toasts: state.context.toasts,
125
+ /**
126
+ * The active toasts by placement
127
+ */
210
128
  toastsByPlacement: getToastsByPlacement(state.context.toasts),
129
+ /**
130
+ * Returns whether the toast id is visible
131
+ */
211
132
  isVisible(id) {
212
133
  if (!state.context.toasts.length)
213
134
  return false;
214
135
  return !!state.context.toasts.find((toast) => toast.id == id);
215
136
  },
137
+ /**
138
+ * Function to create a toast.
139
+ */
216
140
  create(options) {
217
- const uid = `toast:${uuid()}`;
141
+ const uid = `toast:${(0, import_utils.uuid)()}`;
218
142
  const id = options.id ? options.id : uid;
219
143
  if (group2.isVisible(id))
220
144
  return;
221
145
  send({ type: "ADD_TOAST", toast: { ...options, id } });
222
146
  return id;
223
147
  },
148
+ /**
149
+ * Function to create or update a toast.
150
+ */
224
151
  upsert(options) {
225
152
  const { id } = options;
226
153
  const isVisible = id ? group2.isVisible(id) : false;
@@ -230,6 +157,10 @@ function groupConnect(state, send, normalize) {
230
157
  return group2.create(options);
231
158
  }
232
159
  },
160
+ /**
161
+ * Function to dismiss a toast by id.
162
+ * If no id is provided, all toasts will be dismissed.
163
+ */
233
164
  dismiss(id) {
234
165
  if (id == null) {
235
166
  send("DISMISS_ALL");
@@ -237,6 +168,10 @@ function groupConnect(state, send, normalize) {
237
168
  send({ type: "DISMISS_TOAST", id });
238
169
  }
239
170
  },
171
+ /**
172
+ * Function to remove a toast by id.
173
+ * If no id is provided, all toasts will be removed.
174
+ */
240
175
  remove(id) {
241
176
  if (id == null) {
242
177
  send("REMOVE_ALL");
@@ -244,41 +179,64 @@ function groupConnect(state, send, normalize) {
244
179
  send({ type: "REMOVE_TOAST", id });
245
180
  }
246
181
  },
182
+ /**
183
+ * Function to dismiss all toasts by placement.
184
+ */
247
185
  dismissByPlacement(placement) {
248
186
  const toasts = group2.toastsByPlacement[placement];
249
187
  if (toasts) {
250
188
  toasts.forEach((toast) => group2.dismiss(toast.id));
251
189
  }
252
190
  },
191
+ /**
192
+ * Function to update a toast's options by id.
193
+ */
253
194
  update(id, options) {
254
195
  if (!group2.isVisible(id))
255
196
  return;
256
197
  send({ type: "UPDATE_TOAST", id, toast: options });
257
198
  return id;
258
199
  },
200
+ /**
201
+ * Function to create a loading toast.
202
+ */
259
203
  loading(options) {
260
204
  options.type = "loading";
261
205
  return group2.upsert(options);
262
206
  },
207
+ /**
208
+ * Function to create a success toast.
209
+ */
263
210
  success(options) {
264
211
  options.type = "success";
265
212
  return group2.upsert(options);
266
213
  },
214
+ /**
215
+ * Function to create an error toast.
216
+ */
267
217
  error(options) {
268
218
  options.type = "error";
269
219
  return group2.upsert(options);
270
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
+ */
271
226
  promise(promise, options, shared = {}) {
272
227
  const id = group2.loading({ ...shared, ...options.loading });
273
228
  promise.then((response) => {
274
- const successOptions = runIfFn(options.success, response);
229
+ const successOptions = (0, import_utils.runIfFn)(options.success, response);
275
230
  group2.success({ ...shared, ...successOptions, id });
276
231
  }).catch((error) => {
277
- const errorOptions = runIfFn(options.error, error);
232
+ const errorOptions = (0, import_utils.runIfFn)(options.error, error);
278
233
  group2.error({ ...shared, ...errorOptions, id });
279
234
  });
280
235
  return promise;
281
236
  },
237
+ /**
238
+ * Function to pause a toast by id.
239
+ */
282
240
  pause(id) {
283
241
  if (id == null) {
284
242
  send("PAUSE_ALL");
@@ -286,6 +244,9 @@ function groupConnect(state, send, normalize) {
286
244
  send({ type: "PAUSE_TOAST", id });
287
245
  }
288
246
  },
247
+ /**
248
+ * Function to resume a toast by id.
249
+ */
289
250
  resume(id) {
290
251
  if (id == null) {
291
252
  send("RESUME_ALL");
@@ -316,13 +277,17 @@ function groupConnect(state, send, normalize) {
316
277
 
317
278
  // src/toast-group.machine.ts
318
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");
319
282
 
320
283
  // src/toast.machine.ts
321
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");
322
287
  var { not, and, or } = import_core2.guards;
323
288
  function createToastMachine(options = {}) {
324
289
  const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
325
- const ctx = compact(restProps);
290
+ const ctx = (0, import_utils2.compact)(restProps);
326
291
  const computedDuration = getToastDuration(duration, type);
327
292
  return (0, import_core2.createMachine)(
328
293
  {
@@ -409,8 +374,9 @@ function createToastMachine(options = {}) {
409
374
  trackDocumentVisibility(ctx2, _evt, { send }) {
410
375
  if (!ctx2.pauseOnPageIdle)
411
376
  return;
412
- return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
413
- send(hidden ? "PAUSE" : "RESUME");
377
+ const doc = dom.getDoc(ctx2);
378
+ return (0, import_dom_event.addDomEvent)(doc, "visibilitychange", () => {
379
+ send(doc.visibilityState === "hidden" ? "PAUSE" : "RESUME");
414
380
  });
415
381
  }
416
382
  },
@@ -458,7 +424,7 @@ function createToastMachine(options = {}) {
458
424
 
459
425
  // src/toast-group.machine.ts
460
426
  function groupMachine(userContext) {
461
- const ctx = compact(userContext);
427
+ const ctx = (0, import_utils3.compact)(userContext);
462
428
  return (0, import_core3.createMachine)({
463
429
  id: "toaster",
464
430
  initial: "active",
@@ -467,7 +433,7 @@ function groupMachine(userContext) {
467
433
  max: Number.MAX_SAFE_INTEGER,
468
434
  toasts: [],
469
435
  gutter: "1rem",
470
- zIndex: MAX_Z_INDEX,
436
+ zIndex: import_dom_query2.MAX_Z_INDEX,
471
437
  pauseOnPageIdle: false,
472
438
  pauseOnInteraction: true,
473
439
  offsets: { left: "0px", right: "0px", top: "0px", bottom: "0px" },
@@ -547,33 +513,80 @@ function groupMachine(userContext) {
547
513
  }
548
514
 
549
515
  // src/toast.connect.ts
516
+ var import_dom_query3 = require("@zag-js/dom-query");
550
517
  function connect(state, send, normalize) {
551
518
  const isVisible = state.hasTag("visible");
552
519
  const isPaused = state.hasTag("paused");
553
520
  const pauseOnInteraction = state.context.pauseOnInteraction;
554
521
  const placement = state.context.placement;
555
522
  return {
523
+ /**
524
+ * The type of the toast.
525
+ */
556
526
  type: state.context.type,
527
+ /**
528
+ * The title of the toast.
529
+ */
557
530
  title: state.context.title,
531
+ /**
532
+ * The description of the toast.
533
+ */
558
534
  description: state.context.description,
535
+ /**
536
+ * The current placement of the toast.
537
+ */
559
538
  placement,
539
+ /**
540
+ * Whether the toast is visible.
541
+ */
560
542
  isVisible,
543
+ /**
544
+ * Whether the toast is paused.
545
+ */
561
546
  isPaused,
547
+ /**
548
+ * Whether the toast is in RTL mode.
549
+ */
562
550
  isRtl: state.context.dir === "rtl",
551
+ /**
552
+ * Function to pause the toast (keeping it visible).
553
+ */
563
554
  pause() {
564
555
  send("PAUSE");
565
556
  },
557
+ /**
558
+ * Function to resume the toast dismissing.
559
+ */
566
560
  resume() {
567
561
  send("RESUME");
568
562
  },
563
+ /**
564
+ * Function to instantly dismiss the toast.
565
+ */
569
566
  dismiss() {
570
567
  send("DISMISS");
571
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
+ },
572
585
  rootProps: normalize.element({
573
586
  ...parts.root.attrs,
574
587
  dir: state.context.dir,
575
588
  id: dom.getRootId(state.context),
576
- "data-open": dataAttr(isVisible),
589
+ "data-open": (0, import_dom_query3.dataAttr)(isVisible),
577
590
  "data-type": state.context.type,
578
591
  "data-placement": placement,
579
592
  role: "status",
@@ -629,20 +642,7 @@ function connect(state, send, normalize) {
629
642
  onClick() {
630
643
  send("DISMISS");
631
644
  }
632
- }),
633
- render() {
634
- return state.context.render?.({
635
- id: state.context.id,
636
- type: state.context.type,
637
- duration: state.context.duration,
638
- title: state.context.title,
639
- placement: state.context.placement,
640
- description: state.context.description,
641
- dismiss() {
642
- send("DISMISS");
643
- }
644
- });
645
- }
645
+ })
646
646
  };
647
647
  }
648
648
 
@@ -652,8 +652,8 @@ var group = {
652
652
  machine: groupMachine
653
653
  };
654
654
  function api() {
655
- if (!isDom()) {
656
- warn("toast.api() is only available in the browser");
655
+ if (!(0, import_dom_query4.isDom)()) {
656
+ (0, import_utils4.warn)("toast.api() is only available in the browser");
657
657
  } else {
658
658
  return toaster;
659
659
  }
package/dist/index.mjs CHANGED
@@ -1,36 +1,25 @@
1
1
  import {
2
2
  groupConnect,
3
3
  toaster
4
- } from "./chunk-HRZITBY3.mjs";
4
+ } from "./chunk-6TPPYHFP.mjs";
5
5
  import {
6
6
  groupMachine
7
- } from "./chunk-GKMHTDAP.mjs";
7
+ } from "./chunk-TCF4OLIQ.mjs";
8
8
  import {
9
9
  connect
10
- } from "./chunk-HDTOIF42.mjs";
10
+ } from "./chunk-MUJMFD4Y.mjs";
11
11
  import {
12
12
  anatomy
13
13
  } from "./chunk-GQHI2OMI.mjs";
14
14
  import {
15
15
  createToastMachine
16
- } from "./chunk-SSV22S55.mjs";
17
- import "./chunk-LHAZKBRJ.mjs";
18
- import "./chunk-XQSCE3HI.mjs";
19
- import "./chunk-AZ32UDL3.mjs";
20
-
21
- // ../../utilities/core/src/warning.ts
22
- function warn(...a) {
23
- const m = a.length === 1 ? a[0] : a[1];
24
- const c = a.length === 2 ? a[0] : true;
25
- if (c && process.env.NODE_ENV !== "production") {
26
- console.warn(m);
27
- }
28
- }
29
-
30
- // ../../utilities/dom/src/platform.ts
31
- var isDom = () => typeof window !== "undefined";
16
+ } from "./chunk-EQTV3BNM.mjs";
17
+ import "./chunk-DBPKSADW.mjs";
18
+ import "./chunk-DUZD3NLM.mjs";
32
19
 
33
20
  // src/index.ts
21
+ import { isDom } from "@zag-js/dom-query";
22
+ import { warn } from "@zag-js/utils";
34
23
  var group = {
35
24
  connect: groupConnect,
36
25
  machine: groupMachine
@@ -4,21 +4,73 @@ import '@zag-js/core';
4
4
 
5
5
  declare let toaster: Toaster;
6
6
  declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
7
+ /**
8
+ * The total number of toasts
9
+ */
7
10
  count: number;
11
+ /**
12
+ * The active toasts
13
+ */
8
14
  toasts: Service[];
15
+ /**
16
+ * The active toasts by placement
17
+ */
9
18
  toastsByPlacement: Partial<Record<Placement, Service[]>>;
19
+ /**
20
+ * Returns whether the toast id is visible
21
+ */
10
22
  isVisible(id: string): boolean;
23
+ /**
24
+ * Function to create a toast.
25
+ */
11
26
  create(options: Options): string | undefined;
27
+ /**
28
+ * Function to create or update a toast.
29
+ */
12
30
  upsert(options: Options): string | undefined;
31
+ /**
32
+ * Function to dismiss a toast by id.
33
+ * If no id is provided, all toasts will be dismissed.
34
+ */
13
35
  dismiss(id?: string): void;
36
+ /**
37
+ * Function to remove a toast by id.
38
+ * If no id is provided, all toasts will be removed.
39
+ */
14
40
  remove(id?: string): void;
41
+ /**
42
+ * Function to dismiss all toasts by placement.
43
+ */
15
44
  dismissByPlacement(placement: Placement): void;
45
+ /**
46
+ * Function to update a toast's options by id.
47
+ */
16
48
  update(id: string, options: Options): string | undefined;
49
+ /**
50
+ * Function to create a loading toast.
51
+ */
17
52
  loading(options: Options): string | undefined;
53
+ /**
54
+ * Function to create a success toast.
55
+ */
18
56
  success(options: Options): string | undefined;
57
+ /**
58
+ * Function to create an error toast.
59
+ */
19
60
  error(options: Options): string | undefined;
61
+ /**
62
+ * Function to create a toast from a promise.
63
+ * - When the promise resolves, the toast will be updated with the success options.
64
+ * - When the promise rejects, the toast will be updated with the error options.
65
+ */
20
66
  promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
67
+ /**
68
+ * Function to pause a toast by id.
69
+ */
21
70
  pause(id?: string): void;
71
+ /**
72
+ * Function to resume a toast by id.
73
+ */
22
74
  resume(id?: string): void;
23
75
  getGroupProps(options: GroupProps): T["element"];
24
76
  subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;