Skip to content

Commit 699ce97

Browse files
committed
fix(segment): properly move the indicator when direction starts out on the left
1 parent d811221 commit 699ce97

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Component, Element, Event, Host, Listen, Method, Prop, h } from '@stenc
1010
shadow: true,
1111
})
1212
export class SegmentView implements ComponentInterface {
13+
private initialScrollLeft = 0;
1314
private previousScrollLeft = 0;
1415

1516
@Element() el!: HTMLElement;
@@ -23,12 +24,17 @@ export class SegmentView implements ComponentInterface {
2324

2425
@Listen('scroll')
2526
handleScroll(ev: Event) {
27+
const { initialScrollLeft, previousScrollLeft } = this;
2628
const { scrollLeft, offsetWidth } = ev.target as HTMLElement;
2729

28-
const scrollDirection = scrollLeft > this.previousScrollLeft ? 'right' : 'left';
30+
const scrollDirection = scrollLeft > previousScrollLeft ? 'right' : 'left';
2931
this.previousScrollLeft = scrollLeft;
3032

31-
const scrollDistance = scrollLeft;
33+
let scrollDistance = scrollLeft;
34+
35+
if (scrollDirection === 'left') {
36+
scrollDistance = initialScrollLeft - scrollLeft;
37+
}
3238

3339
// Emit the scroll direction and distance
3440
this.ionSegmentViewScroll.emit({
@@ -55,6 +61,11 @@ export class SegmentView implements ComponentInterface {
5561
}
5662
}
5763

64+
@Listen('touchstart')
65+
handleTouchStart() {
66+
this.initialScrollLeft = this.el.scrollLeft;
67+
}
68+
5869
/**
5970
* This method is used to programmatically set the displayed segment content
6071
* in the segment view. Calling this method will update the `value` of the

core/src/components/segment/segment.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,17 @@ export class Segment implements ComponentInterface {
372372
// Calculate the potential transform value based on scroll direction
373373
const transformValue = scrollDirection === 'left' ? -scrollDistance : scrollDistance;
374374

375-
// Calculate the max allowed transformation (indicator should not move beyond the segment boundaries)
376-
const maxTransform = segmentRect.width - buttonRect.width;
377-
const minTransform = 0;
375+
// Calculate the max and min allowed transformations based on the scroll direction
376+
const maxTransform = scrollDirection === 'left' ? 0 : segmentRect.width - buttonRect.width;
377+
const minTransform = scrollDirection === 'left' ?
378+
-(segmentRect.width - buttonRect.width) : 0;
378379

379380
// Clamp the transform value to ensure it doesn't go out of bounds
380381
const clampedTransform = Math.max(minTransform, Math.min(transformValue, maxTransform));
381382

382383
// Apply the clamped transform value to the indicator element
383-
indicatorEl.style.transform = `translate3d(${clampedTransform}px, 0, 0)`;
384+
const transform = `translate3d(${clampedTransform}px, 0, 0)`;
385+
indicatorEl.style.setProperty('transform', transform);
384386
}
385387
}
386388
}

0 commit comments

Comments
 (0)