Skip to content

[Learn] Fix useScrollPosition re-attaching its scroll listener on every frame - #8164

Merged
PARTH-TUSSLE merged 2 commits into
layer5io:masterfrom
ayushgade06:fix/8142-scroll-position-ticking
Oct 1, 2026
Merged

PARTH-TUSSLE merged 2 commits into
layer5io:masterfrom
ayushgade06:fix/8142-scroll-position-ticking

Conversation

@ayushgade06

@ayushgade06 ayushgade06 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR fixes #8142.

useScrollPosition (src/components/Animated-steps-list/scroll-position.js) powers the scroll-scrubbed terminal animation on the Meshery Getting Started page. It throttles updates using requestAnimationFrame, but the ticking throttle flag was stored in React state and included in the effect dependency array ([ticking]).

Since ticking changes from false → true → false on every animation frame, the effect was continuously tearing down and recreating the scroll listener while scrolling.

Performance impact (before this fix)

  • ~60 listener attach/detach cycles per second because every ticking change re-ran the effect, which removed and immediately re-added the scroll listener.
  • ~120 unnecessary re-renders per second from setTicking(true) and setTicking(false) on each frame, even though ticking is not used for rendering.
  • Extra main-thread work during scrolling because the effect was repeatedly cleaned up and recreated.

Fix

  • Replaced ticking state with a local variable scoped inside the effect. The flag now updates synchronously without causing re-renders.
  • Changed the dependency array to [], so the scroll listener is attached once on mount and removed once on unmount.
  • Added { passive: true } to the scroll listener so the browser does not need to wait for the handler before continuing with scrolling.
  • scrollPosition remains the only state that triggers a render, so the animation behavior stays the same.

Net result

The visible behavior remains unchanged, but the repeated listener setup and throttle-related re-renders are eliminated. The hook now keeps one listener active instead of repeatedly attaching and removing it during scrolling.

Notes for Reviewers

  • This is a single-file change and does not change the animation behavior.
  • Verified locally on the Meshery Getting Started page (/cloud-native-management/meshery/getting-started). The animation scrubs as expected.
  • Also verified with an isolated harness running the old and new versions of the hook with the same scroll events:
    • Both versions end at the same scrollPosition.
    • The new version attaches the listener once with 0 detaches.
    • The old version performed 17 attaches and 16 detaches during the same scroll sequence.

Signed commits

  • Yes, I signed my commits.

@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 1d034be7-ef50-4fa9-95a0-364ffa3bbe73

📥 Commits

Reviewing files that changed from the base of the PR and between 7df5a77 and a48977b.

📒 Files selected for processing (1)
  • src/components/Animated-steps-list/scroll-position.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/Animated-steps-list/scroll-position.js

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

useScrollPosition now keeps its animation-frame throttle flag in a local variable. Its passive scroll listener is registered once per effect lifetime and removed during cleanup.

Changes

Scroll Position Throttle

Layer / File(s) Summary
Listener lifecycle and frame throttle
src/components/Animated-steps-list/scroll-position.js
The effect uses a local flag to allow at most one pending animation-frame update. It registers a passive scroll listener once and removes it during cleanup.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: tejaswiverma121-byte

Merge Risk: ⚪ Minimal · up to a4897

The hook avoids repeatedly reattaching its scroll listener while preserving animation updates. No actionable merge-blocking risk remains after normal checks.

Architecture Summary

Architecture risk: 🔵 Low · up to 7df5a

The change affects 1 system.

Changed systems: src

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — src (ui) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in src/components/Animated-steps-list/scroll-position.js: useScrollPosition removes state-backed ticking and the separately declared handler. The effect now uses a local flag to allow at most one pending animation-frame update, registers a passive scroll listener, and removes it on cleanup; the dependency array is now empty rather than tracking ticking.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue [#8142] requires one persistent scroll listener, cleanup on unmount, frame throttling without throttle-state renders, and a passive listener. The hook uses an effect with [], removes the liste…
Out of Scope Changes check ✅ Passed The whole-PR summary shows changes only in src/components/Animated-steps-list/scroll-position.js. The changes implement the listener lifecycle and throttling requirements in [#8142]. No unrelated ch…
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: preventing useScrollPosition from re-attaching its scroll listener on every animation frame.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for PR #8164 removed.

This PR preview was automatically pruned because we keep only the 3 most recently updated previews on GitHub Pages to stay within deployment size limits.

If needed, push a new commit to this PR to generate a fresh preview.

@tejaswiverma121-byte tejaswiverma121-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor Improvements
If this hook is ever used in a server-rendered environment, guard window access before registering the listener.A brief inline comment explaining why the throttle state is intentionally local to the effect could help future maintainers avoid reintroducing the same bug.
Optional test: a small unit/integration test around the hook would make this regression harder to reintroduce later.

Move the rAF throttle flag out of React state into a local variable scoped inside the effect. Storing it in state re-ran the effect every animation frame, detaching and re-attaching the scroll listener ~60x/sec. The flag is internal bookkeeping the UI never renders, so a local let suffices; the listener now attaches once and is passive. Behavior is unchanged. Fixes layer5io#8142

Signed-off-by: Ayush Gade <ayushgade23@gmail.com>
@ayushgade06
ayushgade06 force-pushed the fix/8142-scroll-position-ticking branch from ed12bf6 to 43254c5 Compare October 1, 2026 19:37

@PARTH-TUSSLE PARTH-TUSSLE left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 a proper hook test would need extra tooling (Testing Library + jsdom), so I'd suggest handling that in a separate follow-up issue if anything. So nothing blocking from my side.

@PARTH-TUSSLE

Copy link
Copy Markdown
Contributor

Thanks @ayushgade06 💯

@PARTH-TUSSLE
PARTH-TUSSLE merged commit 23a0088 into layer5io:master Oct 1, 2026
1 check passed
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.

useScrollPosition re-attaches its scroll listener on every frame while scrolling

3 participants