-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add scrollToFn, align options and getIndexOffset
- add scrollToFn - add align options to scrollToIndex function - add align options to scrollToOffset function - add getIndexOffset function
- Loading branch information
1 parent
65e0f3b
commit f0e281f
Showing
4 changed files
with
229 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from "react"; | ||
|
||
import { useVirtual } from "react-virtual"; | ||
|
||
function easeInOutQuint(t) { | ||
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t; | ||
} | ||
|
||
export default function() { | ||
const parentRef = React.useRef(); | ||
|
||
const scrollToFn = React.useCallback(offset => { | ||
const duration = 1000; | ||
const start = parentRef.current.scrollTop; | ||
const startTime = Date.now(); | ||
|
||
const run = () => { | ||
const now = Date.now(); | ||
const elapsed = now - startTime; | ||
const progress = easeInOutQuint(Math.min(elapsed / duration, 1)); | ||
const interpolated = start + (offset - start) * progress; | ||
|
||
if (elapsed < duration) { | ||
parentRef.current.scrollTop = interpolated; | ||
setTimeout(run, 16); | ||
} else { | ||
parentRef.current.scrollTop = interpolated; | ||
} | ||
}; | ||
|
||
setTimeout(run, 16); | ||
}, []); | ||
|
||
const rowVirtualizer = useVirtual({ | ||
size: 10000, | ||
parentRef, | ||
estimateSize: React.useCallback(() => 35, []), | ||
overscan: 5, | ||
scrollToFn | ||
}); | ||
|
||
return ( | ||
<> | ||
<button | ||
onClick={() => | ||
rowVirtualizer.scrollToIndex(Math.floor(Math.random() * 10000)) | ||
} | ||
> | ||
Scroll To Random Index | ||
</button> | ||
|
||
<br /> | ||
<br /> | ||
|
||
<div | ||
ref={parentRef} | ||
className="List" | ||
style={{ | ||
height: `200px`, | ||
width: `400px`, | ||
overflow: "auto" | ||
}} | ||
> | ||
<div | ||
style={{ | ||
height: `${rowVirtualizer.totalSize}px`, | ||
width: "100%", | ||
position: "relative" | ||
}} | ||
> | ||
{rowVirtualizer.virtualItems.map(virtualRow => ( | ||
<div | ||
key={virtualRow.index} | ||
className={virtualRow.index % 2 ? "ListItemOdd" : "ListItemEven"} | ||
style={{ | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
width: "100%", | ||
height: `${virtualRow.size}px`, | ||
transform: `translateY(${virtualRow.start}px)` | ||
}} | ||
> | ||
Row {virtualRow.index} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters