@txstate-mws/svelte-components 1.6.5 → 1.6.7
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/stores/toasts.d.ts +5 -1
- package/dist/stores/toasts.js +14 -4
- package/package.json +1 -1
package/dist/stores/toasts.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface Toast {
|
|
|
5
5
|
type?: 'error' | 'warning' | 'info' | 'success';
|
|
6
6
|
/**
|
|
7
7
|
* how long the message should appear on screen in milliseconds
|
|
8
|
-
* default
|
|
8
|
+
* default 8 seconds for errors and warnings, 5 seconds for success and info
|
|
9
9
|
* use Number.MAX_SAFE_INTEGER for permanent
|
|
10
10
|
*/
|
|
11
11
|
ttlMs?: number;
|
|
@@ -15,10 +15,14 @@ export interface ToastStorage extends Required<Toast> {
|
|
|
15
15
|
id: string;
|
|
16
16
|
/** when the toast was created, so we know when it expires */
|
|
17
17
|
stamp: Date;
|
|
18
|
+
/** True if auto-expiration has been suspended (e.g. user is hovering mouse over the toast) */
|
|
19
|
+
suspended: boolean;
|
|
18
20
|
}
|
|
19
21
|
export declare class ToastStore extends Store<ToastStorage[]> {
|
|
20
22
|
add(msg: string, type?: Toast['type'], ttlMs?: number): void;
|
|
21
23
|
close(id: string): void;
|
|
24
|
+
suspend(id: string): void;
|
|
25
|
+
resume(id: string): void;
|
|
22
26
|
clean(): void;
|
|
23
27
|
}
|
|
24
28
|
export declare const toasts: ToastStore;
|
package/dist/stores/toasts.js
CHANGED
|
@@ -2,12 +2,15 @@ import { Store } from '@txstate-mws/svelte-store';
|
|
|
2
2
|
import { randomid } from 'txstate-utils';
|
|
3
3
|
export class ToastStore extends Store {
|
|
4
4
|
add(msg, type, ttlMs) {
|
|
5
|
+
const resolvedType = type ?? 'error';
|
|
6
|
+
const id = randomid();
|
|
5
7
|
const stored = {
|
|
6
|
-
id
|
|
8
|
+
id,
|
|
7
9
|
stamp: new Date(),
|
|
8
10
|
message: msg,
|
|
9
|
-
type:
|
|
10
|
-
|
|
11
|
+
type: resolvedType,
|
|
12
|
+
suspended: false,
|
|
13
|
+
ttlMs: ttlMs ?? (['error', 'warning'].includes(resolvedType) ? 8000 : 5000)
|
|
11
14
|
};
|
|
12
15
|
this.update(v => [...v.filter(t => t.message !== stored.message), stored]);
|
|
13
16
|
setTimeout(() => { this.clean(); }, stored.ttlMs);
|
|
@@ -15,9 +18,16 @@ export class ToastStore extends Store {
|
|
|
15
18
|
close(id) {
|
|
16
19
|
this.update(v => v.filter(t => t.id !== id));
|
|
17
20
|
}
|
|
21
|
+
suspend(id) {
|
|
22
|
+
this.update(v => v.map(t => t.id !== id ? t : { ...t, suspended: true }));
|
|
23
|
+
}
|
|
24
|
+
resume(id) {
|
|
25
|
+
this.update(v => v.map(t => t.id !== id ? t : { ...t, suspended: false }));
|
|
26
|
+
setTimeout(() => { this.clean(); }, 100);
|
|
27
|
+
}
|
|
18
28
|
clean() {
|
|
19
29
|
const now = new Date().getTime();
|
|
20
|
-
this.update(v => v.filter(t => now - t.stamp.getTime()
|
|
30
|
+
this.update(v => v.some(t => t.suspended) ? v : v.filter(t => now - t.stamp.getTime() <= t.ttlMs));
|
|
21
31
|
}
|
|
22
32
|
}
|
|
23
33
|
export const toasts = new ToastStore([]);
|