I have some problem animating my progress bar component ... I am trying to animate my component (by filling the progress bar component width from 0% to X% with a Spring when it is visible on the screen (by using VisibilitySensor). I have tried a few things so far and I did not figure out how to do it properly yet. PS: I am currently a newbie with React.
Here's what I have tried so far :
Attempt # 1: -Problem with this approach: I get an error saying "Interpolation is not defined" (probably because of width: isVisible ? ${props.percent}% : '0%' in the Spring) Maybe there is a workaround this ?
function SkillProgress(props) {
return (
<VisibilitySensor>
{({ isVisible }) => (
<Spring delay={300} to={{ opacity: isVisible ? 1 : 0, width: isVisible ? `${props.percent}%` : '0%'}}>
{animation =>
<div>
<div>{props.icon}{props.skillname}</div>
<div className={"progressbar"}>
<div className={"progressbarprg"} style={animation}><span
className={"skillLevel"}>{props.level}</span></div>
</div>
</div>
}
</Spring>
)}
</VisibilitySensor>
)
}
Attempt # 2 :
Problem with this approach : The initial animation works but when the component goes from visible to not visible the spring animation does not work, it goes directly from 0% to X%.
function SkillProgress(props) {
const spring = useSpring({from: {width: '0%'}, width: ${props.percent}%, config: config.molasses, delay: 300});
const spring2 = useSpring({from: {width: ${props.percent}%}, width: '0%', config: config.molasses, delay: 300});
return (
<VisibilitySensor>
{({ isVisible }) => (
<div>
<div>{props.icon}{props.skillname}</div>
<div className={"progressbar"}>
<animated.div className={"progressbarprg"} style={isVisible ? spring : spring2}><span
className={"skillLevel"}>{props.level}</span></animated.div>
</div>
</div>
)}
</VisibilitySensor>
)
}
Thank you in advance for your help !