-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useNotify.ts
37 lines (31 loc) · 911 Bytes
/
useNotify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { defineStore } from 'pinia';
import { ref } from 'vue';
export type ColorOptions = 'default' | 'success' | 'warning' | 'error' | 'info';
export type Notification = { message: string; color: string; id: number };
export const useNotify = defineStore('vue-notify', () => {
const notifications = ref<Notification[]>([]);
/**
* Display the notification.
* @param message Message to display in the notification
* @param color Color of the notification. Defaults to 'default'
*/
function show(message: string, color: ColorOptions = 'default') {
const id = notifications.value.length ? notifications.value[0].id + 1 : 1;
notifications.value.unshift({
message,
color,
id,
});
}
/**
* Hide the notification
*/
function hide(index: number) {
notifications.value.splice(index, 1);
}
return {
notifications,
show,
hide,
};
});