mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(frontend): add scroll-to-bottom button, improve scroll disengagement, and tall content detection
This commit is contained in:
1 parent
c3802aa021
commit
f492faa84b
10 files changed
+512
-29
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { useTallScrollContent } from '../useTallScrollContent'
|
||||
|
||||
let mockObserve: ReturnType<typeof vi.fn>
|
||||
let mockDisconnect: ReturnType<typeof vi.fn>
|
||||
let resizeCallback: ((entries: ResizeObserverEntry[]) => void) | null = null
|
||||
|
||||
vi.stubGlobal('ResizeObserver', vi.fn((cb: (entries: ResizeObserverEntry[]) => void) => {
|
||||
resizeCallback = cb
|
||||
return {
|
||||
observe: mockObserve,
|
||||
disconnect: mockDisconnect,
|
||||
unobserve: vi.fn(),
|
||||
}
|
||||
}))
|
||||
|
||||
describe('useTallScrollContent', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockObserve = vi.fn()
|
||||
mockDisconnect = vi.fn()
|
||||
resizeCallback = null
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
function createContainerRef(scrollHeight: number, clientHeight: number) {
|
||||
const el = document.createElement('div')
|
||||
Object.defineProperty(el, 'scrollHeight', { value: scrollHeight, configurable: true })
|
||||
Object.defineProperty(el, 'clientHeight', { value: clientHeight, configurable: true })
|
||||
return { current: el }
|
||||
}
|
||||
|
||||
it('returns false when scrollHeight <= clientHeight * ratio', () => {
|
||||
const ref = createContainerRef(500, 400)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true when scrollHeight > clientHeight * ratio', () => {
|
||||
const ref = createContainerRef(2000, 800)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(true)
|
||||
})
|
||||
|
||||
it('updates from false to true when ResizeObserver fires with taller content', () => {
|
||||
const ref = createContainerRef(500, 400)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(false)
|
||||
|
||||
Object.defineProperty(ref.current, 'scrollHeight', { value: 2000 })
|
||||
act(() => {
|
||||
resizeCallback?.([] as ResizeObserverEntry[])
|
||||
})
|
||||
|
||||
expect(result.current).toBe(true)
|
||||
})
|
||||
|
||||
it('updates from true to false when ResizeObserver fires with smaller container', () => {
|
||||
const ref = createContainerRef(2000, 800)
|
||||
|
||||
const { result } = renderHook(() => useTallScrollContent(ref as React.RefObject<HTMLElement>, 1.5))
|
||||
expect(result.current).toBe(true)
|
||||
|
||||
Object.defineProperty(ref.current, 'clientHeight', { value: 2000 })
|
||||
act(() => {
|
||||
resizeCallback?.([] as ResizeObserverEntry[])
|
||||
})
|
||||
|
||||
expect(result.current).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -28,6 +28,7 @@ export function useAutoScroll({
|
||||
const userScrolledAtRef = useRef(0)
|
||||
const userDisengagedRef = useRef(false)
|
||||
const pointerStartYRef = useRef<number | null>(null)
|
||||
const pointerActiveRef = useRef(false)
|
||||
const onScrollStateChangeRef = useRef(onScrollStateChange)
|
||||
|
||||
onScrollStateChangeRef.current = onScrollStateChange
|
||||
@@ -61,17 +62,19 @@ export function useAutoScroll({
|
||||
|
||||
const handlePointerDown = (e: PointerEvent) => {
|
||||
pointerStartYRef.current = e.clientY
|
||||
pointerActiveRef.current = true
|
||||
}
|
||||
|
||||
const handlePointerMove = (e: PointerEvent) => {
|
||||
if (pointerStartYRef.current === null) return
|
||||
if (e.clientY > pointerStartYRef.current) {
|
||||
if (e.clientY > pointerStartYRef.current + 4) {
|
||||
markDisengaged()
|
||||
}
|
||||
}
|
||||
|
||||
const handlePointerUp = () => {
|
||||
pointerStartYRef.current = null
|
||||
pointerActiveRef.current = false
|
||||
}
|
||||
|
||||
const handleWheel = (e: WheelEvent) => {
|
||||
@@ -87,6 +90,7 @@ export function useAutoScroll({
|
||||
}
|
||||
|
||||
const handleScroll = () => {
|
||||
if (!hasInitialScrolledRef.current) return
|
||||
const { scrollTop, scrollHeight, clientHeight } = container
|
||||
const isAtBottom = scrollHeight - scrollTop - clientHeight < BOTTOM_THRESHOLD_PX
|
||||
|
||||
@@ -96,7 +100,7 @@ export function useAutoScroll({
|
||||
userDisengagedRef.current = false
|
||||
onScrollStateChangeRef.current?.(false)
|
||||
}
|
||||
} else if (!userDisengagedRef.current) {
|
||||
} else if (pointerActiveRef.current && !userDisengagedRef.current) {
|
||||
userScrolledAtRef.current = Date.now()
|
||||
userDisengagedRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
@@ -146,7 +150,7 @@ export function useAutoScroll({
|
||||
const timeSinceUserScroll = Date.now() - userScrolledAtRef.current
|
||||
const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS
|
||||
|
||||
if (recentlyScrolled || userDisengagedRef.current) {
|
||||
if (recentlyScrolled || userDisengagedRef.current || pointerActiveRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
|
||||
export function useTallScrollContent(
|
||||
containerRef: React.RefObject<HTMLElement | null>,
|
||||
ratio = 1.5
|
||||
): boolean {
|
||||
const [isTall, setIsTall] = useState(false)
|
||||
const observingRef = useRef<HTMLElement | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const recompute = () => {
|
||||
const el = containerRef.current
|
||||
if (!el) return
|
||||
setIsTall(el.scrollHeight > el.clientHeight * ratio)
|
||||
}
|
||||
|
||||
const el = containerRef.current
|
||||
|
||||
if (!el || el === observingRef.current) return
|
||||
|
||||
observingRef.current = el
|
||||
recompute()
|
||||
|
||||
const observer = new ResizeObserver(recompute)
|
||||
observer.observe(el)
|
||||
el.addEventListener('scroll', recompute)
|
||||
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
el.removeEventListener('scroll', recompute)
|
||||
observingRef.current = null
|
||||
}
|
||||
})
|
||||
|
||||
return isTall
|
||||
}
|
||||
Reference in new issue
Block a user