@zag-js/toast 0.16.0 → 0.18.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
@@ -118,63 +118,13 @@ type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties &
118
118
  */
119
119
  offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
120
120
  };
121
- type PublicApi<T extends PropTypes = PropTypes> = {
122
- /**
123
- * The type of the toast.
124
- */
125
- type: Type;
126
- /**
127
- * The title of the toast.
128
- */
129
- title: string | undefined;
130
- /**
131
- * The description of the toast.
132
- */
133
- description: string | undefined;
134
- /**
135
- * The current placement of the toast.
136
- */
137
- placement: Placement;
138
- /**
139
- * Whether the toast is visible.
140
- */
141
- isVisible: boolean;
142
- /**
143
- * Whether the toast is paused.
144
- */
145
- isPaused: boolean;
146
- /**
147
- * Whether the toast is in RTL mode.
148
- */
149
- isRtl: boolean;
150
- /**
151
- * Function to pause the toast (keeping it visible).
152
- */
153
- pause(): void;
154
- /**
155
- * Function to resume the toast dismissing.
156
- */
157
- resume(): void;
158
- /**
159
- * Function to instantly dismiss the toast.
160
- */
161
- dismiss(): void;
162
- /**
163
- * Function render the toast in the DOM (based on the defined `render` property)
164
- */
165
- render(): any;
166
- rootProps: T["element"];
167
- titleProps: T["element"];
168
- descriptionProps: T["element"];
169
- closeTriggerProps: T["button"];
170
- };
171
121
  type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
172
122
  type GroupComputedContext = Readonly<{
173
123
  /**
174
124
  * @computed
175
125
  * The total number of toasts in the group
176
126
  */
177
- readonly count: number;
127
+ count: number;
178
128
  }>;
179
129
  type GroupPrivateContext = Context<{}>;
180
130
  type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
@@ -190,20 +140,7 @@ type GroupProps = {
190
140
  placement: Placement;
191
141
  label?: string;
192
142
  };
