Skip to content

Commit

Permalink
Move click/tap events to onClick handler to fix bug with double tappi…
Browse files Browse the repository at this point in the history
…ng on mobile chrome.
  • Loading branch information
broox committed Feb 11, 2024
1 parent fc308b3 commit d6af877
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/core/events/onClick.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { now } from '../../shared/utils.mjs';

export default function onClick(e) {
const swiper = this;
const data = swiper.touchEventsData;

if (!swiper.enabled) return;
if (!swiper.allowClick) {
if (swiper.params.preventClicks) e.preventDefault();
if (swiper.params.preventClicksPropagation && swiper.animating) {
e.stopPropagation();
e.stopImmediatePropagation();
}
} else {
const clickTime = now();
const pathTree = e.path || e.composedPath?.();

swiper.updateClickedSlide((pathTree && pathTree[0]) || e.target, pathTree);
swiper.emit('tap click', e);

if (clickTime - data.lastClickTime < 300) {
swiper.emit('doubleTap doubleClick', e);
}

data.lastClickTime = now();
}
}
17 changes: 1 addition & 16 deletions src/core/events/onTouchEnd.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { now, nextTick } from '../../shared/utils.mjs';
import { nextTick } from '../../shared/utils.mjs';

export default function onTouchEnd(event) {
const swiper = this;
Expand Down Expand Up @@ -55,21 +55,6 @@ export default function onTouchEnd(event) {
swiper.setGrabCursor(false);
}

// Time diff
const touchEndTime = now();
const timeDiff = touchEndTime - data.touchStartTime;

// Tap, doubleTap, Click
if (swiper.allowClick) {
const pathTree = e.path || (e.composedPath && e.composedPath());
swiper.updateClickedSlide((pathTree && pathTree[0]) || e.target, pathTree);
swiper.emit('tap click', e);
if (timeDiff < 300 && touchEndTime - data.lastClickTime < 300) {
swiper.emit('doubleTap doubleClick', e);
}
}

data.lastClickTime = now();
nextTick(() => {
if (!swiper.destroyed) swiper.allowClick = true;
});
Expand Down

0 comments on commit d6af877

Please sign in to comment.