varsel 0.5.1 → 0.5.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/dist/VarselItem.svelte +900 -0
- package/dist/VarselItem.svelte.d.ts +23 -0
- package/dist/VarselItem.svelte.d.ts.map +1 -0
- package/dist/VarselManager.svelte +451 -0
- package/dist/VarselManager.svelte.d.ts +18 -0
- package/dist/VarselManager.svelte.d.ts.map +1 -0
- package/dist/VarselToaster.svelte +104 -0
- package/dist/VarselToaster.svelte.d.ts +32 -0
- package/dist/VarselToaster.svelte.d.ts.map +1 -0
- package/dist/core/accessibility.d.ts +6 -0
- package/dist/core/accessibility.d.ts.map +1 -0
- package/dist/core/accessibility.js +12 -0
- package/dist/core/animations.d.ts +29 -0
- package/dist/core/animations.d.ts.map +1 -0
- package/dist/core/animations.js +28 -0
- package/dist/core/positions.d.ts +71 -0
- package/dist/core/positions.d.ts.map +1 -0
- package/dist/core/positions.js +30 -0
- package/dist/core/swipe.d.ts +20 -0
- package/dist/core/swipe.d.ts.map +1 -0
- package/dist/core/swipe.js +30 -0
- package/dist/core/toast-factory.d.ts +3 -0
- package/dist/core/toast-factory.d.ts.map +1 -0
- package/dist/core/toast-factory.js +144 -0
- package/dist/core/toast-state.d.ts +49 -0
- package/dist/core/toast-state.d.ts.map +1 -0
- package/dist/core/toast-state.js +80 -0
- package/dist/core/toaster-instances.d.ts +28 -0
- package/dist/core/toaster-instances.d.ts.map +1 -0
- package/dist/core/toaster-instances.js +46 -0
- package/dist/core/types.d.ts +144 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +1 -0
- package/dist/core/utils.d.ts +10 -0
- package/dist/core/utils.d.ts.map +1 -0
- package/dist/core/utils.js +9 -0
- package/dist/core/variants.d.ts +18 -0
- package/dist/core/variants.d.ts.map +1 -0
- package/dist/core/variants.js +56 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/internals.d.ts +16 -0
- package/dist/internals.d.ts.map +1 -0
- package/dist/internals.js +14 -0
- package/dist/styles.css +195 -0
- package/dist/variant-icons.d.ts +108 -0
- package/dist/variant-icons.d.ts.map +1 -0
- package/dist/variant-icons.js +45 -0
- package/package.json +7 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"animations.d.ts","sourceRoot":"","sources":["../../src/lib/core/animations.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,gBAAgB;IAC5B,kDAAkD;;IAElD,iDAAiD;;IAEjD,iEAAiE;;IAEjE,6EAA6E;;IAE7E,yEAAyE;;IAEzE,qEAAqE;;IAErE,+DAA+D;;IAE/D,6DAA6D;;IAE7D,wCAAwC;;IAExC,2DAA2D;;IAE3D,2CAA2C;;CAElC,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global configuration constants for toast animations and stacking behavior.
|
|
3
|
+
* These values control timing, spacing, scaling, and easing of toast transitions.
|
|
4
|
+
*/
|
|
5
|
+
export const ANIMATION_CONFIG = {
|
|
6
|
+
/** Duration of the entry animation in seconds. */
|
|
7
|
+
ENTER_DURATION: 0.75,
|
|
8
|
+
/** Duration of the exit animation in seconds. */
|
|
9
|
+
EXIT_DURATION: 0.75,
|
|
10
|
+
/** Duration of the stack reorganization animation in seconds. */
|
|
11
|
+
STACK_DURATION: 0.75,
|
|
12
|
+
/** Vertical offset (in pixels) between stacked toasts in collapsed state. */
|
|
13
|
+
STACK_OFFSET: 16,
|
|
14
|
+
/** Vertical gap (in pixels) between toasts in expanded (hover) state. */
|
|
15
|
+
EXPANDED_GAP: 12,
|
|
16
|
+
/** Scale reduction factor for each subsequent toast in the stack. */
|
|
17
|
+
SCALE_FACTOR: 0.04,
|
|
18
|
+
/** Minimum scale value for the furthest toast in the stack. */
|
|
19
|
+
MIN_SCALE: 0.92,
|
|
20
|
+
/** Maximum number of toasts visible in the stack at once. */
|
|
21
|
+
MAX_VISIBLE_TOASTS: 3,
|
|
22
|
+
/** Base z-index for the toast layer. */
|
|
23
|
+
Z_INDEX_BASE: 50,
|
|
24
|
+
/** Default cubic-bezier easing function for animations. */
|
|
25
|
+
EASING_DEFAULT: "var(--ease-vs-toast)",
|
|
26
|
+
/** Easing function for exit animations. */
|
|
27
|
+
EASING_EXIT: "var(--ease-vs-toast)",
|
|
28
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for initial animation states based on toast position.
|
|
3
|
+
* Defines the starting transform values (x, y) for entering and exiting toasts.
|
|
4
|
+
*/
|
|
5
|
+
export declare const POSITION_CONFIGS: {
|
|
6
|
+
readonly "top-left": {
|
|
7
|
+
readonly animateIn: {
|
|
8
|
+
readonly x: -100;
|
|
9
|
+
readonly y: -20;
|
|
10
|
+
};
|
|
11
|
+
readonly animateOut: {
|
|
12
|
+
readonly x: -100;
|
|
13
|
+
readonly y: -100;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
readonly "top-center": {
|
|
17
|
+
readonly animateIn: {
|
|
18
|
+
readonly x: 0;
|
|
19
|
+
readonly y: -100;
|
|
20
|
+
};
|
|
21
|
+
readonly animateOut: {
|
|
22
|
+
readonly x: 0;
|
|
23
|
+
readonly y: -100;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
readonly "top-right": {
|
|
27
|
+
readonly animateIn: {
|
|
28
|
+
readonly x: 100;
|
|
29
|
+
readonly y: -20;
|
|
30
|
+
};
|
|
31
|
+
readonly animateOut: {
|
|
32
|
+
readonly x: 100;
|
|
33
|
+
readonly y: -100;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
readonly "bottom-left": {
|
|
37
|
+
readonly animateIn: {
|
|
38
|
+
readonly x: -100;
|
|
39
|
+
readonly y: 20;
|
|
40
|
+
};
|
|
41
|
+
readonly animateOut: {
|
|
42
|
+
readonly x: -100;
|
|
43
|
+
readonly y: 100;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
readonly "bottom-center": {
|
|
47
|
+
readonly animateIn: {
|
|
48
|
+
readonly x: 0;
|
|
49
|
+
readonly y: 100;
|
|
50
|
+
};
|
|
51
|
+
readonly animateOut: {
|
|
52
|
+
readonly x: 0;
|
|
53
|
+
readonly y: 100;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
readonly "bottom-right": {
|
|
57
|
+
readonly animateIn: {
|
|
58
|
+
readonly x: 100;
|
|
59
|
+
readonly y: 20;
|
|
60
|
+
};
|
|
61
|
+
readonly animateOut: {
|
|
62
|
+
readonly x: 100;
|
|
63
|
+
readonly y: 100;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Available positions for the toaster on the screen.
|
|
69
|
+
*/
|
|
70
|
+
export type ToastPosition = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right";
|
|
71
|
+
//# sourceMappingURL=positions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions.d.ts","sourceRoot":"","sources":["../../src/lib/core/positions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyBnB,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,aAAa,GACtB,UAAU,GACV,YAAY,GACZ,WAAW,GACX,aAAa,GACb,eAAe,GACf,cAAc,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for initial animation states based on toast position.
|
|
3
|
+
* Defines the starting transform values (x, y) for entering and exiting toasts.
|
|
4
|
+
*/
|
|
5
|
+
export const POSITION_CONFIGS = {
|
|
6
|
+
"top-left": {
|
|
7
|
+
animateIn: { x: -100, y: -20 },
|
|
8
|
+
animateOut: { x: -100, y: -100 },
|
|
9
|
+
},
|
|
10
|
+
"top-center": {
|
|
11
|
+
animateIn: { x: 0, y: -100 },
|
|
12
|
+
animateOut: { x: 0, y: -100 },
|
|
13
|
+
},
|
|
14
|
+
"top-right": {
|
|
15
|
+
animateIn: { x: 100, y: -20 },
|
|
16
|
+
animateOut: { x: 100, y: -100 },
|
|
17
|
+
},
|
|
18
|
+
"bottom-left": {
|
|
19
|
+
animateIn: { x: -100, y: 20 },
|
|
20
|
+
animateOut: { x: -100, y: 100 },
|
|
21
|
+
},
|
|
22
|
+
"bottom-center": {
|
|
23
|
+
animateIn: { x: 0, y: 100 },
|
|
24
|
+
animateOut: { x: 0, y: 100 },
|
|
25
|
+
},
|
|
26
|
+
"bottom-right": {
|
|
27
|
+
animateIn: { x: 100, y: 20 },
|
|
28
|
+
animateOut: { x: 100, y: 100 },
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ToastPosition } from "./positions";
|
|
2
|
+
/** Directions in which a toast can be swiped to dismiss. */
|
|
3
|
+
export type SwipeDirection = "top" | "bottom" | "left" | "right";
|
|
4
|
+
/** Axes along which swipe gestures are tracked. */
|
|
5
|
+
export type SwipeAxis = "x" | "y";
|
|
6
|
+
/** Minimum pixel distance required to trigger a swipe dismissal. */
|
|
7
|
+
export declare const SWIPE_DISMISS_THRESHOLD = 45;
|
|
8
|
+
/** Minimum velocity (pixels/ms) required to trigger a swipe dismissal. */
|
|
9
|
+
export declare const SWIPE_DISMISS_VELOCITY = 0.11;
|
|
10
|
+
/** Distance in pixels to move the toast during the exit animation after a swipe. */
|
|
11
|
+
export declare const SWIPE_EXIT_DISTANCE = 600;
|
|
12
|
+
/**
|
|
13
|
+
* Determines the allowed swipe directions based on the toast's position.
|
|
14
|
+
* For example, a "top-right" toast can be swiped "top" or "right".
|
|
15
|
+
*
|
|
16
|
+
* @param position - The toast's position on screen.
|
|
17
|
+
* @returns An array of allowed swipe directions.
|
|
18
|
+
*/
|
|
19
|
+
export declare const getDefaultSwipeDirections: (position?: ToastPosition | null) => SwipeDirection[];
|
|
20
|
+
//# sourceMappingURL=swipe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swipe.d.ts","sourceRoot":"","sources":["../../src/lib/core/swipe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,4DAA4D;AAC5D,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,CAAC;AACjE,mDAAmD;AACnD,MAAM,MAAM,SAAS,GAAG,GAAG,GAAG,GAAG,CAAC;AAElC,oEAAoE;AACpE,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAC1C,0EAA0E;AAC1E,eAAO,MAAM,sBAAsB,OAAO,CAAC;AAC3C,oFAAoF;AACpF,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAEvC;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GACrC,WAAW,aAAa,GAAG,IAAI,KAC7B,cAAc,EAwBhB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Minimum pixel distance required to trigger a swipe dismissal. */
|
|
2
|
+
export const SWIPE_DISMISS_THRESHOLD = 45;
|
|
3
|
+
/** Minimum velocity (pixels/ms) required to trigger a swipe dismissal. */
|
|
4
|
+
export const SWIPE_DISMISS_VELOCITY = 0.11;
|
|
5
|
+
/** Distance in pixels to move the toast during the exit animation after a swipe. */
|
|
6
|
+
export const SWIPE_EXIT_DISTANCE = 600;
|
|
7
|
+
/**
|
|
8
|
+
* Determines the allowed swipe directions based on the toast's position.
|
|
9
|
+
* For example, a "top-right" toast can be swiped "top" or "right".
|
|
10
|
+
*
|
|
11
|
+
* @param position - The toast's position on screen.
|
|
12
|
+
* @returns An array of allowed swipe directions.
|
|
13
|
+
*/
|
|
14
|
+
export const getDefaultSwipeDirections = (position) => {
|
|
15
|
+
if (!position) {
|
|
16
|
+
return ["top", "bottom", "left", "right"];
|
|
17
|
+
}
|
|
18
|
+
const [vertical, horizontal] = position.split("-");
|
|
19
|
+
const directions = [];
|
|
20
|
+
if (vertical === "top" || vertical === "bottom") {
|
|
21
|
+
directions.push(vertical);
|
|
22
|
+
}
|
|
23
|
+
if (horizontal === "left" || horizontal === "right") {
|
|
24
|
+
directions.push(horizontal);
|
|
25
|
+
}
|
|
26
|
+
if (directions.length === 0) {
|
|
27
|
+
directions.push("top", "bottom");
|
|
28
|
+
}
|
|
29
|
+
return directions;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast-factory.d.ts","sourceRoot":"","sources":["../../src/lib/core/toast-factory.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGX,YAAY,EAEZ,MAAM,SAAS,CAAC;AAuLjB,eAAO,MAAM,KAAK,cAAc,CAAC"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { toastState } from "./toast-state";
|
|
2
|
+
/**
|
|
3
|
+
* Helper to normalize string input into a ToastData object.
|
|
4
|
+
*/
|
|
5
|
+
const normalizeToastData = (data) => {
|
|
6
|
+
if (typeof data === "string") {
|
|
7
|
+
return { description: data };
|
|
8
|
+
}
|
|
9
|
+
return data;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the state of a promise-based toast (success or error) into actual toast data.
|
|
13
|
+
*/
|
|
14
|
+
const resolvePromiseState = async (value, state) => {
|
|
15
|
+
const resolvedValue = typeof state === "function" ? await state(value) : await state;
|
|
16
|
+
return normalizeToastData(resolvedValue);
|
|
17
|
+
};
|
|
18
|
+
const getPromiseFallbackDescription = (error) => {
|
|
19
|
+
if (error instanceof Error) {
|
|
20
|
+
return error.message;
|
|
21
|
+
}
|
|
22
|
+
if (typeof error === "string") {
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
return "An unknown error occurred while updating the notification.";
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The main entry point for creating toasts.
|
|
29
|
+
* @param data - Toast configuration object or description string.
|
|
30
|
+
* @returns The ID of the created toast.
|
|
31
|
+
*/
|
|
32
|
+
const createToast = ((data) => {
|
|
33
|
+
return toastState.add(normalizeToastData(data));
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Creates a success variant toast.
|
|
37
|
+
*/
|
|
38
|
+
createToast.success = (data) => {
|
|
39
|
+
if (typeof data === "string") {
|
|
40
|
+
return toastState.add({ description: data, variant: "success" });
|
|
41
|
+
}
|
|
42
|
+
return toastState.add({ ...data, variant: "success" });
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Creates a warning variant toast.
|
|
46
|
+
*/
|
|
47
|
+
createToast.warning = (data) => {
|
|
48
|
+
if (typeof data === "string") {
|
|
49
|
+
return toastState.add({ description: data, variant: "warning" });
|
|
50
|
+
}
|
|
51
|
+
return toastState.add({ ...data, variant: "warning" });
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Creates an info variant toast.
|
|
55
|
+
*/
|
|
56
|
+
createToast.info = (data) => {
|
|
57
|
+
if (typeof data === "string") {
|
|
58
|
+
return toastState.add({ description: data, variant: "info" });
|
|
59
|
+
}
|
|
60
|
+
return toastState.add({ ...data, variant: "info" });
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Creates an error (destructive) variant toast.
|
|
64
|
+
*/
|
|
65
|
+
createToast.error = (data) => {
|
|
66
|
+
if (typeof data === "string") {
|
|
67
|
+
return toastState.add({ description: data, variant: "destructive" });
|
|
68
|
+
}
|
|
69
|
+
return toastState.add({ ...data, variant: "destructive" });
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Creates a custom component toast.
|
|
73
|
+
* @param component - The Svelte component to render.
|
|
74
|
+
* @param options - Additional options and props for the component.
|
|
75
|
+
*/
|
|
76
|
+
createToast.custom = (component, // Type inferred from ToastInvoker
|
|
77
|
+
options) => {
|
|
78
|
+
return toastState.add({
|
|
79
|
+
...(options ?? {}),
|
|
80
|
+
component: component,
|
|
81
|
+
variant: "custom",
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Creates a toast that tracks a promise's lifecycle.
|
|
86
|
+
* Updates automatically from loading -> success/error.
|
|
87
|
+
*
|
|
88
|
+
* @param promise - The promise to observe.
|
|
89
|
+
* @param options - Configuration for loading, success, and error states.
|
|
90
|
+
* @returns The ID of the toast.
|
|
91
|
+
*/
|
|
92
|
+
createToast.promise = (promise, options) => {
|
|
93
|
+
const loadingData = normalizeToastData(options.loading);
|
|
94
|
+
const toastId = toastState.add({
|
|
95
|
+
...loadingData,
|
|
96
|
+
duration: loadingData.duration ?? 0,
|
|
97
|
+
isLoading: true,
|
|
98
|
+
showClose: loadingData.showClose ?? true,
|
|
99
|
+
});
|
|
100
|
+
const handleResult = async (state, value, defaultVariant) => {
|
|
101
|
+
const payload = await resolvePromiseState(value, state);
|
|
102
|
+
toastState.update(toastId, {
|
|
103
|
+
...payload,
|
|
104
|
+
isLoading: false,
|
|
105
|
+
duration: payload.duration,
|
|
106
|
+
variant: payload.variant ?? defaultVariant,
|
|
107
|
+
showClose: payload.showClose ?? true,
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
const applyPromiseFallback = (error) => {
|
|
111
|
+
toastState.update(toastId, {
|
|
112
|
+
title: "Operation failed",
|
|
113
|
+
description: getPromiseFallbackDescription(error),
|
|
114
|
+
variant: "destructive",
|
|
115
|
+
isLoading: false,
|
|
116
|
+
showClose: true,
|
|
117
|
+
duration: 5000,
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
void Promise.resolve(promise)
|
|
121
|
+
.then((value) => {
|
|
122
|
+
return handleResult(options.success, value, "success");
|
|
123
|
+
})
|
|
124
|
+
.catch((error) => {
|
|
125
|
+
return handleResult(options.error, error, "destructive");
|
|
126
|
+
})
|
|
127
|
+
.catch((error) => {
|
|
128
|
+
applyPromiseFallback(error);
|
|
129
|
+
});
|
|
130
|
+
return toastId;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Dismisses a toast by ID.
|
|
134
|
+
*/
|
|
135
|
+
createToast.dismiss = (id) => {
|
|
136
|
+
toastState.update(id, { shouldClose: true });
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Dismisses all active toasts.
|
|
140
|
+
*/
|
|
141
|
+
createToast.dismissAll = () => {
|
|
142
|
+
toastState.dismissAll();
|
|
143
|
+
};
|
|
144
|
+
export const toast = createToast;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ToastData, ToastSubscriber } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Manages the global state of toast notifications.
|
|
4
|
+
* Implements a singleton pattern with a subscription mechanism.
|
|
5
|
+
*/
|
|
6
|
+
declare class ToastState {
|
|
7
|
+
private toasts;
|
|
8
|
+
private subscribers;
|
|
9
|
+
private idCounter;
|
|
10
|
+
/**
|
|
11
|
+
* Subscribes to changes in the toast list.
|
|
12
|
+
* @param callback - Function to be called when the toast list updates.
|
|
13
|
+
* @returns A cleanup function to unsubscribe.
|
|
14
|
+
*/
|
|
15
|
+
subscribe(callback: ToastSubscriber): () => void;
|
|
16
|
+
private notify;
|
|
17
|
+
private generateId;
|
|
18
|
+
/**
|
|
19
|
+
* Adds a new toast to the state.
|
|
20
|
+
* @param data - The toast configuration (without ID).
|
|
21
|
+
* @returns The generated ID of the new toast.
|
|
22
|
+
*/
|
|
23
|
+
add(data: Omit<ToastData, "id">): string;
|
|
24
|
+
/**
|
|
25
|
+
* Removes a toast from the state by its ID.
|
|
26
|
+
* @param id - The ID of the toast to remove.
|
|
27
|
+
*/
|
|
28
|
+
remove(id: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* Updates an existing toast's data.
|
|
31
|
+
* @param id - The ID of the toast to update.
|
|
32
|
+
* @param data - Partial data to merge into the existing toast.
|
|
33
|
+
*/
|
|
34
|
+
update(id: string, data: Partial<ToastData>): void;
|
|
35
|
+
/**
|
|
36
|
+
* Marks all active toasts for dismissal.
|
|
37
|
+
* Triggers the closing animation by setting `shouldClose` to true.
|
|
38
|
+
*/
|
|
39
|
+
dismissAll(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Returns the current list of active toasts.
|
|
42
|
+
* @returns A copy of the toast array.
|
|
43
|
+
*/
|
|
44
|
+
getToasts(): ToastData[];
|
|
45
|
+
}
|
|
46
|
+
/** Global singleton instance of the toast state. */
|
|
47
|
+
export declare const toastState: ToastState;
|
|
48
|
+
export { ToastState };
|
|
49
|
+
//# sourceMappingURL=toast-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast-state.d.ts","sourceRoot":"","sources":["../../src/lib/core/toast-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1D;;;GAGG;AACH,cAAM,UAAU;IACf,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,WAAW,CAAmC;IACtD,OAAO,CAAC,SAAS,CAAK;IAEtB;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM,IAAI;IAOhD,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,UAAU;IAIlB;;;;OAIG;IACH,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,MAAM;IASxC;;;OAGG;IACH,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAKxB;;;;OAIG;IACH,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,IAAI;IAOlD;;;OAGG;IACH,UAAU,IAAI,IAAI;IASlB;;;OAGG;IACH,SAAS,IAAI,SAAS,EAAE;CAGxB;AAED,oDAAoD;AACpD,eAAO,MAAM,UAAU,YAAmB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages the global state of toast notifications.
|
|
3
|
+
* Implements a singleton pattern with a subscription mechanism.
|
|
4
|
+
*/
|
|
5
|
+
class ToastState {
|
|
6
|
+
toasts = [];
|
|
7
|
+
subscribers = new Set();
|
|
8
|
+
idCounter = 0;
|
|
9
|
+
/**
|
|
10
|
+
* Subscribes to changes in the toast list.
|
|
11
|
+
* @param callback - Function to be called when the toast list updates.
|
|
12
|
+
* @returns A cleanup function to unsubscribe.
|
|
13
|
+
*/
|
|
14
|
+
subscribe(callback) {
|
|
15
|
+
this.subscribers.add(callback);
|
|
16
|
+
return () => {
|
|
17
|
+
this.subscribers.delete(callback);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
notify() {
|
|
21
|
+
this.subscribers.forEach((callback) => {
|
|
22
|
+
callback([...this.toasts]);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
generateId() {
|
|
26
|
+
return `toast-${Date.now()}-${++this.idCounter}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Adds a new toast to the state.
|
|
30
|
+
* @param data - The toast configuration (without ID).
|
|
31
|
+
* @returns The generated ID of the new toast.
|
|
32
|
+
*/
|
|
33
|
+
add(data) {
|
|
34
|
+
const id = this.generateId();
|
|
35
|
+
const newToast = { ...data, id };
|
|
36
|
+
// Add new toasts to the beginning of the array (stack LIFO visually for some positions, logic handled in manager)
|
|
37
|
+
this.toasts = [newToast, ...this.toasts];
|
|
38
|
+
this.notify();
|
|
39
|
+
return id;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Removes a toast from the state by its ID.
|
|
43
|
+
* @param id - The ID of the toast to remove.
|
|
44
|
+
*/
|
|
45
|
+
remove(id) {
|
|
46
|
+
this.toasts = this.toasts.filter((toast) => toast.id !== id);
|
|
47
|
+
this.notify();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Updates an existing toast's data.
|
|
51
|
+
* @param id - The ID of the toast to update.
|
|
52
|
+
* @param data - Partial data to merge into the existing toast.
|
|
53
|
+
*/
|
|
54
|
+
update(id, data) {
|
|
55
|
+
this.toasts = this.toasts.map((toast) => toast.id === id ? { ...toast, ...data } : toast);
|
|
56
|
+
this.notify();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Marks all active toasts for dismissal.
|
|
60
|
+
* Triggers the closing animation by setting `shouldClose` to true.
|
|
61
|
+
*/
|
|
62
|
+
dismissAll() {
|
|
63
|
+
this.toasts = this.toasts.map((toast) => ({
|
|
64
|
+
...toast,
|
|
65
|
+
shouldClose: true,
|
|
66
|
+
duration: 0,
|
|
67
|
+
}));
|
|
68
|
+
this.notify();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the current list of active toasts.
|
|
72
|
+
* @returns A copy of the toast array.
|
|
73
|
+
*/
|
|
74
|
+
getToasts() {
|
|
75
|
+
return [...this.toasts];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Global singleton instance of the toast state. */
|
|
79
|
+
export const toastState = new ToastState();
|
|
80
|
+
export { ToastState };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages unique instances of the Toaster component to prevent conflicts.
|
|
3
|
+
* Ensures that if multiple Toasters are rendered, only one (the first or primary) is active.
|
|
4
|
+
*/
|
|
5
|
+
declare class ToasterInstanceManager {
|
|
6
|
+
private activeInstanceId;
|
|
7
|
+
private instanceCounter;
|
|
8
|
+
private instanceIds;
|
|
9
|
+
/**
|
|
10
|
+
* Registers a new toaster instance.
|
|
11
|
+
* @returns A unique instance ID.
|
|
12
|
+
*/
|
|
13
|
+
registerInstance(): string;
|
|
14
|
+
/**
|
|
15
|
+
* Unregisters an instance, potentially freeing up the active slot.
|
|
16
|
+
* @param instanceId - The ID of the instance to unregister.
|
|
17
|
+
*/
|
|
18
|
+
unregisterInstance(instanceId: string): void;
|
|
19
|
+
/**
|
|
20
|
+
* Checks if the given instance is the currently active one.
|
|
21
|
+
* @param instanceId - The ID to check.
|
|
22
|
+
* @returns True if active, false otherwise.
|
|
23
|
+
*/
|
|
24
|
+
isActiveInstance(instanceId: string): boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare const toasterInstanceManager: ToasterInstanceManager;
|
|
27
|
+
export { ToasterInstanceManager };
|
|
28
|
+
//# sourceMappingURL=toaster-instances.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toaster-instances.d.ts","sourceRoot":"","sources":["../../src/lib/core/toaster-instances.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,cAAM,sBAAsB;IAC3B,OAAO,CAAC,gBAAgB,CAAuB;IAC/C,OAAO,CAAC,eAAe,CAAK;IAC5B,OAAO,CAAC,WAAW,CAAgB;IAEnC;;;OAGG;IACH,gBAAgB,IAAI,MAAM;IAY1B;;;OAGG;IACH,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAY5C;;;;OAIG;IACH,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;CAG7C;AAED,eAAO,MAAM,sBAAsB,wBAA+B,CAAC;AACnE,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages unique instances of the Toaster component to prevent conflicts.
|
|
3
|
+
* Ensures that if multiple Toasters are rendered, only one (the first or primary) is active.
|
|
4
|
+
*/
|
|
5
|
+
class ToasterInstanceManager {
|
|
6
|
+
activeInstanceId = null;
|
|
7
|
+
instanceCounter = 0;
|
|
8
|
+
instanceIds = [];
|
|
9
|
+
/**
|
|
10
|
+
* Registers a new toaster instance.
|
|
11
|
+
* @returns A unique instance ID.
|
|
12
|
+
*/
|
|
13
|
+
registerInstance() {
|
|
14
|
+
const instanceId = `toaster-${++this.instanceCounter}`;
|
|
15
|
+
this.instanceIds.push(instanceId);
|
|
16
|
+
if (!this.activeInstanceId ||
|
|
17
|
+
!this.instanceIds.includes(this.activeInstanceId)) {
|
|
18
|
+
this.activeInstanceId = instanceId;
|
|
19
|
+
}
|
|
20
|
+
return instanceId;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Unregisters an instance, potentially freeing up the active slot.
|
|
24
|
+
* @param instanceId - The ID of the instance to unregister.
|
|
25
|
+
*/
|
|
26
|
+
unregisterInstance(instanceId) {
|
|
27
|
+
this.instanceIds = this.instanceIds.filter((id) => id !== instanceId);
|
|
28
|
+
if (this.activeInstanceId === instanceId) {
|
|
29
|
+
this.activeInstanceId = this.instanceIds[0] ?? null;
|
|
30
|
+
}
|
|
31
|
+
else if (this.activeInstanceId &&
|
|
32
|
+
!this.instanceIds.includes(this.activeInstanceId)) {
|
|
33
|
+
this.activeInstanceId = this.instanceIds[0] ?? null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Checks if the given instance is the currently active one.
|
|
38
|
+
* @param instanceId - The ID to check.
|
|
39
|
+
* @returns True if active, false otherwise.
|
|
40
|
+
*/
|
|
41
|
+
isActiveInstance(instanceId) {
|
|
42
|
+
return this.activeInstanceId === instanceId;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const toasterInstanceManager = new ToasterInstanceManager();
|
|
46
|
+
export { ToasterInstanceManager };
|