193
- type Toaster = {
194
- count: number;
195
- isVisible(id: string): boolean;
196
- upsert(options: ToastOptions): string | undefined;
197
- create(options: ToastOptions): string | undefined;
198
- success(options: ToastOptions): string | undefined;
199
- error(options: ToastOptions): string | undefined;
200
- loading(options: ToastOptions): string | undefined;
201
- dismiss(id?: string | undefined): void;
202
- remove(id?: string | undefined): void;
203
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
204
- };
205
-
206
- declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
143
+ type GroupMachineApi<T extends PropTypes = PropTypes> = {
207
144
  /**
208
145
  * The total number of toasts
209
146
  */
@@ -228,66 +165,121 @@ declare function groupConnect<T extends PropTypes>(state: GroupState, send: Grou
228
165
  * Function to create or update a toast.
229
166
  */
230
167
  upsert(options: Options): string | undefined;
168
+ /**
169
+ * Function to update a toast's options by id.
170
+ */
171
+ update(id: string, options: Options): void;
172
+ /**
173
+ * Function to create a success toast.
174
+ */
175
+ success(options: Options): string | undefined;
176
+ /**
177
+ * Function to create an error toast.
178
+ */
179
+ error(options: Options): string | undefined;
180
+ /**
181
+ * Function to create a loading toast.
182
+ */
183
+ loading(options: Options): string | undefined;
184
+ /**
185
+ * Function to resume a toast by id.
186
+ */
187
+ resume(id?: string | undefined): void;
188
+ /**
189
+ * Function to pause a toast by id.
190
+ */
191
+ pause(id?: string | undefined): void;
231
192
  /**
232
193
  * Function to dismiss a toast by id.
233
194
  * If no id is provided, all toasts will be dismissed.
234
195
  */
235
- dismiss(id?: string): void;
196
+ dismiss(id?: string | undefined): void;
197
+ /**
198
+ * Function to dismiss all toasts by placement.
199
+ */
200
+ dismissByPlacement(placement: Placement): void;
236
201
  /**
237
202
  * Function to remove a toast by id.
238
203
  * If no id is provided, all toasts will be removed.
239
204
  */
240
- remove(id?: string): void;
205
+ remove(id?: string | undefined): void;
241
206
  /**
242
- * Function to dismiss all toasts by placement.
207
+ * Function to create a toast from a promise.
208
+ * - When the promise resolves, the toast will be updated with the success options.
209
+ * - When the promise rejects, the toast will be updated with the error options.
243
210
  */
244
- dismissByPlacement(placement: Placement): void;
211
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
245
212
  /**
246
- * Function to update a toast's options by id.
213
+ * Function to subscribe to the toast group.
247
214
  */
248
- update(id: string, options: Options): string | undefined;
215
+ subscribe(callback: (toasts: Service[]) => void): VoidFunction;
216
+ getGroupProps(options: GroupProps): T["element"];
217
+ };
218
+ type MachineApi<T extends PropTypes = PropTypes> = {
249
219
  /**
250
- * Function to create a loading toast.
220
+ * The type of the toast.
251
221
  */
252
- loading(options: Options): string | undefined;
222
+ type: Type;
253
223
  /**
254
- * Function to create a success toast.
224
+ * The title of the toast.
255
225
  */
256
- success(options: Options): string | undefined;
226
+ title: string | undefined;
257
227
  /**
258
- * Function to create an error toast.
228
+ * The description of the toast.
259
229
  */
260
- error(options: Options): string | undefined;
230
+ description: string | undefined;
261
231
  /**
262
- * Function to create a toast from a promise.
263
- * - When the promise resolves, the toast will be updated with the success options.
264
- * - When the promise rejects, the toast will be updated with the error options.
232
+ * The current placement of the toast.
265
233
  */
266
- promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
234
+ placement: Placement;
267
235
  /**
268
- * Function to pause a toast by id.
236
+ * Whether the toast is visible.
269
237
  */
270
- pause(id?: string): void;
238
+ isVisible: boolean;
271
239
  /**
272
- * Function to resume a toast by id.
240
+ * Whether the toast is paused.
273
241
  */
274
- resume(id?: string): void;
275
- getGroupProps(options: GroupProps): T["element"];
276
- subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
242
+ isPaused: boolean;
243
+ /**
244
+ * Whether the toast is in RTL mode.
245
+ */
246
+ isRtl: boolean;
247
+ /**
248
+ * Function to pause the toast (keeping it visible).
249
+ */
250
+ pause(): void;
251
+ /**
252
+ * Function to resume the toast dismissing.
253
+ */
254
+ resume(): void;
255
+ /**
256
+ * Function to instantly dismiss the toast.
257
+ */
258
+ dismiss(): void;
259
+ /**
260
+ * Function render the toast in the DOM (based on the defined `render` property)
261
+ */
262
+ render(): any;
263
+ rootProps: T["element"];
264
+ titleProps: T["element"];
265
+ descriptionProps: T["element"];
266
+ closeTriggerProps: T["button"];
277
267
  };
278
268
 
269
+ declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): GroupMachineApi<T>;
270
+
279
271
  declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
280
272
 
281
273
  declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
282
274
 
283
275
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
284
276
 
285
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
277
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
286
278
 
287
279
  declare const group: {
288
280
  connect: typeof groupConnect;
289
281
  machine: typeof groupMachine;
290
282
  };
291
- declare function api(): Toaster | undefined;
283
+ declare function api(): GroupMachineApi | undefined;
292
284
 
293
- export { GroupMachineContext, MachineContext, MachineState, Placement, PublicApi, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
285
+ export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
package/dist/index.d.ts CHANGED
@@ -118,63 +118,13 @@ type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties &
118
118
  */
119
119
  offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
120
120
  };
