From cc00ce5c24d205e480b5bde2ae43bb0553a2e41d Mon Sep 17 00:00:00 2001 From: Derek Brooks Date: Sat, 10 Feb 2024 15:44:03 -0600 Subject: [PATCH] Move click/tap events to onClick handler to fix bug with double tapping on mobile chrome. --- src/core/events/onClick.mjs | 16 ++++++++++++++++ src/core/events/onTouchEnd.mjs | 17 +---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/core/events/onClick.mjs b/src/core/events/onClick.mjs index b042f4839..cb3812567 100644 --- a/src/core/events/onClick.mjs +++ b/src/core/events/onClick.mjs @@ -1,5 +1,9 @@ +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(); @@ -7,5 +11,17 @@ export default function onClick(e) { 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(); } } diff --git a/src/core/events/onTouchEnd.mjs b/src/core/events/onTouchEnd.mjs index 4c01519ce..6acfc53ff 100644 --- a/src/core/events/onTouchEnd.mjs +++ b/src/core/events/onTouchEnd.mjs @@ -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; @@ -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; });