@zag-js/toast 0.10.4 → 0.11.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.
@@ -0,0 +1,288 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { Machine, StateMachine } from '@zag-js/core';
3
+ import { RootProperties, CommonProperties, Direction, DirectionProperty, Context, RequiredBy, PropTypes, NormalizeProps } from '@zag-js/types';
4
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
5
+
6
+ type Type = "success" | "error" | "loading" | "info" | "custom";
7
+ type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
8
+ type SharedContext = {
9
+ /**
10
+ * Whether to pause toast when the user leaves the browser tab
11
+ */
12
+ pauseOnPageIdle?: boolean;
13
+ /**
14
+ * Whether to pause the toast when interacted with
15
+ */
16
+ pauseOnInteraction?: boolean;
17
+ };
18
+ type ToastOptions = {
19
+ /**
20
+ * The unique id of the toast
21
+ */
22
+ id: string;
23
+ /**
24
+ * The type of the toast
25
+ */
26
+ type: Type;
27
+ /**
28
+ * The placement of the toast
29
+ */
30
+ placement: Placement;
31
+ /**
32
+ * The message of the toast
33
+ */
34
+ title?: string;
35
+ /**
36
+ * The description of the toast
37
+ */
38
+ description?: string;
39
+ /**
40
+ * The duration the toast will be visible
41
+ */
42
+ duration: number;
43
+ /**
44
+ * Custom function to render the toast element.
45
+ */
46
+ render?: (options: RenderOptions) => any;
47
+ /**
48
+ * The duration for the toast to kept alive before it is removed.
49
+ * Useful for exit transitions.
50
+ */
51
+ removeDelay?: number;
52
+ /**
53
+ * Function called when the toast has been closed and removed
54
+ */
55
+ onClose?: VoidFunction;
56
+ /**
57
+ * Function called when the toast is leaving
58
+ */
59
+ onClosing?: VoidFunction;
60
+ /**
61
+ * Function called when the toast is shown
62
+ */
63
+ onOpen?: VoidFunction;
64
+ /**
65
+ * Function called when the toast is updated
66
+ */
67
+ onUpdate?: VoidFunction;
68
+ };
69
+ type Options = Partial<ToastOptions>;
70
+ type RenderOptions = Omit<ToastOptions, "render"> & {
71
+ dismiss(): void;
72
+ };
73
+ type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
74
+ /**
75
+ * The duration for the toast to kept alive before it is removed.
76
+ * Useful for exit transitions.
77
+ */
78
+ removeDelay: number;
79
+ /**
80
+ * The document's text/writing direction.
81
+ */
82
+ dir?: Direction;
83
+ /**
84
+ * The time the toast was created
85
+ */
86
+ createdAt: number;
87
+ /**
88
+ * The time left before the toast is removed
89
+ */
90
+ remaining: number;
91
+ };
92
+ type MachineState = {
93
+ value: "active" | "active:temp" | "dismissing" | "inactive" | "persist";
94
+ tags: "visible" | "paused" | "updating";
95
+ };
96
+ type State = StateMachine.State<MachineContext, MachineState>;
97
+ type Send = StateMachine.Send;
98
+ type Service = Machine<MachineContext, MachineState>;
99
+ type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties & {
100
+ /**
101
+ * The gutter or spacing between toasts
102
+ */
103
+ gutter: string;
104
+ /**
105
+ * The z-index applied to each toast group
106
+ */
107
+ zIndex: number;
108
+ /**
109
+ * The maximum number of toasts that can be shown at once
110
+ */
111
+ max: number;
112
+ /**
113
+ * The offset from the safe environment edge of the viewport
114
+ */
115
+ offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
116
+ };
117
+ type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
118
+ type GroupComputedContext = Readonly<{
119
+ /**
120
+ * @computed
121
+ * The total number of toasts in the group
122
+ */
123
+ readonly count: number;
124
+ }>;
125
+ type GroupPrivateContext = Context<{}>;
126
+ type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
127
+ type GroupState = StateMachine.State<GroupMachineContext>;
128
+ type GroupSend = (event: StateMachine.Event<StateMachine.AnyEventObject>) => void;
129
+ type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
130
+ type PromiseOptions<Value> = {
131
+ loading: ToastOptions;
132
+ success: MaybeFunction<ToastOptions, Value>;
133
+ error: MaybeFunction<ToastOptions, Error>;
134
+ };
135
+ type GroupProps = {
136
+ placement: Placement;
137
+ label?: string;
138
+ };
139
+ type Toaster = {
140
+ count: number;
141
+ isVisible(id: string): boolean;
142
+ upsert(options: ToastOptions): string | undefined;
143
+ create(options: ToastOptions): string | undefined;
144
+ success(options: ToastOptions): string | undefined;
145
+ error(options: ToastOptions): string | undefined;
146
+ loading(options: ToastOptions): string | undefined;
147
+ dismiss(id?: string | undefined): void;
148
+ remove(id?: string | undefined): void;
149
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
150
+ };
151
+
152
+ declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
153
+ /**
154
+ * The total number of toasts
155
+ */
156
+ count: number;
157
+ /**
158
+ * The active toasts
159
+ */
160
+ toasts: Service[];
161
+ /**
162
+ * The active toasts by placement
163
+ */
164
+ toastsByPlacement: Partial<Record<Placement, Service[]>>;
165
+ /**
166
+ * Returns whether the toast id is visible
167
+ */
168
+ isVisible(id: string): boolean;
169
+ /**
170
+ * Function to create a toast.
171
+ */
172
+ create(options: Options): string | undefined;
173
+ /**
174
+ * Function to create or update a toast.
175
+ */
176
+ upsert(options: Options): string | undefined;
177
+ /**
178
+ * Function to dismiss a toast by id.
179
+ * If no id is provided, all toasts will be dismissed.
180
+ */
181
+ dismiss(id?: string): void;
182
+ /**
183
+ * Function to remove a toast by id.
184
+ * If no id is provided, all toasts will be removed.
185
+ */
186
+ remove(id?: string): void;
187
+ /**
188
+ * Function to dismiss all toasts by placement.
189
+ */
190
+ dismissByPlacement(placement: Placement): void;
191
+ /**
192
+ * Function to update a toast's options by id.
193
+ */
194
+ update(id: string, options: Options): string | undefined;
195
+ /**
196
+ * Function to create a loading toast.
197
+ */
198
+ loading(options: Options): string | undefined;
199
+ /**
200
+ * Function to create a success toast.
201
+ */
202
+ success(options: Options): string | undefined;
203
+ /**
204
+ * Function to create an error toast.
205
+ */
206
+ error(options: Options): string | undefined;
207
+ /**
208
+ * Function to create a toast from a promise.
209
+ * - When the promise resolves, the toast will be updated with the success options.
210
+ * - When the promise rejects, the toast will be updated with the error options.
211
+ */
212
+ promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
213
+ /**
214
+ * Function to pause a toast by id.
215
+ */
216
+ pause(id?: string): void;
217
+ /**
218
+ * Function to resume a toast by id.
219
+ */
220
+ resume(id?: string): void;
221
+ getGroupProps(options: GroupProps): T["element"];
222
+ subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
223
+ };
224
+
225
+ declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
226
+
227
+ declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
228
+
229
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
230
+
231
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
232
+ /**
233
+ * The type of the toast.
234
+ */
235
+ type: Type;
236
+ /**
237
+ * The title of the toast.
238
+ */
239
+ title: string | undefined;
240
+ /**
241
+ * The description of the toast.
242
+ */
243
+ description: string | undefined;
244
+ /**
245
+ * The current placement of the toast.
246
+ */
247
+ placement: Placement;
248
+ /**
249
+ * Whether the toast is visible.
250
+ */
251
+ isVisible: boolean;
252
+ /**
253
+ * Whether the toast is paused.
254
+ */
255
+ isPaused: boolean;
256
+ /**
257
+ * Whether the toast is in RTL mode.
258
+ */
259
+ isRtl: boolean;
260
+ /**
261
+ * Function to pause the toast (keeping it visible).
262
+ */
263
+ pause(): void;
264
+ /**
265
+ * Function to resume the toast dismissing.
266
+ */
267
+ resume(): void;
268
+ /**
269
+ * Function to instantly dismiss the toast.
270
+ */
271
+ dismiss(): void;
272
+ /**
273
+ * Function render the toast in the DOM (based on the defined `render` property)
274
+ */
275
+ render(): any;
276
+ rootProps: T["element"];
277
+ titleProps: T["element"];
278
+ descriptionProps: T["element"];
279
+ closeTriggerProps: T["button"];
280
+ };
281
+
282
+ declare const group: {
283
+ connect: typeof groupConnect;
284
+ machine: typeof groupMachine;
285
+ };
286
+ declare function api(): Toaster | undefined;
287
+
288
+ export { GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,288 @@
1
- import { Toaster } from './toast.types';
2
- import { groupConnect } from "./toast-group.connect";
3
- import { groupMachine } from "./toast-group.machine";
4
- import { createToastMachine as createMachine } from "./toast.machine";
5
- export { anatomy } from "./toast.anatomy";
6
- export { connect } from "./toast.connect";
7
- export type { GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, } from "./toast.types";
8
- export { createMachine };
9
- export declare const group: {
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { Machine, StateMachine } from '@zag-js/core';
3
+ import { RootProperties, CommonProperties, Direction, DirectionProperty, Context, RequiredBy, PropTypes, NormalizeProps } from '@zag-js/types';
4
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
5
+
6
+ type Type = "success" | "error" | "loading" | "info" | "custom";
7
+ type Placement = "top-start" | "top" | "top-end" | "bottom-start" | "bottom" | "bottom-end";
8
+ type SharedContext = {
9
+ /**
10
+ * Whether to pause toast when the user leaves the browser tab
11
+ */
12
+ pauseOnPageIdle?: boolean;
13
+ /**
14
+ * Whether to pause the toast when interacted with
15
+ */
16
+ pauseOnInteraction?: boolean;
17
+ };
18
+ type ToastOptions = {
19
+ /**
20
+ * The unique id of the toast
21
+ */
22
+ id: string;
23
+ /**
24
+ * The type of the toast
25
+ */
26
+ type: Type;
27
+ /**
28
+ * The placement of the toast
29
+ */
30
+ placement: Placement;
31
+ /**
32
+ * The message of the toast
33
+ */
34
+ title?: string;
35
+ /**
36
+ * The description of the toast
37
+ */
38
+ description?: string;
39
+ /**
40
+ * The duration the toast will be visible
41
+ */
42
+ duration: number;
43
+ /**
44
+ * Custom function to render the toast element.
45
+ */
46
+ render?: (options: RenderOptions) => any;
47
+ /**
48
+ * The duration for the toast to kept alive before it is removed.
49
+ * Useful for exit transitions.
50
+ */
51
+ removeDelay?: number;
52
+ /**
53
+ * Function called when the toast has been closed and removed
54
+ */
55
+ onClose?: VoidFunction;
56
+ /**
57
+ * Function called when the toast is leaving
58
+ */
59
+ onClosing?: VoidFunction;
60
+ /**
61
+ * Function called when the toast is shown
62
+ */
63
+ onOpen?: VoidFunction;
64
+ /**
65
+ * Function called when the toast is updated
66
+ */
67
+ onUpdate?: VoidFunction;
68
+ };
69
+ type Options = Partial<ToastOptions>;
70
+ type RenderOptions = Omit<ToastOptions, "render"> & {
71
+ dismiss(): void;
72
+ };
73
+ type MachineContext = SharedContext & RootProperties & CommonProperties & Omit<ToastOptions, "removeDelay"> & {
74
+ /**
75
+ * The duration for the toast to kept alive before it is removed.
76
+ * Useful for exit transitions.
77
+ */
78
+ removeDelay: number;
79
+ /**
80
+ * The document's text/writing direction.
81
+ */
82
+ dir?: Direction;
83
+ /**
84
+ * The time the toast was created
85
+ */
86
+ createdAt: number;
87
+ /**
88
+ * The time left before the toast is removed
89
+ */
90
+ remaining: number;
91
+ };
92
+ type MachineState = {
93
+ value: "active" | "active:temp" | "dismissing" | "inactive" | "persist";
94
+ tags: "visible" | "paused" | "updating";
95
+ };
96
+ type State = StateMachine.State<MachineContext, MachineState>;
97
+ type Send = StateMachine.Send;
98
+ type Service = Machine<MachineContext, MachineState>;
99
+ type GroupPublicContext = SharedContext & DirectionProperty & CommonProperties & {
100
+ /**
101
+ * The gutter or spacing between toasts
102
+ */
103
+ gutter: string;
104
+ /**
105
+ * The z-index applied to each toast group
106
+ */
107
+ zIndex: number;
108
+ /**
109
+ * The maximum number of toasts that can be shown at once
110
+ */
111
+ max: number;
112
+ /**
113
+ * The offset from the safe environment edge of the viewport
114
+ */
115
+ offsets: string | Record<"left" | "right" | "bottom" | "top", string>;
116
+ };
117
+ type UserDefinedGroupContext = RequiredBy<GroupPublicContext, "id">;
118
+ type GroupComputedContext = Readonly<{
119
+ /**
120
+ * @computed
121
+ * The total number of toasts in the group
122
+ */
123
+ readonly count: number;
124
+ }>;
125
+ type GroupPrivateContext = Context<{}>;
126
+ type GroupMachineContext = GroupPublicContext & GroupComputedContext & GroupPrivateContext;
127
+ type GroupState = StateMachine.State<GroupMachineContext>;
128
+ type GroupSend = (event: StateMachine.Event<StateMachine.AnyEventObject>) => void;
129
+ type MaybeFunction<Value, Args> = Value | ((arg: Args) => Value);
130
+ type PromiseOptions<Value> = {
131
+ loading: ToastOptions;
132
+ success: MaybeFunction<ToastOptions, Value>;
133
+ error: MaybeFunction<ToastOptions, Error>;
134
+ };
135
+ type GroupProps = {
136
+ placement: Placement;
137
+ label?: string;
138
+ };
139
+ type Toaster = {
140
+ count: number;
141
+ isVisible(id: string): boolean;
142
+ upsert(options: ToastOptions): string | undefined;
143
+ create(options: ToastOptions): string | undefined;
144
+ success(options: ToastOptions): string | undefined;
145
+ error(options: ToastOptions): string | undefined;
146
+ loading(options: ToastOptions): string | undefined;
147
+ dismiss(id?: string | undefined): void;
148
+ remove(id?: string | undefined): void;
149
+ promise<T>(promise: Promise<T>, options: PromiseOptions<T>, shared?: ToastOptions): Promise<T>;
150
+ };
151
+
152
+ declare function groupConnect<T extends PropTypes>(state: GroupState, send: GroupSend, normalize: NormalizeProps<T>): {
153
+ /**
154
+ * The total number of toasts
155
+ */
156
+ count: number;
157
+ /**
158
+ * The active toasts
159
+ */
160
+ toasts: Service[];
161
+ /**
162
+ * The active toasts by placement
163
+ */
164
+ toastsByPlacement: Partial<Record<Placement, Service[]>>;
165
+ /**
166
+ * Returns whether the toast id is visible
167
+ */
168
+ isVisible(id: string): boolean;
169
+ /**
170
+ * Function to create a toast.
171
+ */
172
+ create(options: Options): string | undefined;
173
+ /**
174
+ * Function to create or update a toast.
175
+ */
176
+ upsert(options: Options): string | undefined;
177
+ /**
178
+ * Function to dismiss a toast by id.
179
+ * If no id is provided, all toasts will be dismissed.
180
+ */
181
+ dismiss(id?: string): void;
182
+ /**
183
+ * Function to remove a toast by id.
184
+ * If no id is provided, all toasts will be removed.
185
+ */
186
+ remove(id?: string): void;
187
+ /**
188
+ * Function to dismiss all toasts by placement.
189
+ */
190
+ dismissByPlacement(placement: Placement): void;
191
+ /**
192
+ * Function to update a toast's options by id.
193
+ */
194
+ update(id: string, options: Options): string | undefined;
195
+ /**
196
+ * Function to create a loading toast.
197
+ */
198
+ loading(options: Options): string | undefined;
199
+ /**
200
+ * Function to create a success toast.
201
+ */
202
+ success(options: Options): string | undefined;
203
+ /**
204
+ * Function to create an error toast.
205
+ */
206
+ error(options: Options): string | undefined;
207
+ /**
208
+ * Function to create a toast from a promise.
209
+ * - When the promise resolves, the toast will be updated with the success options.
210
+ * - When the promise rejects, the toast will be updated with the error options.
211
+ */
212
+ promise<T_1>(promise: Promise<T_1>, options: PromiseOptions<T_1>, shared?: Options): Promise<T_1>;
213
+ /**
214
+ * Function to pause a toast by id.
215
+ */
216
+ pause(id?: string): void;
217
+ /**
218
+ * Function to resume a toast by id.
219
+ */
220
+ resume(id?: string): void;
221
+ getGroupProps(options: GroupProps): T["element"];
222
+ subscribe(fn: (toasts: GroupMachineContext["toasts"]) => void): () => void;
223
+ };
224
+
225
+ declare function groupMachine(userContext: UserDefinedGroupContext): _zag_js_core.Machine<GroupMachineContext, _zag_js_core.StateMachine.StateSchema, _zag_js_core.StateMachine.AnyEventObject>;
226
+
227
+ declare function createToastMachine(options?: Options): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
228
+
229
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"title" | "group" | "root" | "description" | "closeTrigger">;
230
+
231
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
232
+ /**
233
+ * The type of the toast.
234
+ */
235
+ type: Type;
236
+ /**
237
+ * The title of the toast.
238
+ */
239
+ title: string | undefined;
240
+ /**
241
+ * The description of the toast.
242
+ */
243
+ description: string | undefined;
244
+ /**
245
+ * The current placement of the toast.
246
+ */
247
+ placement: Placement;
248
+ /**
249
+ * Whether the toast is visible.
250
+ */
251
+ isVisible: boolean;
252
+ /**
253
+ * Whether the toast is paused.
254
+ */
255
+ isPaused: boolean;
256
+ /**
257
+ * Whether the toast is in RTL mode.
258
+ */
259
+ isRtl: boolean;
260
+ /**
261
+ * Function to pause the toast (keeping it visible).
262
+ */
263
+ pause(): void;
264
+ /**
265
+ * Function to resume the toast dismissing.
266
+ */
267
+ resume(): void;
268
+ /**
269
+ * Function to instantly dismiss the toast.
270
+ */
271
+ dismiss(): void;
272
+ /**
273
+ * Function render the toast in the DOM (based on the defined `render` property)
274
+ */
275
+ render(): any;
276
+ rootProps: T["element"];
277
+ titleProps: T["element"];
278
+ descriptionProps: T["element"];
279
+ closeTriggerProps: T["button"];
280
+ };
281
+
282
+ declare const group: {
10
283
  connect: typeof groupConnect;
11
284
  machine: typeof groupMachine;
12
285
  };
13
- export declare function api(): Toaster | undefined;
286
+ declare function api(): Toaster | undefined;
287
+
288
+ export { GroupMachineContext, MachineContext, MachineState, Placement, Service, ToastOptions, Type, anatomy, api, connect, createToastMachine as createMachine, group };