121
- type PublicApi<T extends PropTypes = PropTypes> = {
122
- /**
123
- * The type of the toast.
124
- */
125
- type: Type;
126
- /**
127
- * The title of the toast.
128
- */
129
- title: string | undefined;
130
- /**
131
- * The description of the toast.
132
- */
133
- description: string | undefined;
134
- /**
135
- * The current placement of the toast.
136
- */
137
- placement: Placement;
138
- /**
139
- * Whether the toast is visible.
140
- */
141
- isVisible: boolean;
142
- /**
143
- * Whether the toast is paused.
144
- */
145
- isPaused: boolean;
146
- /**
147
- * Whether the toast is in RTL mode.
148
- */
149
- isRtl: boolean;
150
- /**
151
- * Function to pause the toast (keeping it visible).
152
- */
153
- pause(): void;
154
- /**
155
- * Function to resume the toast dismissing.
156
- */
157
- resume(): void;
158
- /**
159
- * Function to instantly dismiss the toast.
160
- */
161
- dismiss(): void;
162
- /**
163
- * Function render the toast in the DOM (based on the defined `render` property)
164
- */
165
- render(): any;
166
- rootProps: T["element"];
167
- titleProps: T["element"];
168
- descriptionProps: T["element"];
169
- closeTriggerProps: T["button"];
170
- };
171
121
  type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
172
122
  type GroupComputedContext = Readonly<{
173
123
  /**
174
124
  * @computed
175
125
  * The total number of toasts in the group
176
126
  */
177
- readonly count: number;
127
+ count: number;
178
128
  }>;
179
129
  type GroupPrivateContext = Context<{}>;
180
130
  type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
@@ -190,20 +140,7 @@ type GroupProps = {
190
140
  placement: Placement;
191
141
  label?: string;
192
142
  };
