-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathLoading.ts
45 lines (43 loc) · 1.13 KB
/
Loading.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
import {
computed,
defineComponent,
h,
inject,
onMounted,
onUnmounted,
onUpdated,
ref,
} from '@vue/runtime-core'
import spinners from 'cli-spinners'
import type { PropType } from '@vue/runtime-core'
import { scheduleUpdateSymbol } from '../injectionSymbols'
import type { SpinnerName } from 'cli-spinners'
export const TuiLoading = defineComponent({
props: {
/**
* Type of a loading.
* See [cli-spinners](https://github.com/sindresorhus/cli-spinners) for available spinners.
*
* @default dots
*/
type: String as PropType<SpinnerName>,
},
setup(props) {
const spinner = computed(() => spinners[props.type ?? 'dots'])
const frame = ref(0)
let timer: NodeJS.Timer | null = null
const scheduleUpdate = inject(scheduleUpdateSymbol)!
onUpdated(scheduleUpdate)
onMounted(() => {
timer = setInterval(() => {
frame.value = (frame.value + 1) % spinner.value?.frames?.length
}, spinner.value.interval)
})
onUnmounted(() => {
clearInterval(timer!)
})
return () => {
return h('tui:text', spinner.value?.frames[frame.value])
}
},
})