Fix fast paging with pendingIndex

This commit is contained in:
Eric Bailey
2026-04-14 15:49:01 -05:00
parent 8cc6aec271
commit 2c579e5308
@@ -25,13 +25,14 @@ export function useKeyboardHandlers({
if (imageCount <= 1) return if (imageCount <= 1) return
let stopTween: (() => void) | null = null let stopTween: (() => void) | null = null
let pendingIndex: number | null = null
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
const el = const el =
flatListRef.current?.getScrollableNode() as unknown as HTMLElement | null flatListRef.current?.getScrollableNode() as unknown as HTMLElement | null
if (!el || !el.contains(document.activeElement)) return if (!el || !el.contains(document.activeElement)) return
const current = currentIndexRef.current const current = pendingIndex ?? currentIndexRef.current
let targetIndex: number | undefined let targetIndex: number | undefined
if (e.key === 'ArrowRight' || (e.key === 'Tab' && !e.shiftKey)) { if (e.key === 'ArrowRight' || (e.key === 'Tab' && !e.shiftKey)) {
@@ -47,22 +48,27 @@ export function useKeyboardHandlers({
if (targetIndex != null) { if (targetIndex != null) {
e.preventDefault() e.preventDefault()
console.log('targetIndex', targetIndex)
if (stopTween) { if (stopTween) {
stopTween() stopTween()
stopTween = null stopTween = null
} }
pendingIndex = targetIndex
const from = el.scrollLeft const from = el.scrollLeft
const to = getOffsetForIndex(itemWidthsRef.current, targetIndex) const to = getOffsetForIndex(itemWidthsRef.current, targetIndex)
const idx = targetIndex const idx = targetIndex
stopTween = tween(from, to, SETTLE_DURATION)( stopTween = tween(
from,
to,
SETTLE_DURATION,
)(
v => { v => {
scrollTo(v) scrollTo(v)
}, },
() => { () => {
stopTween = null stopTween = null
pendingIndex = null
onSettle(idx) onSettle(idx)
}, },
) )
@@ -74,5 +80,12 @@ export function useKeyboardHandlers({
window.removeEventListener('keydown', onKeyDown) window.removeEventListener('keydown', onKeyDown)
if (stopTween) stopTween() if (stopTween) stopTween()
} }
}, [flatListRef, itemWidthsRef, currentIndexRef, scrollTo, onSettle, imageCount]) }, [
flatListRef,
itemWidthsRef,
currentIndexRef,
scrollTo,
onSettle,
imageCount,
])
} }