193
- type Toaster = {
194
- count: number;
195
- isVisible(id: string): boolean;
196
- upsert(options: ToastOptions): string | undefined;
197
- create(options: ToastOptions): string | undefined;
198
- success(options: ToastOptions): string | undefined;
199
- error(options: ToastOptions): string | undefined;
200
- loading(options: ToastOptions): string | undefined;
201
- dismiss(id?: string | undefined): void;
202
- remove(id?: string | undefined): void;
203
- promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
204
- };
205
-
206
- declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
143
+ type GroupMachineApi<T extends PropTypes = PropTypes> = {
207
144
  /**
208
145
  * The total number of toasts
209
146
  */
@@ -228,66 +165,121 @@ declare function groupConnect<T extends PropTypes>(state: GroupState, send: Grou
228
165
  * Function to create or update a toast.
229
166
  */
230
167
  upsert(options: Options): string | undefined;
168
+ /**
169
+ * Function to update a toast's options by id.
170
+ */
171
+ update(id: string, options: Options): void;
172
+ /**
173
+ * Function to create a success toast.
174
+ */
175
+ success(options: Options): string | undefined;
176
+ /**
177
+ * Function to create an error toast.
178
+ */
179
+ error(options: Options): string | undefined;
180
+ /**
181
+ * Function to create a loading toast.
182
+ */
183
+ loading(options: Options): string | undefined;
184
+ /**
185
+ * Function to resume a toast by id.
186
+ */
187
+ resume(id?: string | undefined): void;
188
+ /**
189
+ * Function to pause a toast by id.
190
+ */
191
+ pause(id?: string | undefined): void;
231
192
  /**
232
193
  * Function to dismiss a toast by id.
233
194
  * If no id is provided, all toasts will be dismissed.
234
195
  */
235
- dismiss(id?: string): void;
196
+ dismiss(id?: string | undefined): void;
197
+ /**
198
+ * Function to dismiss all toasts by placement.
199
+ */
200
+ dismissByPlacement(placement: Placement): void;
236
201
  /**
237
202
  * Function to remove a toast by id.
238
203
  * If no id is provided, all toasts will be removed.
239
204
  */
240
- remove(id?: string): void;
205
+ remove(id?: string | undefined): void;
241
206
  /**
242
- * Function to dismiss all toasts by placement.
207
+ * Function to create a toast from a promise.
208
+ * - When the promise resolves, the toast will be updated with the success options.
209
+ * - When the promise rejects, the toast will be updated with the error options.
243
210
  */
244
- dismissByPlacement(placement: Placement): void;
211
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
245
212
  /**
246
- * Function to update a toast's options by id.
213
+ * Function to subscribe to the toast group.
247
214
  */
248
- update(id: string, options: Options): string | undefined;
215
+ subscribe(callback: (toasts: Service[]) => void): VoidFunction;
216
+ getGroupProps(options: GroupProps): T["element"];
217
+ };
218
+ type MachineApi<T extends PropTypes = PropTypes> = {
249
219
  /**
250
- * Function to create a loading toast.
220
+ * The type of the toast.
251
221
  */
252
- loading(options: Options): string | undefined;
222
+ type: Type;
253
223
  /**
254
- * Function to create a success toast.
224
+ * The title of the toast.
255
225
  */
256
- success(options: Options): string | undefined;
226
+ title: string | undefined;
257
227
  /**
258
- * Function to create an error toast.
228
+ * The description of the toast.
259
229
  */
260
- error(options: Options): string | undefined;
230
+ description: string | undefined;
261
231
  /**
262
- * Function to create a toast from a promise.
263
- * - When the promise resolves, the toast will be updated with the success options.
264
- * - When the promise rejects, the toast will be updated with the error options.
232
+ * The current placement of the toast.
265
233
  */
266
- promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
234
+ placement: Placement;
267
235
  /**
268
- * Function to pause a toast by id.
236
+ * Whether the toast is visible.
269
237
  */
270
- pause(id?: string): void;
238
+ isVisible: boolean;
271
239
  /**
272
- * Function to resume a toast by id.
240
+ * Whether the toast is paused.
273
241
  */
274
- resume(id?: string): void;
275
- getGroupProps(options: GroupProps): T["element"];
276
- subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
242
+ isPaused: boolean;
243
+ /**
244
+ * Whether the toast is in RTL mode.
245
+ */
246
+ isRtl: boolean;
247
+ /**
248
+ * Function to pause the toast (keeping it visible).
249
+ */
250
+ pause(): void;
251
+ /**
252
+ * Function to resume the toast dismissing.
253
+ */
254
+ resume(): void;
255
+ /**
256
+ * Function to instantly dismiss the toast.
257
+ */
258
+ dismiss(): void;
259
+ /**
260
+ * Function render the toast in the DOM (based on the defined `render` property)
261
+ */
262
+ render(): any;
263
+ rootProps: T["element"];
264
+ titleProps: T["element"];
265
+ descriptionProps: T["element"];
266
+ closeTriggerProps: T["button"];
277
267
  };
278
268
 
269
+ declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): GroupMachineApi<T>;
270
+
279
271
  declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
280
272
 
281
273
  declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
282
274
 
283
275
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
284
276
 
285
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
277
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
286
278
 
287
279
  declare const group: {
288
280
  connect: typeof groupConnect;
289
281
  machine: typeof groupMachine;
290
282
  };
291
- declare function api(): Toaster | undefined;
283
+ declare function api(): GroupMachineApi | undefined;
292
284
 
