MCPcopy Create free account
hub / github.com/bvaughn/react-window / getListCoverage

Function getListCoverage

integrations/tests/tests/scroll-element.spec.tsx:7–42  ·  view source on GitHub ↗
(page: Page, scrollerSelector?: string)

Source from the content-addressed store, hash-verified

5
6// Verifies that rendered rows cover the region of the list that is visible within the viewport (without gaps)
7async function getListCoverage(page: Page, scrollerSelector?: string) {
8 return page.evaluate((scrollerSelector) => {
9 const list = document.querySelector<HTMLElement>('[role="list"]')!;
10 const listRect = list.getBoundingClientRect();
11 const scroller = scrollerSelector
12 ? document.querySelector<HTMLElement>(scrollerSelector)!
13 : null;
14 const viewTop = scroller ? scroller.getBoundingClientRect().top : 0;
15 const viewBottom = scroller
16 ? viewTop + scroller.clientHeight
17 : document.documentElement.clientHeight;
18
19 const visibleTop = Math.max(listRect.top, viewTop);
20 const visibleBottom = Math.min(listRect.bottom, viewBottom);
21
22 const rects = Array.from(list.querySelectorAll("[data-index]"))
23 .map((row) => row.getBoundingClientRect())
24 .sort((a, b) => a.top - b.top);
25
26 let gaps = 0;
27 for (let i = 1; i < rects.length; i++) {
28 if (rects[i].top - rects[i - 1].bottom > 0.5) gaps++;
29 }
30
31 return {
32 covered:
33 visibleBottom <= visibleTop ||
34 (rects.length > 0 &&
35 rects[0].top <= visibleTop + 0.5 &&
36 rects[rects.length - 1].bottom >= visibleBottom - 0.5 &&
37 gaps === 0),
38 listScrolls: list.scrollHeight > list.clientHeight + 1,
39 renderedRows: rects.length
40 };
41 }, scrollerSelector);
42}
43
44// Smooth scrolling takes a variable amount of time;
45// wait until the target row has rendered and the scroll position stops changing

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected