Skip to content

Prevent links from opening if user moves beyond threshold #152

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

Open
wants to merge 2 commits 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
13 changes: 13 additions & 0 deletions src/actions/swipeable/swipeable.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export function swipeable(node, { thresholdProvider }) {
dispatch('swipeStart', { x, y })
addMoveEventListener(window, handleMove)
addEndEventListener(window, handleUp)
// Prevents link text from displaying when content is swiped.
event.preventDefault();
}

function checkShouldOpenLink(node) {
const linkOpenThreshold = 10;
if (Math.abs(moved) > linkOpenThreshold) {
node.addEventListener('click', (event) => event.preventDefault(event), {
once: true,
});
}
}

function handleMove(event) {
Expand All @@ -66,12 +77,14 @@ export function swipeable(node, { thresholdProvider }) {
dispatch('swipeThresholdReached', { direction: moved > 0 ? PREV : NEXT })
removeEndEventListener(window, handleUp)
removeMoveEventListener(window, handleMove)
checkShouldOpenLink(node);
}
}

function handleUp(event) {
removeEndEventListener(window, handleUp)
removeMoveEventListener(window, handleMove)
checkShouldOpenLink(node);

isTouching = false

Expand Down