({
index,
itemCount,
itemSize,
currentScrollTop,
viewportHeight
}: VirtualIndexScrollInput)
| 48 | } |
| 49 | |
| 50 | export function getScrollTopForVirtualIndex({ |
| 51 | index, |
| 52 | itemCount, |
| 53 | itemSize, |
| 54 | currentScrollTop, |
| 55 | viewportHeight |
| 56 | }: VirtualIndexScrollInput): number { |
| 57 | const count = Math.max(0, Math.floor(itemCount)) |
| 58 | if (count === 0) return Math.max(0, currentScrollTop) |
| 59 | |
| 60 | const size = Math.max(1, itemSize) |
| 61 | const safeViewportHeight = Math.max(1, viewportHeight) |
| 62 | const safeIndex = Math.max(0, Math.min(count - 1, Math.floor(index))) |
| 63 | const current = Math.max(0, currentScrollTop) |
| 64 | const itemTop = safeIndex * size |
| 65 | const itemBottom = itemTop + size |
| 66 | const viewportBottom = current + safeViewportHeight |
| 67 | |
| 68 | let next = current |
| 69 | if (itemTop < current) { |
| 70 | next = itemTop |
| 71 | } else if (itemBottom > viewportBottom) { |
| 72 | next = itemBottom - safeViewportHeight |
| 73 | } |
| 74 | |
| 75 | const maxScrollTop = Math.max(0, count * size - safeViewportHeight) |
| 76 | return Math.max(0, Math.min(maxScrollTop, next)) |
| 77 | } |
no outgoing calls
no test coverage detected