Skip to content

Commit 864353b

Browse files
author
Tanner Reits
committed
fix(segment): mode smooth scroll, update content at end of gesture
1 parent 8429322 commit 864353b

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

core/src/components/segment/segment.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export class Segment implements ComponentInterface {
2727

2828
// Value before the segment is dragged
2929
private valueBeforeGesture?: SegmentValue;
30+
/**
31+
* Whether the segment "indicator" is currently being dragged.
32+
* This is used to prevent updating the segment view content while the gesture is still active.
33+
*/
34+
private gesturing = false;
3035

3136
private segmentViewEl?: HTMLIonSegmentViewElement | null = null;
3237
private lastNextIndex?: number;
@@ -36,7 +41,7 @@ export class Segment implements ComponentInterface {
3641
* This behavior is enabled by default, but is set false when scrolling content views
3742
* since we don't want to "double scroll" the segment view.
3843
*/
39-
private triggerScrollOnValueChange?: boolean;
44+
private updateContentOnValueChange?: boolean;
4045

4146
@Element() el!: HTMLIonSegmentElement;
4247

@@ -104,12 +109,10 @@ export class Segment implements ComponentInterface {
104109
if (previous && current) {
105110
if (!this.segmentViewEl) {
106111
this.checkButton(previous, current);
107-
} else if (this.triggerScrollOnValueChange !== false) {
108-
this.updateSegmentView();
112+
} else if (this.updateContentOnValueChange !== false && !this.gesturing) {
113+
this.updateSegmentView(this.modeSmoothScroll);
109114
}
110115
}
111-
} else if (value !== undefined && oldValue === undefined && this.segmentViewEl) {
112-
this.updateSegmentView();
113116
}
114117

115118
/**
@@ -123,7 +126,7 @@ export class Segment implements ComponentInterface {
123126
this.scrollActiveButtonIntoView();
124127
}
125128

126-
this.triggerScrollOnValueChange = undefined;
129+
this.updateContentOnValueChange = undefined;
127130
}
128131

129132
/**
@@ -227,6 +230,7 @@ export class Segment implements ComponentInterface {
227230

228231
onStart(detail: GestureDetail) {
229232
this.valueBeforeGesture = this.value;
233+
this.gesturing = true;
230234
this.activate(detail);
231235
}
232236

@@ -245,10 +249,11 @@ export class Segment implements ComponentInterface {
245249
if (value !== undefined) {
246250
if (this.valueBeforeGesture !== value) {
247251
this.emitValueChange();
248-
this.updateSegmentView();
252+
this.updateSegmentView(this.modeSmoothScroll);
249253
}
250254
}
251255
this.valueBeforeGesture = undefined;
256+
this.gesturing = false;
252257
}
253258

254259
/**
@@ -270,6 +275,13 @@ export class Segment implements ComponentInterface {
270275
return this.getButtons().find((button) => button.value === this.value);
271276
}
272277

278+
/**
279+
* The smooth scroll behavior of the segment view is tied to the mode.
280+
*/
281+
private get modeSmoothScroll() {
282+
return getIonMode(this) === 'ios' ? false : true;
283+
}
284+
273285
/*
274286
* Activate both the segment and the buttons
275287
* due to a bug with ::slotted in Safari
@@ -400,7 +412,7 @@ export class Segment implements ComponentInterface {
400412

401413
if (this.lastNextIndex === undefined || this.lastNextIndex !== nextIndex) {
402414
this.lastNextIndex = nextIndex;
403-
this.triggerScrollOnValueChange = false;
415+
this.updateContentOnValueChange = false;
404416

405417
this.checkButton(current, buttons[nextIndex]);
406418
this.emitValueChange();
@@ -613,7 +625,7 @@ export class Segment implements ComponentInterface {
613625
}
614626

615627
if (this.segmentViewEl) {
616-
this.updateSegmentView();
628+
this.updateSegmentView(this.modeSmoothScroll);
617629

618630
if (this.scrollable && previous) {
619631
this.checkButton(previous, current);

0 commit comments

Comments
 (0)