MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useScrollView

Function useScrollView

packages/react-aria/src/virtualizer/ScrollView.tsx:70–405  ·  view source on GitHub ↗
(
  props: ScrollViewProps,
  ref: RefObject<HTMLElement | null>
)

Source from the content-addressed store, hash-verified

68}
69
70export function useScrollView(
71 props: ScrollViewProps,
72 ref: RefObject<HTMLElement | null>
73): ScrollViewAria {
74 let {
75 contentSize,
76 onVisibleRectChange,
77 onSizeChange,
78 innerStyle,
79 onScrollStart,
80 onScrollEnd,
81 scrollDirection = 'both',
82 onScroll: onScrollProp,
83 allowsWindowScrolling,
84 ...otherProps
85 } = props;
86
87 // oxlint-disable-next-line react/react-compiler
88 let state = useRef({
89 // Internal scroll position of the scroll view.
90 scrollPosition: new Point(),
91 // Size of the scroll view.
92 size: new Size(),
93 // Offset of the scroll view relative to the window viewport.
94 viewportOffset: new Point(),
95 // Size of the window viewport.
96 viewportSize: new Size(),
97 scrollEndTime: 0,
98 scrollTimeout: null as ReturnType<typeof setTimeout> | null,
99 isScrolling: false,
100 lastVisibleRect: new Rect()
101 }).current;
102 let {direction} = useLocale();
103
104 let updateVisibleRect = useCallback(() => {
105 // Intersect the window viewport with the scroll view itself to find the actual visible rectangle.
106 // This allows virtualized components to have unbounded height but still virtualize when scrolled with the page.
107 // While there may be other scrollable elements between the <body> and the scroll view, we do not take
108 // their sizes into account for performance reasons. Their scroll positions are accounted for in viewportOffset
109 // though (due to getBoundingClientRect). This may result in more rows than absolutely necessary being rendered,
110 // but no more than the entire height of the viewport which is good enough for virtualization use cases.
111 let visibleRect = allowsWindowScrolling
112 ? new Rect(
113 state.viewportOffset.x + state.scrollPosition.x,
114 state.viewportOffset.y + state.scrollPosition.y,
115 Math.max(
116 0,
117 Math.min(state.size.width - state.viewportOffset.x, state.viewportSize.width)
118 ),
119 Math.max(
120 0,
121 Math.min(state.size.height - state.viewportOffset.y, state.viewportSize.height)
122 )
123 )
124 : new Rect(
125 state.scrollPosition.x,
126 state.scrollPosition.y,
127 state.size.width,

Callers 2

CollectionRootFunction · 0.90
ScrollViewFunction · 0.85

Calls 15

useLocaleFunction · 0.90
getEventTargetFunction · 0.90
nodeContainsFunction · 0.90
getScrollLeftFunction · 0.90
addEventFunction · 0.90
getPropagationTargetsFunction · 0.90
getOwnerDocumentFunction · 0.90
useEffectEventFunction · 0.90
useResizeObserverFunction · 0.90
fnFunction · 0.85
updateSizeFunction · 0.85
addEventListenerMethod · 0.80

Tested by

no test coverage detected