vue-toastflow 0.0.3 → 0.0.5
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/package.json +1 -1
- package/src/components/Toast.vue +2 -4
- package/src/components/ToastContainer.vue +4 -2
- package/src/symbols.ts +6 -4
- package/src/toast.ts +74 -63
package/package.json
CHANGED
package/src/components/Toast.vue
CHANGED
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
ToastType,
|
|
10
10
|
} from "toastflow-core";
|
|
11
11
|
import { toastStoreKey } from "../symbols";
|
|
12
|
+
import { getToastStore } from "../toast";
|
|
12
13
|
import CheckCircle from "./icons/CheckCircle.vue";
|
|
13
14
|
import XCircle from "./icons/XCircle.vue";
|
|
14
15
|
import Bell from "./icons/Bell.vue";
|
|
@@ -40,10 +41,7 @@ const emit = defineEmits<{
|
|
|
40
41
|
}>();
|
|
41
42
|
|
|
42
43
|
const injectedStore = inject<ToastStore | null>(toastStoreKey, null);
|
|
43
|
-
|
|
44
|
-
throw new Error("[vue-toastflow] Plugin not installed");
|
|
45
|
-
}
|
|
46
|
-
const store: ToastStore = injectedStore;
|
|
44
|
+
const store: ToastStore = injectedStore ?? getToastStore();
|
|
47
45
|
|
|
48
46
|
const isHovered = ref(false);
|
|
49
47
|
const isBumped = ref(false);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
|
2
|
+
import { computed, inject, onMounted, onUnmounted, ref, watch } from "vue";
|
|
3
3
|
import Toast from "./Toast.vue";
|
|
4
4
|
import type {
|
|
5
5
|
ToastAnimation,
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
ToastPosition,
|
|
10
10
|
ToastStore,
|
|
11
11
|
} from "toastflow-core";
|
|
12
|
+
import { toastStoreKey } from "../symbols";
|
|
12
13
|
import { getToastStore } from "../toast";
|
|
13
14
|
|
|
14
15
|
const positions: ToastPosition[] = [
|
|
@@ -20,7 +21,8 @@ const positions: ToastPosition[] = [
|
|
|
20
21
|
"bottom-right",
|
|
21
22
|
];
|
|
22
23
|
|
|
23
|
-
const
|
|
24
|
+
const injectedStore = inject<ToastStore | null>(toastStoreKey, null);
|
|
25
|
+
const store: ToastStore = injectedStore ?? getToastStore();
|
|
24
26
|
|
|
25
27
|
const toasts = ref<ToastInstance[]>([]);
|
|
26
28
|
|
package/src/symbols.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type {InjectionKey} from "vue";
|
|
2
|
-
import type {ToastStore} from "toastflow-core";
|
|
3
|
-
|
|
4
|
-
export const toastStoreKey: InjectionKey<ToastStore> = Symbol(
|
|
1
|
+
import type { InjectionKey } from "vue";
|
|
2
|
+
import type { ToastStore } from "toastflow-core";
|
|
3
|
+
|
|
4
|
+
export const toastStoreKey: InjectionKey<ToastStore> = Symbol.for(
|
|
5
|
+
"vue-toastflow.toast-store",
|
|
6
|
+
);
|
package/src/toast.ts
CHANGED
|
@@ -1,80 +1,91 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ToastConfig,
|
|
3
|
-
ToastContentInput,
|
|
4
|
-
ToastEvent,
|
|
5
|
-
ToastId,
|
|
6
|
-
ToastLoadingConfig,
|
|
7
|
-
ToastLoadingInput,
|
|
8
|
-
ToastLoadingResult,
|
|
9
|
-
ToastShowInput,
|
|
1
|
+
import {
|
|
2
|
+
ToastConfig,
|
|
3
|
+
ToastContentInput,
|
|
4
|
+
ToastEvent,
|
|
5
|
+
ToastId,
|
|
6
|
+
ToastLoadingConfig,
|
|
7
|
+
ToastLoadingInput,
|
|
8
|
+
ToastLoadingResult,
|
|
9
|
+
ToastShowInput,
|
|
10
10
|
ToastState,
|
|
11
11
|
ToastStore,
|
|
12
12
|
ToastUpdateInput,
|
|
13
13
|
} from "toastflow-core";
|
|
14
14
|
|
|
15
|
+
const GLOBAL_STORE_KEY = "__vue_toastflow_store__";
|
|
16
|
+
type GlobalWithStore = typeof globalThis & { [GLOBAL_STORE_KEY]?: ToastStore };
|
|
17
|
+
|
|
15
18
|
let globalStore: ToastStore | null = null;
|
|
16
19
|
|
|
17
20
|
export function setToastStore(store: ToastStore) {
|
|
18
21
|
globalStore = store;
|
|
22
|
+
try {
|
|
23
|
+
(globalThis as GlobalWithStore)[GLOBAL_STORE_KEY] = store;
|
|
24
|
+
} catch {
|
|
25
|
+
// ignore if globalThis is not writable
|
|
26
|
+
}
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
export function getToastStore(): ToastStore {
|
|
30
|
+
if (!globalStore) {
|
|
31
|
+
globalStore = (globalThis as GlobalWithStore)[GLOBAL_STORE_KEY] ?? null;
|
|
32
|
+
}
|
|
22
33
|
if (!globalStore) {
|
|
23
34
|
throw new Error(
|
|
24
35
|
"[vue-toastflow] Toast store not initialized. Did you install the plugin?",
|
|
25
36
|
);
|
|
26
37
|
}
|
|
27
|
-
return globalStore;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const toast = {
|
|
31
|
-
getState(): ToastState {
|
|
32
|
-
return getToastStore().getState();
|
|
33
|
-
},
|
|
34
|
-
subscribeEvents(listener: (event: ToastEvent) => void): () => void {
|
|
35
|
-
return getToastStore().subscribeEvents(listener);
|
|
36
|
-
},
|
|
37
|
-
show(options: ToastShowInput): ToastId {
|
|
38
|
-
return getToastStore().show(options);
|
|
39
|
-
},
|
|
40
|
-
loading<T>(
|
|
41
|
-
input: ToastLoadingInput<T>,
|
|
42
|
-
config: ToastLoadingConfig<T>,
|
|
43
|
-
): ToastLoadingResult<T> {
|
|
44
|
-
return getToastStore().loading(input, config);
|
|
45
|
-
},
|
|
46
|
-
update(id: ToastId, options: ToastUpdateInput): void {
|
|
47
|
-
return getToastStore().update(id, options);
|
|
48
|
-
},
|
|
49
|
-
dismiss(id: ToastId): void {
|
|
50
|
-
return getToastStore().dismiss(id);
|
|
51
|
-
},
|
|
52
|
-
dismissAll(): void {
|
|
53
|
-
return getToastStore().dismissAll();
|
|
54
|
-
},
|
|
55
|
-
pause(id: ToastId): void {
|
|
56
|
-
return getToastStore().pause(id);
|
|
57
|
-
},
|
|
58
|
-
resume(id: ToastId): void {
|
|
59
|
-
return getToastStore().resume(id);
|
|
60
|
-
},
|
|
61
|
-
getConfig(): ToastConfig {
|
|
62
|
-
return getToastStore().getConfig();
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
toast(options: ToastContentInput): ToastId {
|
|
66
|
-
return getToastStore().show({ ...options, type: "default" });
|
|
67
|
-
},
|
|
68
|
-
success(options: ToastContentInput): ToastId {
|
|
69
|
-
return getToastStore().show({ ...options, type: "success" });
|
|
70
|
-
},
|
|
71
|
-
error(options: ToastContentInput): ToastId {
|
|
72
|
-
return getToastStore().show({ ...options, type: "error" });
|
|
73
|
-
},
|
|
74
|
-
info(options: ToastContentInput): ToastId {
|
|
75
|
-
return getToastStore().show({ ...options, type: "info" });
|
|
76
|
-
},
|
|
77
|
-
warning(options: ToastContentInput): ToastId {
|
|
78
|
-
return getToastStore().show({ ...options, type: "warning" });
|
|
79
|
-
},
|
|
80
|
-
};
|
|
38
|
+
return globalStore;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const toast = {
|
|
42
|
+
getState(): ToastState {
|
|
43
|
+
return getToastStore().getState();
|
|
44
|
+
},
|
|
45
|
+
subscribeEvents(listener: (event: ToastEvent) => void): () => void {
|
|
46
|
+
return getToastStore().subscribeEvents(listener);
|
|
47
|
+
},
|
|
48
|
+
show(options: ToastShowInput): ToastId {
|
|
49
|
+
return getToastStore().show(options);
|
|
50
|
+
},
|
|
51
|
+
loading<T>(
|
|
52
|
+
input: ToastLoadingInput<T>,
|
|
53
|
+
config: ToastLoadingConfig<T>,
|
|
54
|
+
): ToastLoadingResult<T> {
|
|
55
|
+
return getToastStore().loading(input, config);
|
|
56
|
+
},
|
|
57
|
+
update(id: ToastId, options: ToastUpdateInput): void {
|
|
58
|
+
return getToastStore().update(id, options);
|
|
59
|
+
},
|
|
60
|
+
dismiss(id: ToastId): void {
|
|
61
|
+
return getToastStore().dismiss(id);
|
|
62
|
+
},
|
|
63
|
+
dismissAll(): void {
|
|
64
|
+
return getToastStore().dismissAll();
|
|
65
|
+
},
|
|
66
|
+
pause(id: ToastId): void {
|
|
67
|
+
return getToastStore().pause(id);
|
|
68
|
+
},
|
|
69
|
+
resume(id: ToastId): void {
|
|
70
|
+
return getToastStore().resume(id);
|
|
71
|
+
},
|
|
72
|
+
getConfig(): ToastConfig {
|
|
73
|
+
return getToastStore().getConfig();
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
toast(options: ToastContentInput): ToastId {
|
|
77
|
+
return getToastStore().show({ ...options, type: "default" });
|
|
78
|
+
},
|
|
79
|
+
success(options: ToastContentInput): ToastId {
|
|
80
|
+
return getToastStore().show({ ...options, type: "success" });
|
|
81
|
+
},
|
|
82
|
+
error(options: ToastContentInput): ToastId {
|
|
83
|
+
return getToastStore().show({ ...options, type: "error" });
|
|
84
|
+
},
|
|
85
|
+
info(options: ToastContentInput): ToastId {
|
|
86
|
+
return getToastStore().show({ ...options, type: "info" });
|
|
87
|
+
},
|
|
88
|
+
warning(options: ToastContentInput): ToastId {
|
|
89
|
+
return getToastStore().show({ ...options, type: "warning" });
|
|
90
|
+
},
|
|
91
|
+
};
|