vben
2020-12-01 5db3ce773793d06fa17336aca42470d2aae93fb8
提交 | 用户 | age
5db3ce 1 import { unref } from 'vue';
V 2 import { createLoading } from './createLoading';
3 import type { LoadingProps } from './types';
4 import type { Ref } from 'vue';
5
6 export interface UseLoadingOptions {
7   target?: HTMLElement | Ref<ElRef>;
8   props?: Partial<LoadingProps>;
9 }
10
11 export function useLoading(props: Partial<LoadingProps>): [Fn, Fn];
12 export function useLoading(opt: Partial<UseLoadingOptions>): [Fn, Fn];
13
14 export function useLoading(opt: Partial<LoadingProps> | Partial<UseLoadingOptions>): [Fn, Fn] {
15   let props: Partial<LoadingProps>;
16   let target: HTMLElement | Ref<ElRef> = document.body;
17
18   if (Reflect.has(opt, 'target') || Reflect.has(opt, 'props')) {
19     const options = opt as Partial<UseLoadingOptions>;
20     props = options.props || {};
21     target = options.target || document.body;
22   } else {
23     props = opt as Partial<LoadingProps>;
24   }
25
26   const instance = createLoading(props);
27
28   const open = (): void => {
29     const t = unref(target);
30     if (!t) return;
31     instance.open(t);
32   };
33
34   const close = (): void => {
35     instance.close();
36   };
37
38   return [open, close];
39 }