@@ -8,7 +8,10 @@ import Ref from "html-tag-js/ref";
88 */
99export 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