Skip to content

fix: wait for DOM updates before scrolling to expanded tree row#680

Open
spike-rabbit wants to merge 1 commit intosiemens:mainfrom
spike-rabbit:refactor/scrolling/wait-for-DOM-render
Open

fix: wait for DOM updates before scrolling to expanded tree row#680
spike-rabbit wants to merge 1 commit intosiemens:mainfrom
spike-rabbit:refactor/scrolling/wait-for-DOM-render

Conversation

@spike-rabbit
Copy link
Copy Markdown
Member

What kind of change does this PR introduce? (check one with "x")

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

When scrollToRow is called on a tree row whose ancestors are still collapsed, the datatable expands the ancestors and then schedules the recursive scroll call as a microtask via queueMicrotask. Microtasks run before Angular's zoneless change detection has flushed the newly visible rows to the DOM, so the scroll container's scrollHeight is still the pre-expansion value. The browser clamps scrollTop to the old maximum and the target row is not actually scrolled into view, especially when the tree grows enough that the target lies beyond the previous scrollable area.

What is the new behavior?

The recursive scroll call is deferred via setTimeout instead of queueMicrotask. This lets Angular's change detection cycle run and update the DOM (including the scroller's height) before the scroll position is computed and applied, so the browser no longer clamps scrollTop and the target row ends up at the requested position.

A new unit test in client-side-scrolling.spec.ts reproduces the failure mode: it expands a parent with 30 children inside a constrained viewport and asserts that the resulting scrollTop matches the freshly rendered target row's offset. The test fails with queueMicrotask and passes with setTimeout. The existing should call expandToRow for a non-visible child row test was updated to use vi.waitFor so it correctly awaits the deferred scroll.

Does this PR introduce a breaking change? (check one with "x")

  • Yes
  • No

Other information:

None.

Replace queueMicrotask with setTimeout when recursively scrolling to a
tree row that required ancestor expansion. Microtasks run before
Angular's zoneless change detection has flushed the new rows to the
DOM, so the scroll container's scrollHeight is still the pre-expansion
value and the browser clamps scrollTop. Deferring via setTimeout lets
the DOM grow first so the scroll lands on the freshly rendered row.
@spike-rabbit spike-rabbit requested a review from a team as a code owner April 28, 2026 16:05
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces queueMicrotask with setTimeout in the scrollToRowTree method to ensure Angular has completed DOM updates before scrolling occurs. It also includes a new test case to verify scrolling behavior when expanding large tree structures. A review comment suggests adding a safety check to verify the component is still connected to the DOM within the setTimeout callback to avoid potential errors if the component is destroyed before the task executes.

Comment thread projects/ngx-datatable/src/lib/components/datatable.component.ts
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes DatatableComponent.scrollToRow in tree mode so scrolling occurs after ancestor expansion has actually updated the rendered viewport, preventing the browser from clamping scrollTop to the pre-expansion max.

Changes:

  • Defer the recursive scrollToRowTree call with setTimeout instead of queueMicrotask after expanding ancestors.
  • Update the existing tree-mode scrolling unit test to await the deferred scroll via vi.waitFor.
  • Add a new unit test covering the “scroll past pre-expansion scrollHeight” scenario (many children revealed after expansion).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
projects/ngx-datatable/src/lib/components/datatable.component.ts Changes scheduling of the post-expansion recursive scroll to run after DOM/render updates.
projects/ngx-datatable/src/lib/components/body/client-side-scrolling.spec.ts Updates tree-mode scroll test synchronization and adds a regression test for large expansion + scroll clamping.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread projects/ngx-datatable/src/lib/components/datatable.component.ts
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.

2 participants