@ailuracode/alpine-toast 0.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,540 +1 @@
1
- // src/types.ts
2
- var TOAST_STORE_KEY = "toast";
3
-
4
- // src/config.ts
5
- var DEFAULT_PLUGIN_OPTIONS = {
6
- defaultPosition: "bottom-right",
7
- defaultDuration: 4e3,
8
- maxToasts: 5,
9
- listenToWindowEvents: true
10
- };
11
- function variantFromList(variants, preferred) {
12
- if (variants.includes(preferred)) {
13
- return preferred;
14
- }
15
- return "default";
16
- }
17
- function resolvePromiseConfig(variants, promise = {}) {
18
- return {
19
- loading: promise.loading ?? "Loading...",
20
- error: promise.error ?? "Error",
21
- duration: promise.duration ?? 4e3,
22
- loadingVariant: promise.loadingVariant ?? variantFromList(variants, "loading"),
23
- successVariant: promise.successVariant ?? variantFromList(variants, "success"),
24
- errorVariant: promise.errorVariant ?? variantFromList(variants, "error")
25
- };
26
- }
27
- function resolveToastPluginConfig(options = {}) {
28
- const variants = options.variants ?? [];
29
- const positions = options.positions ?? [];
30
- const maxToasts = options.maxToasts ?? DEFAULT_PLUGIN_OPTIONS.maxToasts;
31
- let maxVisible = options.maxVisible ?? maxToasts;
32
- if (maxToasts > 0 && maxVisible > maxToasts) {
33
- maxVisible = maxToasts;
34
- }
35
- return {
36
- defaultPosition: options.defaultPosition ?? DEFAULT_PLUGIN_OPTIONS.defaultPosition,
37
- defaultDuration: options.defaultDuration ?? DEFAULT_PLUGIN_OPTIONS.defaultDuration,
38
- maxToasts,
39
- maxVisible,
40
- listenToWindowEvents: options.listenToWindowEvents ?? DEFAULT_PLUGIN_OPTIONS.listenToWindowEvents,
41
- storeKey: options.storeKey ?? TOAST_STORE_KEY,
42
- variants,
43
- positions,
44
- promise: resolvePromiseConfig(variants, options.promise)
45
- };
46
- }
47
- function toastOptions(options) {
48
- return options;
49
- }
50
- function toastVariants(variants) {
51
- return variants;
52
- }
53
- function toastPositions(positions) {
54
- return positions;
55
- }
56
-
57
- // src/store.ts
58
- function resolveToastLimits(options = {}) {
59
- const maxToasts = options.maxToasts ?? 5;
60
- let maxVisible = options.maxVisible ?? maxToasts;
61
- if (maxToasts > 0 && maxVisible > maxToasts) {
62
- maxVisible = maxToasts;
63
- }
64
- return { maxToasts, maxVisible };
65
- }
66
- function resolveStackPositions(defaultPosition, positions) {
67
- const stacks = [defaultPosition];
68
- for (const position of positions ?? []) {
69
- if (!stacks.includes(position)) {
70
- stacks.push(position);
71
- }
72
- }
73
- return stacks;
74
- }
75
- function createToastId() {
76
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
77
- return crypto.randomUUID();
78
- }
79
- return `toast-${Date.now()}-${Math.random().toString(36).slice(2)}`;
80
- }
81
- function normalizeToastDuration(duration) {
82
- if (duration === 0) {
83
- return false;
84
- }
85
- return duration;
86
- }
87
- function resolveToastDuration(duration, defaultDuration) {
88
- if (duration === void 0) {
89
- return normalizeToastDuration(defaultDuration);
90
- }
91
- return normalizeToastDuration(duration);
92
- }
93
- function shouldAutoDismiss(duration) {
94
- return typeof duration === "number" && duration > 0 && Number.isFinite(duration);
95
- }
96
- function isPersistentDuration(duration) {
97
- return !shouldAutoDismiss(duration);
98
- }
99
- var PROMISE_LOADING_DURATION = 36e5;
100
- function itemsAtPosition(items, position) {
101
- return items.filter((item) => item.position === position);
102
- }
103
- function timedStackAt(items, position) {
104
- return itemsAtPosition(items, position).filter((item) => shouldAutoDismiss(item.duration));
105
- }
106
- function persistentStackAt(items, position) {
107
- return itemsAtPosition(items, position).filter((item) => isPersistentDuration(item.duration));
108
- }
109
- function enforcePositionLimit(items, position, maxToasts, dismissMany, stack) {
110
- if (maxToasts <= 0) {
111
- return;
112
- }
113
- const stackAtPosition = stack === "persistent" ? persistentStackAt(items, position) : timedStackAt(items, position);
114
- const activeAtPosition = stackAtPosition.filter((item) => !item.removed);
115
- const overflowIds = activeAtPosition.slice(maxToasts).map((item) => item.id);
116
- dismissMany(overflowIds);
117
- }
118
- function applyToastPatch(item, payload) {
119
- const next = { ...item };
120
- for (const [key, value] of Object.entries(payload)) {
121
- if (value !== void 0) {
122
- if (key === "duration") {
123
- Object.assign(next, { duration: normalizeToastDuration(value) });
124
- } else {
125
- Object.assign(next, { [key]: value });
126
- }
127
- }
128
- }
129
- return next;
130
- }
131
- function createToastStore(options = {}) {
132
- const defaultPosition = options.defaultPosition ?? "bottom-right";
133
- const stackPositions = resolveStackPositions(defaultPosition, options.positions);
134
- const defaultDuration = options.defaultDuration ?? 4e3;
135
- const { maxToasts, maxVisible } = resolveToastLimits(options);
136
- const dismissDelayMs = 400;
137
- const dismissTimers = /* @__PURE__ */ new Map();
138
- const purgeTimers = /* @__PURE__ */ new Map();
139
- const getStore = options.getStore;
140
- const store = {
141
- defaultPosition,
142
- stackPositions,
143
- maxToasts,
144
- maxVisible,
145
- items: [],
146
- itemsAt(position) {
147
- return itemsAtPosition(this.items, position);
148
- },
149
- timedItemsAt(position) {
150
- return timedStackAt(this.items, position);
151
- },
152
- persistentItemsAt(position) {
153
- return persistentStackAt(this.items, position);
154
- },
155
- activeTimedItemsAt(position) {
156
- return this.timedItemsAt(position).filter((item) => !item.removed);
157
- },
158
- activePersistentItemsAt(position) {
159
- return this.persistentItemsAt(position).filter((item) => !item.removed);
160
- },
161
- isVisibleAt(position, index) {
162
- const stack = this.timedItemsAt(position);
163
- const item = stack[index];
164
- if (!item || item.removed) {
165
- return false;
166
- }
167
- if (this.maxVisible <= 0) {
168
- return true;
169
- }
170
- let rank = 0;
171
- for (let i = 0; i <= index; i++) {
172
- if (stack[i] && !stack[i].removed) {
173
- rank++;
174
- }
175
- }
176
- return rank <= this.maxVisible;
177
- },
178
- push(payload = {}) {
179
- const position = payload.position ?? this.defaultPosition;
180
- const id = createToastId();
181
- const toast = {
182
- id,
183
- key: payload.key ?? null,
184
- content: payload.content ?? null,
185
- title: payload.title ?? null,
186
- description: payload.description ?? null,
187
- variant: payload.variant ?? "default",
188
- position,
189
- duration: resolveToastDuration(payload.duration, defaultDuration),
190
- action: payload.action ?? null,
191
- removed: false
192
- };
193
- this.items = [toast, ...this.items];
194
- enforcePositionLimit(
195
- this.items,
196
- position,
197
- this.maxToasts,
198
- (ids) => markRemoved(ids),
199
- shouldAutoDismiss(toast.duration) ? "timed" : "persistent"
200
- );
201
- scheduleDismiss(id, toast.duration);
202
- return id;
203
- },
204
- pushUnique(key, payload = {}) {
205
- const activeIds = this.items.filter((item) => !item.removed && item.key === key).map((item) => item.id);
206
- markRemoved(activeIds);
207
- return this.push({ ...payload, key });
208
- },
209
- update(id, payload = {}) {
210
- const current = this.items.find((toast) => toast.id === id);
211
- if (!current) {
212
- return;
213
- }
214
- const previousPosition = current.position;
215
- const wasPersistent = isPersistentDuration(current.duration);
216
- this.items = this.items.map(
217
- (item) => item.id === id ? applyToastPatch(item, payload) : item
218
- );
219
- const updated = this.items.find((toast) => toast.id === id);
220
- if (!updated) {
221
- return;
222
- }
223
- const nextPosition = updated.position;
224
- const isNowPersistent = isPersistentDuration(updated.duration);
225
- const durationStackChanged = payload.duration !== void 0 && wasPersistent !== isNowPersistent;
226
- const positionChanged = nextPosition !== previousPosition;
227
- if (positionChanged) {
228
- enforcePositionLimit(
229
- this.items,
230
- previousPosition,
231
- this.maxToasts,
232
- (ids) => markRemoved(ids),
233
- "timed"
234
- );
235
- enforcePositionLimit(
236
- this.items,
237
- previousPosition,
238
- this.maxToasts,
239
- (ids) => markRemoved(ids),
240
- "persistent"
241
- );
242
- }
243
- if (positionChanged || durationStackChanged) {
244
- enforcePositionLimit(
245
- this.items,
246
- nextPosition,
247
- this.maxToasts,
248
- (ids) => markRemoved(ids),
249
- "timed"
250
- );
251
- enforcePositionLimit(
252
- this.items,
253
- nextPosition,
254
- this.maxToasts,
255
- (ids) => markRemoved(ids),
256
- "persistent"
257
- );
258
- }
259
- if (payload.duration !== void 0) {
260
- scheduleDismiss(id, updated.duration);
261
- }
262
- },
263
- dismiss(id) {
264
- markRemoved([id]);
265
- },
266
- dismissAt(position) {
267
- const ids = this.itemsAt(position).filter((item) => !item.removed).map((item) => item.id);
268
- markRemoved(ids);
269
- },
270
- dismissAll() {
271
- const ids = this.items.filter((item) => !item.removed).map((item) => item.id);
272
- markRemoved(ids);
273
- },
274
- destroy() {
275
- for (const timer of dismissTimers.values()) {
276
- clearTimeout(timer);
277
- }
278
- dismissTimers.clear();
279
- for (const timer of purgeTimers.values()) {
280
- clearTimeout(timer);
281
- }
282
- purgeTimers.clear();
283
- }
284
- };
285
- function clearDismissTimer(id) {
286
- const timer = dismissTimers.get(id);
287
- if (timer !== void 0) {
288
- clearTimeout(timer);
289
- dismissTimers.delete(id);
290
- }
291
- }
292
- function markRemoved(ids) {
293
- if (ids.length === 0) {
294
- return;
295
- }
296
- const activeStore = getStore?.() ?? store;
297
- const idsToRemove = [];
298
- for (const id of ids) {
299
- const toast = activeStore.items.find((item) => item.id === id);
300
- if (!toast || toast.removed) {
301
- continue;
302
- }
303
- clearDismissTimer(id);
304
- idsToRemove.push(id);
305
- }
306
- if (idsToRemove.length === 0) {
307
- return;
308
- }
309
- const removeSet = new Set(idsToRemove);
310
- activeStore.items = activeStore.items.map(
311
- (item) => removeSet.has(item.id) ? { ...item, removed: true } : item
312
- );
313
- const batchId = createToastId();
314
- const timer = setTimeout(() => {
315
- purgeTimers.delete(batchId);
316
- const storeForFilter = getStore?.() ?? store;
317
- storeForFilter.items = storeForFilter.items.filter((item) => !removeSet.has(item.id));
318
- }, dismissDelayMs);
319
- purgeTimers.set(batchId, timer);
320
- }
321
- function scheduleDismiss(id, duration) {
322
- clearDismissTimer(id);
323
- if (duration === PROMISE_LOADING_DURATION) {
324
- return;
325
- }
326
- if (shouldAutoDismiss(duration)) {
327
- const timer = setTimeout(() => {
328
- dismissTimers.delete(id);
329
- (getStore?.() ?? store).dismiss(id);
330
- }, duration);
331
- dismissTimers.set(id, timer);
332
- }
333
- }
334
- return store;
335
- }
336
-
337
- // src/magic.ts
338
- var RESERVED_TOAST_MAGIC_KEYS = /* @__PURE__ */ new Set([
339
- "dismiss",
340
- "update",
341
- "dismissAt",
342
- "dismissAll",
343
- "pushUnique",
344
- "fromPayload",
345
- "promise"
346
- ]);
347
- function pushToast(store, titleOrPayload, options = {}) {
348
- if (typeof titleOrPayload === "string") {
349
- return store.push({
350
- title: titleOrPayload,
351
- ...options
352
- });
353
- }
354
- return store.push(titleOrPayload);
355
- }
356
- function pushVariantToast(store, variant, titleOrPayload, options = {}) {
357
- if (typeof titleOrPayload === "string") {
358
- return store.push({
359
- title: titleOrPayload,
360
- ...options,
361
- variant
362
- });
363
- }
364
- return store.push({
365
- ...titleOrPayload,
366
- ...options,
367
- variant
368
- });
369
- }
370
- function resolveSuccessTitle(data, messages) {
371
- if (typeof messages.success === "function") {
372
- return messages.success(data);
373
- }
374
- return messages.success;
375
- }
376
- function resolveSuccessContent(data, messages) {
377
- const { successContent } = messages;
378
- if (typeof successContent === "function") {
379
- return successContent(data);
380
- }
381
- return successContent;
382
- }
383
- function resolveErrorTitle(messages, fallbackError) {
384
- return messages.error ?? fallbackError;
385
- }
386
- function resolveToastPromise(factoryOrPromise) {
387
- if (typeof factoryOrPromise === "function") {
388
- return Promise.resolve(factoryOrPromise());
389
- }
390
- return Promise.resolve(factoryOrPromise);
391
- }
392
- function buildPromiseSuccessPatch(data, messages, successVariant, settledDuration) {
393
- const successTitle = resolveSuccessTitle(data, messages);
394
- const successContent = resolveSuccessContent(data, messages);
395
- return {
396
- ...successTitle !== void 0 ? { title: successTitle } : {},
397
- ...successContent !== void 0 ? { content: successContent } : {},
398
- variant: successVariant,
399
- duration: settledDuration
400
- };
401
- }
402
- function buildPromiseErrorPatch(messages, promiseConfig, errorVariant, settledDuration) {
403
- return {
404
- title: resolveErrorTitle(messages, promiseConfig.error),
405
- ...messages.errorContent !== void 0 ? { content: messages.errorContent } : {},
406
- variant: errorVariant,
407
- duration: settledDuration
408
- };
409
- }
410
- function createToastMagic(config, getStore) {
411
- const magic = ((titleOrPayload, options) => pushToast(getStore(), titleOrPayload, options ?? {}));
412
- magic.dismiss = (id) => getStore().dismiss(id);
413
- magic.update = (id, payload) => getStore().update(id, payload);
414
- magic.dismissAt = (position) => getStore().dismissAt(position);
415
- magic.dismissAll = () => getStore().dismissAll();
416
- magic.pushUnique = (key, payload) => getStore().pushUnique(key, payload ?? {});
417
- magic.fromPayload = (payload = {}) => {
418
- const { title = null, content = null, variant = "default", ...options } = payload;
419
- return pushToast(getStore(), {
420
- title,
421
- content,
422
- variant,
423
- ...options
424
- });
425
- };
426
- magic.promise = async (factoryOrPromise, messages = {}) => {
427
- const promiseConfig = config.promise;
428
- const loadingVariant = messages.loadingVariant ?? promiseConfig.loadingVariant;
429
- const successVariant = messages.successVariant ?? promiseConfig.successVariant;
430
- const errorVariant = messages.errorVariant ?? promiseConfig.errorVariant;
431
- const settledDuration = messages.duration ?? promiseConfig.duration;
432
- const id = getStore().push({
433
- title: messages.loading ?? promiseConfig.loading,
434
- content: messages.loadingContent ?? null,
435
- variant: loadingVariant,
436
- duration: PROMISE_LOADING_DURATION
437
- });
438
- try {
439
- const data = await resolveToastPromise(factoryOrPromise);
440
- getStore().update(
441
- id,
442
- buildPromiseSuccessPatch(data, messages, successVariant, settledDuration)
443
- );
444
- return data;
445
- } catch (error) {
446
- getStore().update(
447
- id,
448
- buildPromiseErrorPatch(messages, promiseConfig, errorVariant, settledDuration)
449
- );
450
- throw error;
451
- }
452
- };
453
- for (const variant of config.variants) {
454
- if (RESERVED_TOAST_MAGIC_KEYS.has(variant)) {
455
- continue;
456
- }
457
- Object.assign(magic, {
458
- [variant]: (titleOrPayload, options = {}) => pushVariantToast(getStore(), variant, titleOrPayload, options)
459
- });
460
- }
461
- return magic;
462
- }
463
-
464
- // src/index.ts
465
- function registerAlpineToastStore(Alpine, storeKey, store) {
466
- const registerStore = Alpine.store;
467
- registerStore(storeKey, store);
468
- }
469
- function getToastStore(Alpine, storeKey) {
470
- return Alpine.store(storeKey);
471
- }
472
- function registerToastPlugin(Alpine, options = {}) {
473
- const config = resolveToastPluginConfig(options);
474
- let reactiveStore;
475
- let windowEventsAbort;
476
- const store = createToastStore({
477
- defaultPosition: config.defaultPosition,
478
- positions: config.positions,
479
- defaultDuration: config.defaultDuration,
480
- maxToasts: config.maxToasts,
481
- maxVisible: config.maxVisible,
482
- getStore: () => reactiveStore ?? store
483
- });
484
- const baseDestroy = store.destroy.bind(store);
485
- store.destroy = () => {
486
- windowEventsAbort?.abort();
487
- windowEventsAbort = void 0;
488
- baseDestroy();
489
- };
490
- registerAlpineToastStore(Alpine, config.storeKey, store);
491
- reactiveStore = getToastStore(Alpine, config.storeKey);
492
- const toast = createToastMagic(
493
- config,
494
- () => getToastStore(Alpine, config.storeKey)
495
- );
496
- Alpine.magic("toast", () => toast);
497
- if (config.listenToWindowEvents && typeof window !== "undefined") {
498
- windowEventsAbort = new AbortController();
499
- window.addEventListener(
500
- "toast",
501
- (event) => {
502
- if (event instanceof CustomEvent) {
503
- toast.fromPayload(
504
- event.detail ?? {}
505
- );
506
- }
507
- },
508
- { signal: windowEventsAbort.signal }
509
- );
510
- }
511
- }
512
- function toastPlugin(optionsOrAlpine) {
513
- if (optionsOrAlpine && typeof optionsOrAlpine.magic === "function") {
514
- registerToastPlugin(optionsOrAlpine, {});
515
- return;
516
- }
517
- const options = optionsOrAlpine ?? {};
518
- return (Alpine) => {
519
- registerToastPlugin(Alpine, options);
520
- };
521
- }
522
- export {
523
- PROMISE_LOADING_DURATION,
524
- RESERVED_TOAST_MAGIC_KEYS,
525
- TOAST_STORE_KEY,
526
- createToastMagic,
527
- createToastStore,
528
- toastPlugin as default,
529
- isPersistentDuration,
530
- normalizeToastDuration,
531
- resolveStackPositions,
532
- resolveToastDuration,
533
- resolveToastLimits,
534
- resolveToastPluginConfig,
535
- shouldAutoDismiss,
536
- toastOptions,
537
- toastPositions,
538
- toastVariants
539
- };
540
- //# sourceMappingURL=index.js.map
1
+ import{BaseController as _,createSingleton as M,generateId as h,releaseSingleton as K}from"@ailuracode/alpine-core";var c="toast",C="toast";var R=400,U=5,W=4e3,A="@ailuracode/alpine-toast/default";function x(s={}){let{scope:t,...o}=s;return M(A,()=>{let n=new y(o);return n.mount(),n},{scope:t})}var y=class extends _{#e;#l;#d;#a;#r;#y;#u;#P;#s=new Map;#T=new Map;#t=[];constructor(t){super(t.id??h("toast")),this.#e=t.defaultPosition??"bottom-right",this.#l=S(this.#e,t.positions),this.#d=t.defaultDuration??W;let o=I({maxToasts:t.maxToasts,maxVisible:t.maxVisible});this.#a=o.maxToasts,this.#r=o.maxVisible,this.#y=t.storeKey??c,this.#u=t.listenToWindowEvents!==!1,this.#P=t.getStore}destroy(){this.isDestroyed||(super.destroy(),this.#p(),K(A,this))}mount(){if(!this.isMounted){if(super.mount(),this.#u&&typeof window<"u"){let t=new AbortController,o=n=>{n instanceof CustomEvent&&this.fromPayload(n.detail??{})};window.addEventListener("toast",o,{signal:t.signal}),this.registerCleanup(()=>t.abort())}queueMicrotask(()=>{this.isDestroyed||this.#o("initialization")})}}get defaultPosition(){return this.#e}get stackPositions(){return this.#l}get maxToasts(){return this.#a}get maxVisible(){return this.#r}get items(){return this.#t}push(t={}){if(this.isDestroyed)return"";let o=t.position??this.#e,n=h("toast"),i={id:n,key:t.key??null,content:t.content??null,title:t.title??null,description:t.description??null,variant:t.variant??"default",position:o,duration:b(t.duration,this.#d),action:t.action??null,removed:!1};return this.#t=[i,...this.#t],this.#i(o,u(i.duration)?"timed":"persistent"),this.#m(n,i.duration),this.#o("push"),n}pushUnique(t,o={}){if(this.isDestroyed)return"";let n=this.#t.filter(i=>!i.removed&&i.key===t).map(i=>i.id);return this.#n(n),this.push({...o,key:t})}update(t,o={}){if(this.isDestroyed)return;let n=this.#t.find(T=>T.id===t);if(!n)return;let i=n.position,a=m(n.duration);this.#t=this.#t.map(T=>T.id===t?N(T,o):T);let e=this.#t.find(T=>T.id===t);if(!e)return;let r=e.position,l=m(e.duration),P=o.duration!==void 0&&a!==l,d=r!==i;d&&(this.#i(i,"timed"),this.#i(i,"persistent")),(d||P)&&(this.#i(r,"timed"),this.#i(r,"persistent")),o.duration!==void 0&&this.#m(t,e.duration),this.#o("update")}dismiss(t){this.isDestroyed||(this.#n([t]),this.#o("dismiss"))}dismissAt(t){if(this.isDestroyed)return;let o=this.itemsAt(t).filter(n=>!n.removed).map(n=>n.id);this.#n(o),this.#o("dismissAt")}dismissAll(){if(this.isDestroyed)return;let t=this.#t.filter(o=>!o.removed).map(o=>o.id);this.#n(t),this.#o("dismissAll")}itemsAt(t){return this.#t.filter(o=>o.position===t)}timedItemsAt(t){return this.itemsAt(t).filter(o=>u(o.duration))}persistentItemsAt(t){return this.itemsAt(t).filter(o=>m(o.duration))}activeTimedItemsAt(t){return this.timedItemsAt(t).filter(o=>!o.removed)}activePersistentItemsAt(t){return this.persistentItemsAt(t).filter(o=>!o.removed)}isVisibleAt(t,o){let n=this.timedItemsAt(t),i=n[o];if(!i||i.removed)return!1;if(this.#r<=0)return!0;let a=0;for(let e=0;e<=o;e++)n[e]&&!n[e].removed&&a++;return a<=this.#r}fromPayload(t={}){let{title:o=null,content:n=null,variant:i="default",...a}=t;return this.push({title:o,content:n,variant:i,...a})}#o(t){if(this.isDestroyed)return;let o={source:t,items:this.#t};this.emit("change",o)}#n(t){if(t.length===0)return;let o=[];for(let e of t){let r=this.#t.find(l=>l.id===e);!r||r.removed||(this.#c(e),o.push(e))}if(o.length===0)return;let n=new Set(o);this.#t=this.#t.map(e=>n.has(e.id)?{...e,removed:!0}:e);let i=h("toast-purge"),a=setTimeout(()=>{this.#T.delete(i),!this.isDestroyed&&(this.#t=this.#t.filter(e=>!n.has(e.id)),this.#o("dismiss"))},R);this.#T.set(i,a)}#i(t,o){if(this.#a<=0)return;let n=this.itemsAt(t),e=(o==="persistent"?n.filter(r=>m(r.duration)):n.filter(r=>u(r.duration))).filter(r=>!r.removed).slice(this.#a).map(r=>r.id);this.#n(e)}#m(t,o){if(this.#c(t),o!==g&&u(o)){let n=setTimeout(()=>{if(this.#s.delete(t),this.isDestroyed)return;let i=this.#P?.()??null;i?i.dismiss(t):this.dismiss(t)},o);this.#s.set(t,n)}}#c(t){let o=this.#s.get(t);o!==void 0&&(clearTimeout(o),this.#s.delete(t))}#p(){for(let t of this.#s.values())clearTimeout(t);this.#s.clear();for(let t of this.#T.values())clearTimeout(t);this.#T.clear()}};function f(s){let t={defaultPosition:s.defaultPosition,stackPositions:s.stackPositions,maxToasts:s.maxToasts,maxVisible:s.maxVisible,items:[...s.items],push(n){return s.push(n)},pushUnique(n,i){return s.pushUnique(n,i??{})},update(n,i){s.update(n,i)},dismiss(n){s.dismiss(n)},dismissAt(n){s.dismissAt(n)},dismissAll(){s.dismissAll()},destroy(){o(),s.destroy()},itemsAt(n){return this.items.filter(i=>i.position===n)},timedItemsAt(n){return this.itemsAt(n).filter(i=>u(i.duration))},persistentItemsAt(n){return this.itemsAt(n).filter(i=>m(i.duration))},activeTimedItemsAt(n){return this.timedItemsAt(n).filter(i=>!i.removed)},activePersistentItemsAt(n){return this.persistentItemsAt(n).filter(i=>!i.removed)},isVisibleAt(n,i){let a=this.timedItemsAt(n),e=a[i];if(!e||e.removed)return!1;if(this.maxVisible<=0)return!0;let r=0;for(let l=0;l<=i;l++)a[l]&&!a[l].removed&&r++;return r<=this.maxVisible}},o=s.on("change",n=>{t.items=[...n.items]});return t}function L(s={}){let t=new y(s);return t.mount(),f(t)}var g=36e5;function p(s){return s===0?!1:s}function b(s,t){return p(s===void 0?t:s)}function u(s){return typeof s=="number"&&s>0&&Number.isFinite(s)}function m(s){return!u(s)}function I(s={}){let t=s.maxToasts??U,o=s.maxVisible??t;return t>0&&o>t&&(o=t),{maxToasts:t,maxVisible:o}}function S(s,t){let o=[s];for(let n of t??[])o.includes(n)||o.push(n);return o}function N(s,t){let o={...s};return t.content!==void 0&&(o={...o,content:t.content}),t.title!==void 0&&(o={...o,title:t.title}),t.description!==void 0&&(o={...o,description:t.description}),t.variant!==void 0&&(o={...o,variant:t.variant}),t.position!==void 0&&(o={...o,position:t.position}),t.duration!==void 0&&(o={...o,duration:p(t.duration)}),t.action!==void 0&&(o={...o,action:t.action}),t.key!==void 0&&(o={...o,key:t.key}),o}import{bridgeControllerStore as Y}from"@ailuracode/alpine-core";var V={defaultPosition:"bottom-right",defaultDuration:4e3,maxToasts:5,listenToWindowEvents:!0},k=new Set(["dismiss","update","dismissAt","dismissAll","pushUnique","fromPayload","promise"]);function v(s,t){return s.includes(t)?t:"default"}function q(s,t={}){return{loading:t.loading??"Loading...",error:t.error??"Error",duration:t.duration??4e3,loadingVariant:t.loadingVariant??v(s,"loading"),successVariant:t.successVariant??v(s,"success"),errorVariant:t.errorVariant??v(s,"error")}}function O(s={}){let t=s.variants??[],o=s.positions??[],n=s.maxToasts??V.maxToasts,i=s.maxVisible??n;return n>0&&i>n&&(i=n),{defaultPosition:s.defaultPosition??V.defaultPosition,defaultDuration:s.defaultDuration??V.defaultDuration,maxToasts:n,maxVisible:i,listenToWindowEvents:s.listenToWindowEvents??V.listenToWindowEvents,storeKey:s.storeKey??c,magicKey:s.magicKey??C,variants:t,positions:o,promise:q(t,s.promise)}}function G(s){return s}function F(s){return s}function z(s){return s}function w(s={}){return function(o){let n=o,i={defaultPosition:s.defaultPosition,positions:s.positions,defaultDuration:s.defaultDuration,maxToasts:s.maxToasts,maxVisible:s.maxVisible,listenToWindowEvents:s.listenToWindowEvents,storeKey:s.storeKey},a=x(i),e=O(s),r=f(a),l=E(e,()=>r);Y({alpine:n,storeKey:e.storeKey,magicKey:e.magicKey,store:r,controller:a,packageName:"toast",magicAccessor:()=>l,subscribe:P=>{let d=P;return a.on("change",T=>{d.items=[...T.items]})}})}}function E(s,t){let o=((n,i)=>D(t(),n,i??{}));o.dismiss=n=>t().dismiss(n),o.update=(n,i)=>t().update(n,i),o.dismissAt=n=>t().dismissAt(n),o.dismissAll=()=>t().dismissAll(),o.pushUnique=(n,i)=>t().pushUnique(n,i??{}),o.fromPayload=(n={})=>{let{title:i=null,content:a=null,variant:e="default",...r}=n;return D(t(),{title:i,content:a,variant:e,...r})},o.promise=async(n,i={})=>{let a=s.promise,e=i.loadingVariant??a.loadingVariant,r=i.successVariant??a.successVariant,l=i.errorVariant??a.errorVariant,P=i.duration??a.duration,d=t().push({title:i.loading??a.loading,content:i.loadingContent??null,variant:e,duration:g});try{let T=await J(n);return t().update(d,Q(T,i,r,P)),T}catch(T){throw t().update(d,Z(i,a,l,P)),T}};for(let n of s.variants)k.has(n)||Object.assign(o,{[n]:(i,a={})=>j(t(),n,i,a)});return o}function D(s,t,o={}){return typeof t=="string"?s.push({title:t,...o}):s.push(t)}function j(s,t,o,n={}){return typeof o=="string"?s.push({title:o,...n,variant:t}):s.push({...o,...n,variant:t})}function B(s,t){return typeof t.success=="function"?t.success(s):t.success}function X(s,t){let{successContent:o}=t;return typeof o=="function"?o(s):o}function H(s,t){return s.error??t}function J(s){return typeof s=="function"?Promise.resolve(s()):Promise.resolve(s)}function Q(s,t,o,n){let i=B(s,t),a=X(s,t);return{...i!==void 0?{title:i}:{},...a!==void 0?{content:a}:{},variant:o,duration:n}}function Z(s,t,o,n){return{title:H(s,t.error),...s.errorContent!==void 0?{content:s.errorContent}:{},variant:o,duration:n}}export{g as PROMISE_LOADING_DURATION,k as RESERVED_TOAST_MAGIC_KEYS,C as TOAST_MAGIC_KEY,c as TOAST_STORE_KEY,y as ToastController,x as createToastController,E as createToastMagic,L as createToastStore,w as default,m as isPersistentDuration,p as normalizeToastDuration,S as resolveStackPositions,b as resolveToastDuration,I as resolveToastLimits,O as resolveToastPluginConfig,u as shouldAutoDismiss,G as toastOptions,w as toastPlugin,z as toastPositions,F as toastVariants,f as wrapToastStore};
package/package.json CHANGED
@@ -1,26 +1,28 @@
1
1
  {
2
2
  "name": "@ailuracode/alpine-toast",
3
- "version": "0.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "Alpine.js in-app toast queue magic",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": "ailuracode",
8
+ "sideEffects": false,
8
9
  "publishConfig": {
9
10
  "access": "public"
10
11
  },
11
12
  "repository": {
12
13
  "type": "git",
13
- "url": "git+https://github.com/ailuracode/alpine.git",
14
+ "url": "git+https://github.com/ailuracode/alpinejs-toolkit.git",
14
15
  "directory": "packages/toast"
15
16
  },
16
17
  "bugs": {
17
- "url": "https://github.com/ailuracode/alpine/issues"
18
+ "url": "https://github.com/ailuracode/alpinejs-toolkit/issues"
18
19
  },
19
- "homepage": "https://github.com/ailuracode/alpine/tree/master/packages/toast#readme",
20
+ "homepage": "https://github.com/ailuracode/alpinejs-toolkit/tree/master/packages/toast#readme",
20
21
  "files": [
21
22
  "dist",
22
23
  "README.md"
23
24
  ],
25
+ "types": "./dist/global.d.ts",
24
26
  "exports": {
25
27
  ".": {
26
28
  "types": "./dist/index.d.ts",
@@ -30,16 +32,20 @@
30
32
  "types": "./dist/global.d.ts"
31
33
  }
32
34
  },
33
- "types": "./dist/global.d.ts",
34
35
  "peerDependencies": {
35
- "alpinejs": "^3.0.0",
36
- "@types/alpinejs": "^3.13.11"
36
+ "@ailuracode/alpine-core": "^0.3.0",
37
+ "alpinejs": "^3.0.0"
37
38
  },
38
39
  "peerDependenciesMeta": {
39
40
  "@types/alpinejs": {
40
41
  "optional": true
41
42
  }
42
43
  },
44
+ "devDependencies": {
45
+ "@types/alpinejs": "^3.13.11",
46
+ "alpinejs": "^3.15.12",
47
+ "@ailuracode/alpine-core": "0.3.0"
48
+ },
43
49
  "keywords": [
44
50
  "alpinejs",
45
51
  "alpine",
@@ -48,8 +54,29 @@
48
54
  "sonner",
49
55
  "notifications"
50
56
  ],
57
+ "toolkit": {
58
+ "bundleBudget": {
59
+ "category": "complex-feature"
60
+ }
61
+ },
62
+ "size-limit": [
63
+ {
64
+ "name": "full surface",
65
+ "path": "dist/index.js",
66
+ "import": "*",
67
+ "ignore": [
68
+ "alpinejs",
69
+ "@ailuracode/alpine-core"
70
+ ],
71
+ "gzip": true,
72
+ "brotli": true,
73
+ "limit": "3.7 kB"
74
+ }
75
+ ],
51
76
  "scripts": {
52
- "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
53
- "test": "vitest run --config ../../vitest.config.ts test"
77
+ "build": "tsup src/index.ts --format esm --dts --clean --out-dir dist --minify && cp src/global.d.ts dist/global.d.ts",
78
+ "size": "size-limit",
79
+ "test": "vitest run --config ../../vitest.config.ts packages/toast",
80
+ "test:e2e": "playwright test --config playwright.config.ts"
54
81
  }
55
82
  }