-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use custom autoscroll with virtualization
- Loading branch information
samuelarbibe
committed
Jul 2, 2024
1 parent
182b5b3
commit e0e747b
Showing
4 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
interface UseAutoScrollOptions { | ||
acceleration: number; | ||
threshold: number; | ||
} | ||
|
||
interface DragOverlay { | ||
nodeRef: React.MutableRefObject<HTMLElement | null>; | ||
} | ||
|
||
interface UseAutoScrollProps { | ||
containerRef: React.MutableRefObject<HTMLElement | null>; | ||
dragOverlay: DragOverlay; | ||
options?: Partial<UseAutoScrollOptions>; | ||
} | ||
|
||
const DEFAULT_OPTIONS: UseAutoScrollOptions = { | ||
acceleration: 10, | ||
threshold: 100, | ||
}; | ||
|
||
export const useAutoscroll = (props: UseAutoScrollProps) => { | ||
const [scroll, setScroll] = useState<number>(0); | ||
|
||
const options = { | ||
...DEFAULT_OPTIONS, | ||
...props.options, | ||
}; | ||
|
||
useEffect(() => { | ||
const node = props.dragOverlay.nodeRef.current; | ||
const container = props.containerRef.current; | ||
|
||
if (!node || !container) return; | ||
|
||
const handleMouseMove = (event: MouseEvent) => { | ||
const { clientY } = event; | ||
|
||
const { top, bottom } = container.getBoundingClientRect(); | ||
|
||
const distanceFromTopThreshold = top + options.threshold - clientY; | ||
const distanceFromBottomThreshold = | ||
clientY - (bottom - options.threshold); | ||
|
||
if (distanceFromTopThreshold > 0) { | ||
setScroll( | ||
-(distanceFromTopThreshold / options.threshold) * | ||
options.acceleration, | ||
); | ||
} else if (distanceFromBottomThreshold > 0) { | ||
setScroll( | ||
(distanceFromBottomThreshold / options.threshold) * | ||
options.acceleration, | ||
); | ||
} else { | ||
setScroll(0); | ||
} | ||
}; | ||
|
||
node.addEventListener("mousemove", handleMouseMove); | ||
|
||
return () => { | ||
setScroll(0); | ||
node.removeEventListener("mousemove", handleMouseMove); | ||
}; | ||
}, [ | ||
props.dragOverlay, | ||
props.containerRef.current, | ||
options.acceleration, | ||
options.threshold, | ||
]); | ||
|
||
useEffect(() => { | ||
const container = props.containerRef.current; | ||
if (!container) return; | ||
|
||
const scrollContainer = () => { | ||
container.scrollTop += scroll; | ||
}; | ||
|
||
const scrollInterval = setInterval(scrollContainer, 10); | ||
|
||
return () => clearInterval(scrollInterval); | ||
}, [props.containerRef, scroll]); | ||
}; |