({
itemCount,
itemSize,
scrollTop,
viewportHeight,
overscan = 6
}: VirtualRangeInput)
| 13 | } |
| 14 | |
| 15 | export function getVirtualRange({ |
| 16 | itemCount, |
| 17 | itemSize, |
| 18 | scrollTop, |
| 19 | viewportHeight, |
| 20 | overscan = 6 |
| 21 | }: VirtualRangeInput): VirtualRange { |
| 22 | const count = Math.max(0, Math.floor(itemCount)) |
| 23 | if (count === 0) return { start: 0, end: 0, totalSize: 0 } |
| 24 | |
| 25 | const size = Math.max(1, itemSize) |
| 26 | const safeScrollTop = Math.max(0, scrollTop) |
| 27 | const safeOverscan = Math.max(0, Math.floor(overscan)) |
| 28 | const visibleStart = Math.min(count - 1, Math.floor(safeScrollTop / size)) |
| 29 | const visibleCount = |
| 30 | viewportHeight > 0 ? Math.max(1, Math.ceil(viewportHeight / size)) : safeOverscan + 1 |
| 31 | |
| 32 | const start = Math.max(0, visibleStart - safeOverscan) |
| 33 | const end = Math.min(count, visibleStart + visibleCount + safeOverscan) |
| 34 | |
| 35 | return { |
| 36 | start, |
| 37 | end, |
| 38 | totalSize: count * size |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | export interface VirtualIndexScrollInput { |
| 43 | index: number |
no outgoing calls
no test coverage detected