Skip to content

Improve timeline scrolling with dynamic speed #10677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions web/src/hooks/use-draggable-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function useDraggableElement({
}: DraggableElementProps) {
const [clientYPosition, setClientYPosition] = useState<number | null>(null);
const [initialClickAdjustment, setInitialClickAdjustment] = useState(0);
const [scrollEdgeSize, setScrollEdgeSize] = useState<number>();
const [segments, setSegments] = useState<HTMLDivElement[]>([]);
const { alignStartDateToTimeline, getCumulativeScrollTop } = useTimelineUtils(
{
Expand All @@ -52,23 +53,24 @@ function useDraggableElement({
);

const draggingAtTopEdge = useMemo(() => {
if (clientYPosition && timelineRef.current) {
if (clientYPosition && timelineRef.current && scrollEdgeSize) {
return (
clientYPosition - timelineRef.current.offsetTop <
timelineRef.current.clientHeight * 0.03 && isDragging
clientYPosition - timelineRef.current.offsetTop < scrollEdgeSize &&
isDragging
);
}
}, [clientYPosition, timelineRef, isDragging]);
}, [clientYPosition, timelineRef, isDragging, scrollEdgeSize]);

const draggingAtBottomEdge = useMemo(() => {
if (clientYPosition && timelineRef.current) {
if (clientYPosition && timelineRef.current && scrollEdgeSize) {
return (
clientYPosition >
(timelineRef.current.clientHeight + timelineRef.current.offsetTop) *
0.97 && isDragging
timelineRef.current.clientHeight +
timelineRef.current.offsetTop -
scrollEdgeSize && isDragging
);
}
}, [clientYPosition, timelineRef, isDragging]);
}, [clientYPosition, timelineRef, isDragging, scrollEdgeSize]);

const getClientYPosition = useCallback(
(e: MouseEvent | TouchEvent) => {
Expand Down Expand Up @@ -290,17 +292,26 @@ function useDraggableElement({
}
});

if (draggingAtTopEdge || draggingAtBottomEdge) {
let newPosition = clientYPosition;

if ((draggingAtTopEdge || draggingAtBottomEdge) && scrollEdgeSize) {
if (draggingAtTopEdge) {
newPosition = scrolled - segmentHeight;
timelineRef.current.scrollTop = newPosition;
const intensity = Math.max(
0,
(scrollEdgeSize - (clientYPosition - timelineTopAbsolute)) /
scrollEdgeSize,
);
timelineRef.current.scrollTop -= segmentHeight * intensity;
}

if (draggingAtBottomEdge) {
newPosition = scrolled + segmentHeight;
timelineRef.current.scrollTop = newPosition;
const intensity = Math.max(
0,
(clientYPosition -
timelineTopAbsolute -
(timelineRef.current.getBoundingClientRect().height -
scrollEdgeSize)) /
scrollEdgeSize,
);
timelineRef.current.scrollTop += segmentHeight * intensity;
}
}

Expand Down Expand Up @@ -436,6 +447,12 @@ function useDraggableElement({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [timelineCollapsed]);

useEffect(() => {
if (timelineRef.current) {
setScrollEdgeSize(timelineRef.current.clientHeight * 0.03);
}
}, [timelineRef]);

return { handleMouseDown, handleMouseUp, handleMouseMove };
}

Expand Down