Skip to content

Commit 0bce76d

Browse files
author
Gijs Nieuwenhuis
committed
Use window.scrollTo instead of smooth scrolling as this is somewhat confusing for the client
1 parent e12c868 commit 0bce76d

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

src/react/hooks/__tests__/useScrollToTopOnDependencyChange.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { renderHook, act } from '@testing-library/react-hooks';
22
import useScrollToTopOnDependencyChange from '../useScrollToTopOnDependencyChange';
33

44
describe('useScrollToTopOnDependencyChange', () => {
5-
let oldWindowScroll: Function;
5+
let oldWindowScrollTo: Function;
66

77
beforeEach(() => {
8-
oldWindowScroll = window.scroll;
9-
window.scroll = jest.fn();
8+
oldWindowScrollTo = window.scrollTo;
9+
window.scrollTo = jest.fn();
1010
});
1111

1212
afterEach(() => {
1313
// @ts-ignore => cannot copy type of window.scroll somehow
14-
window.scroll = oldWindowScroll;
14+
window.scrollTo = oldWindowScrollTo;
1515
});
1616

1717
describe('on mount', () => {
1818
it('should execute window.scroll', () => {
1919
renderHook(() => useScrollToTopOnDependencyChange());
2020

21-
expect(window.scroll).toBeCalledTimes(1);
21+
expect(window.scrollTo).toBeCalledTimes(1);
2222
});
2323
});
2424

@@ -34,7 +34,7 @@ describe('useScrollToTopOnDependencyChange', () => {
3434
depedencies = ['second'];
3535
rerender();
3636

37-
expect(window.scroll).toBeCalledTimes(2);
37+
expect(window.scrollTo).toBeCalledTimes(2);
3838
});
3939
});
4040
});

src/react/hooks/useScrollToTopOnDependencyChange.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,5 @@ import { useEffect } from 'react';
33
export default function useScrollToTopOnDependencyChange(
44
...dependencies: any[]
55
): void {
6-
useEffect(
7-
() =>
8-
window.scroll({
9-
top: 0,
10-
left: 0,
11-
behavior: 'smooth',
12-
}),
13-
dependencies
14-
);
6+
useEffect(() => window.scrollTo(0, 0), dependencies);
157
}

0 commit comments

Comments
 (0)