Skip to content
Source
Sign inSign up
TanStack/virtual

feat(virtual-core): add cancelScroll() to stop an in-flight scroll reconciliation #1300

Mergedpiecykmerged 1 commit intomain fromrefs/pull/1300/headmerged by piecyk

piecykcommented

🎯 Changes

Closes #1285.

After scrollToIndex / scrollToOffset / scrollBy / scrollToEnd, reconcileScroll keeps correcting toward the target for up to 5s. If the user scrolls during that window, any measurement that moves the target pulls the viewport back. During a smooth scroll, size-change compensation is also off until the scroll finishes or times out. Until now, the only way out was writing the private scrollState field.

 scrollToIndex(i)
   scrollState = { index: i, ... }
   _scrollToOffset(target)
   scheduleScrollReconcile()        // rAF loop, ≤ 5s
+
+cancelScroll()
+  cancelAnimationFrame(rafId)
+  scrollState = null               // scroll position untouched

The scroll position is deliberately left as it is. Writing it would stop iOS momentum, and a real gesture already interrupts a native smooth scroll. The app decides when the user has taken over, for example from wheel / touchstart / keydown handlers. Detecting that automatically in core is left for a follow-up.

 packages/
 ├── virtual-core/src/index.ts              + cancelScroll()
 ├── angular-virtual/src/index.ts           + 'cancelScroll' in the signal proxy's pass-through list
 └── marko-virtual/src/tags/
     ├── virtualizer/index.marko            + cancelScroll on the handle
     └── window-virtualizer/index.marko     + cancelScroll on the handle

React, Vue, Solid, Svelte and Lit return the core instance, so they get it without changes. Docs: api/virtualizer.md, the chat guide's API list, and the Marko docs and README tables.

Evidence

New core tests in packages/virtual-core/tests/index.test.ts:

scrollToIndex(50) → cancelScroll() → resizeItem(10, 200) → flush rAF
  expect scrollToFn not called again           // no yank toward the moved target

scrollOffset = 1000 → scrollToIndex(50, smooth) → cancelScroll() → resizeItem(0, 80)
  expect scrollToFn(1000, { adjustments: 30 }) // compensation is back

cancelScroll() with nothing in flight → no-op; later scrollToOffset still settles
  • Before (cancelScroll stubbed to return): the first two tests fail.
  • After: all 3 pass. virtual-core: 174 tests pass. pnpm run test:pr --base=upstream/main passes for 69 projects.

Merge Danger

Door: two-way. The new method only runs when called. Adding a public method can't be undone without a major release, though, so the name is worth agreeing on now (cancelScroll, since it also covers scrollBy / scrollToEnd).

Blast Radius: none. Nothing calls it internally, so existing behaviour is unchanged.

