-
Notifications
You must be signed in to change notification settings - Fork 647
perf(hooks): Add opt-in throttling to useResizeObserver and useOverflow #7335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…useOverflow - useResizeObserver fires callback immediately on first observation, then throttles with rAF - useOverflow uses the same pattern to avoid initial flash of incorrect overflow state - Added isFirstCallback ref pattern to skip throttling on initial mount Part of #7312
🦋 Changeset detectedLatest commit: 64a0de0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the |
| return | ||
| } | ||
| } | ||
| setHasOverflow(false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is a current bug, since things can never go to not overflow states
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds performance optimizations to useResizeObserver and useOverflow hooks by implementing a "first-immediate" throttling pattern. The changes aim to reduce INP (Interaction to Next Paint) scores by batching ResizeObserver callbacks using requestAnimationFrame while ensuring the initial callback fires immediately to prevent UI flash.
Key changes:
- Immediate execution on first ResizeObserver callback, then rAF throttling for subsequent callbacks
- Proper cleanup of pending animation frames on component unmount
- Behavioral fix in
useOverflowwhere overflow state now correctly resets to false
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/react/src/hooks/useResizeObserver.ts | Added first-immediate throttling pattern with rAF batching to reduce layout thrashing during rapid resize events |
| packages/react/src/hooks/useOverflow.ts | Applied same throttling pattern and fixed logic to properly reset hasOverflow to false when overflow is removed |
| .changeset/perf-use-resize-observer.md | Documents the performance improvements as a patch-level change |
francinelucca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! some non-blocking comments
|
👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/9375 |
Summary
Performance optimizations for useResizeObserver and useOverflow hooks to improve INP, with opt-in throttling for backwards compatibility.
Changes
throttleparameter (4th argument, defaults tofalse)throttle=false(default): behaves exactly as before - no throttling, immediate callbacksthrottle=true: fires callback immediately on first observation, then throttles subsequent callbacks with rAFuseResizeObserverwiththrottle=truefor internal optimizationAPI
Expected INP Impact (when throttle=true)
Why opt-in?
Why this matters
ResizeObserver can fire many times per frame during animations or rapid resizing:
The "first-immediate" pattern ensures initial render gets correct size without delay.
Part of the INP performance optimization effort. See #7312 for full context.