@zag-js/toast 0.2.12 → 0.2.14

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,31 +1,43 @@
1
1
  import {
2
2
  parts
3
3
  } from "./chunk-GQHI2OMI.mjs";
4
- import {
5
- runIfFn,
6
- uuid
7
- } from "./chunk-LHAZKBRJ.mjs";
8
4
  import {
9
5
  getGroupPlacementStyle,
10
6
  getToastsByPlacement
11
- } from "./chunk-XQSCE3HI.mjs";
7
+ } from "./chunk-DBPKSADW.mjs";
12
8
  import {
13
9
  dom
14
- } from "./chunk-AZ32UDL3.mjs";
10
+ } from "./chunk-DUZD3NLM.mjs";
15
11
 
16
12
  // src/toast-group.connect.ts
17
13
  import { subscribe } from "@zag-js/core";
14
+ import { runIfFn, uuid } from "@zag-js/utils";
18
15
  var toaster = {};
19
16
  function groupConnect(state, send, normalize) {
20
17
  const group = {
18
+ /**
19
+ * The total number of toasts
20
+ */
21
21
  count: state.context.count,
22
+ /**
23
+ * The active toasts
24
+ */
22
25
  toasts: state.context.toasts,
26
+ /**
27
+ * The active toasts by placement
28
+ */
23
29
  toastsByPlacement: getToastsByPlacement(state.context.toasts),
30
+ /**
31
+ * Returns whether the toast id is visible
32
+ */
24
33
  isVisible(id) {
25
34
  if (!state.context.toasts.length)
26
35
  return false;
27
36
  return !!state.context.toasts.find((toast) => toast.id == id);
28
37
  },
38
+ /**
39
+ * Function to create a toast.
40
+ */
29
41
  create(options) {
30
42
  const uid = `toast:${uuid()}`;
31
43
  const id = options.id ? options.id : uid;
@@ -34,6 +46,9 @@ function groupConnect(state, send, normalize) {
34
46
  send({ type: "ADD_TOAST", toast: { ...options, id } });
35
47
  return id;
36
48
  },
49
+ /**
50
+ * Function to create or update a toast.
51
+ */
37
52
  upsert(options) {
38
53
  const { id } = options;
39
54
  const isVisible = id ? group.isVisible(id) : false;
@@ -43,6 +58,10 @@ function groupConnect(state, send, normalize) {
43
58
  return group.create(options);
44
59
  }
45
60
  },
61
+ /**
62
+ * Function to dismiss a toast by id.
63
+ * If no id is provided, all toasts will be dismissed.
64
+ */
46
65
  dismiss(id) {
47
66
  if (id == null) {
48
67
  send("DISMISS_ALL");
@@ -50,6 +69,10 @@ function groupConnect(state, send, normalize) {
50
69
  send({ type: "DISMISS_TOAST", id });
51
70
  }
52
71
  },
72
+ /**
73
+ * Function to remove a toast by id.
74
+ * If no id is provided, all toasts will be removed.
75
+ */
53
76
  remove(id) {
54
77
  if (id == null) {
55
78
  send("REMOVE_ALL");
@@ -57,30 +80,50 @@ function groupConnect(state, send, normalize) {
57
80
  send({ type: "REMOVE_TOAST", id });
58
81
  }
59
82
  },
83
+ /**
84
+ * Function to dismiss all toasts by placement.
85
+ */
60
86
  dismissByPlacement(placement) {
61
87
  const toasts = group.toastsByPlacement[placement];
62
88
  if (toasts) {
63
89
  toasts.forEach((toast) => group.dismiss(toast.id));
64
90
  }
65
91
  },
92
+ /**
93
+ * Function to update a toast's options by id.
94
+ */
66
95
  update(id, options) {
67
96
  if (!group.isVisible(id))
68
97
  return;
69
98
  send({ type: "UPDATE_TOAST", id, toast: options });
70
99
  return id;
71
100
  },
101
+ /**
102
+ * Function to create a loading toast.
103
+ */
72
104
  loading(options) {
73
105
  options.type = "loading";
74
106
  return group.upsert(options);
75
107
  },
108
+ /**
109
+ * Function to create a success toast.
110
+ */
76
111
  success(options) {
77
112
  options.type = "success";
78
113
  return group.upsert(options);
79
114
  },
115
+ /**
116
+ * Function to create an error toast.
117
+ */
80
118
  error(options) {
81
119
  options.type = "error";
82
120
  return group.upsert(options);
83
121
  },
122
+ /**
123
+ * Function to create a toast from a promise.
124
+ * - When the promise resolves, the toast will be updated with the success options.
125
+ * - When the promise rejects, the toast will be updated with the error options.
126
+ */
84
127
  promise(promise, options, shared = {}) {
85
128
  const id = group.loading({ ...shared, ...options.loading });
86
129
  promise.then((response) => {
@@ -92,6 +135,9 @@ function groupConnect(state, send, normalize) {
92
135
  });
93
136
  return promise;
94
137
  },
138
+ /**
139
+ * Function to pause a toast by id.
140
+ */
95
141
  pause(id) {
96
142
  if (id == null) {
97
143
  send("PAUSE_ALL");
@@ -99,6 +145,9 @@ function groupConnect(state, send, normalize) {
99
145
  send({ type: "PAUSE_TOAST", id });
100
146
  }
101
147
  },
148
+ /**
149
+ * Function to resume a toast by id.
150
+ */
102
151
  resume(id) {
103
152
  if (id == null) {
104
153
  send("RESUME_ALL");
@@ -20,11 +20,11 @@ function getToastDuration(duration, type) {
20
20
  }
21
21
  function getGroupPlacementStyle(ctx, placement) {
22
22
  const offset = ctx.offsets;
23
- const __offset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
23
+ const computedOffset = typeof offset === "string" ? { left: offset, right: offset, bottom: offset, top: offset } : offset;
24
24
  const rtl = ctx.dir === "rtl";
25
- const __placement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
26
- const isRighty = __placement.includes("right");
27
- const isLefty = __placement.includes("left");
25
+ const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
26
+ const isRighty = computedPlacement.includes("right");
27
+ const isLefty = computedPlacement.includes("left");
28
28
  const styles = {
29
29
  position: "fixed",
30
30
  pointerEvents: ctx.count > 0 ? void 0 : "none",
@@ -39,20 +39,20 @@ function getGroupPlacementStyle(ctx, placement) {
39
39
  if (isLefty)
40
40
  alignItems = "flex-start";
41
41
  styles.alignItems = alignItems;
42
- if (__placement.includes("top")) {
43
- const offset2 = __offset.top;
42
+ if (computedPlacement.includes("top")) {
43
+ const offset2 = computedOffset.top;
44
44
  styles.top = `calc(env(safe-area-inset-top, 0px) + ${offset2})`;
45
45
  }
46
- if (__placement.includes("bottom")) {
47
- const offset2 = __offset.bottom;
46
+ if (computedPlacement.includes("bottom")) {
47
+ const offset2 = computedOffset.bottom;
48
48
  styles.bottom = `calc(env(safe-area-inset-bottom, 0px) + ${offset2})`;
49
49
  }
50
- if (!__placement.includes("left")) {
51
- const offset2 = __offset.right;
50
+ if (!computedPlacement.includes("left")) {
51
+ const offset2 = computedOffset.right;
52
52
  styles.right = `calc(env(safe-area-inset-right, 0px) + ${offset2})`;
53
53
  }
54
- if (!__placement.includes("right")) {
55
- const offset2 = __offset.left;
54
+ if (!computedPlacement.includes("right")) {
55
+ const offset2 = computedOffset.left;
56
56
  styles.left = `calc(env(safe-area-inset-left, 0px) + ${offset2})`;
57
57
  }
58
58
  return styles;
@@ -0,0 +1,14 @@
1
+ // src/toast.dom.ts
2
+ import { createScope } from "@zag-js/dom-query";
3
+ var dom = createScope({
4
+ getGroupId: (placement) => `toast-group:${placement}`,
5
+ getRootId: (ctx) => `toast:${ctx.id}`,
6
+ getTitleId: (ctx) => `toast:${ctx.id}:title`,
7
+ getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
8
+ getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
9
+ getPortalId: (ctx) => `toast-portal:${ctx.id}`
10
+ });
11
+
12
+ export {
13
+ dom
14
+ };
@@ -1,51 +1,14 @@
1
- import {
2
- cast,
3
- runIfFn
4
- } from "./chunk-LHAZKBRJ.mjs";
5
1
  import {
6
2
  getToastDuration
7
- } from "./chunk-XQSCE3HI.mjs";
3
+ } from "./chunk-DBPKSADW.mjs";
8
4
  import {
9
5
  dom
10
- } from "./chunk-AZ32UDL3.mjs";
6
+ } from "./chunk-DUZD3NLM.mjs";
11
7
 
12
8
  // src/toast.machine.ts
13
9
  import { createMachine, guards } from "@zag-js/core";
14
-
15
- // ../../utilities/core/src/guard.ts
16
- var isArray = (v) => Array.isArray(v);
17
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
18
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
19
-
20
- // ../../utilities/core/src/object.ts
21
- function compact(obj) {
22
- if (obj === void 0)
23
- return obj;
24
- return Object.fromEntries(
25
- Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
26
- );
27
- }
28
-
29
- // ../../utilities/dom/src/listener.ts
30
- var isRef = (v) => hasProp(v, "current");
31
- function addDomEvent(target, eventName, handler, options) {
32
- const node = isRef(target) ? target.current : runIfFn(target);
33
- node?.addEventListener(eventName, handler, options);
34
- return () => {
35
- node?.removeEventListener(eventName, handler, options);
36
- };
37
- }
38
-
39
- // ../../utilities/dom/src/visibility-event.ts
40
- function trackDocumentVisibility(_doc, callback) {
41
- const doc = cast(_doc);
42
- return addDomEvent(doc, "visibilitychange", () => {
43
- const hidden = doc.hidden || doc.msHidden || doc.webkitHidden;
44
- callback(!!hidden);
45
- });
46
- }
47
-
48
- // src/toast.machine.ts
10
+ import { addDomEvent } from "@zag-js/dom-event";
11
+ import { compact } from "@zag-js/utils";
49
12
  var { not, and, or } = guards;
50
13
  function createToastMachine(options = {}) {
51
14
  const { type = "info", duration, id = "toast", placement = "bottom", removeDelay = 0, ...restProps } = options;
@@ -136,8 +99,9 @@ function createToastMachine(options = {}) {
136
99
  trackDocumentVisibility(ctx2, _evt, { send }) {
137
100
  if (!ctx2.pauseOnPageIdle)
138
101
  return;
139
- return trackDocumentVisibility(dom.getDoc(ctx2), function(hidden) {
140
- send(hidden ? "PAUSE" : "RESUME");
102
+ const doc = dom.getDoc(ctx2);
103
+ return addDomEvent(doc, "visibilitychange", () => {
104
+ send(doc.visibilityState === "hidden" ? "PAUSE" : "RESUME");
141
105
  });
142
106
  }
143
107
  },
@@ -184,6 +148,5 @@ function createToastMachine(options = {}) {
184
148
  }
185
149
 
186
150
  export {
187
- compact,
188
151
  createToastMachine
189
152
  };
@@ -3,36 +3,78 @@ import {
3
3
  } from "./chunk-GQHI2OMI.mjs";
4
4
  import {
5
5
  dom
6
- } from "./chunk-AZ32UDL3.mjs";
7
-
8
- // ../../utilities/dom/src/attrs.ts
9
- var dataAttr = (guard) => {
10
- return guard ? "" : void 0;
11
- };
6
+ } from "./chunk-DUZD3NLM.mjs";
12
7
 
13
8
  // src/toast.connect.ts
9
+ import { dataAttr } from "@zag-js/dom-query";
14
10
  function connect(state, send, normalize) {
15
11
  const isVisible = state.hasTag("visible");
16
12
  const isPaused = state.hasTag("paused");
17
13
  const pauseOnInteraction = state.context.pauseOnInteraction;
18
14
  const placement = state.context.placement;
19
15
  return {
16
+ /**
17
+ * The type of the toast.
18
+ */
20
19
  type: state.context.type,
20
+ /**
21
+ * The title of the toast.
22
+ */
21
23
  title: state.context.title,
24
+ /**
25
+ * The description of the toast.
26
+ */
22
27
  description: state.context.description,
28
+ /**
29
+ * The current placement of the toast.
30
+ */
23
31
  placement,
32
+ /**
33
+ * Whether the toast is visible.
34
+ */
24
35
  isVisible,
36
+ /**
37
+ * Whether the toast is paused.
38
+ */
25
39
  isPaused,
40
+ /**
41
+ * Whether the toast is in RTL mode.
42
+ */
26
43
  isRtl: state.context.dir === "rtl",
44
+ /**
45
+ * Function to pause the toast (keeping it visible).
46
+ */
27
47
  pause() {
28
48
  send("PAUSE");
29
49
  },
50
+ /**
51
+ * Function to resume the toast dismissing.
52
+ */
30
53
  resume() {
31
54
  send("RESUME");
32
55
  },
56
+ /**
57
+ * Function to instantly dismiss the toast.
58
+ */
33
59
  dismiss() {
34
60
  send("DISMISS");
35
61
  },
62
+ /**
63
+ * Function render the toast in the DOM (based on the defined `render` property)
64
+ */
65
+ render() {
66
+ return state.context.render?.({
67
+ id: state.context.id,
68
+ type: state.context.type,
69
+ duration: state.context.duration,
70
+ title: state.context.title,
71
+ placement: state.context.placement,
72
+ description: state.context.description,
73
+ dismiss() {
74
+ send("DISMISS");
75
+ }
76
+ });
77
+ },
36
78
  rootProps: normalize.element({
37
79
  ...parts.root.attrs,
38
80
  dir: state.context.dir,
@@ -93,20 +135,7 @@ function connect(state, send, normalize) {
93
135
  onClick() {
94
136
  send("DISMISS");
95
137
  }
96
- }),
97
- render() {
98
- return state.context.render?.({
99
- id: state.context.id,
100
- type: state.context.type,
101
- duration: state.context.duration,
102
- title: state.context.title,
103
- placement: state.context.placement,
104
- description: state.context.description,
105
- dismiss() {
106
- send("DISMISS");
107
- }
108
- });
109
- }
138
+ })
110
139
  };
111
140
  }
112
141
 
@@ -1,15 +1,11 @@
1
1
  import {
2
- compact,
3
2
  createToastMachine
4
- } from "./chunk-SSV22S55.mjs";
3
+ } from "./chunk-EQTV3BNM.mjs";
5
4
 
6
5
  // src/toast-group.machine.ts
7
6
  import { createMachine } from "@zag-js/core";
8
-
9
- // ../../utilities/dom/src/constants.ts
10
- var MAX_Z_INDEX = 2147483647;
11
-
12
- // src/toast-group.machine.ts
7
+ import { MAX_Z_INDEX } from "@zag-js/dom-query";
8
+ import { compact } from "@zag-js/utils";
13
9
  function groupMachine(userContext) {
14
10
  const ctx = compact(userContext);
15
11
  return createMachine({