293
- export { GroupMachineContext, MachineContext, MachineState, Placement, PublicApi, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
285
+ export { MachineApi as Api, GroupMachineApi as GroupApi, GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
package/dist/index.js CHANGED
@@ -114,29 +114,14 @@ function getGroupPlacementStyle(ctx, placement) {
114
114
  var toaster = {};
115
115
  function groupConnect(state, send, normalize) {
116
116
  const group2 = {
117
- /**
118
- * The total number of toasts
119
- */
120
117
  count: state.context.count,
121
- /**
122
- * The active toasts
123
- */
124
118
  toasts: state.context.toasts,
125
- /**
126
- * The active toasts by placement
127
- */
128
119
  toastsByPlacement: getToastsByPlacement(state.context.toasts),
129
- /**
130
- * Returns whether the toast id is visible
131
- */
132
120
  isVisible(id) {
133
121
  if (!state.context.toasts.length)
134
122
  return false;
135
123
  return !!state.context.toasts.find((toast) => toast.id == id);
136
124
  },
137
- /**
138
- * Function to create a toast.
139
- */
140
125
  create(options) {
141
126
  const uid = `toast:${(0, import_utils.uuid)()}`;
142
127
  const id = options.id ? options.id : uid;
@@ -145,9 +130,6 @@ function groupConnect(state, send, normalize) {
145
130
  send({ type: "ADD_TOAST", toast: { ...options, id } });
146
131
  return id;
147
132
  },
148
- /**
149
- * Function to create or update a toast.
150
- */
151
133
  upsert(options) {
152
134
  const { id } = options;
153
135
  const isVisible = id ? group2.isVisible(id) : false;
@@ -157,10 +139,6 @@ function groupConnect(state, send, normalize) {
157
139
  return group2.create(options);
158
140
  }
159
141
  },
160
- /**
161
- * Function to dismiss a toast by id.
162
- * If no id is provided, all toasts will be dismissed.
163
- */
164
142
  dismiss(id) {
165
143
  if (id == null) {
166
144
  send("DISMISS_ALL");
@@ -168,10 +146,6 @@ function groupConnect(state, send, normalize) {
168
146
  send({ type: "DISMISS_TOAST", id });
169
147
  }
170
148
  },
171
- /**
172
- * Function to remove a toast by id.
173
- * If no id is provided, all toasts will be removed.
174
- */
175
149
  remove(id) {
176
150
  if (id == null) {
177
151
  send("REMOVE_ALL");
@@ -179,50 +153,30 @@ function groupConnect(state, send, normalize) {
179
153
  send({ type: "REMOVE_TOAST", id });
180
154
  }
181
155
  },
182
- /**
183
- * Function to dismiss all toasts by placement.
184
- */
185
156
  dismissByPlacement(placement) {
186
157
  const toasts = group2.toastsByPlacement[placement];
187
158
  if (toasts) {
188
159
  toasts.forEach((toast) => group2.dismiss(toast.id));
189
160
  }
190
161
  },
191
- /**
192
- * Function to update a toast's options by id.
193
- */
194
162
  update(id, options) {
195
163
  if (!group2.isVisible(id))
196
164
  return;
197
165
  send({ type: "UPDATE_TOAST", id, toast: options });
198
166
  return id;
199
167
  },
200
- /**
201
- * Function to create a loading toast.
202
- */
203
168
  loading(options) {
204
169
  options.type = "loading";
205
170
  return group2.upsert(options);
206
171
  },
207
- /**
208
- * Function to create a success toast.
209
- */
210
172
  success(options) {
211
173
  options.type = "success";
212
174
  return group2.upsert(options);
213
175
  },
214
- /**
215
- * Function to create an error toast.
216
- */
217
176
  error(options) {
218
177
  options.type = "error";
219
178
  return group2.upsert(options);
220
179
  },
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
- */
226
180
  promise(promise, options, shared = {}) {
227
181
  const id = group2.loading({ ...shared, ...options.loading });
228
182
  promise.then((response) => {
@@ -234,9 +188,6 @@ function groupConnect(state, send, normalize) {
234
188
  });
235
189
  return promise;
236
190
  },
237
- /**
238
- * Function to pause a toast by id.
239
- */
240
191
  pause(id) {
241
192
  if (id == null) {
242
193
  send("PAUSE_ALL");
@@ -244,9 +195,6 @@ function groupConnect(state, send, normalize) {
244
195
  send({ type: "PAUSE_TOAST", id });
245
196
  }
246
197
  },
247
- /**
248
- * Function to resume a toast by id.
249
- */
250
198
  resume(id) {
251
199
  if (id == null) {
252
200
  send("RESUME_ALL");