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

Function useScrollToIndex

lib/core/useScrollToIndex.ts:32–284  ·  view source on GitHub ↗
({
  direction,
  element,
  getCellBounds,
  isDynamicSize,
  itemCount,
  itemElements,
  scrollTarget,
  getScrollPositionForIndex
}: {
  direction: Direction;
  element: HTMLElement | null;
  getCellBounds: (index: number) => Bounds;
  isDynamicSize: boolean;
  itemCount: number;
  itemElements: RefObject<Element[]>;
  scrollTarget: ScrollTarget | null;
  getScrollPositionForIndex: (options: {
    align?: Align;
    index: number;
  }) => { scrollOffset: number; scrollTarget: ScrollTarget } | undefined;
})

Source from the content-addressed store, hash-verified

30 * ℹ️ Dynamic sizes are only supported for vertical lists.
31 */
32export function useScrollToIndex({
33 direction,
34 element,
35 getCellBounds,
36 isDynamicSize,
37 itemCount,
38 itemElements,
39 scrollTarget,
40 getScrollPositionForIndex
41}: {
42 direction: Direction;
43 element: HTMLElement | null;
44 getCellBounds: (index: number) => Bounds;
45 isDynamicSize: boolean;
46 itemCount: number;
47 itemElements: RefObject<Element[]>;
48 scrollTarget: ScrollTarget | null;
49 getScrollPositionForIndex: (options: {
50 align?: Align;
51 index: number;
52 }) => { scrollOffset: number; scrollTarget: ScrollTarget } | undefined;
53}): (options: {
54 align?: Align;
55 behavior?: ScrollBehavior;
56 index: number;
57}) => void {
58 const isVertical = direction === "vertical";
59 const pendingScroll = useRef<{
60 align: Align;
61 index: number;
62 smooth: boolean;
63 stableFrames: number;
64 // Set on the first frame, since frames are paused while the page is hidden
65 animation?: {
66 deadline: number;
67 startOffset: number;
68 startTime: number;
69 };
70 } | null>(null);
71
72 // Track the window that scheduled the frame so it can be cancelled there.
73 const scrollFrame = useRef<{ id: number; view: Window } | undefined>(
74 undefined
75 );
76
77 const cancelScroll = useStableCallback(() => {
78 pendingScroll.current = null;
79 if (scrollFrame.current !== undefined) {
80 scrollFrame.current.view.cancelAnimationFrame(scrollFrame.current.id);
81 scrollFrame.current = undefined;
82 }
83 });
84
85 // Use the list's own window (e.g. a popup rendered into via a portal),
86 // since another window's frames may be throttled while it's in the background.
87 const scheduleCorrectScroll = useStableCallback(() => {
88 const view = getOwnerWindow(element);
89 scrollFrame.current = {

Callers 2

ListComponentFunction · 0.90
useVirtualizerFunction · 0.90

Calls 6

useStableCallbackFunction · 0.90
getOwnerWindowFunction · 0.90
getMaxScrollPositionFunction · 0.90
getScrollPositionFunction · 0.90
getItemIndexFunction · 0.90
easeOutCubicFunction · 0.85

Tested by

no test coverage detected