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

Window resizing causes numbers to be positioned incorrectly.

Open mmousawy opened this issue 2 years ago • 4 comments

After resizing the window and the font sizes changes, the numbers seem to be positioned incorrectly. I think it's because the translations are all based on pixels (height of numbers divs). Is there a way to use percentages for the translation? That way it will always stay relative to the container/font size.

image

mmousawy avatar Aug 26 '22 14:08 mmousawy

Hi, did you manage to solve this issue? I got the same problem.

JosefHobler avatar Jan 14 '23 17:01 JosefHobler

I am also experiencing this issue.

Dylan700 avatar Nov 16 '23 03:11 Dylan700

Here's a super hacky temporary fix... add this to your functional component:

    // this is used to trigger a re-render because of the bug in AnimatedNumbers
    const [, updateState] = useState<{}>()
    const rerender = useCallback(() => updateState({}), [])

    useEffect(() => {
       addEventListener("resize", rerender)
        return () => {
            removeEventListener("resize", rerender)
        }
    }, [])

Essentially, whenever the window size changes, we force the current component (containing the AnimatedNumbers component) to re-render so that the numbers get positioned correctly. Just be mindful that this is bascially bypassing the optimisations in the react rendering lifecycle so yeah.

Dylan700 avatar Nov 16 '23 03:11 Dylan700

Here's a super hacky temporary fix... add this to your functional component:

this one didn't work for me. Instead I used key to force remount.

const [key, setKey] = useState(0);

  useEffect(() => {
    const updateKey = () => setKey((prev) => prev + 1);

    addEventListener('resize', updateKey);
    return () => {
      removeEventListener('resize', updateKey);
    };
  }, []);

...

<AnimatedNumbers key={`type-${key}`} animateToNumber={value} />

exceedcat avatar May 07 '24 13:05 exceedcat