Skip to content
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

fix: don't trigger long press on scroll #321

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,17 +662,19 @@ export function useLockBodyScroll() {
}

export function useLongPress(callback, options = {}) {
const { threshold = 400, onStart, onFinish, onCancel } = options;
const { threshold = 400, onStart, onFinish, onCancel, allowScroll = true, scrollThreshold = 20 } = options;
const isLongPressActive = React.useRef(false);
const isPressed = React.useRef(false);
const timerId = React.useRef();
let startY;

return React.useMemo(() => {
if (typeof callback !== "function") {
return {};
}
}

const start = (event) => {
startY = event.touches[0].clientY;
if (!isMouseEvent(event) && !isTouchEvent(event)) return;

if (onStart) {
Expand Down Expand Up @@ -707,15 +709,23 @@ export function useLongPress(callback, options = {}) {
}
};

const move = (event) => {
if (!allowScroll && Math.abs(event.touches[0].clientY - startY) > scrollThreshold) {
cancel(event)
}
}

const mouseHandlers = {
onMouseDown: start,
onMouseUp: cancel,
onMouseLeave: cancel,
onMouseMove: move,
};

const touchHandlers = {
onTouchStart: start,
onTouchEnd: cancel,
onTouchMove: move,
};

return {
Expand Down