Skip to content

Commit ce677d4

Browse files
authored
feat: Improve tab view gesture handling to distinguish between scroll and swipe (#1131)
- Add vertical scroll detection to prevent unwanted tab changes - Implement threshold check for horizontal swipe gestures
1 parent 80e54a1 commit ce677d4

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@
108108
"yargs": "^17.7.2"
109109
},
110110
"browserslist": "cover 100%,not android < 5"
111-
}
111+
}

src/components/tabView.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import Ref from "html-tag-js/ref";
88
*/
99
export default function TabView({ id }, children) {
1010
let moveX = 0;
11+
let moveY = 0;
1112
let lastX = 0;
13+
let lastY = 0;
14+
let isScrolling = false;
1215
const el = new Ref();
1316
return (
1417
<div
@@ -24,32 +27,52 @@ export default function TabView({ id }, children) {
2427

2528
function ontouchstart(e) {
2629
moveX = 0;
30+
moveY = 0;
2731
lastX = e.touches[0].clientX;
32+
lastY = e.touches[0].clientY;
33+
isScrolling = false;
34+
2835
document.addEventListener("touchmove", omtouchmove, { passive: true });
2936
document.addEventListener("touchend", omtouchend);
3037
document.addEventListener("touchcancel", omtouchend);
3138
}
3239

3340
function omtouchmove(e) {
34-
const { clientX } = e.touches[0];
35-
moveX += lastX - clientX;
41+
const { clientX, clientY } = e.touches[0];
42+
const deltaX = lastX - clientX;
43+
const deltaY = lastY - clientY;
44+
45+
// Determine if the user is primarily scrolling vertically
46+
if (!isScrolling) {
47+
isScrolling = Math.abs(deltaY) > Math.abs(deltaX);
48+
}
49+
50+
if (!isScrolling) {
51+
moveX += deltaX;
52+
e.preventDefault();
53+
}
54+
3655
lastX = clientX;
56+
lastY = clientY;
3757
}
3858

3959
function omtouchend() {
4060
document.removeEventListener("touchmove", omtouchmove);
4161
document.removeEventListener("touchend", omtouchend);
4262
document.removeEventListener("touchcancel", omtouchend);
43-
if (Math.abs(moveX) <= 100) return;
44-
const tabs = Array.from(el.get(".options").children);
45-
const currentTab = el.get(".options>span.active");
46-
const direction = moveX > 0 ? 1 : -1;
47-
const currentTabIndex = tabs.indexOf(currentTab);
48-
const nextTabIndex =
49-
(currentTabIndex + direction + tabs.length) % tabs.length;
50-
tabs[nextTabIndex].click();
51-
currentTab.classList.remove("active");
52-
tabs[nextTabIndex].classList.add("active");
63+
64+
// Only change tabs when a significant horizontal swipe is detected and not scrolling vertically
65+
if (!isScrolling && Math.abs(moveX) > 100) {
66+
const tabs = Array.from(el.get(".options").children);
67+
const currentTab = el.get(".options>span.active");
68+
const direction = moveX > 0 ? 1 : -1;
69+
const currentTabIndex = tabs.indexOf(currentTab);
70+
const nextTabIndex =
71+
(currentTabIndex + direction + tabs.length) % tabs.length;
72+
tabs[nextTabIndex].click();
73+
currentTab.classList.remove("active");
74+
tabs[nextTabIndex].classList.add("active");
75+
}
5376
}
5477

5578
function changeTab(e) {

0 commit comments

Comments
 (0)