Fix scroll to top behaviour (#5228)

This commit is contained in:
WithoutPants 2024-09-06 13:53:23 +10:00 committed by GitHub
parent ca970b9706
commit 0a98296642
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View file

@ -92,6 +92,9 @@ export const ItemList = <T extends QueryResult, E extends IHasID>(
onSelectNone,
} = useListContext<E>();
// scroll to the top of the page when the page changes
useScrollToTopOnPageChange(filter.currentPage, result.loading);
const { modal, showModal, closeModal } = useModal();
const metadataByline = useMemo(() => {
@ -320,9 +323,6 @@ export const ItemListContext = <T extends QueryResult, E extends IHasID>(
view
);
// scroll to the top of the page when the page changes
useScrollToTopOnPageChange(filter.currentPage);
if (defaultFilterLoading) return null;
return (

View file

@ -338,9 +338,19 @@ export function useCachedQueryResult<T extends QueryResult>(
return cachedResult;
}
export function useScrollToTopOnPageChange(currentPage: number) {
export function useScrollToTopOnPageChange(
currentPage: number,
loading: boolean
) {
const prevPage = usePrevious(currentPage);
// scroll to the top of the page when the page changes
// only scroll to top if the page has changed and is not loading
useEffect(() => {
if (loading || currentPage === prevPage || prevPage === undefined) {
return;
}
// if the current page has a detail-header, then
// scroll up relative to that rather than 0, 0
const detailHeader = document.querySelector(".detail-header");
@ -349,7 +359,7 @@ export function useScrollToTopOnPageChange(currentPage: number) {
} else {
window.scrollTo(0, 0);
}
}, [currentPage]);
}, [prevPage, currentPage, loading]);
}
// handle case where page is more than there are pages