| Server IP : 52.25.153.185 / Your IP : 216.73.216.194 Web Server : Apache System : Linux ip-172-26-6-158 5.10.0-45-cloud-amd64 #1 SMP Debian 5.10.259-1 (2026-07-02) x86_64 User : daemon ( 1) PHP Version : 8.1.10 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /bitnami/wordpress/wp-content/plugins/code-snippets/js/hooks/ |
Upload File : |
import { useCallback, useEffect, useRef, useState } from 'react'
interface HorizontalScrollOverflow {
atStart: boolean
atEnd: boolean
scrollRef: React.RefObject<HTMLElement>
}
const EDGE_TOLERANCE = 1
export const useHorizontalScrollOverflow = (): HorizontalScrollOverflow => {
const scrollRef = useRef<HTMLElement>(null)
const [position, setPosition] = useState({ atStart: true, atEnd: true })
const updatePosition = useCallback(() => {
const element = scrollRef.current
if (!element) {
return
}
const hasOverflow = element.scrollWidth - element.clientWidth > EDGE_TOLERANCE
const atStart = !hasOverflow || element.scrollLeft <= EDGE_TOLERANCE
const atEnd = !hasOverflow || element.scrollLeft + element.clientWidth >= element.scrollWidth - EDGE_TOLERANCE
setPosition(previous => previous.atStart === atStart && previous.atEnd === atEnd
? previous
: { atStart, atEnd })
}, [])
useEffect(() => {
const element = scrollRef.current
if (!element) {
return
}
updatePosition()
element.addEventListener('scroll', updatePosition, { passive: true })
const observer = new ResizeObserver(updatePosition)
observer.observe(element)
observer.observe(element.firstElementChild ?? element)
return () => {
element.removeEventListener('scroll', updatePosition)
observer.disconnect()
}
}, [updatePosition])
return { ...position, scrollRef }
}