pmndrs / pmndrs/react-spring

[bug]: useResize is jumpy

Open
#2,234 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: core kind: bug
Dominant language
TypeScript
Stars
29.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Which react-spring target are you using?
  • @react-spring/web
  • @react-spring/three
  • @react-spring/native
  • @react-spring/konva
  • @react-spring/zdog
What version of react-spring are you using?

9.7.3

What's Wrong?

Hello 👋 useResize does not always animate to/from 0 height due to immediate attribute
https://github.com/pmndrs/react-spring/blob/cfc74ea3495906bca6f1b3febdd6f86a580a9057/packages/core/src/hooks/useResize.ts#L60

Hook could be misused by me - there are not many examples in docs. Please correct me if I'm wrong 🙏

To Reproduce

I created a sandbox where I tried disabling immediate flag inside useResize hook impl. Though then it animates on mount which is even worse... 🤷 Sandbox contains both OG and modded useResize without that single attribute 😊

For the record: the workaround I found for now is to always provide at least 1px of content within the referenced container:

export function AnimateHeight({ children }) {
  const container = useRef();
  const { height } = useResize({ container });

  return (
    <animated.div style={{ height, overflow: 'hidden' }}>
      <div ref={container}>
        <div style={{ height: '1px', marginTop: '-1px' }} />
        {children}
      </div>
    </animated.div>
  );
}
Expected Behaviour

Animated wrapper should be animating height when referenced container height changes from/to 0

Link to repo

https://codesandbox.io/p/sandbox/react-spring-use-resize-jitter-p2npzl

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting packages/core/src/hooks/useResize.ts at the referenced line and compare its behavior with the CodeSandbox reproduction. Verify height transitions involving 0 for @react-spring/web, including mount behavior; done means the wrapper animates correctly when the referenced container changes to or from zero height without the 1px workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.