import { unref } from 'vue'; import { createLoading } from './createLoading'; import type { LoadingProps } from './typing'; import type { Ref } from 'vue'; export interface UseLoadingOptions { target?: any; props?: Partial; } interface Fn { (): void; } export function useLoading(props: Partial): [Fn, Fn, (string) => void]; export function useLoading(opt: Partial): [Fn, Fn, (string) => void]; export function useLoading( opt: Partial | Partial, ): [Fn, Fn, (string) => void] { let props: Partial; let target: HTMLElement | Ref = document.body; if (Reflect.has(opt, 'target') || Reflect.has(opt, 'props')) { const options = opt as Partial; props = options.props || {}; target = options.target || document.body; } else { props = opt as Partial; } const instance = createLoading(props, undefined, true); const open = (): void => { const t = unref(target as Ref); if (!t) return; instance.open(t); }; const close = (): void => { instance.close(); }; const setTip = (tip: string) => { instance.setTip(tip); }; return [open, close, setTip]; }