Skip to content

Commit ee754cb

Browse files
author
Amrit Kashyap Borah
committed
feat: add progress bar
1 parent 95230ba commit ee754cb

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useEffect } from 'react'
2+
import { AnimatePresence, motion, useMotionValue, useSpring, useTransform } from 'framer-motion'
3+
4+
import { ProgressBarProps } from './types'
5+
6+
export const ProgressBar = ({ isLoading, intervalTime = 50 }: ProgressBarProps) => {
7+
const progress = useMotionValue<number>(0)
8+
const smoothProgress = useSpring(progress, { stiffness: 50, damping: 20 })
9+
10+
const width = useTransform(smoothProgress, (v) => `${v}%`)
11+
12+
useEffect(() => {
13+
let interval = null
14+
15+
if (isLoading) {
16+
interval = setInterval(() => {
17+
const next = progress.get() + Math.random() * 5
18+
progress.set(next < 95 ? next : 95)
19+
}, intervalTime)
20+
}
21+
22+
return () => clearInterval(interval)
23+
}, [isLoading])
24+
25+
return (
26+
<AnimatePresence>
27+
{isLoading && (
28+
<motion.div
29+
initial={{ opacity: 1 }}
30+
exit={{ opacity: 0 }}
31+
transition={{ duration: 1, ease: 'easeOut' }}
32+
className="dc__position-abs dc__top-0 dc__left-0 dc__zi-10 h-2 w-100 bcn-2"
33+
>
34+
<motion.div
35+
className="h-100 bcb-5"
36+
style={{
37+
width,
38+
}}
39+
/>
40+
</motion.div>
41+
)}
42+
</AnimatePresence>
43+
)
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ProgressBar.component'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface ProgressBarProps {
2+
isLoading: boolean
3+
/**
4+
* Interval in milliseconds for the progress bar simulation.
5+
*
6+
* @default 50
7+
*/
8+
intervalTime?: number
9+
}

src/Shared/Components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export * from './ModalSidebarPanel'
7979
export * from './NumbersCount'
8080
export * from './PhoneInput'
8181
export * from './Plugin'
82+
export * from './ProgressBar'
8283
export { default as QRCode } from './QRCode'
8384
export * from './ReactSelect'
8485
export * from './RegistryIcon'

0 commit comments

Comments
 (0)