MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / useVirtualScroll

Function useVirtualScroll

source code/hooks/useVirtualScroll.ts:144–723  ·  view source on GitHub ↗
(
  scrollRef: RefObject<ScrollBoxHandle | null>,
  itemKeys: readonly string[],
  /**
   * Terminal column count. On change, cached heights are stale (text
   * rewraps) — SCALED by oldCols/newCols rather than cleared. Clearing
   * made the pessimistic coverage back-walk mount ~190 items (every
   * uncached item → PESSIMISTIC_HEIGHT=1 → walk 190 to reach
   * viewport+2×overscan). Each fresh mount runs marked.lexer + syntax
   * highlighting ≈ 3ms; ~600ms React reconcile on first resize with a
   * long conversation. Scaling keeps heightCache populated → back-walk
   * uses real-ish heights → mount range stays tight. Scaled estimates
   * are overwritten by real Yoga heights on next useLayoutEffect.
   *
   * Scaled heights are close enough that the black-screen-on-widen bug
   * (inflated pre-resize offsets overshoot post-resize scrollTop → end
   * loop stops short of tail) doesn't trigger: ratio<1 on widen scales
   * heights DOWN, keeping offsets roughly aligned with post-resize Yoga.
   */
  columns: number,
)

Source from the content-addressed store, hash-verified

142 * the last N items regardless of what scrollTop claims.
143 */
144export function useVirtualScroll(
145 scrollRef: RefObject<ScrollBoxHandle | null>,
146 itemKeys: readonly string[],
147 /**
148 * Terminal column count. On change, cached heights are stale (text
149 * rewraps) — SCALED by oldCols/newCols rather than cleared. Clearing
150 * made the pessimistic coverage back-walk mount ~190 items (every
151 * uncached item → PESSIMISTIC_HEIGHT=1 → walk 190 to reach
152 * viewport+2×overscan). Each fresh mount runs marked.lexer + syntax
153 * highlighting ≈ 3ms; ~600ms React reconcile on first resize with a
154 * long conversation. Scaling keeps heightCache populated → back-walk
155 * uses real-ish heights → mount range stays tight. Scaled estimates
156 * are overwritten by real Yoga heights on next useLayoutEffect.
157 *
158 * Scaled heights are close enough that the black-screen-on-widen bug
159 * (inflated pre-resize offsets overshoot post-resize scrollTop → end
160 * loop stops short of tail) doesn't trigger: ratio<1 on widen scales
161 * heights DOWN, keeping offsets roughly aligned with post-resize Yoga.
162 */
163 columns: number,
164): VirtualScrollResult {
165 const heightCache = useRef(new Map<string, number>())
166 // Bump whenever heightCache mutates so offsets rebuild on next read. Ref
167 // (not state) — checked during render phase, zero extra commits.
168 const offsetVersionRef = useRef(0)
169 // scrollTop at last commit, for detecting fast-scroll mode (slide cap gate).
170 const lastScrollTopRef = useRef(0)
171 const offsetsRef = useRef<{ arr: Float64Array; version: number; n: number }>({
172 arr: new Float64Array(0),
173 version: -1,
174 n: -1,
175 })
176 const itemRefs = useRef(new Map<string, DOMElement>())
177 const refCache = useRef(new Map<string, (el: DOMElement | null) => void>())
178 // Inline ref-compare: must run before offsets is computed below. The
179 // skip-flag guards useLayoutEffect from re-populating heightCache with
180 // PRE-resize Yoga heights (useLayoutEffect reads Yoga from the frame
181 // BEFORE this render's calculateLayout — the one that had the old width).
182 // Next render's useLayoutEffect reads post-resize Yoga → correct.
183 const prevColumns = useRef(columns)
184 const skipMeasurementRef = useRef(false)
185 // Freeze the mount range for the resize-settling cycle. Already-mounted
186 // items have warm useMemo (marked.lexer, highlighting); recomputing range
187 // from scaled/pessimistic estimates causes mount/unmount churn (~3ms per
188 // fresh mount = ~150ms visible as a second flash). The pre-resize range is
189 // as good as any — items visible at old width are what the user wants at
190 // new width. Frozen for 2 renders: render #1 has skipMeasurement (Yoga
191 // still pre-resize), render #2's useLayoutEffect reads post-resize Yoga
192 // into heightCache. Render #3 has accurate heights → normal recompute.
193 const prevRangeRef = useRef<readonly [number, number] | null>(null)
194 const freezeRendersRef = useRef(0)
195 if (prevColumns.current !== columns) {
196 const ratio = prevColumns.current / columns
197 prevColumns.current = columns
198 for (const [k, h] of heightCache.current) {
199 heightCache.current.set(k, Math.max(1, Math.round(h * ratio)))
200 }
201 offsetVersionRef.current++

Callers 1

VirtualMessageListFunction · 0.85

Calls 9

setMethod · 0.80
maxMethod · 0.80
keysMethod · 0.80
deleteMethod · 0.80
hasMethod · 0.45
getMethod · 0.45
getComputedWidthMethod · 0.45
getComputedTopMethod · 0.45
getComputedHeightMethod · 0.45

Tested by

no test coverage detected