react-animated-numbers icon indicating copy to clipboard operation
react-animated-numbers copied to clipboard

CSS Issue: fontWeight disturb animation

Open alexabidri opened this issue 2 years ago • 10 comments

Hey guys!

When I am setting the animation with this following pattern:

<AnimatedNumbers animateToNumber={cartTotal} fontStyle={{ fontSize: 16, letterSpacing: 0, fontWeight: 600 }}></AnimatedNumbers>;

The first time the component animates and render 0, the rendering got stuck between two numbers. ⚠️

Screenshot 2022-06-21 at 10 48 20

It works fine if i dont mention the fontWeight

<AnimatedNumbers animateToNumber={cartTotal} fontStyle={{ fontSize: 16, letterSpacing: 0 }}></AnimatedNumbers>;

Any clues? Thanks everyone

alexabidri avatar Jun 21 '22 02:06 alexabidri

@alexabidri really sorry for late reply. I will check it as soon as possible

heyman333 avatar Jul 03 '22 16:07 heyman333

The spinning is affected even if the font-weight is in the CSS file. I need guidance here. Do you guys know how to fix it or what file I should look into more precisely to fix this?

Mako-L avatar Dec 15 '22 09:12 Mako-L

Hey guys!

When I am setting the animation with this following pattern:

<AnimatedNumbers animateToNumber={cartTotal} fontStyle={{ fontSize: 16, letterSpacing: 0, fontWeight: 600 }}></AnimatedNumbers>;

The first time the component animates and render 0, the rendering got stuck between two numbers. ⚠️

Screenshot 2022-06-21 at 10 48 20

It works fine if i dont mention the fontWeight

<AnimatedNumbers animateToNumber={cartTotal} fontStyle={{ fontSize: 16, letterSpacing: 0 }}></AnimatedNumbers>;

Any clues? Thanks everyone

<div
  key={index}
  style={{
    height: numberHeight,
    width: numberWidth,
    overflow: "hidden",
  }}
>
<Spring
  key={`${keyCount.current++}`}
  from={{
    transform: "translateY(0px)",
  }}
  to={{
    transform: `translateY(${
      -1 * numberHeight * animateToNumbersArr[index]
    }) translateX(${-0.5 * numberWidth})`,
  }}
  config={setConfig(configs, n, index)}
  delay={index * 150}
>
  {(props) =>
    NUMBERS.map((number, i) => (
      <animated.div
        key={i}
        style={{
          ...fontStyle,
          ...props,
          height: numberHeight,
          width: numberWidth,
        }}
      >
        {number}
      </animated.div>
    ))
  }
</Spring>
</div>
const numberDivRef = React.useRef(null);

React.useEffect(() => {
  const height = numberDivRef.current.getClientRects()?.[0]?.height;
  const width = numberDivRef.current.getClientRects()?.[0]?.width;
  if (height && width) {
    setNumberHeight(height);
    setNumberWidth(width);
  }
}, [animateToNumber, fontStyle]);
<div
  ref={numberDivRef}
  style={{ position: "absolute", top: -9999, ...fontStyle }}
>
  {0}
</div>

Solution is to adjust the transform value used in the to prop of the Spring component. Instead of using a fixed value for the translateY transform, you can use a dynamic value that is based on the number being animated. For example, you can use the numberHeight and numberWidth variables to calculate the vertical and horizontal offsets for each number, and then use these values to position the numbers correctly.

Note that in this example, the translateX transform is being used to horizontally center the numbers within their container. This should help to prevent the numbers from overlapping at the end of the animation.

Mako-L avatar Dec 15 '22 10:12 Mako-L

@Mako-L Thank you for your wonderful solution! Why don't you make pull-request?

heyman333 avatar Feb 03 '23 15:02 heyman333

I did make one. It just has the code formated and didnt get accepted and i didnt had time to make a version where is not yet.

You said:


It's hard to know the code changes 😹
Can you simply re-commit your code changes without formatting the code?

thank you so much for your interest

Mako-L avatar Feb 03 '23 15:02 Mako-L

Screenshot 2024-01-24 at 6 16 35 PM

@heyman333 I really greatful for the library, but it would be better if we can fix this. This issue is occuring even when I am not using custom font-weight.

@Mako-L it would be great if you can able to make the changes for the pr and get it merged.

venkatesh1146 avatar Jan 24 '24 12:01 venkatesh1146

Screenshot 2024-01-24 at 6 16 35 PM @heyman333 I really greatful for the library, but it would be better if we can fix this. This issue is occuring even when I am not using custom font-weight.

@Mako-L it would be great if you can able to make the changes for the pr and get it merged.

Are you using the latest version? Also I remade the pr request and tested on mine and it worked all fine. Lmk.

Mako-L avatar Jan 25 '24 13:01 Mako-L

Yes I update to v0.17.1 and tried. Still the issue persists.

venkatesh1146 avatar Jan 25 '24 13:01 venkatesh1146

Try the last version and tell me if the same issue occurs.

Mako-L avatar Jan 30 '24 21:01 Mako-L

Hi @Mako-L @heyman333

I have updated the version "react-animated-numbers": "^0.18.0" Still it is same. This is happening sometimes only not everytime. Screenshot 2024-01-31 at 5 47 02 PM

I am using "next": "12.3.1", "react": "18.2.0",

venkatesh1146 avatar Jan 31 '24 12:01 venkatesh1146