MCPcopy
hub / github.com/bvaughn/react-window / getOffsetForIndex

Function getOffsetForIndex

lib/core/getOffsetForIndex.ts:5–90  ·  view source on GitHub ↗
({
  align,
  cachedBounds,
  index,
  itemCount,
  itemSize,
  containerScrollOffset,
  containerSize
}: {
  align: Align;
  cachedBounds: CachedBounds;
  index: number;
  itemCount: number;
  itemSize: number | SizeFunction<Props>;
  containerScrollOffset: number;
  containerSize: number;
})

Source from the content-addressed store, hash-verified

3import type { CachedBounds, SizeFunction } from "./types";
4
5export function getOffsetForIndex<Props extends object>({
6 align,
7 cachedBounds,
8 index,
9 itemCount,
10 itemSize,
11 containerScrollOffset,
12 containerSize
13}: {
14 align: Align;
15 cachedBounds: CachedBounds;
16 index: number;
17 itemCount: number;
18 itemSize: number | SizeFunction<Props>;
19 containerScrollOffset: number;
20 containerSize: number;
21}) {
22 if (index < 0 || index >= itemCount) {
23 throw RangeError(`Invalid index specified: ${index}`, {
24 cause: `Index ${index} is not within the range of 0 - ${itemCount - 1}`
25 });
26 }
27
28 const estimatedTotalSize = getEstimatedSize({
29 cachedBounds,
30 itemCount,
31 itemSize
32 });
33
34 const bounds = cachedBounds.get(index);
35 const maxOffset = Math.max(
36 0,
37 Math.min(estimatedTotalSize - containerSize, bounds.scrollOffset)
38 );
39 const minOffset = Math.max(
40 0,
41 bounds.scrollOffset - containerSize + bounds.size
42 );
43
44 if (align === "smart") {
45 if (
46 containerScrollOffset >= minOffset &&
47 containerScrollOffset <= maxOffset
48 ) {
49 align = "auto";
50 } else {
51 align = "center";
52 }
53 }
54
55 switch (align) {
56 case "start": {
57 return maxOffset;
58 }
59 case "end": {
60 return minOffset;
61 }
62 case "center": {

Callers 2

useVirtualizerFunction · 0.90
createTestHelperFunction · 0.90

Calls 1

getEstimatedSizeFunction · 0.90

Tested by 1

createTestHelperFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…