MCPcopy Create free account
hub / github.com/anomalyco/opencode / createAutoScroll

Function createAutoScroll

packages/ui/src/hooks/create-auto-scroll.tsx:13–237  ·  view source on GitHub ↗
(options: AutoScrollOptions)

Source from the content-addressed store, hash-verified

11}
12
13export function createAutoScroll(options: AutoScrollOptions) {
14 let settling = false
15 let settleTimer: ReturnType<typeof setTimeout> | undefined
16 let autoTimer: ReturnType<typeof setTimeout> | undefined
17 let auto: { top: number; time: number } | undefined
18
19 const threshold = () => options.bottomThreshold ?? 10
20
21 const [store, setStore] = createStore({
22 contentRef: undefined as HTMLElement | undefined,
23 scrollRef: undefined as HTMLElement | undefined,
24 userScrolled: false,
25 })
26
27 const active = () => options.working() || settling
28
29 const distanceFromBottom = (el: HTMLElement) => {
30 return el.scrollHeight - el.clientHeight - el.scrollTop
31 }
32
33 const canScroll = (el: HTMLElement) => {
34 return el.scrollHeight - el.clientHeight > 1
35 }
36
37 // Browsers can dispatch scroll events asynchronously. If new content arrives
38 // between us calling `scrollTo()` and the subsequent `scroll` event firing,
39 // the handler can see a non-zero `distanceFromBottom` and incorrectly assume
40 // the user scrolled.
41 const markAuto = (el: HTMLElement) => {
42 auto = {
43 top: Math.max(0, el.scrollHeight - el.clientHeight),
44 time: Date.now(),
45 }
46
47 if (autoTimer) clearTimeout(autoTimer)
48 autoTimer = setTimeout(() => {
49 auto = undefined
50 autoTimer = undefined
51 }, 1500)
52 }
53
54 const isAuto = (el: HTMLElement) => {
55 const a = auto
56 if (!a) return false
57
58 if (Date.now() - a.time > 1500) {
59 auto = undefined
60 return false
61 }
62
63 return Math.abs(el.scrollTop - a.top) < 2
64 }
65
66 const scrollToBottomNow = (behavior: ScrollBehavior) => {
67 const el = store.scrollRef
68 if (!el) return
69 markAuto(el)
70 if (behavior === "smooth") {

Callers 2

PageFunction · 0.90
SessionTurnFunction · 0.90

Calls 6

canScrollFunction · 0.85
setStoreFunction · 0.85
updateOverflowAnchorFunction · 0.85
activeFunction · 0.70
scrollToBottomFunction · 0.70
onFunction · 0.50

Tested by

no test coverage detected