-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
useSettings.ts
58 lines (52 loc) · 1.56 KB
/
useSettings.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
import type { Settings } from '../type';
import { reactive, readonly } from 'vue';
import type { DeepReadonly } from 'vue';
type DefaultSettings = Omit<Required<Settings>, 'transition'> & Pick<Settings, 'transition'>;
const settings = reactive<DefaultSettings>({
singular: false,
withBackdrop: false,
backdrop: 'rgba(0, 0, 0, 0.2)',
position: 'bottom-right',
defaultTitle: true,
canTimeout: true,
pauseOnHover: false,
pauseOnFocusLoss: true,
iconEnabled: true,
draggable: true,
dragThreshold: 0.75,
hideProgressbar: false,
errorDuration: 8000,
successDuration: 4000,
warningInfoDuration: 6000,
theme: 'dark',
baseIconClass: '',
orderLatest: true,
transition: undefined,
oneType: false,
maxToasts: 6,
customNotifications: {}
});
type UseSettings = {
settings: DeepReadonly<DefaultSettings>;
updateSettings: <T extends keyof Settings | Settings>(
key: T,
value?: T extends keyof Settings ? Settings[T] : never
) => Settings;
};
/**
* Base settings applying plugin wide.
*/
export default function useSettings(): UseSettings {
return {
settings: readonly(settings),
updateSettings: (key, newSettings) => {
let settingsNew = {} as Settings;
if (typeof key === 'object' && newSettings === undefined) {
settingsNew = key;
} else if (typeof key === 'string') {
settingsNew = { [key]: newSettings };
}
return Object.assign(settings, settingsNew);
}
};
}