Spaces:
Running
Running
File size: 1,056 Bytes
e6a9f90 | 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 51 | import React, { useEffect, useState } from 'react';
interface ProgressBarProps {
duration: number;
isActive: boolean;
onComplete?: () => void;
}
const ProgressBar: React.FC<ProgressBarProps> = ({
duration,
isActive,
onComplete
}) => {
const [progress, setProgress] = useState(0);
useEffect(() => {
if (!isActive) {
setProgress(0);
return;
}
setProgress(0);
const interval = setInterval(() => {
setProgress(prevProgress => {
const newProgress = prevProgress + (100 / (duration * 10));
if (newProgress >= 100) {
clearInterval(interval);
if (onComplete) onComplete();
return 100;
}
return newProgress;
});
}, 100);
return () => clearInterval(interval);
}, [duration, isActive, onComplete]);
return (
<div className="progress-container">
<div
className="progress-bar"
style={{ width: `${progress}%` }}
/>
</div>
);
};
export default ProgressBar; |