#1294 also changes reconcileScroll. The two don't conflict in purpose, but whichever merges second needs a small rebase.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added cancelScroll() to stop an in-progress virtualized scroll without changing the current position. Available across the core, Angular, and Marko APIs.
  • Documentation
    • Added cancelScroll() to the API references.
    1. 🦋 Changeset detected

      Latest commit: 88129a9681a203b2df96b9d0d5785a38786ecb4f

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

      This PR includes changesets to release 9 packages
      NameType
      @tanstack/virtual-coreMinor
      @tanstack/angular-virtualMinor
      @tanstack/marko-virtualMinor
      @tanstack/lit-virtualPatch
      @tanstack/react-virtualPatch
      @tanstack/solid-virtualPatch
      @tanstack/svelte-virtualPatch
      @tanstack/vue-virtualPatch
      @tanstack/virtual-benchmarksPatch

      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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: defaults
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 1c47744a-2e37-4ca8-a288-cb623b3f5e21

      📥 Commits

      Reviewing files that changed from the base of the PR and between c644f1096c9f4d8221d92862afdc9316b17df62a and 88129a9681a203b2df96b9d0d5785a38786ecb4f.


      📒 Files selected for processing (10)
      • .changeset/cancel-scroll.md
      • docs/api/virtualizer.md
      • docs/chat.md
      • docs/framework/marko/marko-virtual.md
      • packages/angular-virtual/src/index.ts
      • packages/marko-virtual/README.md
      • packages/marko-virtual/src/tags/virtualizer/index.marko
      • packages/marko-virtual/src/tags/window-virtualizer/index.marko
      • packages/virtual-core/src/index.ts
      • packages/virtual-core/tests/index.test.ts

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



      📝 Walkthrough

      Walkthrough

      Adds cancelScroll() to virtual-core to stop active scroll reconciliation without changing the current scroll position. Angular and Marko APIs expose the method. Tests cover cancellation behavior, and documentation and release notes describe the API.

      Changes

      Scroll cancellation

      Layer / File(s)Summary
      Core cancellation and tests
      packages/virtual-core/src/index.ts, packages/virtual-core/tests/index.test.ts
      Virtualizer.cancelScroll() cancels a scheduled animation frame and clears scrollState. Tests cover indexed and smooth scroll cancellation, cancellation with no active scroll, and subsequent offset scrolling.
      Framework API exposure
      packages/angular-virtual/src/index.ts, packages/marko-virtual/src/tags/virtualizer/index.marko, packages/marko-virtual/src/tags/window-virtualizer/index.marko
      The Angular proxy passes through cancelScroll. Marko virtualizer handles declare the method and delegate it to the active virtualizer.
      API documentation and release note
      docs/api/virtualizer.md, docs/chat.md, docs/framework/marko/marko-virtual.md, packages/marko-virtual/README.md, .changeset/cancel-scroll.md
      The documentation describes cancelScroll() and its behavior. The changeset declares minor releases for the virtual-core, Angular, and Marko packages.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Feature · Severity of issue fixed: Medium


      Merge Risk: ⚪ Minimal · up to 88129

      This adds an optional cancelScroll() method to stop in-flight scroll reconciliation without moving the current scroll position. Existing behavior is unchanged unless callers use the new method, so the merge risk is minimal.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (7 skipped: 7 …Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title checkPassedThe title clearly identifies the main change: adding cancelScroll() to stop in-flight scroll reconciliation.
      Description checkPassedThe description is complete and relevant. It explains the motivation, implementation, affected packages, tests, release impact, and checklist status. It also documents compatibility considerations and…
      Linked Issues checkPassedIssue #1285 requires a public core API that cancels active scroll reconciliation for index and offset scrolling without changing the current DOM offset, and preserves later measurement compensation. T…
      Out of Scope Changes checkPassedThe changes stay within Issue #1285. The core method, adapter exposure, documentation, changeset, and focused tests support the public cancellation API. Extending the documented method to scrollBy a…

      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (7 skipped: 7 unsupported.)



      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR


      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Warning

      Some tools did not complete. Review the errors below.

      🔧 ESLint

      If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

      packages/virtual-core/tests/index.test.ts

      Parsing error: "parserOptions.project" has been provided for @typescript-eslint/parser. The file was not found in any of the provided project(s): packages/virtual-core/tests/index.test.ts




      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.
    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 88129a9681a203b2df96b9d0d5785a38786ecb4f

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded4m 2sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded21sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-10-09 12:16:32 UTC

    4. More templates

      @tanstack/angular-virtual

      npm i https://pkg.pr.new/@tanstack/angular-virtual@1300

      @tanstack/lit-virtual

      npm i https://pkg.pr.new/@tanstack/lit-virtual@1300

      @tanstack/marko-virtual

      npm i https://pkg.pr.new/@tanstack/marko-virtual@1300

      @tanstack/react-virtual

      npm i https://pkg.pr.new/@tanstack/react-virtual@1300

      @tanstack/solid-virtual

      npm i https://pkg.pr.new/@tanstack/solid-virtual@1300

      @tanstack/svelte-virtual

      npm i https://pkg.pr.new/@tanstack/svelte-virtual@1300

      @tanstack/virtual-core

      npm i https://pkg.pr.new/@tanstack/virtual-core@1300

      @tanstack/vue-virtual

      npm i https://pkg.pr.new/@tanstack/vue-virtual@1300

      commit: 88129a9

    5. piecyk merged this43675bc

    6. piecyk closed this

Sign in to join this conversation.