From cf5fb433778fa04e29e189297d068bd845ac40b9 Mon Sep 17 00:00:00 2001 From: 1xDed Date: Fri, 27 Nov 2026 03:11:23 +0300 Subject: [PATCH] fix: stop library cards from jumping while scrolling - stable measureElement ref (the inline ref re-attached on every render and re-triggered measurement, causing continuous row re-layout) - reactive scrollMargin instead of reading offsetTop once at mount - size rows from the real card aspect ratio (portrait vs square) and text block - remove duplicated row padding/gap --- src/components/libraryItemsGrid/index.tsx | 44 ++++++++++++++--------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/components/libraryItemsGrid/index.tsx b/src/components/libraryItemsGrid/index.tsx index 70ee75e..b640c19 100644 --- a/src/components/libraryItemsGrid/index.tsx +++ b/src/components/libraryItemsGrid/index.tsx @@ -127,6 +127,7 @@ const LibraryItemsGrid = () => { // --- Virtualization setup --- const parentRef = useRef(null); const [containerWidth, setContainerWidth] = useState(0); + const [scrollMargin, setScrollMargin] = useState(0); // Determine column count based on container width (simple heuristic) const [columns, setColumns] = useState(1); @@ -134,6 +135,7 @@ const LibraryItemsGrid = () => { if (!parentRef.current) return; const width = parentRef.current.clientWidth; setContainerWidth(width); + setScrollMargin(parentRef.current.offsetTop); // Base desired approximate card width (including gap) 180px const ideal = Math.max(1, Math.floor(width / 180)); setColumns(ideal); @@ -145,7 +147,14 @@ const LibraryItemsGrid = () => { const ro = new ResizeObserver(() => measureColumns()); ro.observe(parentRef.current); - return () => ro.disconnect(); + // The header above the grid can change height (filters, wrapping): keep the + // window-virtualizer offset in sync so rows never jump while scrolling. + ro.observe(document.body); + window.addEventListener("resize", measureColumns); + return () => { + ro.disconnect(); + window.removeEventListener("resize", measureColumns); + }; }, [measureColumns]); const itemCount = items.data?.Items?.length ?? 0; @@ -161,23 +170,30 @@ const LibraryItemsGrid = () => { return Math.floor((containerWidth - totalGap) / columns); }, [columns, containerWidth]); - // Approximate tallest card height (portrait ratio ~ 2:3 => height ≈ width / 0.666) + text/footer area - // We add a small buffer (20px) for hover overlays and progress bars - const estimatedTallCardHeight = useMemo( - () => Math.round(cardWidth / 0.666666 + 90), - [cardWidth], - ); + // Approximate card height: width / aspect ratio + text block (title + caption). + // Square artwork (music, genres) is shorter than portrait posters. + const cardAspect = useMemo(() => { + const first = items.data?.Items?.[0]; + const square = + first?.Type === BaseItemKind.MusicAlbum || + first?.Type === BaseItemKind.Audio || + first?.Type === BaseItemKind.Genre || + first?.Type === BaseItemKind.MusicGenre || + first?.Type === BaseItemKind.Studio || + first?.Type === BaseItemKind.Playlist; + return square ? 1 : 0.666666; + }, [items.data?.Items]); const estimateRowHeight = useCallback( - () => estimatedTallCardHeight, - [estimatedTallCardHeight], + () => Math.round(cardWidth / cardAspect + 60), + [cardWidth, cardAspect], ); const virtualizer = useWindowVirtualizer({ count: rowCount, estimateSize: estimateRowHeight, - overscan: 6, - scrollMargin: parentRef.current?.offsetTop ?? 0, + overscan: 4, + scrollMargin, }); const virtualItems = virtualizer.getVirtualItems(); @@ -245,13 +261,9 @@ const LibraryItemsGrid = () => { flexDirection: "row", alignItems: "flex-start", gap: `${cardGap}px`, - // Allow natural height; virtualizer will update size after measurement. - paddingBottom: "0.6em", }} data-index={row.index} - ref={(el) => { - if (el) virtualizer.measureElement(el); - }} + ref={virtualizer.measureElement} > {items.data?.Items?.slice(startIndex, endIndex).map( (item: BaseItemDto, localIndex: number) => {