reset scroll position on list page change

This commit is contained in:
jeffvli
2025-10-23 00:50:24 -07:00
parent 74d5f2c61f
commit 8e1eed655f
6 changed files with 37 additions and 2 deletions
@@ -179,6 +179,7 @@ export interface GridItemProps {
} }
export interface ItemGridListProps { export interface ItemGridListProps {
currentPage?: number;
data: unknown[]; data: unknown[];
enableExpansion?: boolean; enableExpansion?: boolean;
enableSelection?: boolean; enableSelection?: boolean;
@@ -199,6 +200,7 @@ export interface ItemGridListProps {
} }
export const ItemGridList = ({ export const ItemGridList = ({
currentPage,
data, data,
enableExpansion = true, enableExpansion = true,
enableSelection = true, enableSelection = true,
@@ -331,8 +333,23 @@ export const ItemGridList = ({
], ],
); );
// Scroll to top when currentPage changes
useEffect(() => {
if (currentPage !== undefined && tableMeta?.itemHeight) {
scrollToGridOffset(0);
}
}, [currentPage, scrollToGridOffset, tableMeta?.itemHeight]);
useEffect(() => { useEffect(() => {
if (!initialTop || isInitialScrollPositionSet.current || !tableMeta?.itemHeight) return; if (!initialTop || isInitialScrollPositionSet.current || !tableMeta?.itemHeight) return;
// Only set initial scroll position if we haven't done it yet AND we're not on a page change
// This prevents the initial scroll position from being restored on every page change
if (currentPage !== undefined && currentPage > 0) {
isInitialScrollPositionSet.current = true;
return;
}
isInitialScrollPositionSet.current = true; isInitialScrollPositionSet.current = true;
if (initialTop.type === 'offset') { if (initialTop.type === 'offset') {
@@ -343,7 +360,7 @@ export const ItemGridList = ({
index: initialTop.to, index: initialTop.to,
}); });
} }
}, [initialTop, itemGridRef, scrollToGridOffset, tableMeta?.itemHeight]); }, [initialTop, itemGridRef, scrollToGridOffset, tableMeta?.itemHeight, currentPage]);
const imperativeHandle: ItemListHandle = useMemo(() => { const imperativeHandle: ItemListHandle = useMemo(() => {
return { return {
@@ -403,6 +403,7 @@ interface ItemTableListProps {
CellComponent: JSXElementConstructor<CellComponentProps<TableItemProps>>; CellComponent: JSXElementConstructor<CellComponentProps<TableItemProps>>;
cellPadding?: 'lg' | 'md' | 'sm' | 'xl' | 'xs'; cellPadding?: 'lg' | 'md' | 'sm' | 'xl' | 'xs';
columns: ItemTableListColumnConfig[]; columns: ItemTableListColumnConfig[];
currentPage?: number;
data: unknown[]; data: unknown[];
enableAlternateRowColors?: boolean; enableAlternateRowColors?: boolean;
enableExpansion?: boolean; enableExpansion?: boolean;
@@ -435,6 +436,7 @@ export const ItemTableList = ({
CellComponent, CellComponent,
cellPadding = 'sm', cellPadding = 'sm',
columns, columns,
currentPage,
data, data,
enableAlternateRowColors = false, enableAlternateRowColors = false,
enableExpansion = true, enableExpansion = true,
@@ -1079,6 +1081,13 @@ export const ItemTableList = ({
} }
}, [initialTop, scrollToTableIndex, scrollToTableOffset]); }, [initialTop, scrollToTableIndex, scrollToTableOffset]);
// Scroll to top when currentPage changes
useEffect(() => {
if (currentPage !== undefined) {
scrollToTableOffset(0);
}
}, [currentPage, scrollToTableOffset]);
const imperativeHandle: ItemListHandle = useMemo(() => { const imperativeHandle: ItemListHandle = useMemo(() => {
return { return {
clearExpanded: () => { clearExpanded: () => {
@@ -64,6 +64,7 @@ export const AlbumListPaginatedGrid = forwardRef<any, AlbumListPaginatedGridProp
totalItemCount={totalItemCount} totalItemCount={totalItemCount}
> >
<ItemGridList <ItemGridList
currentPage={currentPage}
data={data || []} data={data || []}
gap={gap} gap={gap}
initialTop={{ initialTop={{
@@ -73,6 +73,7 @@ export const AlbumListPaginatedTable = forwardRef<any, AlbumListPaginatedTablePr
<ItemTableList <ItemTableList
CellComponent={ItemTableListColumn} CellComponent={ItemTableListColumn}
columns={columns} columns={columns}
currentPage={currentPage}
data={data || []} data={data || []}
enableAlternateRowColors={enableAlternateRowColors} enableAlternateRowColors={enableAlternateRowColors}
enableHorizontalBorders={enableHorizontalBorders} enableHorizontalBorders={enableHorizontalBorders}
@@ -52,7 +52,13 @@ export const SongListPaginatedGrid = forwardRef<any, SongListPaginatedGridProps>
pageCount={pageCount} pageCount={pageCount}
totalItemCount={totalItemCount} totalItemCount={totalItemCount}
> >
<ItemGridList data={data || []} gap={gap} itemType={LibraryItem.SONG} ref={ref} /> <ItemGridList
currentPage={currentPage}
data={data || []}
gap={gap}
itemType={LibraryItem.SONG}
ref={ref}
/>
</ItemListWithPagination> </ItemListWithPagination>
); );
}, },
@@ -68,6 +68,7 @@ export const SongListPaginatedTable = forwardRef<any, SongListPaginatedTableProp
<ItemTableList <ItemTableList
CellComponent={ItemTableListColumn} CellComponent={ItemTableListColumn}
columns={columns} columns={columns}
currentPage={currentPage}
data={data || []} data={data || []}
enableAlternateRowColors={enableAlternateRowColors} enableAlternateRowColors={enableAlternateRowColors}
enableHorizontalBorders={enableHorizontalBorders} enableHorizontalBorders={enableHorizontalBorders}