@zag-js/toast 1.38.1 → 1.39.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.
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _zag_js_core from '@zag-js/core';
2
2
  import { ToastGroupSchema } from './toast.types.mjs';
3
- export { ActionOptions, ToastApi as Api, ToastGroupApi as GroupApi, ToastGroupMachine as GroupMachine, ToastGroupProps as GroupProps, ToastGroupService as GroupService, IntlTranslations, ToastMachine as Machine, Options, Placement, PromiseOptions, ToastProps as Props, ToastService as Service, Status, StatusChangeDetails, ToastStore as Store, ToastStoreProps as StoreProps, ToastHeight, Type } from './toast.types.mjs';
3
+ export { ActionOptions, ToastApi as Api, ToastGroupApi as GroupApi, ToastGroupMachine as GroupMachine, ToastGroupProps as GroupProps, ToastGroupService as GroupService, IntlTranslations, ToastMachine as Machine, Options, Placement, PromiseOptions, ToastProps as Props, ToastService as Service, Status, StatusChangeDetails, ToastStore as Store, ToastStoreProps as StoreProps, ToastHeight, ToastQueuePriority, Type } from './toast.types.mjs';
4
4
  import { groupConnect } from './toast-group.connect.mjs';
5
5
  export { anatomy } from './toast.anatomy.mjs';
6
6
  export { connect } from './toast.connect.mjs';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _zag_js_core from '@zag-js/core';
2
2
  import { ToastGroupSchema } from './toast.types.js';
3
- export { ActionOptions, ToastApi as Api, ToastGroupApi as GroupApi, ToastGroupMachine as GroupMachine, ToastGroupProps as GroupProps, ToastGroupService as GroupService, IntlTranslations, ToastMachine as Machine, Options, Placement, PromiseOptions, ToastProps as Props, ToastService as Service, Status, StatusChangeDetails, ToastStore as Store, ToastStoreProps as StoreProps, ToastHeight, Type } from './toast.types.js';
3
+ export { ActionOptions, ToastApi as Api, ToastGroupApi as GroupApi, ToastGroupMachine as GroupMachine, ToastGroupProps as GroupProps, ToastGroupService as GroupService, IntlTranslations, ToastMachine as Machine, Options, Placement, PromiseOptions, ToastProps as Props, ToastService as Service, Status, StatusChangeDetails, ToastStore as Store, ToastStoreProps as StoreProps, ToastHeight, ToastQueuePriority, Type } from './toast.types.js';
4
4
  import { groupConnect } from './toast-group.connect.js';
5
5
  export { anatomy } from './toast.anatomy.js';
6
6
  export { connect } from './toast.connect.js';
@@ -1,6 +1,6 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
2
 
3
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger">;
4
- declare const parts: Record<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger", _zag_js_anatomy.AnatomyPart>;
3
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger">;
4
+ declare const parts: Record<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger", _zag_js_anatomy.AnatomyPart>;
5
5
 
6
6
  export { anatomy, parts };
@@ -1,6 +1,6 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
2
 
3
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger">;
4
- declare const parts: Record<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger", _zag_js_anatomy.AnatomyPart>;
3
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger">;
4
+ declare const parts: Record<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger", _zag_js_anatomy.AnatomyPart>;
5
5
 
6
6
  export { anatomy, parts };
@@ -27,6 +27,25 @@ var import_utils = require("@zag-js/utils");
27
27
  var withDefaults = (options, defaults) => {
28
28
  return { ...defaults, ...(0, import_utils.compact)(options) };
29
29
  };
30
+ var priorities = {
31
+ error: [1, 2],
32
+ warning: [3, 6],
33
+ loading: [4, 5],
34
+ success: [5, 7],
35
+ info: [6, 8]
36
+ };
37
+ var DEFAULT_TYPE = "info";
38
+ var getPriorityForType = (type, hasAction) => {
39
+ const [actionable, nonActionable] = priorities[type ?? DEFAULT_TYPE];
40
+ return hasAction ? actionable : nonActionable;
41
+ };
42
+ var sortToastsByPriority = (toastArray) => {
43
+ return toastArray.sort((a, b) => {
44
+ const priorityA = a.priority ?? getPriorityForType(a.type, !!a.action);
45
+ const priorityB = b.priority ?? getPriorityForType(b.type, !!b.action);
46
+ return priorityA - priorityB;
47
+ });
48
+ };
30
49
  function createToastStore(props = {}) {
31
50
  const attrs = withDefaults(props, {
32
51
  placement: "bottom",
@@ -62,6 +81,7 @@ function createToastStore(props = {}) {
62
81
  toasts.unshift(data);
63
82
  };
64
83
  const processQueue = () => {
84
+ toastQueue = sortToastsByPriority(toastQueue);
65
85
  while (toastQueue.length > 0 && toasts.length < attrs.max) {
66
86
  const nextToast = toastQueue.shift();
67
87
  if (nextToast) {
@@ -82,15 +102,17 @@ function createToastStore(props = {}) {
82
102
  return toast;
83
103
  });
84
104
  } else {
85
- addToast({
105
+ const newToast = {
86
106
  id,
87
107
  duration: attrs.duration,
88
108
  removeDelay: attrs.removeDelay,
89
- type: "info",
109
+ type: DEFAULT_TYPE,
90
110
  ...data,
91
111
  stacked: !attrs.overlap,
92
112
  gap: attrs.gap
93
- });
113
+ };
114
+ const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
115
+ addToast({ ...newToast, priority });
94
116
  }
95
117
  return id;
96
118
  };
@@ -152,7 +174,7 @@ function createToastStore(props = {}) {
152
174
  } else if (options.success !== void 0) {
153
175
  removable = false;
154
176
  const successOptions = (0, import_utils.runIfFn)(options.success, response);
155
- create({ ...shared, ...successOptions, id, type: "success" });
177
+ create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
156
178
  }
157
179
  }).catch(async (error2) => {
158
180
  result = ["reject", error2];
@@ -3,6 +3,25 @@ import { compact, runIfFn, uuid, warn } from "@zag-js/utils";
3
3
  var withDefaults = (options, defaults) => {
4
4
  return { ...defaults, ...compact(options) };
5
5
  };
6
+ var priorities = {
7
+ error: [1, 2],
8
+ warning: [3, 6],
9
+ loading: [4, 5],
10
+ success: [5, 7],
11
+ info: [6, 8]
12
+ };
13
+ var DEFAULT_TYPE = "info";
14
+ var getPriorityForType = (type, hasAction) => {
15
+ const [actionable, nonActionable] = priorities[type ?? DEFAULT_TYPE];
16
+ return hasAction ? actionable : nonActionable;
17
+ };
18
+ var sortToastsByPriority = (toastArray) => {
19
+ return toastArray.sort((a, b) => {
20
+ const priorityA = a.priority ?? getPriorityForType(a.type, !!a.action);
21
+ const priorityB = b.priority ?? getPriorityForType(b.type, !!b.action);
22
+ return priorityA - priorityB;
23
+ });
24
+ };
6
25
  function createToastStore(props = {}) {
7
26
  const attrs = withDefaults(props, {
8
27
  placement: "bottom",
@@ -38,6 +57,7 @@ function createToastStore(props = {}) {
38
57
  toasts.unshift(data);
39
58
  };
40
59
  const processQueue = () => {
60
+ toastQueue = sortToastsByPriority(toastQueue);
41
61
  while (toastQueue.length > 0 && toasts.length < attrs.max) {
42
62
  const nextToast = toastQueue.shift();
43
63
  if (nextToast) {
@@ -58,15 +78,17 @@ function createToastStore(props = {}) {
58
78
  return toast;
59
79
  });
60
80
  } else {
61
- addToast({
81
+ const newToast = {
62
82
  id,
63
83
  duration: attrs.duration,
64
84
  removeDelay: attrs.removeDelay,
65
- type: "info",
85
+ type: DEFAULT_TYPE,
66
86
  ...data,
67
87
  stacked: !attrs.overlap,
68
88
  gap: attrs.gap
69
- });
89
+ };
90
+ const priority = newToast.priority ?? getPriorityForType(newToast.type, !!newToast.action);
91
+ addToast({ ...newToast, priority });
70
92
  }
71
93
  return id;
72
94
  };
@@ -128,7 +150,7 @@ function createToastStore(props = {}) {
128
150
  } else if (options.success !== void 0) {
129
151
  removable = false;
130
152
  const successOptions = runIfFn(options.success, response);
131
- create({ ...shared, ...successOptions, id, type: "success" });
153
+ create({ ...shared, ...successOptions, id, type: successOptions.type ?? "success" });
132
154
  }
133
155
  }).catch(async (error2) => {
134
156
  result = ["reject", error2];
@@ -2,7 +2,8 @@ import { PropTypes, CommonProperties, Direction, DirectionProperty, Required, Re
2
2
  import { Service, EventObject, Machine } from '@zag-js/core';
3
3
  import { AnimationFrame } from '@zag-js/dom-query';
4
4
 
5
- type Type = "success" | "error" | "loading" | "info" | (string & {});
5
+ type Type = "success" | "error" | "loading" | "info" | "warning" | (string & {});
6
+ type ToastQueuePriority = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
6
7
  type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
7
8
  type Status = "visible" | "dismissing" | "unmounted";
8
9
  interface StatusChangeDetails {
@@ -64,6 +65,10 @@ interface Options<T = any> {
64
65
  * The type of the toast
65
66
  */
66
67
  type?: Type | undefined;
68
+ /**
69
+ * The priority of the toast (1 = highest, 8 = lowest)
70
+ */
71
+ priority?: ToastQueuePriority | undefined;
67
72
  /**
68
73
  * Function called when the toast is visible
69
74
  */
@@ -300,7 +305,9 @@ interface ToastStore<V = any> {
300
305
  type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
301
306
  interface PromiseOptions<V, O = any> {
302
307
  loading: Omit<Options<O>, "type">;
303
- success?: MaybeFunction<Omit<Options<O>, "type">, V> | undefined;
308
+ success?: MaybeFunction<Omit<Options<O>, "type"> & {
309
+ type?: "success" | "warning";
310
+ }, V> | undefined;
304
311
  error?: MaybeFunction<Omit<Options<O>, "type">, unknown> | undefined;
305
312
  finally?: (() => void | Promise<void>) | undefined;
306
313
  }
@@ -375,4 +382,4 @@ interface ToastApi<T extends PropTypes = PropTypes, O = any> {
375
382
  getActionTriggerProps: () => T["button"];
376
383
  }
377
384
 
378
- export type { ActionOptions, GroupProps, IntlTranslations, Options, Placement, PromiseOptions, Status, StatusChangeDetails, ToastApi, ToastGroupApi, ToastGroupMachine, ToastGroupProps, ToastGroupSchema, ToastGroupService, ToastHeight, ToastMachine, ToastProps, ToastSchema, ToastService, ToastStore, ToastStoreProps, Type };
385
+ export type { ActionOptions, GroupProps, IntlTranslations, Options, Placement, PromiseOptions, Status, StatusChangeDetails, ToastApi, ToastGroupApi, ToastGroupMachine, ToastGroupProps, ToastGroupSchema, ToastGroupService, ToastHeight, ToastMachine, ToastProps, ToastQueuePriority, ToastSchema, ToastService, ToastStore, ToastStoreProps, Type };
@@ -2,7 +2,8 @@ import { PropTypes, CommonProperties, Direction, DirectionProperty, Required, Re
2
2
  import { Service, EventObject, Machine } from '@zag-js/core';
3
3
  import { AnimationFrame } from '@zag-js/dom-query';
4
4
 
5
- type Type = "success" | "error" | "loading" | "info" | (string & {});
5
+ type Type = "success" | "error" | "loading" | "info" | "warning" | (string & {});
6
+ type ToastQueuePriority = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
6
7
  type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
7
8
  type Status = "visible" | "dismissing" | "unmounted";
8
9
  interface StatusChangeDetails {
@@ -64,6 +65,10 @@ interface Options<T = any> {
64
65
  * The type of the toast
65
66
  */
66
67
  type?: Type | undefined;
68
+ /**
69
+ * The priority of the toast (1 = highest, 8 = lowest)
70
+ */
71
+ priority?: ToastQueuePriority | undefined;
67
72
  /**
68
73
  * Function called when the toast is visible
69
74
  */
@@ -300,7 +305,9 @@ interface ToastStore<V = any> {
300
305
  type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
301
306
  interface PromiseOptions<V, O = any> {
302
307
  loading: Omit<Options<O>, "type">;
303
- success?: MaybeFunction<Omit<Options<O>, "type">, V> | undefined;
308
+ success?: MaybeFunction<Omit<Options<O>, "type"> & {
309
+ type?: "success" | "warning";
310
+ }, V> | undefined;
304
311
  error?: MaybeFunction<Omit<Options<O>, "type">, unknown> | undefined;
305
312
  finally?: (() => void | Promise<void>) | undefined;
306
313
  }
@@ -375,4 +382,4 @@ interface ToastApi<T extends PropTypes = PropTypes, O = any> {
375
382
  getActionTriggerProps: () => T["button"];
376
383
  }
377
384
 
378
- export type { ActionOptions, GroupProps, IntlTranslations, Options, Placement, PromiseOptions, Status, StatusChangeDetails, ToastApi, ToastGroupApi, ToastGroupMachine, ToastGroupProps, ToastGroupSchema, ToastGroupService, ToastHeight, ToastMachine, ToastProps, ToastSchema, ToastService, ToastStore, ToastStoreProps, Type };
385
+ export type { ActionOptions, GroupProps, IntlTranslations, Options, Placement, PromiseOptions, Status, StatusChangeDetails, ToastApi, ToastGroupApi, ToastGroupMachine, ToastGroupProps, ToastGroupSchema, ToastGroupService, ToastHeight, ToastMachine, ToastProps, ToastQueuePriority, ToastSchema, ToastService, ToastStore, ToastStoreProps, Type };
@@ -34,6 +34,7 @@ var defaultTimeouts = {
34
34
  error: 5e3,
35
35
  success: 2e3,
36
36
  loading: Infinity,
37
+ warning: 5e3,
37
38
  DEFAULT: 5e3
38
39
  };
39
40
  function getToastDuration(duration, type) {
@@ -5,6 +5,7 @@ var defaultTimeouts = {
5
5
  error: 5e3,
6
6
  success: 2e3,
7
7
  loading: Infinity,
8
+ warning: 5e3,
8
9
  DEFAULT: 5e3
9
10
  };
10
11
  function getToastDuration(duration, type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/toast",
3
- "version": "1.38.1",
3
+ "version": "1.39.0",
4
4
  "description": "Core logic for the toast widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -26,12 +26,12 @@
26
26
  "url": "https://github.com/chakra-ui/zag/issues"
27
27
  },
28
28
  "dependencies": {
29
- "@zag-js/anatomy": "1.38.1",
30
- "@zag-js/core": "1.38.1",
31
- "@zag-js/dom-query": "1.38.1",
32
- "@zag-js/dismissable": "1.38.1",
33
- "@zag-js/utils": "1.38.1",
34
- "@zag-js/types": "1.38.1"
29
+ "@zag-js/anatomy": "1.39.0",
30
+ "@zag-js/core": "1.39.0",
31
+ "@zag-js/dom-query": "1.39.0",
32
+ "@zag-js/dismissable": "1.39.0",
33
+ "@zag-js/utils": "1.39.0",
34
+ "@zag-js/types": "1.39.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "clean-package": "2.2.0"