-
-
Notifications
You must be signed in to change notification settings - Fork 55
fix(ScrollArea): reset scroll only on direct viewport child swaps (#1349) #2045
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||
| import { act, fireEvent, render, screen } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||
| import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import ScrollArea from '../ScrollArea'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -176,4 +176,42 @@ describe('ScrollArea', () => { | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(screen.getByTestId('scrollbar')).toHaveAttribute('data-state', 'visible'); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| test('resets scroll when direct viewport child is swapped, not on nested mutations', async() => { | ||||||||||||||||||||||||||||||||||||||
| const { rerender } = render( | ||||||||||||||||||||||||||||||||||||||
| <ScrollArea.Root style={{ height: 100 }}> | ||||||||||||||||||||||||||||||||||||||
| <ScrollArea.Viewport data-testid="viewport" style={{ height: 100, overflow: 'auto' }}> | ||||||||||||||||||||||||||||||||||||||
| <div key="panel-a" data-testid="panel-a" style={{ height: 400 }}>Panel A</div> | ||||||||||||||||||||||||||||||||||||||
| </ScrollArea.Viewport> | ||||||||||||||||||||||||||||||||||||||
| </ScrollArea.Root> | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const viewport = screen.getByTestId('viewport') as HTMLDivElement; | ||||||||||||||||||||||||||||||||||||||
| viewport.scrollTop = 120; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| act(() => { | ||||||||||||||||||||||||||||||||||||||
| const nested = document.createElement('span'); | ||||||||||||||||||||||||||||||||||||||
| nested.textContent = 'nested'; | ||||||||||||||||||||||||||||||||||||||
| screen.getByTestId('panel-a').appendChild(nested); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(viewport.scrollTop).toBe(120); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+192
to
+198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Flush the nested mutation before the negative assertion.
💚 Proposed test hardening act(() => {
const nested = document.createElement('span');
nested.textContent = 'nested';
screen.getByTestId('panel-a').appendChild(nested);
});
+ await act(async() => {
+ await Promise.resolve();
+ });
+
expect(viewport.scrollTop).toBe(120);📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| rerender( | ||||||||||||||||||||||||||||||||||||||
| <ScrollArea.Root style={{ height: 100 }}> | ||||||||||||||||||||||||||||||||||||||
| <ScrollArea.Viewport data-testid="viewport" style={{ height: 100, overflow: 'auto' }}> | ||||||||||||||||||||||||||||||||||||||
| <div key="panel-b" data-testid="panel-b" style={{ height: 400 }}>Panel B</div> | ||||||||||||||||||||||||||||||||||||||
| </ScrollArea.Viewport> | ||||||||||||||||||||||||||||||||||||||
| </ScrollArea.Root> | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| await act(async() => { | ||||||||||||||||||||||||||||||||||||||
| await Promise.resolve(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const nextViewport = screen.getByTestId('viewport') as HTMLDivElement; | ||||||||||||||||||||||||||||||||||||||
| await waitFor(() => { | ||||||||||||||||||||||||||||||||||||||
| expect(nextViewport.scrollTop).toBe(0); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Narrow the reset predicate to actual child replacements.
directChildSwapcurrently fires for any direct child add/remove, so appending or removing an item directly under the viewport will reset the user to the top. Track added and removed nodes across the batch and reset only when both occur for the viewport.🐛 Proposed predicate tightening
📝 Committable suggestion
🤖 Prompt for AI Agents