@zag-js/toast 0.23.0 → 0.25.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,45 +1,54 @@
1
+ import { RootProperties, CommonProperties, Direction, PropTypes, DirectionProperty, Context, RequiredBy, NormalizeProps } from '@zag-js/types';
1
2
  import * as _zag_js_core from '@zag-js/core';
2
3
  import { Machine, StateMachine } from '@zag-js/core';
3
- import * as _zag_js_types from '@zag-js/types';
4
- import { RootProperties, CommonProperties, Direction, PropTypes, DirectionProperty, Context, RequiredBy, NormalizeProps } from '@zag-js/types';
5
4
  import * as _zag_js_anatomy from '@zag-js/anatomy';
6
5
 
7
6
  type Type = "success" | "error" | "loading" | "info" | "custom";
8
7
  type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
9
- interface ToastOptions {
8
+ interface BaseOptions {
10
9
  /**
11
- * The unique id of the toast
10
+ * Whether to pause toast when the user leaves the browser tab
12
11
  */
13
- id: string;
12
+ pauseOnPageIdle?: boolean;
14
13
  /**
15
- * The type of the toast
14
+ * Whether to pause the toast when interacted with
16
15
  */
17
- type: Type;
16
+ pauseOnInteraction?: boolean;
17
+ /**
18
+ * The duration the toast will be visible
19
+ */
20
+ duration?: number;
21
+ /**
22
+ * The duration for the toast to kept alive before it is removed.
23
+ * Useful for exit transitions.
24
+ */
25
+ removeDelay?: number;
18
26
  /**
19
27
  * The placement of the toast
20
28
  */
21
- placement: Placement;
29
+ placement?: Placement;
22
30
  /**
23
- * The message of the toast
31
+ * Custom function to render the toast element.
24
32
  */
25
- title?: string;
33
+ render?: (options: MachineApi<any>) => any;
34
+ }
35
+ interface ToastOptions {
26
36
  /**
27
- * The description of the toast
37
+ * The unique id of the toast
28
38
  */
29
- description?: string;
39
+ id: string;
30
40
  /**
31
- * The duration the toast will be visible
41
+ * The type of the toast
32
42
  */
33
- duration: number;
43
+ type: Type;
34
44
  /**
35
- * Custom function to render the toast element.
45
+ * The message of the toast
36
46
  */
37
- render?: (options: RenderOptions) => any;
47
+ title?: string;
38
48
  /**
39
- * The duration for the toast to kept alive before it is removed.
40
- * Useful for exit transitions.
49
+ * The description of the toast
41
50
  */
42
- removeDelay?: number;
51
+ description?: string;
43
52
  /**
44
53
  * Function called when the toast has been closed and removed
45
54
  */
@@ -57,25 +66,8 @@ interface ToastOptions {
57
66
  */
58
67
  onUpdate?: VoidFunction;
59
68
  }
60
- type Options = Partial<ToastOptions>;
61
- type RenderOptions = Omit<ToastOptions, "render"> & {
62
- dismiss(): void;
63
- };
64
- interface SharedContext {
65
- /**
66
- * Whether to pause toast when the user leaves the browser tab
67
- */
68
- pauseOnPageIdle?: boolean;
69
- /**
70
- * Whether to pause the toast when interacted with
71
- */
72
- pauseOnInteraction?: boolean;
73
- /**
74
- * The default options for the toast
75
- */
76
- defaultOptions?: Partial<Pick<ToastOptions, "duration" | "removeDelay" | "placement">>;
77
- }
78
- interface MachineContext extends SharedContext, RootProperties, CommonProperties, Omit<ToastOptions, "removeDelay"> {
69
+ type Options = Partial<ToastOptions & BaseOptions>;
70
+ interface MachineContext extends BaseOptions, RootProperties, CommonProperties, Omit<ToastOptions, "removeDelay"> {
79
71
  /**
80
72
  * The duration for the toast to kept alive before it is removed.
81
73
  * Useful for exit transitions.
@@ -101,7 +93,7 @@ interface MachineState {
101
93
  type State = StateMachine.State<MachineContext, MachineState>;
102
94
  type Send = StateMachine.Send;
103
95
  type Service = Machine<MachineContext, MachineState>;
104
- interface GroupPublicContext extends SharedContext, DirectionProperty, CommonProperties {
96
+ interface GroupPublicContext extends BaseOptions, DirectionProperty, CommonProperties {
105
97
  /**
106
98
  * The gutter or spacing between toasts
107
99
  */
@@ -210,7 +202,7 @@ interface GroupMachineApi<T extends PropTypes = PropTypes> {
210
202
  * - When the promise resolves, the toast will be updated with the success options.
211
203
  * - When the promise rejects, the toast will be updated with the error options.
212
204
  */
213
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
205
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: Partial<ToastOptions>): Promise<T>;
214
206
  /**
215
207
  * Function to subscribe to the toast group.
216
208
  */
@@ -258,10 +250,6 @@ interface MachineApi<T extends PropTypes = PropTypes> {
258
250
  * Function to instantly dismiss the toast.
259
251
  */
260
252
  dismiss(): void;
261
- /**
262
- * Function render the toast in the DOM (based on the defined `render` property)
263
- */
264
- render(): any;
265
253
  rootProps: T["element"];
266
254
  titleProps: T["element"];
267
255
  descriptionProps: T["element"];
@@ -274,7 +262,7 @@ declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_cor
274
262
 
275
263
  declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
276
264
 
277
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
265
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "closeTrigger">;
278
266
 
279
267
  declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
280
268
 
@@ -282,6 +270,5 @@ declare const group: {
282
270
  connect: typeof groupConnect;
283
271
  machine: typeof groupMachine;
284
272
  };
285
- declare function api(): GroupMachineApi<_zag_js_types.PropTypes> | undefined;
286
273
 
287
- export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
274
+ export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, PromiseOptions, Service, ToastOptions, Type, anatomy, connect, createToastMachine as createMachine, group };
package/dist/index.d.ts CHANGED
@@ -1,45 +1,54 @@
1
+ import { RootProperties, CommonProperties, Direction, PropTypes, DirectionProperty, Context, RequiredBy, NormalizeProps } from '@zag-js/types';
1
2
  import * as _zag_js_core from '@zag-js/core';
2
3
  import { Machine, StateMachine } from '@zag-js/core';
3
- import * as _zag_js_types from '@zag-js/types';
4
- import { RootProperties, CommonProperties, Direction, PropTypes, DirectionProperty, Context, RequiredBy, NormalizeProps } from '@zag-js/types';
5
4
  import * as _zag_js_anatomy from '@zag-js/anatomy';
6
5
 
7
6
  type Type = "success" | "error" | "loading" | "info" | "custom";
8
7
  type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
9
- interface ToastOptions {
8
+ interface BaseOptions {
10
9
  /**
11
- * The unique id of the toast
10
+ * Whether to pause toast when the user leaves the browser tab
12
11
  */
13
- id: string;
12
+ pauseOnPageIdle?: boolean;
14
13
  /**
15
- * The type of the toast
14
+ * Whether to pause the toast when interacted with
16
15
  */
17
- type: Type;
16
+ pauseOnInteraction?: boolean;
17
+ /**
18
+ * The duration the toast will be visible
19
+ */
20
+ duration?: number;
21
+ /**
22
+ * The duration for the toast to kept alive before it is removed.
23
+ * Useful for exit transitions.
24
+ */
25
+ removeDelay?: number;
18
26
  /**
19
27
  * The placement of the toast
20
28
  */
21
- placement: Placement;
29
+ placement?: Placement;
22
30
  /**
23
- * The message of the toast
31
+ * Custom function to render the toast element.
24
32
  */
25
- title?: string;
33
+ render?: (options: MachineApi<any>) => any;
34
+ }
35
+ interface ToastOptions {
26
36
  /**
27
- * The description of the toast
37
+ * The unique id of the toast
28
38
  */
29
- description?: string;
39
+ id: string;
30
40
  /**
31
- * The duration the toast will be visible
41
+ * The type of the toast
32
42
  */
33
- duration: number;
43
+ type: Type;
34
44
  /**
35
- * Custom function to render the toast element.
45
+ * The message of the toast
36
46
  */
37
- render?: (options: RenderOptions) => any;
47
+ title?: string;
38
48
  /**
39
- * The duration for the toast to kept alive before it is removed.
40
- * Useful for exit transitions.
49
+ * The description of the toast
41
50
  */
42
- removeDelay?: number;
51
+ description?: string;
43
52
  /**
44
53
  * Function called when the toast has been closed and removed
45
54
  */
@@ -57,25 +66,8 @@ interface ToastOptions {
57
66
  */
58
67
  onUpdate?: VoidFunction;
59
68
  }
60
- type Options = Partial<ToastOptions>;
61
- type RenderOptions = Omit<ToastOptions, "render"> & {
62
- dismiss(): void;
63
- };
64
- interface SharedContext {
65
- /**
66
- * Whether to pause toast when the user leaves the browser tab
67
- */
68
- pauseOnPageIdle?: boolean;
69
- /**
70
- * Whether to pause the toast when interacted with
71
- */
72
- pauseOnInteraction?: boolean;
73
- /**
74
- * The default options for the toast
75
- */
76
- defaultOptions?: Partial<Pick<ToastOptions, "duration" | "removeDelay" | "placement">>;
77
- }
78
- interface MachineContext extends SharedContext, RootProperties, CommonProperties, Omit<ToastOptions, "removeDelay"> {
69
+ type Options = Partial<ToastOptions & BaseOptions>;
70
+ interface MachineContext extends BaseOptions, RootProperties, CommonProperties, Omit<ToastOptions, "removeDelay"> {
79
71
  /**
80
72
  * The duration for the toast to kept alive before it is removed.
81
73
  * Useful for exit transitions.
@@ -101,7 +93,7 @@ interface MachineState {
101
93
  type State = StateMachine.State<MachineContext, MachineState>;
102
94
  type Send = StateMachine.Send;
103
95
  type Service = Machine<MachineContext, MachineState>;
104
- interface GroupPublicContext extends SharedContext, DirectionProperty, CommonProperties {
96
+ interface GroupPublicContext extends BaseOptions, DirectionProperty, CommonProperties {
105
97
  /**
106
98
  * The gutter or spacing between toasts
107
99
  */
@@ -210,7 +202,7 @@ interface GroupMachineApi<T extends PropTypes = PropTypes> {
210
202
  * - When the promise resolves, the toast will be updated with the success options.
211
203
  * - When the promise rejects, the toast will be updated with the error options.
212
204
  */
213
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
205
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: Partial<ToastOptions>): Promise<T>;
214
206
  /**
215
207
  * Function to subscribe to the toast group.
216
208
  */
@@ -258,10 +250,6 @@ interface MachineApi<T extends PropTypes = PropTypes> {
258
250
  * Function to instantly dismiss the toast.
259
251
  */
260
252
  dismiss(): void;
261
- /**
262
- * Function render the toast in the DOM (based on the defined `render` property)
263
- */
264
- render(): any;
265
253
  rootProps: T["element"];
266
254
  titleProps: T["element"];
267
255
  descriptionProps: T["element"];
@@ -274,7 +262,7 @@ declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_cor
274
262
 
275
263
  declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
276
264
 
277
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
265
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "closeTrigger">;
278
266
 
279
267
  declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
280
268
 
@@ -282,6 +270,5 @@ declare const group: {
282
270
  connect: typeof groupConnect;
283
271
  machine: typeof groupMachine;
284
272
  };
285
- declare function api(): GroupMachineApi<_zag_js_types.PropTypes> | undefined;
286
273
 
287
- export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
274
+ export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, PromiseOptions, Service, ToastOptions, Type, anatomy, connect, createToastMachine as createMachine, group };
package/dist/index.js CHANGED
@@ -21,14 +21,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  anatomy: () => anatomy,
24
- api: () => api,
25
24
  connect: () => connect,
26
25
  createMachine: () => createToastMachine,
27
26
  group: () => group
28
27
  });
29
28
  module.exports = __toCommonJS(src_exports);
30
- var import_dom_query3 = require("@zag-js/dom-query");
31
- var import_utils4 = require("@zag-js/utils");
32
29
 
33
30
  // src/toast-group.connect.ts
34
31
  var import_core = require("@zag-js/core");
@@ -46,8 +43,7 @@ var dom = (0, import_dom_query.createScope)({
46
43
  getRootId: (ctx) => `toast:${ctx.id}`,
47
44
  getTitleId: (ctx) => `toast:${ctx.id}:title`,
48
45
  getDescriptionId: (ctx) => `toast:${ctx.id}:description`,
49
- getCloseTriggerId: (ctx) => `toast${ctx.id}:close`,
50
- getPortalId: (ctx) => `toast-portal:${ctx.id}`
46
+ getCloseTriggerId: (ctx) => `toast${ctx.id}:close`
51
47
  });
52
48
 
53
49
  // src/toast.utils.ts
@@ -111,94 +107,96 @@ function getGroupPlacementStyle(ctx, placement) {
111
107
  }
112
108
 
113
109
  // src/toast-group.connect.ts
114
- var toaster = {};
115
110
  function groupConnect(state, send, normalize) {
116
- const group2 = {
111
+ const toastsByPlacement = getToastsByPlacement(state.context.toasts);
112
+ function isVisible(id) {
113
+ if (!state.context.toasts.length)
114
+ return false;
115
+ return !!state.context.toasts.find((toast) => toast.id == id);
116
+ }
117
+ function create(options) {
118
+ const uid = `toast:${(0, import_utils.uuid)()}`;
119
+ const id = options.id ? options.id : uid;
120
+ if (isVisible(id))
121
+ return;
122
+ send({ type: "ADD_TOAST", toast: { ...options, id } });
123
+ return id;
124
+ }
125
+ function update(id, options) {
126
+ if (!isVisible(id))
127
+ return;
128
+ send({ type: "UPDATE_TOAST", id, toast: options });
129
+ return id;
130
+ }
131
+ function upsert(options) {
132
+ const { id } = options;
133
+ const visible = id ? isVisible(id) : false;
134
+ if (visible && id != null) {
135
+ return update(id, options);
136
+ } else {
137
+ return create(options);
138
+ }
139
+ }
140
+ function dismiss(id) {
141
+ if (id == null) {
142
+ send("DISMISS_ALL");
143
+ } else if (isVisible(id)) {
144
+ send({ type: "DISMISS_TOAST", id });
145
+ }
146
+ }
147
+ return {
117
148
  count: state.context.count,
118
149
  toasts: state.context.toasts,
119
- toastsByPlacement: getToastsByPlacement(state.context.toasts),
120
- isVisible(id) {
121
- if (!state.context.toasts.length)
122
- return false;
123
- return !!state.context.toasts.find((toast) => toast.id == id);
124
- },
125
- create(options) {
126
- const uid = `toast:${(0, import_utils.uuid)()}`;
127
- const id = options.id ? options.id : uid;
128
- if (group2.isVisible(id))
129
- return;
130
- send({ type: "ADD_TOAST", toast: { ...options, id } });
131
- return id;
132
- },
133
- upsert(options) {
134
- const { id } = options;
135
- const isVisible = id ? group2.isVisible(id) : false;
136
- if (isVisible && id != null) {
137
- return group2.update(id, options);
138
- } else {
139
- return group2.create(options);
140
- }
141
- },
142
- dismiss(id) {
143
- if (id == null) {
144
- send("DISMISS_ALL");
145
- } else if (group2.isVisible(id)) {
146
- send({ type: "DISMISS_TOAST", id });
147
- }
148
- },
150
+ toastsByPlacement,
151
+ isVisible,
152
+ create,
153
+ update,
154
+ upsert,
155
+ dismiss,
149
156
  remove(id) {
150
157
  if (id == null) {
151
158
  send("REMOVE_ALL");
152
- } else if (group2.isVisible(id)) {
159
+ } else if (isVisible(id)) {
153
160
  send({ type: "REMOVE_TOAST", id });
154
161
  }
155
162
  },
156
163
  dismissByPlacement(placement) {
157
- const toasts = group2.toastsByPlacement[placement];
164
+ const toasts = toastsByPlacement[placement];
158
165
  if (toasts) {
159
- toasts.forEach((toast) => group2.dismiss(toast.id));
166
+ toasts.forEach((toast) => dismiss(toast.id));
160
167
  }
161
168
  },
162
- update(id, options) {
163
- if (!group2.isVisible(id))
164
- return;
165
- send({ type: "UPDATE_TOAST", id, toast: options });
166
- return id;
167
- },
168
169
  loading(options) {
169
- options.type = "loading";
170
- return group2.upsert(options);
170
+ return upsert({ ...options, type: "loading" });
171
171
  },
172
172
  success(options) {
173
- options.type = "success";
174
- return group2.upsert(options);
173
+ return upsert({ ...options, type: "success" });
175
174
  },
176
175
  error(options) {
177
- options.type = "error";
178
- return group2.upsert(options);
176
+ return upsert({ ...options, type: "error" });
179
177
  },
180
178
  promise(promise, options, shared = {}) {
181
- const id = group2.loading({ ...shared, ...options.loading });
179
+ const id = upsert({ ...shared, ...options.loading, type: "loading" });
182
180
  promise.then((response) => {
183
181
  const successOptions = (0, import_utils.runIfFn)(options.success, response);
184
- group2.success({ ...shared, ...successOptions, id });
182
+ upsert({ ...shared, ...successOptions, id, type: "success" });
185
183
  }).catch((error) => {
186
184
  const errorOptions = (0, import_utils.runIfFn)(options.error, error);
187
- group2.error({ ...shared, ...errorOptions, id });
185
+ upsert({ ...shared, ...errorOptions, id, type: "error" });
188
186
  });
189
187
  return promise;
190
188
  },
191
189
  pause(id) {
192
190
  if (id == null) {
193
191
  send("PAUSE_ALL");
194
- } else if (group2.isVisible(id)) {
192
+ } else if (isVisible(id)) {
195
193
  send({ type: "PAUSE_TOAST", id });
196
194
  }
197
195
  },
198
196
  resume(id) {
199
197
  if (id == null) {
200
198
  send("RESUME_ALL");
201
- } else if (group2.isVisible(id)) {
199
+ } else if (isVisible(id)) {
202
200
  send({ type: "RESUME_TOAST", id });
203
201
  }
204
202
  },
@@ -219,8 +217,6 @@ function groupConnect(state, send, normalize) {
219
217
  return (0, import_core.subscribe)(state.context.toasts, () => fn(state.context.toasts));
220
218
  }
221
219
  };
222
- Object.assign(toaster, group2);
223
- return group2;
224
220
  }
225
221
 
226
222
  // src/toast-group.machine.ts
@@ -416,7 +412,10 @@ function groupMachine(userContext) {
416
412
  guard: (ctx2) => ctx2.toasts.length < ctx2.max,
417
413
  actions: (ctx2, evt, { self }) => {
418
414
  const options = {
419
- ...ctx2.defaultOptions,
415
+ placement: ctx2.placement,
416
+ duration: ctx2.duration,
417
+ removeDelay: ctx2.removeDelay,
418
+ render: ctx2.render,
420
419
  ...evt.toast,
421
420
  pauseOnPageIdle: ctx2.pauseOnPageIdle,
422
421
  pauseOnInteraction: ctx2.pauseOnInteraction,
@@ -484,19 +483,6 @@ function connect(state, send, normalize) {
484
483
  dismiss() {
485
484
  send("DISMISS");
486
485
  },
487
- render() {
488
- return state.context.render?.({
489
- id: state.context.id,
490
- type: state.context.type,
491
- duration: state.context.duration,
492
- title: state.context.title,
493
- placement: state.context.placement,
494
- description: state.context.description,
495
- dismiss() {
496
- send("DISMISS");
497
- }
498
- });
499
- },
500
486
  rootProps: normalize.element({
501
487
  ...parts.root.attrs,
502
488
  dir: state.context.dir,
@@ -566,17 +552,9 @@ var group = {
566
552
  connect: groupConnect,
567
553
  machine: groupMachine
568
554
  };
569
- function api() {
570
- if (!(0, import_dom_query3.isDom)()) {
571
- (0, import_utils4.warn)("toast.api() is only available in the browser");
572
- } else {
573
- return toaster;
574
- }
575
- }
576
555
  // Annotate the CommonJS export names for ESM import in node:
577
556
  0 && (module.exports = {
578
557
  anatomy,
579
- api,
580
558
  connect,
581
559
  createMachine,
582
560
  group