MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / getScrollTopForVirtualIndex

Function getScrollTopForVirtualIndex

packages/app-core/src/lib/virtual-list.ts:50–77  ·  view source on GitHub ↗
({
  index,
  itemCount,
  itemSize,
  currentScrollTop,
  viewportHeight
}: VirtualIndexScrollInput)

Source from the content-addressed store, hash-verified

48}
49
50export 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}

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected