Skip to content

fix(shared): report the window size when useResize mounts - #2695

Open
Dev-next-gen wants to merge 1 commit into
pmndrs:nextfrom
Dev-next-gen:fix/use-resize-window-size
Open

Dev-next-gen wants to merge 1 commit into
pmndrs:nextfrom
Dev-next-gen:fix/use-resize-window-size

Conversation

@Dev-next-gen

Copy link
Copy Markdown

Why

useResize() without a container returns width and height of 0 after mount, and they stay at 0 until the window fires a resize event. resizeWindow only calls its callbacks from the resize listener, so nothing measures the window when useResize subscribes. The ResizeObserver path doesn't have this problem because the observer reports once on observe.

Resolves #2694

What

resizeWindow now calls the new callback with the current innerWidth / innerHeight when it subscribes. useResize already treats its first measurement as immediate, so the spring jumps to the window size on mount, and later resize events animate as before.

The tests in useResize.animation.test.tsx used the first dispatched resize event as the first measurement. With this change the first measurement happens on mount, so mount now stubs the initial innerWidth / innerHeight and the tests cover the same cases from there. On next, three of them fail against the adapted mount (expected +0 to be 100), which is the same bug.

I added a test to useResize.test.tsx that mounts useResize({}) and checks the springs match the window size after advanceUntilIdle(). On next it fails with expected +0 to be 414; with the fix, the useResize, useScroll, onScroll and resizeElement tests all pass (14/14, Chromium). oxfmt --check and oxlint are clean on the changed files. pnpm check:types fails for me on module resolution in files this PR doesn't touch, because I didn't run a build first, so I'm leaving that to CI.

Checklist

  • Linked an issue (Resolves #…)
  • pnpm format:check, pnpm lint, pnpm check:types and pnpm test:unit pass locally
  • New tests fail without the fix
  • Changeset added (if published behaviour changes)
  • Ready to be merged

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.

resizeWindow only called its callback from the window resize listener, so useResize() without a container stayed at width 0 and height 0 until the window was resized. The ResizeObserver path already reports on observe; report the current window size on subscribe as well.
@changeset-bot

changeset-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a6c40b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@react-spring/shared Patch
@react-spring/animated Patch
@react-spring/core Patch
@react-spring/parallax Patch
@react-spring/rafz Patch
@react-spring/types Patch
@react-spring/three Patch
@react-spring/web Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: useResize without a container stays at 0x0 until the window is resized

1 participant