Skip to content

Commit acd7ca3

Browse files
committed
feat: Add scrollToIndex and scrollToOffset API
- `scrollToIndex: Function(index: Integer) => void 0` - Call this function to scroll the top/left of the parentRef element to the start of the item located at the passed index. - `scrollToOffset: Function(offsetInPixels: Integer) => void 0` - Call this function to scroll the top/left of the parentRef element to the passed pixel offset.
1 parent 09d7dbc commit acd7ca3

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ const {
256256
/* ... */
257257
],
258258
totalSize,
259+
scrollToIndex,
260+
scrollToOffset,
259261
} = useVirtual({
260262
size,
261263
parentRef,
@@ -300,11 +302,15 @@ const {
300302
- The static/variable or, if dynamically rendered, the measured size of the item
301303
- `end: Integer`
302304
- The ending measurement of the item
303-
- `measureRef: React.useRef | Function(el) => void 0`
305+
- `measureRef: React.useRef | Function(el: DOMElement) => void 0`
304306
- The ref/function to place on the rendered element to enable dynamic measurement rendering
305307
- `totalSize: Integer`
306308
- The total size of the entire virtualizer
307309
- When using dynamic measurement refs, this number may change as items are measured after they are rendered.
310+
- `scrollToIndex: Function(index: Integer) => void 0`
311+
- Call this function to scroll the top/left of the parentRef element to the start of the item located at the passed index.
312+
- `scrollToOffset: Function(offsetInPixels: Integer) => void 0`
313+
- Call this function to scroll the top/left of the parentRef element to the passed pixel offset.
308314

309315
# Contributors ✨
310316

examples/sandbox/src/styles.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ html {
44
}
55

66
.List {
7-
border: 1px solid #d9dddd;
7+
border: 1px solid #e6e4dc;
88
}
99

1010
.ListItemEven,
@@ -15,11 +15,11 @@ html {
1515
}
1616

1717
.ListItemEven {
18-
background-color: #f8f8f0;
18+
background-color: #e6e4dc;
1919
}
2020

2121
.Grid {
22-
border: 1px solid #d9dddd;
22+
border: 1px solid #e6e4dc;
2323
}
2424

2525
.GridItemEven,
@@ -30,5 +30,5 @@ html {
3030
}
3131

3232
.GridItemEven {
33-
background-color: #f8f8f0;
33+
background-color: #e6e4dc;
3434
}

src/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ export function useVirtual({
2525
_setScrollOffset(newScrollOffset)
2626
})
2727

28-
const setScrollOffset = React.useCallback(
29-
offset => {
30-
_setScrollOffset(offset)
31-
parentRef.current[scrollKey] = offset
32-
},
33-
[parentRef, scrollKey]
34-
)
35-
3628
const scrollOffsetPlusSize = scrollOffset + outerSize
3729

3830
const [measuredCache, setMeasuredCache] = React.useState({})
@@ -107,9 +99,29 @@ export function useVirtual({
10799
return items
108100
}, [startIndex, endIndex, measurements, sizeKey])
109101

102+
const scrollToOffset = React.useCallback(
103+
offset => {
104+
_setScrollOffset(offset)
105+
parentRef.current[scrollKey] = offset
106+
},
107+
[parentRef, scrollKey]
108+
)
109+
110+
const scrollToIndex = React.useCallback(
111+
index => {
112+
const measurement = measurements[index]
113+
114+
if (measurement) {
115+
scrollToOffset(measurement.start)
116+
}
117+
},
118+
[measurements, scrollToOffset]
119+
)
120+
110121
return {
111122
items,
112123
totalSize: total,
113-
setScrollOffset,
124+
scrollToOffset,
125+
scrollToIndex,
114126
}
115127
}

0 commit comments

Comments
 (0)