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
This commit is contained in:
1 parent
5a9b3ad273
commit
cf5fb43377
1 file changed
+28
-16
@@ -127,6 +127,7 @@ const LibraryItemsGrid = () => {
|
|||||||
// --- Virtualization setup ---
|
// --- Virtualization setup ---
|
||||||
const parentRef = useRef<HTMLDivElement | null>(null);
|
const parentRef = useRef<HTMLDivElement | null>(null);
|
||||||
const [containerWidth, setContainerWidth] = useState<number>(0);
|
const [containerWidth, setContainerWidth] = useState<number>(0);
|
||||||
|
const [scrollMargin, setScrollMargin] = useState<number>(0);
|
||||||
|
|
||||||
// Determine column count based on container width (simple heuristic)
|
// Determine column count based on container width (simple heuristic)
|
||||||
const [columns, setColumns] = useState(1);
|
const [columns, setColumns] = useState(1);
|
||||||
@@ -134,6 +135,7 @@ const LibraryItemsGrid = () => {
|
|||||||
if (!parentRef.current) return;
|
if (!parentRef.current) return;
|
||||||
const width = parentRef.current.clientWidth;
|
const width = parentRef.current.clientWidth;
|
||||||
setContainerWidth(width);
|
setContainerWidth(width);
|
||||||
|
setScrollMargin(parentRef.current.offsetTop);
|
||||||
// Base desired approximate card width (including gap) 180px
|
// Base desired approximate card width (including gap) 180px
|
||||||
const ideal = Math.max(1, Math.floor(width / 180));
|
const ideal = Math.max(1, Math.floor(width / 180));
|
||||||
setColumns(ideal);
|
setColumns(ideal);
|
||||||
@@ -145,7 +147,14 @@ const LibraryItemsGrid = () => {
|
|||||||
|
|
||||||
const ro = new ResizeObserver(() => measureColumns());
|
const ro = new ResizeObserver(() => measureColumns());
|
||||||
ro.observe(parentRef.current);
|
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]);
|
}, [measureColumns]);
|
||||||
|
|
||||||
const itemCount = items.data?.Items?.length ?? 0;
|
const itemCount = items.data?.Items?.length ?? 0;
|
||||||
@@ -161,23 +170,30 @@ const LibraryItemsGrid = () => {
|
|||||||
return Math.floor((containerWidth - totalGap) / columns);
|
return Math.floor((containerWidth - totalGap) / columns);
|
||||||
}, [columns, containerWidth]);
|
}, [columns, containerWidth]);
|
||||||
|
|
||||||
// Approximate tallest card height (portrait ratio ~ 2:3 => height ≈ width / 0.666) + text/footer area
|
// Approximate card height: width / aspect ratio + text block (title + caption).
|
||||||
// We add a small buffer (20px) for hover overlays and progress bars
|
// Square artwork (music, genres) is shorter than portrait posters.
|
||||||
const estimatedTallCardHeight = useMemo(
|
const cardAspect = useMemo(() => {
|
||||||
() => Math.round(cardWidth / 0.666666 + 90),
|
const first = items.data?.Items?.[0];
|
||||||
[cardWidth],
|
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(
|
const estimateRowHeight = useCallback(
|
||||||
() => estimatedTallCardHeight,
|
() => Math.round(cardWidth / cardAspect + 60),
|
||||||
[estimatedTallCardHeight],
|
[cardWidth, cardAspect],
|
||||||
);
|
);
|
||||||
|
|
||||||
const virtualizer = useWindowVirtualizer({
|
const virtualizer = useWindowVirtualizer({
|
||||||
count: rowCount,
|
count: rowCount,
|
||||||
estimateSize: estimateRowHeight,
|
estimateSize: estimateRowHeight,
|
||||||
overscan: 6,
|
overscan: 4,
|
||||||
scrollMargin: parentRef.current?.offsetTop ?? 0,
|
scrollMargin,
|
||||||
});
|
});
|
||||||
|
|
||||||
const virtualItems = virtualizer.getVirtualItems();
|
const virtualItems = virtualizer.getVirtualItems();
|
||||||
@@ -245,13 +261,9 @@ const LibraryItemsGrid = () => {
|
|||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
alignItems: "flex-start",
|
alignItems: "flex-start",
|
||||||
gap: `${cardGap}px`,
|
gap: `${cardGap}px`,
|
||||||
// Allow natural height; virtualizer will update size after measurement.
|
|
||||||
paddingBottom: "0.6em",
|
|
||||||
}}
|
}}
|
||||||
data-index={row.index}
|
data-index={row.index}
|
||||||
ref={(el) => {
|
ref={virtualizer.measureElement}
|
||||||
if (el) virtualizer.measureElement(el);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{items.data?.Items?.slice(startIndex, endIndex).map(
|
{items.data?.Items?.slice(startIndex, endIndex).map(
|
||||||
(item: BaseItemDto, localIndex: number) => {
|
(item: BaseItemDto, localIndex: number) => {
|
||||||
|
|||||||
Reference in new issue
Block a user