From 3bf958bcf4f6380b68203ce479d7052f0bd4d96e Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Sat, 8 Jan 2022 18:17:18 -0800 Subject: [PATCH] Preserve scroll when zooming/resizing --- src/SkinTable.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/SkinTable.js b/src/SkinTable.js index f6fc582e..05ba6912 100644 --- a/src/SkinTable.js +++ b/src/SkinTable.js @@ -31,12 +31,24 @@ const SkinTable = ({ return skin ? skin.hash : `unfectched-index-${requestToken}`; } const gridRef = React.useRef(); + const itemRef = React.useRef(); React.useLayoutEffect(() => { if (gridRef.current == null) { return; } gridRef.current.scrollTo({ scrollLeft: 0, scrollTop: 0 }); }, [skinCount]); + + React.useLayoutEffect(() => { + if (gridRef.current == null) { + return; + } + + const itemRow = Math.floor(itemRef.current / columnCount) + + gridRef.current.scrollTo({ scrollLeft: 0, scrollTop: rowHeight * itemRow }); + }, [rowHeight, columnCount]); + const showGrid = loadingSearchQuery || skinCount > 0 || searchQuery === ""; return (
@@ -53,6 +65,9 @@ const SkinTable = ({ rowHeight={rowHeight} width={windowWidth} overscanRowsCount={5} + onScroll={scrollData => { + itemRef.current = Math.round(scrollData.scrollTop / rowHeight) * columnCount + }} style={{ overflowY: "scroll" }} > {Cell}