vue-toasts-lite 0.1.2 → 0.1.4

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