From a515697e086b3167eb9fefa232c9055fac40299c Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 3 Mar 2024 13:50:38 -0600 Subject: [PATCH] Use touch events for mobile review timeline (#10212) * mobile touch events * rebase * fix scroll mode * clean up deps and remove unneeded useeffect * remove vite --- .../timeline/EventReviewTimeline.tsx | 25 +++++++------------ web/src/hooks/use-handle-dragging.ts | 24 ++++++++++++++---- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/web/src/components/timeline/EventReviewTimeline.tsx b/web/src/components/timeline/EventReviewTimeline.tsx index e78f9ff21..998234711 100644 --- a/web/src/components/timeline/EventReviewTimeline.tsx +++ b/web/src/components/timeline/EventReviewTimeline.tsx @@ -157,23 +157,13 @@ export function EventReviewTimeline({ } }, [isDragging, onHandlebarDraggingChange]); - useEffect(() => { - generateSegments(); - - // TODO: touch events for mobile - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", handleMouseUp); - return () => { - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", handleMouseUp); - }; - // we know that these deps are correct - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [generateSegments, timelineStart, handleMouseUp, handleMouseMove]); - return (
{segments}
{showHandlebar && (
-
+
; @@ -34,7 +35,9 @@ function useDraggableHandler({ setIsDragging, }: DragHandlerProps) { const handleMouseDown = useCallback( - (e: React.MouseEvent) => { + ( + e: React.MouseEvent | React.TouchEvent, + ) => { e.preventDefault(); e.stopPropagation(); setIsDragging(true); @@ -43,7 +46,9 @@ function useDraggableHandler({ ); const handleMouseUp = useCallback( - (e: MouseEvent) => { + ( + e: React.MouseEvent | React.TouchEvent, + ) => { e.preventDefault(); e.stopPropagation(); if (isDragging) { @@ -91,7 +96,9 @@ function useDraggableHandler({ ); const handleMouseMove = useCallback( - (e: MouseEvent) => { + ( + e: React.MouseEvent | React.TouchEvent, + ) => { if ( !contentRef.current || !timelineRef.current || @@ -100,10 +107,17 @@ function useDraggableHandler({ return; } + let clientY; + if (isMobile && e.nativeEvent instanceof TouchEvent) { + clientY = e.nativeEvent.touches[0].clientY; + } else if (e.nativeEvent instanceof MouseEvent) { + clientY = e.nativeEvent.clientY; + } + e.preventDefault(); e.stopPropagation(); - if (showHandlebar && isDragging) { + if (showHandlebar && isDragging && clientY) { const { scrollHeight: timelineHeight, clientHeight: visibleTimelineHeight, @@ -120,7 +134,7 @@ function useDraggableHandler({ visibleTimelineHeight - timelineTop + parentScrollTop, Math.max( segmentHeight + scrolled, - e.clientY - timelineTop + parentScrollTop, + clientY - timelineTop + parentScrollTop, ), );