[bug]: useResize is jumpy
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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