Skip to content

Natural progress animation #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor to new file
Signed-off-by: Shubham <shubhamshahrising@gmail.com>
  • Loading branch information
SavvyShah committed Aug 19, 2020
commit 696b27fd00c9391456ee0828b363cdae54da555a
19 changes: 2 additions & 17 deletions web-app/src/containers/Tutorial/components/ProgressPie.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import * as React from 'react'
import { Progress, Icon } from '@alifd/next'
import useNaturalProgress from 'services/hooks/useNaturalProgress'

interface Props {
current: number
max: number
}

const ProgressPie = (props: Props) => {
const [progress, setProgress] = React.useState(0)
React.useEffect(() => {
let timeout: any
let difference = (props.current - progress) / 4
// for difference>0.01 update progress or make it stop
let newProgress = difference > 0.01 ? progress + difference : props.current
if (progress < props.current) {
timeout = setTimeout(() => {
setProgress(newProgress)
}, 100)
}
return () => {
if (timeout) {
clearTimeout(timeout)
}
}
}, [progress])
const progress = useNaturalProgress({ stop: props.current })

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

Expand Down
31 changes: 31 additions & 0 deletions web-app/src/services/hooks/useNaturalProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useState } from 'react'

interface ProgressConfig {
start?: number
stop: number
updateDuration?: number
}

const useNaturalProgress = (config: ProgressConfig): number => {
const { start, stop, updateDuration } = config
const [progress, setProgress] = useState(start || 0)
useEffect(() => {
let timeout: any
let difference = (stop - progress) / 4
// for difference>0.01 update progress or make it stop
let newProgress = difference > 0.01 ? progress + difference : stop
if (progress < stop) {
timeout = setTimeout(() => {
setProgress(newProgress)
}, updateDuration || 100)
}
return () => {
if (timeout) {
clearTimeout(timeout)
}
}
}, [progress, stop, updateDuration])
return progress
}

export default useNaturalProgress