forked from CyCraft/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
50 lines (44 loc) · 1.21 KB
/
index.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
import nprogress, { NProgressOptions } from 'nprogress'
import { MaybeRef, tryOnScopeDispose, isNumber } from '@vueuse/shared'
import { ref, isRef, watchEffect, computed } from 'vue-demi'
/**
* Reactive progress bar.
*
* @see https://vueuse.org/useNProgress
* @param currentProgress
* @param options
*/
export function useNProgress(
currentProgress: MaybeRef<number | null | undefined> = null,
options?: NProgressOptions | undefined,
) {
const progress = isRef(currentProgress)
? currentProgress
: ref<number|null>(currentProgress)
const isLoading = computed({
set: load => load ? nprogress.start() : nprogress.done(),
get: () => isNumber(progress.value) && progress.value < 1,
})
if (options)
nprogress.configure(options)
const setProgress = nprogress.set
nprogress.set = (n: number) => {
progress.value = n
return setProgress.call(nprogress, n)
}
watchEffect(() => {
if (isNumber(progress.value))
setProgress.call(nprogress, progress.value)
})
tryOnScopeDispose(nprogress.remove)
return {
isLoading,
progress,
start: nprogress.start,
done: nprogress.done,
remove: () => {
progress.value = null
nprogress.remove()
},
}
}