Skip to content

Commit 75417bb

Browse files
authored
Fix TableView resize observer loop limit (#5432)
* Fix resize observer loop limit
1 parent b249a56 commit 75417bb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/@react-aria/virtualizer/src/ScrollView.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ interface ScrollViewProps extends HTMLAttributes<HTMLElement> {
3838
scrollDirection?: 'horizontal' | 'vertical' | 'both'
3939
}
4040

41+
let isOldReact = React.version.startsWith('16.') || React.version.startsWith('17.');
42+
4143
function ScrollView(props: ScrollViewProps, ref: RefObject<HTMLDivElement>) {
4244
let {
4345
contentSize,
@@ -148,7 +150,25 @@ function ScrollView(props: ScrollViewProps, ref: RefObject<HTMLDivElement>) {
148150
useLayoutEffect(() => {
149151
updateSize();
150152
}, [updateSize]);
151-
useResizeObserver({ref, onResize: updateSize});
153+
let raf = useRef<ReturnType<typeof requestAnimationFrame> | null>();
154+
let onResize = () => {
155+
if (isOldReact) {
156+
raf.current ??= requestAnimationFrame(() => {
157+
updateSize();
158+
raf.current = null;
159+
});
160+
} else {
161+
updateSize();
162+
}
163+
};
164+
useResizeObserver({ref, onResize});
165+
useEffect(() => {
166+
return () => {
167+
if (raf.current) {
168+
cancelAnimationFrame(raf.current);
169+
}
170+
};
171+
}, []);
152172

153173
let style: React.CSSProperties = {
154174
// Reset padding so that relative positioning works correctly. Padding will be done in JS layout.

packages/@react-spectrum/list/test/ListView.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,9 @@ describe('ListView', function () {
14261426
act(() => {
14271427
fireEvent(window, new Event('resize'));
14281428
});
1429+
act(() => {
1430+
jest.runAllTimers();
1431+
});
14291432
await user.tab();
14301433
expect(grid.scrollTop).toBe(0);
14311434

@@ -1475,6 +1478,9 @@ describe('ListView', function () {
14751478
act(() => {
14761479
fireEvent(window, new Event('resize'));
14771480
});
1481+
act(() => {
1482+
jest.runAllTimers();
1483+
});
14781484
expect(grid.scrollTop).toBe(0);
14791485

14801486
focusRow(tree, 'Item 1');

0 commit comments

Comments
 (0)