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 CHANGED
@@ -20,3 +20,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
22
 
23
+
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
- export declare type Id = string;
6
+ declare type Id = string;
7
7
 
8
- export declare interface Toast {
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: ToastProps[]) => void) => () => void;
18
+ onToastsListChange: (callback: (toasts: Toast[]) => void) => () => void;
19
19
  }
20
20
 
21
- export declare type ToastOptions = Partial<ToastProps>;
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
- export declare type ToastProps = {
34
- /** toast id */
35
- id: Id;
36
- /** toast type */
37
- type: ToastType;
38
- /** toast message */
39
- message: string;
40
- /** auto close the toast */
41
- autoClose: boolean;
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 Toast {
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
- * Add a toast
60
- * @param options - The options for the toast
61
- * @returns The id of the toast
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
- * Update a toast by id
66
- * @param id - The id of the toast to update
67
- * @param options - The options for the toast
68
- * @returns The id of the toast
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
- * Remove a toast by id
73
- * If no id is provided, all current toasts will be removed
74
- * @param id - The id of the toast to remove
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
- * Remove all current toasts
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
- * Add a success toast
83
- * @param message - The message to display in the toast
84
- * @param options - The options for the toast
85
- * @returns The id of the toast
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
- * Add an error toast
90
- * @param message - The message to display in the toast
91
- * @param options - The options for the toast
92
- * @returns The id of the toast
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
- * Add a warning toast
97
- * @param message - The message to display in the toast
98
- * @param options - The options for the toast
99
- * @returns The id of the toast
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
- * Add a loading toast
104
- * @param message - The message to display in the toast
105
- * @param options - The options for the toast
106
- * @returns The id of the toast
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
- * Add a promise toast
111
- * @param promise - The promise to display in the toast
112
- * @param options - The options for the toast
113
- * @returns The id of the toast
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<ToastProps, "message">>;
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 L = Object.defineProperty;
2
- var M = (r, t, e) => t in r ? L(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
- var v = (r, t, e) => M(r, typeof t != "symbol" ? t + "" : t, e);
4
- import { ref as h, defineComponent as C, watch as $, onMounted as U, onUnmounted as A, createElementBlock as _, openBlock as d, withModifiers as D, normalizeStyle as E, createCommentVNode as k, createElementVNode as p, normalizeClass as O, toDisplayString as x, computed as B, createBlock as b, Teleport as P, Fragment as T, renderList as w, unref as m, createVNode as I, TransitionGroup as N, withCtx as R } from "vue";
5
- class S extends Map {
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, s) {
11
- return super.set(e, s), this.notify(), this;
10
+ set(e, o) {
11
+ return super.set(e, o), this.notify(), this;
12
12
  }
13
13
  delete(e) {
14
- const s = super.delete(e);
15
- return this.notify(), s;
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((s) => s !== e);
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 V = {
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 z {
36
+ class j {
37
37
  constructor() {
38
38
  v(this, "counter", 0);
39
39
  v(this, "toasts");
40
- this.toasts = new S();
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
- * Add a toast
61
- * @param options - The options for the toast
62
- * @returns The id of the toast
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
- * Update a toast by id
69
- * @param id - The id of the toast to update
70
- * @param options - The options for the toast
71
- * @returns The id of the toast
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
- * Remove a toast by id
78
- * If no id is provided, all current toasts will be removed
79
- * @param id - The id of the toast to remove
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
- * Remove all current toasts
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
- * Add a success toast
92
- * @param message - The message to display in the toast
93
- * @param options - The options for the toast
94
- * @returns The id of the toast
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
- * Add an error toast
101
- * @param message - The message to display in the toast
102
- * @param options - The options for the toast
103
- * @returns The id of the toast
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
- * Add a warning toast
110
- * @param message - The message to display in the toast
111
- * @param options - The options for the toast
112
- * @returns The id of the toast
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
- * Add a loading toast
119
- * @param message - The message to display in the toast
120
- * @param options - The options for the toast
121
- * @returns The id of the toast
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
- * Add a promise toast
128
- * @param promise - The promise to display in the toast
129
- * @param options - The options for the toast
130
- * @returns The id of the toast
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 s = this.loading(e.loading, { position: e.position, id: e.id });
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: s }), s;
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: s }), new Error(e.error);
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 y = new z();
142
- function F() {
303
+ const C = new j();
304
+ function z() {
143
305
  const r = h(/* @__PURE__ */ new Map());
144
- function t(n, o, a) {
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[o] = a);
309
+ a && "pause" in a && "resume" in a && (i[s] = a);
148
310
  }
149
311
  function e(n) {
150
- var o;
151
- (o = r.value.get(n)) == null || o.forEach((a) => a == null ? void 0 : a.pause());
312
+ var s;
313
+ (s = r.value.get(n)) == null || s.forEach((a) => a == null ? void 0 : a.pause());
152
314
  }
153
- function s(n) {
154
- var o;
155
- (o = r.value.get(n)) == null || o.forEach((a) => a == null ? void 0 : a.resume());
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: s
322
+ resumeAll: o
161
323
  };
162
324
  }
163
- const j = {
325
+ const F = {
164
326
  key: 0,
165
327
  class: "toasts-lite__icon"
166
- }, G = { class: "toasts-lite__content" }, H = { class: "toasts-lite__content-message" }, q = /* @__PURE__ */ C({
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 s = r, n = e, o = h(null), a = h(0), i = h(0);
340
+ const o = r, n = e, s = h(null), a = h(0), i = h(0);
179
341
  function c() {
180
- o.value && clearTimeout(o.value), n("close");
342
+ s.value && clearTimeout(s.value), n("close");
181
343
  }
182
344
  function u() {
183
- o.value && (clearTimeout(o.value), o.value = null, i.value = s.duration - (Date.now() - a.value));
345
+ s.value && (clearTimeout(s.value), s.value = null, i.value = o.duration - (Date.now() - a.value));
184
346
  }
185
347
  function l() {
186
- !o.value && i.value > 0 && (a.value = Date.now(), o.value = setTimeout(c, i.value));
348
+ !s.value && i.value > 0 && (a.value = Date.now(), s.value = setTimeout(c, i.value));
187
349
  }
188
350
  function f() {
189
- o.value && (clearTimeout(o.value), o.value = null), s.autoClose && (a.value = Date.now(), i.value = s.duration, o.value = setTimeout(c, s.duration));
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
- () => [s.autoClose, s.duration],
353
+ return L(
354
+ () => [o.autoClose, o.duration],
193
355
  () => {
194
356
  f();
195
357
  }
196
358
  ), U(() => {
197
359
  f();
198
- }), A(() => {
199
- o.value && clearTimeout(o.value);
360
+ }), k(() => {
361
+ s.value && clearTimeout(s.value);
200
362
  }), t({
201
363
  pause: u,
202
364
  resume: l
203
- }), (g, K) => (d(), _("div", {
365
+ }), (_, J) => (d(), g("div", {
204
366
  class: "toasts-lite__toast",
205
367
  style: E(`--toast-duration: ${r.duration}s;`),
206
- onClick: D(c, ["prevent"])
368
+ onClick: A(c, ["prevent"])
207
369
  }, [
208
- ["success", "error", "loading", "warn"].includes(r.type) ? (d(), _("div", j, [
370
+ ["success", "error", "loading", "warn"].includes(r.type) ? (d(), g("div", F, [
209
371
  p("div", {
210
- class: O(`toasts-lite__${r.type}`)
372
+ class: D(`toasts-lite__${r.type}`)
211
373
  }, null, 2)
212
- ])) : k("", !0),
213
- p("div", G, [
214
- p("div", H, x(r.message), 1)
374
+ ])) : x("", !0),
375
+ p("div", W, [
376
+ p("div", G, B(r.message), 1)
215
377
  ])
216
378
  ], 4));
217
379
  }
218
- }), J = ["onMouseenter", "onMouseleave"], X = /* @__PURE__ */ C({
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(y.toastList), { setRef: s, pauseAll: n, resumeAll: o } = F();
222
- y.onToastsListChange((i) => {
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 = B(() => {
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(), b(P, { to: "body" }, [
233
- (d(), _(T, null, w(t, (u) => p("div", {
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: O(["toasts-lite__toast-container", `toasts-lite__${u}`])
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(o)(u)
402
+ onMouseleave: (l) => m(s)(u)
241
403
  }, [
242
- I(N, {
404
+ N(R, {
243
405
  name: "toasts-lite",
244
406
  appear: ""
245
407
  }, {
246
- default: R(() => [
247
- (d(!0), _(T, null, w(a.value.get(u) || [], (l, f) => (d(), b(q, {
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: (g) => m(s)(u, f, g),
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: (g) => m(y).remove(l.id)
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, J)
424
+ ], 40, q)
263
425
  ], 2)), 64))
264
426
  ]));
265
427
  }
266
428
  });
267
429
  export {
268
430
  X as ToastsLiteProvider,
269
- y as toasts
431
+ C as toasts
270
432
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-toasts-lite",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "scheron",