-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathuse-notification.ts
60 lines (52 loc) · 1.5 KB
/
use-notification.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { computed, watch } from 'vue';
import type { ComputedRef } from 'vue';
import { NotificationProps, EmitEventFn, VoidFn } from './notification-types';
import { useNamespace } from '@devui/shared/utils';
const ns= useNamespace('notification')
export function useNotification(props: NotificationProps): { classes: ComputedRef<Record<string, boolean>> } {
const classes = computed(() => ({
[ns.e('item-container')]:true,
[ns.em('message',props.type)]:true,
}));
return { classes };
}
export function useEvent(
props: NotificationProps,
emit: EmitEventFn
): { interrupt: VoidFn; removeReset: VoidFn; close: VoidFn; handleDestroy: VoidFn } {
let timer: NodeJS.Timeout | null = null;
let timestamp: number;
const close = () => {
timer && clearTimeout(timer);
timer = null;
props.onClose?.();
emit('update:modelValue', false);
};
const interrupt = () => {
if (timer && props.duration) {
clearTimeout(timer);
timer = null;
}
};
const removeReset = () => {
if (props.modelValue && props.duration) {
const remainTime = props.duration - (Date.now() - timestamp);
timer = setTimeout(close, remainTime);
}
};
const handleDestroy = () => {
emit('destroy');
};
watch(
() => props.modelValue,
(val) => {
if (val) {
timestamp = Date.now();
if (props.duration) {
timer = setTimeout(close, props.duration);
}
}
}
);
return { interrupt, removeReset, close, handleDestroy };
}