@@ -45,17 +45,16 @@ export class SegmentView implements ComponentInterface {
4545 const { initialScrollLeft, previousScrollLeft } = this ;
4646 const { scrollLeft, offsetWidth } = ev . target as HTMLElement ;
4747
48- if ( initialScrollLeft === undefined ) {
49- this . initialScrollLeft = scrollLeft ;
50- }
48+ // Set initial scroll position if it's undefined
49+ this . initialScrollLeft = initialScrollLeft ?? scrollLeft ;
5150
5251 // Determine the scroll direction based on the previous scroll position
5352 const scrollDirection = scrollLeft > previousScrollLeft ? 'right' : 'left' ;
5453 this . previousScrollLeft = scrollLeft ;
5554
5655 // Calculate the distance scrolled based on the initial scroll position
5756 // and then transform it to a percentage of the segment view width
58- const scrollDistance = scrollLeft - initialScrollLeft ! ;
57+ const scrollDistance = scrollLeft - this . initialScrollLeft ;
5958 const scrollDistancePercentage = Math . abs ( scrollDistance ) / offsetWidth ;
6059
6160 // Emit the scroll direction and distance
@@ -65,20 +64,28 @@ export class SegmentView implements ComponentInterface {
6564 scrollDistancePercentage,
6665 } ) ;
6766
67+ // Check if the scroll is at a snapping point and return if not
6868 const atSnappingPoint = scrollLeft % offsetWidth === 0 ;
69-
7069 if ( ! atSnappingPoint ) return ;
7170
72- const index = Math . round ( scrollLeft / offsetWidth ) ;
73- const segmentContent = this . getSegmentContents ( ) [ index ] ;
71+ // Find the current segment content based on the scroll position
72+ const currentIndex = Math . round ( scrollLeft / offsetWidth ) ;
73+ let segmentContent = this . getSegmentContents ( ) [ currentIndex ] ;
7474
75- if ( segmentContent === null || segmentContent === undefined ) {
76- return ;
75+ // Exit if no valid segment content found
76+ if ( ! segmentContent ) return ;
77+
78+ // If the current content is disabled, find the next enabled content
79+ if ( segmentContent . disabled ) {
80+ const nextIndex = scrollDirection === 'right' ? currentIndex + 1 : currentIndex - 1 ;
81+ segmentContent = this . getNextSegmentContent ( nextIndex ) ;
7782 }
7883
79- // Store the active `ion-segment- content` id so we can emit it when the scroll ends
84+ // Update active content ID and scroll to the segment content
8085 this . activeContentId = segmentContent . id ;
86+ this . setContent ( segmentContent . id ) ;
8187
88+ // Reset the timeout to check for scroll end
8289 this . resetScrollEndTimeout ( ) ;
8390 }
8491
@@ -120,11 +127,15 @@ export class SegmentView implements ComponentInterface {
120127
121128 /**
122129 * Check if the scroll has ended and the user is not actively touching.
123- * If both conditions are met, reset the initial scroll position and
124- * emit the scroll end event.
130+ * If the conditions are met (active content is enabled and no active touch),
131+ * reset the scroll position and emit the scroll end event.
125132 */
126133 private checkForScrollEnd ( ) {
127- if ( ! this . isTouching ) {
134+ const activeContent = this . getSegmentContents ( ) . find ( content => content . id === this . activeContentId ) ;
135+
136+ // Only emit scroll end event if the active content is not disabled and
137+ // the user is not touching the segment view
138+ if ( activeContent ?. disabled === false && ! this . isTouching ) {
128139 this . ionSegmentViewScrollEnd . emit ( { activeContentId : this . activeContentId } ) ;
129140 this . initialScrollLeft = undefined ;
130141 }
@@ -153,7 +164,12 @@ export class SegmentView implements ComponentInterface {
153164 }
154165
155166 private getSegmentContents ( ) : HTMLIonSegmentContentElement [ ] {
156- return Array . from ( this . el . querySelectorAll ( 'ion-segment-content:not([disabled])' ) ) ;
167+ return Array . from ( this . el . querySelectorAll ( 'ion-segment-content' ) ) ;
168+ }
169+
170+ private getNextSegmentContent ( index : number ) : HTMLIonSegmentContentElement {
171+ const contents = this . getSegmentContents ( ) ;
172+ return contents [ index ] ;
157173 }
158174
159175 render ( ) {
0 commit comments