Skip to content

Commit 8327dad

Browse files
committed
2 parents 87312bc + 59470ab commit 8327dad

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,13 @@ const {
267267
totalSize,
268268
scrollToIndex,
269269
scrollToOffset,
270-
getIndexOffset,
271-
scrollToFn,
272270
} = useVirtual({
273271
size,
274272
parentRef,
275273
estimateSize,
276274
overscan,
277275
horizontal,
276+
scrollToFn
278277
})
279278
```
280279

@@ -326,7 +325,7 @@ const {
326325
- `scrollToIndex: Function(index: Integer, { align: String }) => void 0`
327326
- Call this function to scroll the top/left of the parentRef element to the start of the item located at the passed index.
328327
- `align: 'start' | 'center' | 'end' | 'auto'`
329-
- Defaults to `start`
328+
- Defaults to `auto`
330329
- `start` places the item at the top/left of the visible scroll area
331330
- `center` places the item in the center of the visible scroll area
332331
- `end` places the item at the bottom/right of the visible scroll area
@@ -339,15 +338,6 @@ const {
339338
- `center` places the offset in the center of the visible scroll area
340339
- `end` places the offset at the bottom/right of the visible scroll area
341340
- `auto` brings the offset into the visible scroll area either at the start or end, depending on which is closer. If the offset is already in view, it is placed at the `top/left` of the visible scroll area.
342-
- `getIndexOffset: Function(index: Integer, { align: String }) => Integer | undefined`
343-
- Call this function to return the offset of the item located at the passed index.
344-
- `align: 'start' | 'center' | 'end' | 'auto'`
345-
- Defaults to `start`
346-
- `start` returns the item's top/left offset
347-
- `center` returns the item's center offset
348-
- `end` returns the item's bottom/right offset
349-
- `auto` return's the item's start or end offset, depending on which is closer. If the item is already in view, the `top/left` offset will be returned
350-
- If `align: 'auto'` is passed and no scrolling is necessary to bring the item into view, `undefined` is returned.
351341

352342
# Contributors ✨
353343

src/index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ export function useVirtual({
153153
[scrollToFn]
154154
)
155155

156-
const getIndexOffset = React.useCallback(
157-
(index, { align = 'start' } = {}) => {
156+
const scrollToIndex = React.useCallback(
157+
(index, { align = 'auto' } = {}) => {
158158
const measurement = measurements[index]
159159

160160
if (!measurement) {
@@ -177,26 +177,14 @@ export function useVirtual({
177177
: align === 'end'
178178
? measurement.end
179179
: measurement.start
180-
181-
return offset
182-
},
183-
[measurements, scrollOffset, scrollOffsetPlusOuterSize]
184-
)
185-
186-
const scrollToIndex = React.useCallback(
187-
(index, options) => {
188-
const offset = getIndexOffset(index, options)
189-
if (typeof offset !== 'undefined') {
190-
scrollToOffset(offset, options)
191-
}
180+
scrollToOffset(offset, options)
192181
},
193-
[getIndexOffset, scrollToOffset]
182+
[scrollToOffset]
194183
)
195184

196185
return {
197186
virtualItems,
198187
totalSize,
199-
getIndexOffset,
200188
scrollToOffset,
201189
scrollToIndex,
202190
}

types/index.d.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
declare module 'react-virtual/index'
1+
type ScrollAlignment = 'start' | 'center' | 'end' | 'auto'
2+
3+
interface ScrollToOptions {
4+
align: ScrollAlignment
5+
}
6+
7+
interface ScrollToOffsetOptions extends ScrollToOptions {}
8+
interface ScrollToIndexOptions extends ScrollToOptions {}
9+
10+
export type VirtualItem = {
11+
index: number
12+
start: number
13+
end: number
14+
size: number
15+
measureRef: React.RefObject<any>
16+
}
17+
18+
declare function useVirtual<T>(options: {
19+
size: number
20+
parentRef: React.RefObject<T>
21+
estimateSize: (index?: number) => number
22+
overscan?: number
23+
horizontal?: boolean
24+
scrollToFn?: (offset: number) => void
25+
}): {
26+
virtualItems: VirtualItem[]
27+
totalSize: number
28+
scrollToOffset: (index: number, options?: ScrollToOffsetOptions) => void
29+
scrollToIndex: (index: number, options?: ScrollToIndexOptions) => void
30+
}
31+
32+
export { useVirtual }

0 commit comments

Comments
 (0)