@zag-js/toast 0.82.1 → 1.0.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,23 +1,30 @@
1
- import { CommonProperties, Direction, RequiredBy, PropTypes, DirectionProperty, NormalizeProps } from '@zag-js/types';
2
1
  import * as _zag_js_core from '@zag-js/core';
3
- import { Machine, StateMachine } from '@zag-js/core';
2
+ import { Service, EventObject } from '@zag-js/core';
3
+ import { CommonProperties, Direction, DirectionProperty, Required, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
4
4
  import * as _zag_js_anatomy from '@zag-js/anatomy';
5
5
 
6
6
  type Type = "success" | "error" | "loading" | "info" | (string & {});
7
7
  type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
8
8
  type Status = "visible" | "dismissing" | "unmounted";
9
- interface GenericOptions<T = string> {
9
+ interface StatusChangeDetails {
10
10
  /**
11
- * The title of the toast.
11
+ * The status of the toast
12
12
  */
13
- title?: T | undefined;
13
+ status: Status;
14
14
  /**
15
- * The description of the toast.
15
+ * The reason for the status change
16
16
  */
17
- description?: T | undefined;
17
+ src?: string | undefined;
18
18
  }
19
- interface StatusChangeDetails {
20
- status: Status;
19
+ interface ToastHeight {
20
+ /**
21
+ * The id of the toast
22
+ */
23
+ id: string;
24
+ /**
25
+ * The height of the toast
26
+ */
27
+ height: number;
21
28
  }
22
29
  interface ActionOptions {
23
30
  /**
@@ -29,7 +36,15 @@ interface ActionOptions {
29
36
  */
30
37
  onClick: () => void;
31
38
  }
32
- interface Options<T> extends GenericOptions<T> {
39
+ interface Options<T = any> {
40
+ /**
41
+ * The title of the toast.
42
+ */
43
+ title?: T | undefined;
44
+ /**
45
+ * The description of the toast.
46
+ */
47
+ description?: T | undefined;
33
48
  /**
34
49
  * The duration the toast will be visible
35
50
  */
@@ -39,10 +54,6 @@ interface Options<T> extends GenericOptions<T> {
39
54
  * Useful for exit transitions.
40
55
  */
41
56
  removeDelay?: number | undefined;
42
- /**
43
- * The placement of the toast
44
- */
45
- placement?: Placement | undefined;
46
57
  /**
47
58
  * The unique id of the toast
48
59
  */
@@ -54,202 +65,249 @@ interface Options<T> extends GenericOptions<T> {
54
65
  /**
55
66
  * Function called when the toast is visible
56
67
  */
57
- onStatusChange?(details: StatusChangeDetails): void;
68
+ onStatusChange?: ((details: StatusChangeDetails) => void) | undefined;
58
69
  /**
59
70
  * The action of the toast
60
71
  */
61
72
  action?: ActionOptions | undefined;
73
+ /**
74
+ * Whether the toast is closable
75
+ */
76
+ closable?: boolean | undefined;
62
77
  /**
63
78
  * The metadata of the toast
64
79
  */
65
80
  meta?: Record<string, any> | undefined;
66
81
  }
67
- interface MachineContext<T = any> extends Omit<CommonProperties, "id">, MachinePrivateContext, Omit<Options<T>, "removeDelay"> {
82
+ interface ToastProps<T = any> extends Omit<CommonProperties, "id">, Options<T> {
68
83
  /**
69
- * The duration for the toast to kept alive before it is removed.
70
- * Useful for exit transitions.
84
+ * The direction of the toast
71
85
  */
72
- removeDelay: number;
86
+ dir?: Direction | undefined;
73
87
  /**
74
- * The document's text/writing direction.
88
+ * The gap of the toast
75
89
  */
76
- dir?: Direction | undefined;
90
+ gap?: number;
91
+ }
92
+ type ToastPropsWithDefault = "type" | "parent" | "duration" | "id";
93
+ type ToastSchema<O = any> = {
94
+ props: RequiredBy<ToastProps<O>, ToastPropsWithDefault>;
95
+ context: {
96
+ mounted: boolean;
97
+ initialHeight: number;
98
+ remainingTime: number;
99
+ };
100
+ computed: {
101
+ height: number;
102
+ heightIndex: number;
103
+ heightBefore: number;
104
+ frontmost: boolean;
105
+ zIndex: number;
106
+ shouldPersist: boolean;
107
+ };
108
+ refs: {
109
+ closeTimerStartTime: number;
110
+ lastCloseStartTimerStartTime: number;
111
+ };
112
+ state: "visible" | "visible:updating" | "dismissing" | "unmounted" | "visible:persist";
113
+ tag: "visible" | "paused" | "updating";
114
+ guard: string;
115
+ action: string;
116
+ effect: string;
117
+ event: EventObject;
118
+ };
119
+ type ToastService = Service<ToastSchema>;
120
+ interface ToastStoreProps {
77
121
  /**
78
- * The time the toast was created
122
+ * The placement of the toast
123
+ * @default "bottom"
79
124
  */
80
- createdAt: number;
125
+ placement?: Placement | undefined;
81
126
  /**
82
- * The time left before the toast is removed
127
+ * The maximum number of toasts
128
+ * @default 24
83
129
  */
84
- remaining: number;
85
- }
86
- interface MachinePrivateContext {
87
- }
88
- interface MachineState {
89
- value: "visible" | "visible:updating" | "dismissing" | "unmounted" | "visible:persist";
90
- tags: "visible" | "paused" | "updating";
91
- }
92
- type State<T = any> = StateMachine.State<MachineContext<T>, MachineState>;
93
- type Send = StateMachine.Send;
94
- type Service<T = any> = Machine<MachineContext<T>, MachineState>;
95
- interface GroupPublicContext extends DirectionProperty, CommonProperties {
130
+ max?: number | undefined;
96
131
  /**
97
- * Whether to pause toast when the user leaves the browser tab
98
- * @default false
132
+ * Whether to overlap the toasts
99
133
  */
100
- pauseOnPageIdle: boolean;
134
+ overlap?: boolean | undefined;
101
135
  /**
102
- * The gap or spacing between toasts
103
- * @default 16
136
+ * The duration of the toast.
137
+ * By default, it is determined by the type of the toast.
104
138
  */
105
- gap: number;
139
+ duration?: number | undefined;
106
140
  /**
107
- * The maximum number of toasts that can be shown at once
108
- * @default Number.MAX_SAFE_INTEGER
141
+ * The gap between the toasts
142
+ * @default 16
109
143
  */
110
- max: number;
144
+ gap?: number | undefined;
111
145
  /**
112
146
  * The offset from the safe environment edge of the viewport
113
147
  * @default "1rem"
114
148
  */
115
- offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
149
+ offsets?: string | Record<"left" | "right" | "bottom" | "top", string> | undefined;
116
150
  /**
117
151
  * The hotkey that will move focus to the toast group
118
152
  * @default '["altKey", "KeyT"]'
119
153
  */
120
- hotkey: string[];
121
- /**
122
- * Whether the toasts should overlap each other
123
- */
124
- overlap?: boolean | undefined;
125
- /**
126
- * The placement of the toast
127
- */
128
- placement: Placement;
154
+ hotkey?: string[] | undefined;
129
155
  /**
130
156
  * The duration for the toast to kept alive before it is removed.
131
157
  * Useful for exit transitions.
132
158
  *
133
159
  * @default 200
134
160
  */
135
- removeDelay: number;
161
+ removeDelay?: number | undefined;
136
162
  /**
137
- * The duration the toast will be visible
163
+ * Whether to pause toast when the user leaves the browser tab
164
+ * @default false
138
165
  */
139
- duration?: number | undefined;
140
- }
141
- interface UserDefinedGroupContext extends RequiredBy<GroupPublicContext, "id"> {
166
+ pauseOnPageIdle?: boolean | undefined;
142
167
  }
143
- type GroupComputedContext = Readonly<{
168
+ interface ToastGroupProps extends DirectionProperty, CommonProperties {
144
169
  /**
145
- * @computed
146
- * The total number of toasts in the group
170
+ * The store of the toast
147
171
  */
148
- count: number;
149
- }>;
150
- interface GroupPrivateContext<T> extends GenericOptions<T> {
151
- }
152
- interface GroupMachineContext<T = any> extends GroupPublicContext, GroupPrivateContext<T>, GroupComputedContext {
153
- }
154
- interface GroupMachineState {
155
- value: "stack" | "overlap";
156
- }
157
- type GroupState<T = any> = StateMachine.State<GroupMachineContext<T>>;
158
- type GroupSend = StateMachine.Send;
159
- type GroupService<T = any> = Machine<GroupMachineContext<T>, GroupMachineState>;
160
- type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
161
- interface PromiseOptions<V, O = any> {
162
- loading: Options<O>;
163
- success: MaybeFunction<Options<O>, V>;
164
- error: MaybeFunction<Options<O>, Error>;
165
- finally?: (() => void | Promise<void>) | undefined;
172
+ store: ToastStore;
166
173
  }
167
- interface GroupProps {
174
+ type ToastGroupSchema = {
175
+ state: "stack" | "overlap";
176
+ props: ToastGroupProps;
177
+ context: {
178
+ toasts: RequiredBy<ToastProps, ToastPropsWithDefault>[];
179
+ heights: ToastHeight[];
180
+ };
181
+ computed: {
182
+ count: number;
183
+ overlap: boolean;
184
+ placement: Placement;
185
+ };
186
+ refs: {
187
+ dismissableCleanup?: VoidFunction | undefined;
188
+ lastFocusedEl: HTMLElement | null;
189
+ isFocusWithin: boolean;
190
+ };
191
+ guard: string;
192
+ effect: string;
193
+ action: string;
194
+ event: EventObject;
195
+ };
196
+ type ToastGroupService = Service<ToastGroupSchema>;
197
+ interface ToastStore<V = any> {
168
198
  /**
169
- * The placement of the toast region
199
+ * The attributes of the toast store
170
200
  */
171
- placement: Placement;
201
+ attrs: Required<ToastStoreProps>;
172
202
  /**
173
- * The human-readable label for the toast region
203
+ * Subscribe to the toast store
174
204
  */
175
- label?: string | undefined;
176
- }
177
- interface GroupMachineApi<T extends PropTypes = PropTypes, O = any> {
205
+ subscribe: (subscriber: (...args: any[]) => void) => VoidFunction;
178
206
  /**
179
- * The total number of toasts
207
+ * Create a new toast with the given options
180
208
  */
181
- getCount(): number;
209
+ create: (data: Options<V>) => string;
182
210
  /**
183
- * The placements of the active toasts
211
+ * Update an existing toast with new properties
184
212
  */
185
- getPlacements(): Placement[];
213
+ update: (id: string, data: Partial<ToastProps<V>>) => string;
186
214
  /**
187
- * The active toasts by placement
215
+ * Remove a toast by its ID
188
216
  */
189
- getToastsByPlacement(placement: Placement): Service<O>[];
217
+ remove: (id: string) => void;
190
218
  /**
191
- * Returns whether the toast id is visible
219
+ * Dismiss a toast by its ID. If no ID is provided, dismisses all toasts
192
220
  */
193
- isVisible(id: string): boolean;
221
+ dismiss: (id?: string) => void;
194
222
  /**
195
- * Function to create a toast.
223
+ * Create an error toast with the given options
196
224
  */
197
- create(options: Options<O>): string | undefined;
225
+ error: (data: Options<V>) => void;
198
226
  /**
199
- * Function to create or update a toast.
227
+ * Create a success toast with the given options
200
228
  */
201
- upsert(options: Options<O>): string | undefined;
229
+ success: (data: Options<V>) => void;
202
230
  /**
203
- * Function to update a toast's options by id.
231
+ * Create an info toast with the given options
204
232
  */
205
- update(id: string, options: Options<O>): void;
233
+ info: (data: Options<V>) => void;
206
234
  /**
207
- * Function to create a success toast.
235
+ * Create a warning toast with the given options
208
236
  */
209
- success(options: Options<O>): string | undefined;
237
+ warning: (data: Options<V>) => void;
210
238
  /**
211
- * Function to create an error toast.
239
+ * Create a loading toast with the given options
212
240
  */
213
- error(options: Options<O>): string | undefined;
241
+ loading: (data: Options<V>) => void;
214
242
  /**
215
- * Function to create a loading toast.
243
+ * Get all currently visible toasts
216
244
  */
217
- loading(options: Options<O>): string | undefined;
245
+ getVisibleToasts: () => Partial<ToastProps<V>>[];
218
246
  /**
219
- * Function to resume a toast by id.
247
+ * Get the total number of toasts
220
248
  */
221
- resume(id?: string | undefined): void;
249
+ getCount: () => number;
222
250
  /**
223
- * Function to pause a toast by id.
251
+ * Create a toast that tracks a promise's state
224
252
  */
225
- pause(id?: string | undefined): void;
253
+ promise: <T>(promise: Promise<T> | (() => Promise<T>), options: PromiseOptions<T, V>, shared?: Omit<Options<V>, "type">) => {
254
+ id: string | undefined;
255
+ unwrap: () => Promise<T>;
256
+ } | undefined;
226
257
  /**
227
- * Function to dismiss a toast by id.
228
- * If no id is provided, all toasts will be dismissed.
258
+ * Pause a toast's auto-dismiss timer. If no ID is provided, pauses all toasts
229
259
  */
230
- dismiss(id?: string | undefined): void;
260
+ pause: (id?: string) => void;
231
261
  /**
232
- * Function to dismiss all toasts by placement.
262
+ * Resume a toast's auto-dismiss timer. If no ID is provided, resumes all toasts
233
263
  */
234
- dismissByPlacement(placement: Placement): void;
264
+ resume: (id?: string) => void;
235
265
  /**
236
- * Function to remove a toast by id.
237
- * If no id is provided, all toasts will be removed.
266
+ * Check if a toast with the given ID is currently visible
238
267
  */
239
- remove(id?: string | undefined): void;
268
+ isVisible: (id: string) => boolean;
240
269
  /**
241
- * Function to create a toast from a promise.
242
- * - When the promise resolves, the toast will be updated with the success options.
243
- * - When the promise rejects, the toast will be updated with the error options.
270
+ * Check if a toast with the given ID has been dismissed
244
271
  */
245
- promise<T>(promise: Promise<T> | (() => Promise<T>), options: PromiseOptions<T, O>, shared?: Partial<Options<O>>): string;
272
+ isDismissed: (id: string) => boolean;
273
+ }
274
+ type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
275
+ interface PromiseOptions<V, O = any> {
276
+ loading?: Omit<Options<O>, "type">;
277
+ success?: MaybeFunction<Omit<Options<O>, "type">, V>;
278
+ error?: MaybeFunction<Omit<Options<O>, "type">, unknown>;
279
+ finally?: (() => void | Promise<void>) | undefined;
280
+ }
281
+ interface GroupProps {
282
+ /**
283
+ * The human-readable label for the toast region
284
+ */
285
+ label?: string | undefined;
286
+ }
287
+ interface ToastGroupApi<T extends PropTypes = PropTypes, O = any> {
246
288
  /**
247
- * Function to subscribe to the toast group.
289
+ * The total number of toasts
290
+ */
291
+ getCount(): number;
292
+ /**
293
+ * The toasts
294
+ */
295
+ getToasts(): ToastProps[];
296
+ /**
297
+ * Subscribe to the toast group
248
298
  */
249
299
  subscribe(callback: (toasts: Options<O>[]) => void): VoidFunction;
250
- getGroupProps(options: GroupProps): T["element"];
300
+ getGroupProps(options?: GroupProps): T["element"];
251
301
  }
252
- interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOptions<O> {
302
+ interface ToastApi<T extends PropTypes = PropTypes, O = any> {
303
+ /**
304
+ * The title of the toast.
305
+ */
306
+ title: O;
307
+ /**
308
+ * The description of the toast.
309
+ */
310
+ description: O;
253
311
  /**
254
312
  * The type of the toast.
255
313
  */
@@ -262,6 +320,10 @@ interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOp
262
320
  * Whether the toast is visible.
263
321
  */
264
322
  visible: boolean;
323
+ /**
324
+ * Whether the toast should render a close button
325
+ */
326
+ closable: boolean;
265
327
  /**
266
328
  * Whether the toast is paused.
267
329
  */
@@ -287,19 +349,19 @@ interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOp
287
349
  getActionTriggerProps(): T["button"];
288
350
  }
289
351
 
290
- declare function groupConnect<T extends PropTypes, O = any>(serviceOrState: GroupState<O> | GroupService<O>, send: GroupSend, normalize: NormalizeProps<T>): GroupMachineApi<T, O>;
352
+ declare function groupConnect<T extends PropTypes, O = any>(service: Service<ToastGroupSchema>, normalize: NormalizeProps<T>): ToastGroupApi<T, O>;
291
353
 
292
- declare function groupMachine<T = any>(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext<T>, GroupMachineState, _zag_js_core.StateMachine.AnyEventObject>;
354
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger">;
293
355
 
294
- declare function createToastMachine<T>(options: Options<T>): _zag_js_core.Machine<MachineContext<T>, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
356
+ declare function connect<T extends PropTypes, O>(service: Service<ToastSchema<O>>, normalize: NormalizeProps<T>): ToastApi<T, O>;
295
357
 
296
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger">;
358
+ declare const machine: _zag_js_core.MachineConfig<ToastSchema>;
297
359
 
298
- declare function connect<T extends PropTypes, O>(state: State<O>, send: Send, normalize: NormalizeProps<T>): MachineApi<T, O>;
360
+ declare function createToastStore<V = any>(props: ToastStoreProps): ToastStore<V>;
299
361
 
300
362
  declare const group: {
301
363
  connect: typeof groupConnect;
302
- machine: typeof groupMachine;
364
+ machine: _zag_js_core.MachineConfig<ToastGroupSchema>;
303
365
  };
304
366
 
305
- export { type ActionOptions, type MachineApi as Api, type GenericOptions, type GroupMachineApi as GroupApi, type UserDefinedGroupContext as GroupMachineContext, type GroupProps, type GroupService, type GroupState, type MachineContext, type Options, type Placement, type PromiseOptions, type Service, type Status, type StatusChangeDetails, type Type, anatomy, connect, createToastMachine as createMachine, group };
367
+ export { type ActionOptions, type ToastApi as Api, type ToastGroupApi as GroupApi, type ToastGroupProps as GroupProps, type ToastGroupService as GroupService, type Options, type Placement, type PromiseOptions, type ToastProps as Props, type ToastService as Service, type Status, type StatusChangeDetails, type ToastStore as Store, type ToastStoreProps as StoreProps, type Type, anatomy, connect, createToastStore as createStore, group, machine };