Skip to content

Commit 7746faa

Browse files
committed
fix(chat): only upward scroll keys authorize a keyboard detach
onKeyDown stamped the gesture window on any bubbling key, so an unrelated keypress within USER_GESTURE_WINDOW of a programmatic virtualizer re-pin could satisfy userDriven and detach mid-stream. Filter to the upward scroll keys (ArrowUp, PageUp, Home, Shift+Space), mirroring the wheel handler's upward-only rule, so only a genuine upward keyboard scroll authorizes detach.
1 parent 2a3f48d commit 7746faa

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

apps/sim/hooks/use-auto-scroll.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const STICK_THRESHOLD = 30
55
/** User must scroll back to within this distance to re-engage auto-scroll. */
66
const REATTACH_THRESHOLD = 5
77
/**
8-
* A keyboard-driven scroll (PageUp, arrows) only emits `scroll` events, so its
9-
* detach is honored when it lands within this window of a `keydown`. Wheel and
8+
* An upward keyboard scroll ({@link SCROLL_UP_KEYS}) only emits `scroll` events, so
9+
* its detach is honored when it lands within this window of the `keydown`. Wheel and
1010
* touch detach directly via their own handlers, and scrollbar drags are tracked
1111
* through {@link pointerDownRef}, so neither feeds this window.
1212
*
@@ -17,6 +17,13 @@ const REATTACH_THRESHOLD = 5
1717
* as the user scrolling away and auto-scroll detaches mid-stream.
1818
*/
1919
const USER_GESTURE_WINDOW = 250
20+
/**
21+
* Keys that scroll the viewport upward. Only these authorize a keyboard detach,
22+
* mirroring the wheel handler's upward-only ({@link WheelEvent.deltaY} < 0) rule,
23+
* so an unrelated keypress can't open the detach window. `Shift`+`Space` (handled
24+
* in the listener) is the other upward shortcut; plain `Space` pages down.
25+
*/
26+
const SCROLL_UP_KEYS = new Set(['ArrowUp', 'PageUp', 'Home'])
2027

2128
interface UseAutoScrollOptions {
2229
scrollOnMount?: boolean
@@ -111,8 +118,10 @@ export function useAutoScroll(
111118
const onPointerUp = () => {
112119
pointerDownRef.current = false
113120
}
114-
const onKeyDown = () => {
115-
lastUserGestureAtRef.current = performance.now()
121+
const onKeyDown = (e: KeyboardEvent) => {
122+
if (SCROLL_UP_KEYS.has(e.key) || (e.key === ' ' && e.shiftKey)) {
123+
lastUserGestureAtRef.current = performance.now()
124+
}
116125
}
117126

118127
/**

0 commit comments

Comments
 (0)