Skip to content

Commit 9c581dd

Browse files
authored
Merge pull request #453 from ShubhamCanMakeCommit/feat/natural-progress-animation
Natural progress animation
2 parents b42ac77 + 696b27f commit 9c581dd

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

web-app/src/containers/Tutorial/components/ProgressPie.tsx

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
import * as React from 'react'
22
import { Progress, Icon } from '@alifd/next'
3+
import useNaturalProgress from 'services/hooks/useNaturalProgress'
34

45
interface Props {
56
current: number
67
max: number
78
}
89

910
const ProgressPie = (props: Props) => {
10-
const [progress, setProgress] = React.useState(0)
11-
12-
React.useEffect(() => {
13-
let timeout: any
14-
if (progress < props.current) {
15-
timeout = setTimeout(() => {
16-
setProgress(progress + 1)
17-
}, 100)
18-
}
19-
return () => {
20-
if (timeout) {
21-
clearTimeout(timeout)
22-
}
23-
}
24-
}, [progress])
11+
const progress = useNaturalProgress({ stop: props.current })
2512

2613
const progressPercent = Math.floor((progress / props.max) * 100)
2714

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useEffect, useState } from 'react'
2+
3+
interface ProgressConfig {
4+
start?: number
5+
stop: number
6+
updateDuration?: number
7+
}
8+
9+
const useNaturalProgress = (config: ProgressConfig): number => {
10+
const { start, stop, updateDuration } = config
11+
const [progress, setProgress] = useState(start || 0)
12+
useEffect(() => {
13+
let timeout: any
14+
let difference = (stop - progress) / 4
15+
// for difference>0.01 update progress or make it stop
16+
let newProgress = difference > 0.01 ? progress + difference : stop
17+
if (progress < stop) {
18+
timeout = setTimeout(() => {
19+
setProgress(newProgress)
20+
}, updateDuration || 100)
21+
}
22+
return () => {
23+
if (timeout) {
24+
clearTimeout(timeout)
25+
}
26+
}
27+
}, [progress, stop, updateDuration])
28+
return progress
29+
}
30+
31+
export default useNaturalProgress

0 commit comments

Comments
 (0)