@zag-js/toast 0.82.2 → 1.0.1

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.ts 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, Machine, 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,251 @@ 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
+ type ToastMachine = Machine<ToastSchema>;
121
+ interface ToastStoreProps {
77
122
  /**
78
- * The time the toast was created
123
+ * The placement of the toast
124
+ * @default "bottom"
79
125
  */
80
- createdAt: number;
126
+ placement?: Placement | undefined;
81
127
  /**
82
- * The time left before the toast is removed
128
+ * The maximum number of toasts
129
+ * @default 24
83
130
  */
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 {
131
+ max?: number | undefined;
96
132
  /**
97
- * Whether to pause toast when the user leaves the browser tab
98
- * @default false
133
+ * Whether to overlap the toasts
99
134
  */
100
- pauseOnPageIdle: boolean;
135
+ overlap?: boolean | undefined;
101
136
  /**
102
- * The gap or spacing between toasts
103
- * @default 16
137
+ * The duration of the toast.
138
+ * By default, it is determined by the type of the toast.
104
139
  */
105
- gap: number;
140
+ duration?: number | undefined;
106
141
  /**
107
- * The maximum number of toasts that can be shown at once
108
- * @default Number.MAX_SAFE_INTEGER
142
+ * The gap between the toasts
143
+ * @default 16
109
144
  */
110
- max: number;
145
+ gap?: number | undefined;
111
146
  /**
112
147
  * The offset from the safe environment edge of the viewport
113
148
  * @default "1rem"
114
149
  */
115
- offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
150
+ offsets?: string | Record<"left" | "right" | "bottom" | "top", string> | undefined;
116
151
  /**
117
152
  * The hotkey that will move focus to the toast group
118
153
  * @default '["altKey", "KeyT"]'
119
154
  */
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;
155
+ hotkey?: string[] | undefined;
129
156
  /**
130
157
  * The duration for the toast to kept alive before it is removed.
131
158
  * Useful for exit transitions.
132
159
  *
133
160
  * @default 200
134
161
  */
135
- removeDelay: number;
162
+ removeDelay?: number | undefined;
136
163
  /**
137
- * The duration the toast will be visible
164
+ * Whether to pause toast when the user leaves the browser tab
165
+ * @default false
138
166
  */
139
- duration?: number | undefined;
140
- }
141
- interface UserDefinedGroupContext extends RequiredBy<GroupPublicContext, "id"> {
167
+ pauseOnPageIdle?: boolean | undefined;
142
168
  }
143
- type GroupComputedContext = Readonly<{
169
+ interface ToastGroupProps extends DirectionProperty, CommonProperties {
144
170
  /**
145
- * @computed
146
- * The total number of toasts in the group
171
+ * The store of the toast
147
172
  */
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;
173
+ store: ToastStore;
166
174
  }
167
- interface GroupProps {
175
+ type ToastGroupSchema = {
176
+ state: "stack" | "overlap";
177
+ props: ToastGroupProps;
178
+ context: {
179
+ toasts: RequiredBy<ToastProps, ToastPropsWithDefault>[];
180
+ heights: ToastHeight[];
181
+ };
182
+ computed: {
183
+ count: number;
184
+ overlap: boolean;
185
+ placement: Placement;
186
+ };
187
+ refs: {
188
+ dismissableCleanup?: VoidFunction | undefined;
189
+ lastFocusedEl: HTMLElement | null;
190
+ isFocusWithin: boolean;
191
+ };
192
+ guard: string;
193
+ effect: string;
194
+ action: string;
195
+ event: EventObject;
196
+ };
197
+ type ToastGroupService = Service<ToastGroupSchema>;
198
+ type ToastGroupMachine = Machine<ToastGroupSchema>;
199
+ interface ToastStore<V = any> {
168
200
  /**
169
- * The placement of the toast region
201
+ * The attributes of the toast store
170
202
  */
171
- placement: Placement;
203
+ attrs: Required<ToastStoreProps>;
172
204
  /**
173
- * The human-readable label for the toast region
205
+ * Subscribe to the toast store
174
206
  */
175
- label?: string | undefined;
176
- }
177
- interface GroupMachineApi<T extends PropTypes = PropTypes, O = any> {
207
+ subscribe: (subscriber: (...args: any[]) => void) => VoidFunction;
178
208
  /**
179
- * The total number of toasts
209
+ * Create a new toast with the given options
180
210
  */
181
- getCount(): number;
211
+ create: (data: Options<V>) => string;
182
212
  /**
183
- * The placements of the active toasts
213
+ * Update an existing toast with new properties
184
214
  */
185
- getPlacements(): Placement[];
215
+ update: (id: string, data: Partial<ToastProps<V>>) => string;
186
216
  /**
187
- * The active toasts by placement
217
+ * Remove a toast by its ID
188
218
  */
189
- getToastsByPlacement(placement: Placement): Service<O>[];
219
+ remove: (id: string) => void;
190
220
  /**
191
- * Returns whether the toast id is visible
221
+ * Dismiss a toast by its ID. If no ID is provided, dismisses all toasts
192
222
  */
193
- isVisible(id: string): boolean;
223
+ dismiss: (id?: string) => void;
194
224
  /**
195
- * Function to create a toast.
225
+ * Create an error toast with the given options
196
226
  */
197
- create(options: Options<O>): string | undefined;
227
+ error: (data: Options<V>) => void;
198
228
  /**
199
- * Function to create or update a toast.
229
+ * Create a success toast with the given options
200
230
  */
201
- upsert(options: Options<O>): string | undefined;
231
+ success: (data: Options<V>) => void;
202
232
  /**
203
- * Function to update a toast's options by id.
233
+ * Create an info toast with the given options
204
234
  */
205
- update(id: string, options: Options<O>): void;
235
+ info: (data: Options<V>) => void;
206
236
  /**
207
- * Function to create a success toast.
237
+ * Create a warning toast with the given options
208
238
  */
209
- success(options: Options<O>): string | undefined;
239
+ warning: (data: Options<V>) => void;
210
240
  /**
211
- * Function to create an error toast.
241
+ * Create a loading toast with the given options
212
242
  */
213
- error(options: Options<O>): string | undefined;
243
+ loading: (data: Options<V>) => void;
214
244
  /**
215
- * Function to create a loading toast.
245
+ * Get all currently visible toasts
216
246
  */
217
- loading(options: Options<O>): string | undefined;
247
+ getVisibleToasts: () => Partial<ToastProps<V>>[];
218
248
  /**
219
- * Function to resume a toast by id.
249
+ * Get the total number of toasts
220
250
  */
221
- resume(id?: string | undefined): void;
251
+ getCount: () => number;
222
252
  /**
223
- * Function to pause a toast by id.
253
+ * Create a toast that tracks a promise's state
224
254
  */
225
- pause(id?: string | undefined): void;
255
+ promise: <T>(promise: Promise<T> | (() => Promise<T>), options: PromiseOptions<T, V>, shared?: Omit<Options<V>, "type">) => {
256
+ id: string | undefined;
257
+ unwrap: () => Promise<T>;
258
+ } | undefined;
226
259
  /**
227
- * Function to dismiss a toast by id.
228
- * If no id is provided, all toasts will be dismissed.
260
+ * Pause a toast's auto-dismiss timer. If no ID is provided, pauses all toasts
229
261
  */
230
- dismiss(id?: string | undefined): void;
262
+ pause: (id?: string) => void;
231
263
  /**
232
- * Function to dismiss all toasts by placement.
264
+ * Resume a toast's auto-dismiss timer. If no ID is provided, resumes all toasts
233
265
  */
234
- dismissByPlacement(placement: Placement): void;
266
+ resume: (id?: string) => void;
235
267
  /**
236
- * Function to remove a toast by id.
237
- * If no id is provided, all toasts will be removed.
268
+ * Check if a toast with the given ID is currently visible
238
269
  */
239
- remove(id?: string | undefined): void;
270
+ isVisible: (id: string) => boolean;
240
271
  /**
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.
272
+ * Check if a toast with the given ID has been dismissed
244
273
  */
245
- promise<T>(promise: Promise<T> | (() => Promise<T>), options: PromiseOptions<T, O>, shared?: Partial<Options<O>>): string;
274
+ isDismissed: (id: string) => boolean;
275
+ }
276
+ type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
277
+ interface PromiseOptions<V, O = any> {
278
+ loading?: Omit<Options<O>, "type">;
279
+ success?: MaybeFunction<Omit<Options<O>, "type">, V>;
280
+ error?: MaybeFunction<Omit<Options<O>, "type">, unknown>;
281
+ finally?: (() => void | Promise<void>) | undefined;
282
+ }
283
+ interface GroupProps {
284
+ /**
285
+ * The human-readable label for the toast region
286
+ */
287
+ label?: string | undefined;
288
+ }
289
+ interface ToastGroupApi<T extends PropTypes = PropTypes, O = any> {
246
290
  /**
247
- * Function to subscribe to the toast group.
291
+ * The total number of toasts
292
+ */
293
+ getCount(): number;
294
+ /**
295
+ * The toasts
296
+ */
297
+ getToasts(): ToastProps[];
298
+ /**
299
+ * Subscribe to the toast group
248
300
  */
249
301
  subscribe(callback: (toasts: Options<O>[]) => void): VoidFunction;
250
- getGroupProps(options: GroupProps): T["element"];
302
+ getGroupProps(options?: GroupProps): T["element"];
251
303
  }
252
- interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOptions<O> {
304
+ interface ToastApi<T extends PropTypes = PropTypes, O = any> {
305
+ /**
306
+ * The title of the toast.
307
+ */
308
+ title: O;
309
+ /**
310
+ * The description of the toast.
311
+ */
312
+ description: O;
253
313
  /**
254
314
  * The type of the toast.
255
315
  */
@@ -262,6 +322,10 @@ interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOp
262
322
  * Whether the toast is visible.
263
323
  */
264
324
  visible: boolean;
325
+ /**
326
+ * Whether the toast should render a close button
327
+ */
328
+ closable: boolean;
265
329
  /**
266
330
  * Whether the toast is paused.
267
331
  */
@@ -287,19 +351,19 @@ interface MachineApi<T extends PropTypes = PropTypes, O = any> extends GenericOp
287
351
  getActionTriggerProps(): T["button"];
288
352
  }
289
353
 
290
- declare function groupConnect<T extends PropTypes, O = any>(serviceOrState: GroupState<O> | GroupService<O>, send: GroupSend, normalize: NormalizeProps<T>): GroupMachineApi<T, O>;
354
+ declare function groupConnect<T extends PropTypes, O = any>(service: Service<ToastGroupSchema>, normalize: NormalizeProps<T>): ToastGroupApi<T, O>;
291
355
 
292
- declare function groupMachine<T = any>(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext<T>, GroupMachineState, _zag_js_core.StateMachine.AnyEventObject>;
356
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "description" | "group" | "root" | "actionTrigger" | "closeTrigger">;
293
357
 
294
- declare function createToastMachine<T>(options: Options<T>): _zag_js_core.Machine<MachineContext<T>, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
358
+ declare function connect<T extends PropTypes, O>(service: Service<ToastSchema<O>>, normalize: NormalizeProps<T>): ToastApi<T, O>;
295
359
 
296
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"group" | "title" | "root" | "description" | "actionTrigger" | "closeTrigger">;
360
+ declare const machine: _zag_js_core.Machine<ToastSchema>;
297
361
 
298
- declare function connect<T extends PropTypes, O>(state: State<O>, send: Send, normalize: NormalizeProps<T>): MachineApi<T, O>;
362
+ declare function createToastStore<V = any>(props: ToastStoreProps): ToastStore<V>;
299
363
 
300
364
  declare const group: {
301
365
  connect: typeof groupConnect;
302
- machine: typeof groupMachine;
366
+ machine: _zag_js_core.Machine<ToastGroupSchema>;
303
367
  };
304
368
 
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 };
369
+ export { type ActionOptions, type ToastApi as Api, type ToastGroupApi as GroupApi, type ToastGroupMachine as GroupMachine, type ToastGroupProps as GroupProps, type ToastGroupService as GroupService, type ToastMachine as Machine, 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 };