Skip to content

Commit 7fe1c09

Browse files
committed
fix(segment-view): always check the scrollLeft against the initial to get scrollDistance
1 parent c7bae07 commit 7fe1c09

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

core/src/components/segment-view/segment-view.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export class SegmentView implements ComponentInterface {
4141
this.initialScrollLeft = scrollLeft;
4242
}
4343

44+
// Determine the scroll direction based on the previous scroll position
4445
const scrollDirection = scrollLeft > previousScrollLeft ? 'right' : 'left';
4546
this.previousScrollLeft = scrollLeft;
4647

47-
// If the scroll direction is left then we need to calculate where we started and subtract
48-
// the current scrollLeft to get the distance scrolled. Otherwise, we use the scrollLeft.
49-
const scrollDistance = scrollDirection === 'left' ? initialScrollLeft! - scrollLeft : scrollLeft;
48+
// Calculate the distance scrolled based on the initial scroll position
49+
const scrollDistance = scrollLeft - initialScrollLeft!;
5050

5151
// Emit the scroll direction and distance
5252
this.ionSegmentViewScroll.emit({

core/src/components/segment/segment.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,12 @@ export class Segment implements ComponentInterface {
369369
const segmentRect = segmentEl.getBoundingClientRect();
370370
const buttonRect = current.getBoundingClientRect();
371371

372-
// Calculate the potential transform value based on scroll direction
373-
const transformValue = scrollDirection === 'left' ? -scrollDistance : scrollDistance;
374-
375372
// Calculate the max and min allowed transformations based on the scroll direction
376373
const maxTransform = scrollDirection === 'left' ? 0 : segmentRect.width - buttonRect.width;
377374
const minTransform = scrollDirection === 'left' ? -(segmentRect.width - buttonRect.width) : 0;
378375

379376
// Clamp the transform value to ensure it doesn't go out of bounds
380-
const clampedTransform = Math.max(minTransform, Math.min(transformValue, maxTransform));
377+
const clampedTransform = Math.max(minTransform, Math.min(scrollDistance, maxTransform));
381378

382379
// Apply the clamped transform value to the indicator element
383380
const transform = `translate3d(${clampedTransform}px, 0, 0)`;

0 commit comments

Comments
 (0)