Fix workspace sidebar scroll jitter (#2282)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-05-18 16:54:46 -07:00
committed by GitHub
co-authored by Orca
parent 87a83ae4f2
commit 0f162ab76b
3 changed files with 108 additions and 5 deletions
@@ -299,6 +299,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
}: VirtualizedWorktreeViewportProps) {
const scrollRef = useRef<HTMLDivElement>(null)
const suppressMeasurementAdjustmentUntilRef = useRef(0)
const directScrollInputUntilRef = useRef(0)
const [dragOverStatus, setDragOverStatus] = useState<WorkspaceStatus | null>(null)
const [pinDragOver, setPinDragOver] = useState(false)
const [lineageReconnectWorktreeId, setLineageReconnectWorktreeId] = useState<string | null>(null)
@@ -394,12 +395,23 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
},
[isCurrentVirtualRowElement]
)
const markDirectScrollInput = useCallback(() => {
const markScrollMovement = useCallback(() => {
suppressMeasurementAdjustmentUntilRef.current =
window.performance.now() + USER_SCROLL_MEASUREMENT_ADJUSTMENT_SUPPRESS_MS
}, [])
const markDirectScrollInput = useCallback(() => {
const suppressUntil = window.performance.now() + USER_SCROLL_MEASUREMENT_ADJUSTMENT_SUPPRESS_MS
suppressMeasurementAdjustmentUntilRef.current = suppressUntil
directScrollInputUntilRef.current = suppressUntil
}, [])
const hasDirectScrollInput = useCallback(
() => window.performance.now() < directScrollInputUntilRef.current,
[]
)
// Why: programmatic scrolls should keep measurement correction quiet, but
// only direct input should block anchor restoration retries.
const shouldSkipScrollAnchorRestore = useCallback(
() => window.performance.now() < suppressMeasurementAdjustmentUntilRef.current,
() => window.performance.now() < directScrollInputUntilRef.current,
[]
)
@@ -566,6 +578,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
rows: renderRows,
scrollElementRef: scrollRef,
scrollOffsetRef,
hasDirectScrollInput,
shouldSkipRestore: shouldSkipScrollAnchorRestore,
totalSize,
virtualizer
@@ -665,6 +678,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
}
if (mod && e.shiftKey && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
markDirectScrollInput()
navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down')
e.preventDefault()
}
@@ -672,7 +686,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
window.addEventListener('keydown', handleKeyDown, { capture: true })
return () => window.removeEventListener('keydown', handleKeyDown, { capture: true })
}, [activeModal, navigateWorktree])
}, [activeModal, markDirectScrollInput, navigateWorktree])
// Why: lightweight nested cards do not mount WorktreeCard, so the viewport
// owns the SSH reconnect prompt for an active lineage child.
@@ -688,6 +702,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
if (e.target !== e.currentTarget) {
return
}
markDirectScrollInput()
navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down')
e.preventDefault()
} else if (e.key === 'Enter') {
@@ -698,9 +713,25 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
helper.focus()
}
e.preventDefault()
} else if (['PageUp', 'PageDown', 'Home', 'End', ' '].includes(e.key)) {
markDirectScrollInput()
}
},
[navigateWorktree]
[markDirectScrollInput, navigateWorktree]
)
const handleScrollPointerDown = useCallback(
(event: React.PointerEvent<HTMLDivElement>) => {
const scrollbarWidth = event.currentTarget.offsetWidth - event.currentTarget.clientWidth
if (scrollbarWidth <= 0) {
return
}
const rect = event.currentTarget.getBoundingClientRect()
if (event.clientX >= rect.right - scrollbarWidth) {
markDirectScrollInput()
}
},
[markDirectScrollInput]
)
const firstHeaderIndex = useMemo(
@@ -800,6 +831,11 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
aria-multiselectable="true"
aria-activedescendant={activeDescendantId}
onKeyDown={handleContainerKeyDown}
// Why: trackpad momentum can continue as sparse scroll events after the
// original wheel/touch event stream quiets down. Keep measurement-based
// scroll correction suppressed until the viewport itself has stopped.
onScroll={markScrollMovement}
onPointerDown={handleScrollPointerDown}
onTouchMove={markDirectScrollInput}
onWheel={markDirectScrollInput}
className="worktree-sidebar-scrollbar flex-1 overflow-y-scroll overflow-x-hidden pl-1 scrollbar-sleek outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-inset pt-px"
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest'
import { shouldCancelVirtualizedScrollOffsetRestore } from './useVirtualizedScrollAnchor'
describe('shouldCancelVirtualizedScrollOffsetRestore', () => {
it('keeps a pending restore when direct user scroll input is not tracked', () => {
expect(
shouldCancelVirtualizedScrollOffsetRestore({
restoring: true
})
).toBe(false)
})
it('keeps a pending restore during programmatic scroll movement', () => {
expect(
shouldCancelVirtualizedScrollOffsetRestore({
hasDirectScrollInput: () => false,
restoring: true
})
).toBe(false)
})
it('does not cancel when there is no pending restore', () => {
expect(
shouldCancelVirtualizedScrollOffsetRestore({
hasDirectScrollInput: () => true,
restoring: false
})
).toBe(false)
})
it('cancels a pending restore while direct user scroll input is active', () => {
expect(
shouldCancelVirtualizedScrollOffsetRestore({
hasDirectScrollInput: () => true,
restoring: true
})
).toBe(true)
})
})
@@ -9,6 +9,13 @@ export type VirtualizedScrollAnchor = {
export const VIRTUALIZED_SCROLL_ANCHOR_RECORD_EVENT = 'orca-record-virtualized-scroll-anchor'
const RECORD_ANCHOR_SCROLL_IDLE_DELAY_MS = 150
export function shouldCancelVirtualizedScrollOffsetRestore(args: {
hasDirectScrollInput?: () => boolean
restoring: boolean
}): boolean {
return args.restoring && args.hasDirectScrollInput?.() === true
}
type UseVirtualizedScrollAnchorOptions<
TRow,
TScrollElement extends Element,
@@ -17,6 +24,7 @@ type UseVirtualizedScrollAnchorOptions<
anchorRef: MutableRefObject<VirtualizedScrollAnchor>
getItemElementKey?: (element: TItemElement) => string | null
getRowKey: (row: TRow) => string
hasDirectScrollInput?: () => boolean
itemElementSelector?: string
rows: readonly TRow[]
scrollElementRef: RefObject<TScrollElement | null>
@@ -42,6 +50,7 @@ export function useVirtualizedScrollAnchor<
anchorRef,
getItemElementKey,
getRowKey,
hasDirectScrollInput,
itemElementSelector,
rows,
scrollElementRef,
@@ -178,6 +187,19 @@ export function useVirtualizedScrollAnchor<
recordScrollAnchor(el.scrollTop)
}
const onScroll = (): void => {
if (
shouldCancelVirtualizedScrollOffsetRestore({
hasDirectScrollInput,
restoring
})
) {
// Why: direct wheel/touch input means the user has taken control of the
// viewport. Treat the current offset as intentional instead of snapping
// back to a stale persisted offset while restoration is still pending.
restoring = false
recordCurrentAnchor()
return
}
if (restoring) {
// Why: during a fresh virtualizer mount, total height may still be
// estimate-based. Avoid persisting a browser-clamped offset as the
@@ -210,7 +232,13 @@ export function useVirtualizedScrollAnchor<
el.removeEventListener(VIRTUALIZED_SCROLL_ANCHOR_RECORD_EVENT, recordCurrentAnchor)
el.removeEventListener('scroll', onScroll)
}
}, [recordScrollAnchor, recordVirtualScrollAnchor, scrollElementRef, scrollOffsetRef])
}, [
recordScrollAnchor,
recordVirtualScrollAnchor,
scrollElementRef,
scrollOffsetRef,
hasDirectScrollInput
])
useLayoutEffect(() => {
const anchor = anchorRef.current