Skip to content

Commit

Permalink
fix: click event should be worked on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
linxianxi committed Oct 17, 2024
1 parent 6708b9c commit 720ebaa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
15 changes: 12 additions & 3 deletions src/Selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ function Selectable<T>(

const onMouseMove = (e: MouseEvent | TouchEvent) => {
if (isMouseDowning) {
// Prevent scroll on mobile
if (e instanceof TouchEvent) {
e.preventDefault();
}
const { clientX, clientY } = getClientXY(e);
moveClient.current = { x: clientX, y: clientY };
const { left, top } = scrollContainer.getBoundingClientRect();
Expand Down Expand Up @@ -248,12 +252,17 @@ function Selectable<T>(
};

const onMouseDown = (e: MouseEvent | TouchEvent) => {
if (e instanceof MouseEvent && e.button !== 0) {
const isMouseEvent = e instanceof MouseEvent;
if (isMouseEvent && e.button !== 0) {
return;
}

// disable text selection, but it will prevent default scroll behavior when mouse move, so we used `useScroll`
e.preventDefault();
// Disable text selection, but it will prevent default scroll behavior when mouse move, so we used `useScroll`
// And it will prevent click events on mobile devices, so don't trigger it
if (isMouseEvent) {
e.preventDefault();
}

isMouseDowning = true;

if (selectStartRangeRef.current !== 'all') {
Expand Down
51 changes: 17 additions & 34 deletions src/hooks/useScroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef } from 'react';
import { useEffect, useRef } from 'react';
import { getClientXY } from '../utils';

const SCROLL_STEP = 4;
Expand All @@ -11,41 +11,24 @@ export default function useScroll() {
const leftRaf = useRef<number | null>(null);
const rightRaf = useRef<number | null>(null);

const cancelTopRaf = () => {
if (topRaf.current) {
cancelAnimationFrame(topRaf.current);
topRaf.current = null;
}
};

const cancelBottomRaf = () => {
if (bottomRaf.current) {
cancelAnimationFrame(bottomRaf.current);
bottomRaf.current = null;
}
};

const cancelLeftRaf = () => {
if (leftRaf.current) {
cancelAnimationFrame(leftRaf.current);
leftRaf.current = null;
}
};

const cancelRightRaf = () => {
if (rightRaf.current) {
cancelAnimationFrame(rightRaf.current);
rightRaf.current = null;
const cancelRaf = (raf: React.MutableRefObject<number | null>) => {
if (raf.current) {
cancelAnimationFrame(raf.current);
raf.current = null;
}
};

const cancelScroll = () => {
cancelTopRaf();
cancelBottomRaf();
cancelLeftRaf();
cancelRightRaf();
cancelRaf(topRaf);
cancelRaf(bottomRaf);
cancelRaf(leftRaf);
cancelRaf(rightRaf);
};

useEffect(() => {
return cancelScroll;
}, []);

const smoothScroll = (e: MouseEvent | TouchEvent, _container: HTMLElement) => {
const container = _container === document.body ? document.documentElement : _container;
const { clientX, clientY } = getClientXY(e);
Expand All @@ -64,7 +47,7 @@ export default function useScroll() {
callback();
}
} else {
cancelTopRaf();
cancelRaf(topRaf);
}

// bottom
Expand All @@ -84,7 +67,7 @@ export default function useScroll() {
callback();
}
} else {
cancelBottomRaf();
cancelRaf(bottomRaf);
}

// left
Expand All @@ -101,7 +84,7 @@ export default function useScroll() {
callback();
}
} else {
cancelLeftRaf();
cancelRaf(leftRaf);
}

// right
Expand All @@ -121,7 +104,7 @@ export default function useScroll() {
callback();
}
} else {
cancelRightRaf();
cancelRaf(rightRaf);
}
};

Expand Down

0 comments on commit 720ebaa

Please sign in to comment.