vue-toasts-lite 0.1.2 → 0.1.3
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/LICENSE +1 -0
- package/dist/index.d.ts +245 -56
- package/dist/index.js +263 -101
- package/package.json +1 -1
package/LICENSE
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { ComponentProvideOptions } from 'vue';
|
|
|
3
3
|
import { DefineComponent } from 'vue';
|
|
4
4
|
import { PublicProps } from 'vue';
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
declare type Id = string;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
declare interface IToastsController {
|
|
9
9
|
add: (options: ToastOptions) => Id;
|
|
10
10
|
update: (id: Id, options: ToastOptions) => Id;
|
|
11
11
|
success: (message: string, options?: ToastSimpleOptions) => Id;
|
|
@@ -15,107 +15,296 @@ export declare interface Toast {
|
|
|
15
15
|
promise: <T>(promise: Promise<T>, options: ToastPromiseOptions) => Promise<Id>;
|
|
16
16
|
remove: (id?: Id) => void;
|
|
17
17
|
clear: () => void;
|
|
18
|
-
onToastsListChange: (callback: (toasts:
|
|
18
|
+
onToastsListChange: (callback: (toasts: Toast[]) => void) => () => void;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
declare type Toast = {
|
|
22
|
+
/**
|
|
23
|
+
* Unique identifier for the toast.
|
|
24
|
+
* Auto-generated if not provided when creating a toast.
|
|
25
|
+
*/
|
|
26
|
+
id: Id;
|
|
27
|
+
/**
|
|
28
|
+
* Visual type of the toast notification.
|
|
29
|
+
* Determines the icon and color scheme.
|
|
30
|
+
* @default "success"
|
|
31
|
+
*/
|
|
32
|
+
type: ToastType;
|
|
33
|
+
/**
|
|
34
|
+
* Text content displayed in the toast.
|
|
35
|
+
* @default "Hello from Toasts Lite"
|
|
36
|
+
*/
|
|
37
|
+
message: string;
|
|
38
|
+
/**
|
|
39
|
+
* Whether the toast should automatically close after {@link duration} milliseconds.
|
|
40
|
+
* @default true
|
|
41
|
+
*/
|
|
42
|
+
autoClose: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Time in milliseconds before the toast auto-closes.
|
|
45
|
+
* Only applies when {@link autoClose} is `true`.
|
|
46
|
+
* @default 3000
|
|
47
|
+
*/
|
|
48
|
+
duration: number;
|
|
49
|
+
/**
|
|
50
|
+
* Position of the toast on the screen.
|
|
51
|
+
* @default "top-center"
|
|
52
|
+
*/
|
|
53
|
+
position?: ToastPosition;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export declare type ToastOptions = Partial<Toast>;
|
|
22
57
|
|
|
23
58
|
export declare type ToastPosition = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "middle-center";
|
|
24
59
|
|
|
25
60
|
export declare type ToastPromiseOptions = {
|
|
61
|
+
/** Message displayed while the promise is pending */
|
|
26
62
|
loading: string;
|
|
63
|
+
/** Message displayed when the promise resolves successfully */
|
|
27
64
|
success: string;
|
|
65
|
+
/** Message displayed when the promise rejects */
|
|
28
66
|
error: string;
|
|
67
|
+
/** Optional position for the toast on the screen */
|
|
29
68
|
position?: ToastPosition;
|
|
69
|
+
/** Optional custom ID for the toast */
|
|
30
70
|
id?: Id;
|
|
31
71
|
};
|
|
32
72
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/** duration in milliseconds */
|
|
43
|
-
duration: number;
|
|
44
|
-
/** Position of the toast on the screen. */
|
|
45
|
-
position?: ToastPosition;
|
|
46
|
-
};
|
|
47
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Main toast controller instance.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { toasts } from 'vue-toasts-lite'
|
|
78
|
+
*
|
|
79
|
+
* toasts.success('Hello, World!')
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
48
82
|
export declare const toasts: ToastsController;
|
|
49
83
|
|
|
50
|
-
declare class ToastsController implements
|
|
84
|
+
declare class ToastsController implements IToastsController {
|
|
51
85
|
private counter;
|
|
52
86
|
private toasts;
|
|
53
87
|
constructor();
|
|
54
|
-
get toastList(): ToastProps[];
|
|
55
|
-
onToastsListChange(callback: (toasts: ToastProps[]) => void): () => void;
|
|
56
|
-
private ID;
|
|
57
|
-
private addOrUpdate;
|
|
58
88
|
/**
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
61
|
-
*
|
|
89
|
+
* Returns an array of all currently active toasts.
|
|
90
|
+
* @description Useful for debugging or external monitoring
|
|
91
|
+
*
|
|
92
|
+
* @returns Array of toast properties for all active toasts
|
|
93
|
+
*/
|
|
94
|
+
get toastList(): Toast[];
|
|
95
|
+
/**
|
|
96
|
+
* Subscribes to changes in the toast list.
|
|
97
|
+
*
|
|
98
|
+
* @param callback - Function called whenever toasts are added, updated, or removed
|
|
99
|
+
* @returns Unsubscribe function to stop listening for changes
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const unsubscribe = toasts.onToastsListChange((toastsList) => {
|
|
104
|
+
* console.log('Toasts updated:', toastsList)
|
|
105
|
+
* })
|
|
106
|
+
*
|
|
107
|
+
* // Later, to stop listening:
|
|
108
|
+
* unsubscribe()
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
onToastsListChange(callback: (toasts: Toast[]) => void): () => void;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a toast notification with custom options. If toast with the same ID already exists, it will be updated.
|
|
114
|
+
* @description Use this method when you need full control over toast configuration.
|
|
115
|
+
* @description For simpler use cases, consider using {@link success}, {@link error}, {@link warn}, or {@link loading} methods.
|
|
116
|
+
*
|
|
117
|
+
* @param options - Configuration options for the toast
|
|
118
|
+
* @returns Unique identifier of the created toast
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* // Create a custom toast
|
|
123
|
+
* const id = toasts.add({
|
|
124
|
+
* type: 'success',
|
|
125
|
+
* message: 'Custom notification',
|
|
126
|
+
* duration: 5000,
|
|
127
|
+
* position: 'top-right',
|
|
128
|
+
* autoClose: true
|
|
129
|
+
* })
|
|
130
|
+
*
|
|
131
|
+
* // Create with custom ID for later reference
|
|
132
|
+
* toasts.add({ id: 'my-toast', type: 'loading', message: 'Processing...' })
|
|
133
|
+
* ```
|
|
62
134
|
*/
|
|
63
135
|
add(options: ToastOptions): string;
|
|
64
136
|
/**
|
|
65
|
-
*
|
|
66
|
-
* @
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
137
|
+
* Updates an existing toast notification by its ID.
|
|
138
|
+
* @description Useful for changing the content or appearance of a toast that is already displayed, such as updating a loading toast to show success or error state.
|
|
139
|
+
*
|
|
140
|
+
* @param id - Unique identifier of the toast to update
|
|
141
|
+
* @param options - New configuration options to apply
|
|
142
|
+
* @returns The same toast ID
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* // Create a loading toast
|
|
147
|
+
* const id = toasts.loading('Uploading file...')
|
|
148
|
+
*
|
|
149
|
+
* // Later, update it to show success
|
|
150
|
+
* toasts.update(id, { type: 'success', message: 'File uploaded!' })
|
|
151
|
+
* ```
|
|
69
152
|
*/
|
|
70
153
|
update(id: Id, options: ToastOptions): string;
|
|
71
154
|
/**
|
|
72
|
-
*
|
|
73
|
-
* If no
|
|
74
|
-
*
|
|
155
|
+
* Removes a toast notification from the screen.
|
|
156
|
+
* @description If no ID is provided, all currently visible toasts will be removed. If the specified ID doesn't exist, this method does nothing.
|
|
157
|
+
*
|
|
158
|
+
* @param id - Optional unique identifier of the toast to remove.
|
|
159
|
+
* If omitted, all toasts are removed.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* // Remove a specific toast
|
|
164
|
+
* const id = toasts.success('Hello!')
|
|
165
|
+
* toasts.remove(id)
|
|
166
|
+
*
|
|
167
|
+
* // Remove all toasts
|
|
168
|
+
* toasts.remove()
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @see {@link clear} for explicitly removing all toasts
|
|
75
172
|
*/
|
|
76
173
|
remove(id?: Id): void;
|
|
77
174
|
/**
|
|
78
|
-
*
|
|
175
|
+
* Removes all currently visible toast notifications.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* // Clear all toasts on route change
|
|
180
|
+
* router.beforeEach(() => {
|
|
181
|
+
* toasts.clear()
|
|
182
|
+
* })
|
|
183
|
+
* ```
|
|
79
184
|
*/
|
|
80
185
|
clear(): void;
|
|
81
186
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* @param
|
|
85
|
-
* @
|
|
187
|
+
* Displays a **success** toast notification.
|
|
188
|
+
*
|
|
189
|
+
* @param message - Text content to display in the toast
|
|
190
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
191
|
+
* @returns Unique identifier of the created toast
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* // Simple success message
|
|
196
|
+
* toasts.success('Changes saved successfully!')
|
|
197
|
+
*
|
|
198
|
+
* // With custom options
|
|
199
|
+
* toasts.success('Profile updated', {
|
|
200
|
+
* duration: 5000,
|
|
201
|
+
* position: 'bottom-center'
|
|
202
|
+
* })
|
|
203
|
+
* ```
|
|
86
204
|
*/
|
|
87
205
|
success(message: string, options?: Omit<ToastSimpleOptions, "type">): string;
|
|
88
206
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* @param
|
|
92
|
-
* @
|
|
207
|
+
* Displays an **error** toast notification.
|
|
208
|
+
*
|
|
209
|
+
* @param message - Text content to display in the toast
|
|
210
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
211
|
+
* @returns Unique identifier of the created toast
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* // Simple error message
|
|
216
|
+
* toasts.error('Failed to save changes')
|
|
217
|
+
*
|
|
218
|
+
* // Non-auto-closing error
|
|
219
|
+
* toasts.error('Critical error occurred', { autoClose: false })
|
|
220
|
+
* ```
|
|
93
221
|
*/
|
|
94
222
|
error(message: string, options?: Omit<ToastSimpleOptions, "type">): string;
|
|
95
223
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @param
|
|
99
|
-
* @
|
|
224
|
+
* Displays a **warning** toast notification.
|
|
225
|
+
*
|
|
226
|
+
* @param message - Text content to display in the toast
|
|
227
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
228
|
+
* @returns Unique identifier of the created toast
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* // Simple warning message
|
|
233
|
+
* toasts.warn('Your session will expire soon')
|
|
234
|
+
*
|
|
235
|
+
* // With longer duration
|
|
236
|
+
* toasts.warn('Unsaved changes detected', { duration: 10000 })
|
|
237
|
+
* ```
|
|
100
238
|
*/
|
|
101
239
|
warn(message: string, options?: Omit<ToastSimpleOptions, "type">): string;
|
|
102
240
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
241
|
+
* Displays a **loading** toast notification.
|
|
242
|
+
*
|
|
243
|
+
* Loading toasts are typically used as placeholders while
|
|
244
|
+
* an async operation is in progress.
|
|
245
|
+
*
|
|
246
|
+
* @param message - Text content to display in the toast
|
|
247
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
248
|
+
* @returns Unique identifier of the created toast
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* // Manual loading state management
|
|
253
|
+
* const id = toasts.loading('Saving...')
|
|
254
|
+
*
|
|
255
|
+
* try {
|
|
256
|
+
* await saveData()
|
|
257
|
+
* toasts.update(id, { type: 'success', message: 'Saved!' })
|
|
258
|
+
* } catch {
|
|
259
|
+
* toasts.update(id, { type: 'error', message: 'Save failed' })
|
|
260
|
+
* }
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* @see {@link promise} for automatic promise-based state management
|
|
107
264
|
*/
|
|
108
265
|
loading(message: string, options?: Omit<ToastSimpleOptions, "type">): string;
|
|
109
266
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
267
|
+
* Displays a **promise** toast that automatically updates based on promise resolution.
|
|
268
|
+
*
|
|
269
|
+
* Shows a loading toast while the promise is pending, then automatically
|
|
270
|
+
* transitions to success or error state based on the promise outcome.
|
|
271
|
+
*
|
|
272
|
+
* @template T - Type of the promise result
|
|
273
|
+
* @param promise - The promise to track
|
|
274
|
+
* @param options - Messages for each state and optional positioning
|
|
275
|
+
* @param options.loading - Message to show while promise is pending
|
|
276
|
+
* @param options.success - Message to show when promise resolves
|
|
277
|
+
* @param options.error - Message to show when promise rejects
|
|
278
|
+
* @param options.position - Optional toast position on screen
|
|
279
|
+
* @param options.id - Optional custom ID for the toast
|
|
280
|
+
* @returns Promise that resolves to the toast ID
|
|
281
|
+
* @throws Error with the error message if the promise rejects
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* // Basic usage
|
|
286
|
+
* await toasts.promise(fetchUserData(), {
|
|
287
|
+
* loading: 'Loading user data...',
|
|
288
|
+
* success: 'User data loaded!',
|
|
289
|
+
* error: 'Failed to load user data'
|
|
290
|
+
* })
|
|
291
|
+
*
|
|
292
|
+
* // With custom position
|
|
293
|
+
* await toasts.promise(uploadFile(file), {
|
|
294
|
+
* loading: 'Uploading...',
|
|
295
|
+
* success: 'File uploaded successfully!',
|
|
296
|
+
* error: 'Upload failed',
|
|
297
|
+
* position: 'bottom-right'
|
|
298
|
+
* })
|
|
299
|
+
* ```
|
|
114
300
|
*/
|
|
115
301
|
promise<T>(promise: Promise<T>, options: ToastPromiseOptions): Promise<string>;
|
|
302
|
+
private ID;
|
|
303
|
+
private addOrUpdate;
|
|
304
|
+
private mergeWithDefaultOptions;
|
|
116
305
|
}
|
|
117
306
|
|
|
118
|
-
export declare type ToastSimpleOptions = Partial<Omit<
|
|
307
|
+
export declare type ToastSimpleOptions = Partial<Omit<Toast, "message">>;
|
|
119
308
|
|
|
120
309
|
export declare const ToastsLiteProvider: DefineComponent<{}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, ComponentProvideOptions, true, {}, any>;
|
|
121
310
|
|
package/dist/index.js
CHANGED
|
@@ -1,169 +1,331 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var v = (r, t, e) =>
|
|
4
|
-
import { ref as h, defineComponent as
|
|
5
|
-
class
|
|
1
|
+
var M = Object.defineProperty;
|
|
2
|
+
var $ = (r, t, e) => t in r ? M(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var v = (r, t, e) => $(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { ref as h, defineComponent as O, watch as L, onMounted as U, onUnmounted as k, createElementBlock as g, openBlock as d, withModifiers as A, normalizeStyle as E, createCommentVNode as x, createElementVNode as p, normalizeClass as D, toDisplayString as B, computed as P, createBlock as y, Teleport as I, Fragment as b, renderList as T, unref as m, createVNode as N, TransitionGroup as R, withCtx as S } from "vue";
|
|
5
|
+
class V extends Map {
|
|
6
6
|
constructor() {
|
|
7
7
|
super(...arguments);
|
|
8
8
|
v(this, "subscribers", []);
|
|
9
9
|
}
|
|
10
|
-
set(e,
|
|
11
|
-
return super.set(e,
|
|
10
|
+
set(e, o) {
|
|
11
|
+
return super.set(e, o), this.notify(), this;
|
|
12
12
|
}
|
|
13
13
|
delete(e) {
|
|
14
|
-
const
|
|
15
|
-
return this.notify(),
|
|
14
|
+
const o = super.delete(e);
|
|
15
|
+
return this.notify(), o;
|
|
16
16
|
}
|
|
17
17
|
clear() {
|
|
18
18
|
super.clear(), this.notify();
|
|
19
19
|
}
|
|
20
20
|
subscribe(e) {
|
|
21
21
|
return this.subscribers.push(e), () => {
|
|
22
|
-
this.subscribers = this.subscribers.filter((
|
|
22
|
+
this.subscribers = this.subscribers.filter((o) => o !== e);
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
notify() {
|
|
26
26
|
this.subscribers.forEach((e) => e(Array.from(this.values())));
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
const
|
|
29
|
+
const w = {
|
|
30
30
|
type: "success",
|
|
31
31
|
message: "Hello from Toasts Lite",
|
|
32
32
|
autoClose: !0,
|
|
33
33
|
duration: 3e3,
|
|
34
34
|
position: "top-center"
|
|
35
35
|
};
|
|
36
|
-
class
|
|
36
|
+
class j {
|
|
37
37
|
constructor() {
|
|
38
38
|
v(this, "counter", 0);
|
|
39
39
|
v(this, "toasts");
|
|
40
|
-
this.toasts = new
|
|
40
|
+
this.toasts = new V();
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns an array of all currently active toasts.
|
|
44
|
+
* @description Useful for debugging or external monitoring
|
|
45
|
+
*
|
|
46
|
+
* @returns Array of toast properties for all active toasts
|
|
47
|
+
*/
|
|
42
48
|
get toastList() {
|
|
43
49
|
return Array.from(this.toasts.values());
|
|
44
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Subscribes to changes in the toast list.
|
|
53
|
+
*
|
|
54
|
+
* @param callback - Function called whenever toasts are added, updated, or removed
|
|
55
|
+
* @returns Unsubscribe function to stop listening for changes
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const unsubscribe = toasts.onToastsListChange((toastsList) => {
|
|
60
|
+
* console.log('Toasts updated:', toastsList)
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* // Later, to stop listening:
|
|
64
|
+
* unsubscribe()
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
45
67
|
onToastsListChange(t) {
|
|
46
68
|
return this.toasts.subscribe(t);
|
|
47
69
|
}
|
|
48
|
-
ID() {
|
|
49
|
-
return `toast:${Date.now().toString(16)}-${this.counter++}`;
|
|
50
|
-
}
|
|
51
|
-
addOrUpdate(t) {
|
|
52
|
-
let { id: e = this.ID(), ...s } = t;
|
|
53
|
-
if (this.toasts.has(e)) {
|
|
54
|
-
const n = this.toasts.get(e);
|
|
55
|
-
return Object.assign(n, s), this.toasts.set(e, n), e;
|
|
56
|
-
}
|
|
57
|
-
return this.toasts.set(e, { id: e, ...V, ...s }), e;
|
|
58
|
-
}
|
|
59
70
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @
|
|
62
|
-
* @
|
|
71
|
+
* Creates a toast notification with custom options. If toast with the same ID already exists, it will be updated.
|
|
72
|
+
* @description Use this method when you need full control over toast configuration.
|
|
73
|
+
* @description For simpler use cases, consider using {@link success}, {@link error}, {@link warn}, or {@link loading} methods.
|
|
74
|
+
*
|
|
75
|
+
* @param options - Configuration options for the toast
|
|
76
|
+
* @returns Unique identifier of the created toast
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* // Create a custom toast
|
|
81
|
+
* const id = toasts.add({
|
|
82
|
+
* type: 'success',
|
|
83
|
+
* message: 'Custom notification',
|
|
84
|
+
* duration: 5000,
|
|
85
|
+
* position: 'top-right',
|
|
86
|
+
* autoClose: true
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* // Create with custom ID for later reference
|
|
90
|
+
* toasts.add({ id: 'my-toast', type: 'loading', message: 'Processing...' })
|
|
91
|
+
* ```
|
|
63
92
|
*/
|
|
64
93
|
add(t) {
|
|
65
94
|
return this.addOrUpdate(t);
|
|
66
95
|
}
|
|
67
96
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
97
|
+
* Updates an existing toast notification by its ID.
|
|
98
|
+
* @description Useful for changing the content or appearance of a toast that is already displayed, such as updating a loading toast to show success or error state.
|
|
99
|
+
*
|
|
100
|
+
* @param id - Unique identifier of the toast to update
|
|
101
|
+
* @param options - New configuration options to apply
|
|
102
|
+
* @returns The same toast ID
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* // Create a loading toast
|
|
107
|
+
* const id = toasts.loading('Uploading file...')
|
|
108
|
+
*
|
|
109
|
+
* // Later, update it to show success
|
|
110
|
+
* toasts.update(id, { type: 'success', message: 'File uploaded!' })
|
|
111
|
+
* ```
|
|
72
112
|
*/
|
|
73
113
|
update(t, e) {
|
|
74
114
|
return this.addOrUpdate({ ...e, id: t });
|
|
75
115
|
}
|
|
76
116
|
/**
|
|
77
|
-
*
|
|
78
|
-
* If no
|
|
79
|
-
*
|
|
117
|
+
* Removes a toast notification from the screen.
|
|
118
|
+
* @description If no ID is provided, all currently visible toasts will be removed. If the specified ID doesn't exist, this method does nothing.
|
|
119
|
+
*
|
|
120
|
+
* @param id - Optional unique identifier of the toast to remove.
|
|
121
|
+
* If omitted, all toasts are removed.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* // Remove a specific toast
|
|
126
|
+
* const id = toasts.success('Hello!')
|
|
127
|
+
* toasts.remove(id)
|
|
128
|
+
*
|
|
129
|
+
* // Remove all toasts
|
|
130
|
+
* toasts.remove()
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @see {@link clear} for explicitly removing all toasts
|
|
80
134
|
*/
|
|
81
135
|
remove(t) {
|
|
82
136
|
t && !this.toasts.has(t) || (t ? this.toasts.delete(t) : this.clear());
|
|
83
137
|
}
|
|
84
138
|
/**
|
|
85
|
-
*
|
|
139
|
+
* Removes all currently visible toast notifications.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* // Clear all toasts on route change
|
|
144
|
+
* router.beforeEach(() => {
|
|
145
|
+
* toasts.clear()
|
|
146
|
+
* })
|
|
147
|
+
* ```
|
|
86
148
|
*/
|
|
87
149
|
clear() {
|
|
88
150
|
this.toasts.clear();
|
|
89
151
|
}
|
|
90
152
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* @param
|
|
94
|
-
* @
|
|
153
|
+
* Displays a **success** toast notification.
|
|
154
|
+
*
|
|
155
|
+
* @param message - Text content to display in the toast
|
|
156
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
157
|
+
* @returns Unique identifier of the created toast
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* // Simple success message
|
|
162
|
+
* toasts.success('Changes saved successfully!')
|
|
163
|
+
*
|
|
164
|
+
* // With custom options
|
|
165
|
+
* toasts.success('Profile updated', {
|
|
166
|
+
* duration: 5000,
|
|
167
|
+
* position: 'bottom-center'
|
|
168
|
+
* })
|
|
169
|
+
* ```
|
|
95
170
|
*/
|
|
96
171
|
success(t, e) {
|
|
97
172
|
return this.addOrUpdate({ ...e, message: t, type: "success" });
|
|
98
173
|
}
|
|
99
174
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @param
|
|
103
|
-
* @
|
|
175
|
+
* Displays an **error** toast notification.
|
|
176
|
+
*
|
|
177
|
+
* @param message - Text content to display in the toast
|
|
178
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
179
|
+
* @returns Unique identifier of the created toast
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* // Simple error message
|
|
184
|
+
* toasts.error('Failed to save changes')
|
|
185
|
+
*
|
|
186
|
+
* // Non-auto-closing error
|
|
187
|
+
* toasts.error('Critical error occurred', { autoClose: false })
|
|
188
|
+
* ```
|
|
104
189
|
*/
|
|
105
190
|
error(t, e) {
|
|
106
191
|
return this.addOrUpdate({ ...e, message: t, type: "error" });
|
|
107
192
|
}
|
|
108
193
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* @param
|
|
112
|
-
* @
|
|
194
|
+
* Displays a **warning** toast notification.
|
|
195
|
+
*
|
|
196
|
+
* @param message - Text content to display in the toast
|
|
197
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
198
|
+
* @returns Unique identifier of the created toast
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* // Simple warning message
|
|
203
|
+
* toasts.warn('Your session will expire soon')
|
|
204
|
+
*
|
|
205
|
+
* // With longer duration
|
|
206
|
+
* toasts.warn('Unsaved changes detected', { duration: 10000 })
|
|
207
|
+
* ```
|
|
113
208
|
*/
|
|
114
209
|
warn(t, e) {
|
|
115
210
|
return this.addOrUpdate({ ...e, message: t, type: "warn" });
|
|
116
211
|
}
|
|
117
212
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
213
|
+
* Displays a **loading** toast notification.
|
|
214
|
+
*
|
|
215
|
+
* Loading toasts are typically used as placeholders while
|
|
216
|
+
* an async operation is in progress.
|
|
217
|
+
*
|
|
218
|
+
* @param message - Text content to display in the toast
|
|
219
|
+
* @param options - Optional configuration (position, duration, autoClose, id)
|
|
220
|
+
* @returns Unique identifier of the created toast
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* // Manual loading state management
|
|
225
|
+
* const id = toasts.loading('Saving...')
|
|
226
|
+
*
|
|
227
|
+
* try {
|
|
228
|
+
* await saveData()
|
|
229
|
+
* toasts.update(id, { type: 'success', message: 'Saved!' })
|
|
230
|
+
* } catch {
|
|
231
|
+
* toasts.update(id, { type: 'error', message: 'Save failed' })
|
|
232
|
+
* }
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @see {@link promise} for automatic promise-based state management
|
|
122
236
|
*/
|
|
123
237
|
loading(t, e) {
|
|
124
238
|
return this.addOrUpdate({ ...e, message: t, type: "loading" });
|
|
125
239
|
}
|
|
126
240
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
241
|
+
* Displays a **promise** toast that automatically updates based on promise resolution.
|
|
242
|
+
*
|
|
243
|
+
* Shows a loading toast while the promise is pending, then automatically
|
|
244
|
+
* transitions to success or error state based on the promise outcome.
|
|
245
|
+
*
|
|
246
|
+
* @template T - Type of the promise result
|
|
247
|
+
* @param promise - The promise to track
|
|
248
|
+
* @param options - Messages for each state and optional positioning
|
|
249
|
+
* @param options.loading - Message to show while promise is pending
|
|
250
|
+
* @param options.success - Message to show when promise resolves
|
|
251
|
+
* @param options.error - Message to show when promise rejects
|
|
252
|
+
* @param options.position - Optional toast position on screen
|
|
253
|
+
* @param options.id - Optional custom ID for the toast
|
|
254
|
+
* @returns Promise that resolves to the toast ID
|
|
255
|
+
* @throws Error with the error message if the promise rejects
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* // Basic usage
|
|
260
|
+
* await toasts.promise(fetchUserData(), {
|
|
261
|
+
* loading: 'Loading user data...',
|
|
262
|
+
* success: 'User data loaded!',
|
|
263
|
+
* error: 'Failed to load user data'
|
|
264
|
+
* })
|
|
265
|
+
*
|
|
266
|
+
* // With custom position
|
|
267
|
+
* await toasts.promise(uploadFile(file), {
|
|
268
|
+
* loading: 'Uploading...',
|
|
269
|
+
* success: 'File uploaded successfully!',
|
|
270
|
+
* error: 'Upload failed',
|
|
271
|
+
* position: 'bottom-right'
|
|
272
|
+
* })
|
|
273
|
+
* ```
|
|
131
274
|
*/
|
|
132
275
|
async promise(t, e) {
|
|
133
|
-
const
|
|
276
|
+
const o = this.loading(e.loading, { position: e.position, id: e.id });
|
|
134
277
|
try {
|
|
135
|
-
return await t, this.success(e.success, { position: e.position, id:
|
|
278
|
+
return await t, this.success(e.success, { position: e.position, id: o }), o;
|
|
136
279
|
} catch {
|
|
137
|
-
throw this.error(e.error, { position: e.position, id:
|
|
280
|
+
throw this.error(e.error, { position: e.position, id: o }), new Error(e.error);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
ID() {
|
|
284
|
+
return `toast:${Date.now().toString(16)}-${this.counter++}`;
|
|
285
|
+
}
|
|
286
|
+
addOrUpdate(t) {
|
|
287
|
+
let { id: e = this.ID(), ...o } = t;
|
|
288
|
+
if (this.toasts.has(e)) {
|
|
289
|
+
const n = this.toasts.get(e);
|
|
290
|
+
return Object.assign(n, o), this.toasts.set(e, n), e;
|
|
291
|
+
}
|
|
292
|
+
return this.toasts.set(e, { ...this.mergeWithDefaultOptions(o), id: e }), e;
|
|
293
|
+
}
|
|
294
|
+
mergeWithDefaultOptions(t) {
|
|
295
|
+
const e = Object.assign({}, w);
|
|
296
|
+
for (let o in w) {
|
|
297
|
+
const n = o, s = t[n];
|
|
298
|
+
s != null && (e[n] = s);
|
|
138
299
|
}
|
|
300
|
+
return e;
|
|
139
301
|
}
|
|
140
302
|
}
|
|
141
|
-
const
|
|
142
|
-
function
|
|
303
|
+
const C = new j();
|
|
304
|
+
function z() {
|
|
143
305
|
const r = h(/* @__PURE__ */ new Map());
|
|
144
|
-
function t(n,
|
|
306
|
+
function t(n, s, a) {
|
|
145
307
|
r.value.has(n) || r.value.set(n, []);
|
|
146
308
|
const i = r.value.get(n);
|
|
147
|
-
a && "pause" in a && "resume" in a && (i[
|
|
309
|
+
a && "pause" in a && "resume" in a && (i[s] = a);
|
|
148
310
|
}
|
|
149
311
|
function e(n) {
|
|
150
|
-
var
|
|
151
|
-
(
|
|
312
|
+
var s;
|
|
313
|
+
(s = r.value.get(n)) == null || s.forEach((a) => a == null ? void 0 : a.pause());
|
|
152
314
|
}
|
|
153
|
-
function
|
|
154
|
-
var
|
|
155
|
-
(
|
|
315
|
+
function o(n) {
|
|
316
|
+
var s;
|
|
317
|
+
(s = r.value.get(n)) == null || s.forEach((a) => a == null ? void 0 : a.resume());
|
|
156
318
|
}
|
|
157
319
|
return {
|
|
158
320
|
setRef: t,
|
|
159
321
|
pauseAll: e,
|
|
160
|
-
resumeAll:
|
|
322
|
+
resumeAll: o
|
|
161
323
|
};
|
|
162
324
|
}
|
|
163
|
-
const
|
|
325
|
+
const F = {
|
|
164
326
|
key: 0,
|
|
165
327
|
class: "toasts-lite__icon"
|
|
166
|
-
},
|
|
328
|
+
}, W = { class: "toasts-lite__content" }, G = { class: "toasts-lite__content-message" }, H = /* @__PURE__ */ O({
|
|
167
329
|
__name: "ToastsLiteItem",
|
|
168
330
|
props: {
|
|
169
331
|
id: {},
|
|
@@ -175,96 +337,96 @@ const j = {
|
|
|
175
337
|
},
|
|
176
338
|
emits: ["close"],
|
|
177
339
|
setup(r, { expose: t, emit: e }) {
|
|
178
|
-
const
|
|
340
|
+
const o = r, n = e, s = h(null), a = h(0), i = h(0);
|
|
179
341
|
function c() {
|
|
180
|
-
|
|
342
|
+
s.value && clearTimeout(s.value), n("close");
|
|
181
343
|
}
|
|
182
344
|
function u() {
|
|
183
|
-
|
|
345
|
+
s.value && (clearTimeout(s.value), s.value = null, i.value = o.duration - (Date.now() - a.value));
|
|
184
346
|
}
|
|
185
347
|
function l() {
|
|
186
|
-
!
|
|
348
|
+
!s.value && i.value > 0 && (a.value = Date.now(), s.value = setTimeout(c, i.value));
|
|
187
349
|
}
|
|
188
350
|
function f() {
|
|
189
|
-
|
|
351
|
+
s.value && (clearTimeout(s.value), s.value = null), o.autoClose && (a.value = Date.now(), i.value = o.duration, s.value = setTimeout(c, o.duration));
|
|
190
352
|
}
|
|
191
|
-
return
|
|
192
|
-
() => [
|
|
353
|
+
return L(
|
|
354
|
+
() => [o.autoClose, o.duration],
|
|
193
355
|
() => {
|
|
194
356
|
f();
|
|
195
357
|
}
|
|
196
358
|
), U(() => {
|
|
197
359
|
f();
|
|
198
|
-
}),
|
|
199
|
-
|
|
360
|
+
}), k(() => {
|
|
361
|
+
s.value && clearTimeout(s.value);
|
|
200
362
|
}), t({
|
|
201
363
|
pause: u,
|
|
202
364
|
resume: l
|
|
203
|
-
}), (
|
|
365
|
+
}), (_, J) => (d(), g("div", {
|
|
204
366
|
class: "toasts-lite__toast",
|
|
205
367
|
style: E(`--toast-duration: ${r.duration}s;`),
|
|
206
|
-
onClick:
|
|
368
|
+
onClick: A(c, ["prevent"])
|
|
207
369
|
}, [
|
|
208
|
-
["success", "error", "loading", "warn"].includes(r.type) ? (d(),
|
|
370
|
+
["success", "error", "loading", "warn"].includes(r.type) ? (d(), g("div", F, [
|
|
209
371
|
p("div", {
|
|
210
|
-
class:
|
|
372
|
+
class: D(`toasts-lite__${r.type}`)
|
|
211
373
|
}, null, 2)
|
|
212
|
-
])) :
|
|
213
|
-
p("div",
|
|
214
|
-
p("div",
|
|
374
|
+
])) : x("", !0),
|
|
375
|
+
p("div", W, [
|
|
376
|
+
p("div", G, B(r.message), 1)
|
|
215
377
|
])
|
|
216
378
|
], 4));
|
|
217
379
|
}
|
|
218
|
-
}),
|
|
380
|
+
}), q = ["onMouseenter", "onMouseleave"], X = /* @__PURE__ */ O({
|
|
219
381
|
__name: "ToastsLiteProvider",
|
|
220
382
|
setup(r) {
|
|
221
|
-
const t = ["top-left", "top-center", "top-right", "middle-center", "bottom-left", "bottom-center", "bottom-right"], e = h(
|
|
222
|
-
|
|
383
|
+
const t = ["top-left", "top-center", "top-right", "middle-center", "bottom-left", "bottom-center", "bottom-right"], e = h([]), { setRef: o, pauseAll: n, resumeAll: s } = z();
|
|
384
|
+
C.onToastsListChange((i) => {
|
|
223
385
|
e.value = i;
|
|
224
386
|
});
|
|
225
|
-
const a =
|
|
387
|
+
const a = P(() => {
|
|
226
388
|
const i = /* @__PURE__ */ new Map();
|
|
227
389
|
return e.value.forEach((c) => {
|
|
228
390
|
const u = c.position || "top-center";
|
|
229
391
|
i.has(u) || i.set(u, []), i.get(u).push(c);
|
|
230
392
|
}), i;
|
|
231
393
|
});
|
|
232
|
-
return (i, c) => (d(),
|
|
233
|
-
(d(),
|
|
394
|
+
return (i, c) => (d(), y(I, { to: "body" }, [
|
|
395
|
+
(d(), g(b, null, T(t, (u) => p("div", {
|
|
234
396
|
key: u,
|
|
235
|
-
class:
|
|
397
|
+
class: D(["toasts-lite__toast-container", `toasts-lite__${u}`])
|
|
236
398
|
}, [
|
|
237
399
|
p("div", {
|
|
238
400
|
class: "toasts-lite__toast-wrapper",
|
|
239
401
|
onMouseenter: (l) => m(n)(u),
|
|
240
|
-
onMouseleave: (l) => m(
|
|
402
|
+
onMouseleave: (l) => m(s)(u)
|
|
241
403
|
}, [
|
|
242
|
-
|
|
404
|
+
N(R, {
|
|
243
405
|
name: "toasts-lite",
|
|
244
406
|
appear: ""
|
|
245
407
|
}, {
|
|
246
|
-
default:
|
|
247
|
-
(d(!0),
|
|
408
|
+
default: S(() => [
|
|
409
|
+
(d(!0), g(b, null, T(a.value.get(u) || [], (l, f) => (d(), y(H, {
|
|
248
410
|
id: l.id,
|
|
249
411
|
ref_for: !0,
|
|
250
|
-
ref: (
|
|
412
|
+
ref: (_) => m(o)(u, f, _),
|
|
251
413
|
key: l.id,
|
|
252
414
|
type: l.type,
|
|
253
415
|
message: l.message,
|
|
254
416
|
"auto-close": l.autoClose,
|
|
255
417
|
duration: l.duration,
|
|
256
418
|
position: l.position,
|
|
257
|
-
onClose: (
|
|
419
|
+
onClose: (_) => m(C).remove(l.id)
|
|
258
420
|
}, null, 8, ["id", "type", "message", "auto-close", "duration", "position", "onClose"]))), 128))
|
|
259
421
|
]),
|
|
260
422
|
_: 2
|
|
261
423
|
}, 1024)
|
|
262
|
-
], 40,
|
|
424
|
+
], 40, q)
|
|
263
425
|
], 2)), 64))
|
|
264
426
|
]));
|
|
265
427
|
}
|
|
266
428
|
});
|
|
267
429
|
export {
|
|
268
430
|
X as ToastsLiteProvider,
|
|
269
|
-
|
|
431
|
+
C as toasts
|
|
270
432
|
};
|