Skip to content

Commit 73ea64c

Browse files
authoredNov 16, 2022
fix(segment): scrollable segments center button on click (#26285)
resolves #25367
1 parent 4aabbdf commit 73ea64c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎core/src/components/segment/segment.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ export class Segment implements ComponentInterface {
8888
this.valueAfterGesture = value;
8989
}
9090
}
91+
92+
if (this.scrollable) {
93+
const buttons = this.getButtons();
94+
const activeButton = buttons.find((button) => button.value === value);
95+
if (activeButton !== undefined) {
96+
/**
97+
* Scrollable segment buttons should be
98+
* centered within the view including
99+
* buttons that are partially offscreen.
100+
*/
101+
activeButton.scrollIntoView({
102+
behavior: 'smooth',
103+
inline: 'center',
104+
});
105+
}
106+
}
91107
}
92108

93109
/**

‎core/src/components/segment/test/basic/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
</ion-segment>
3030
</ion-toolbar>
3131

32+
<ion-toolbar>
33+
<ion-segment value="2" scrollable="true">
34+
<ion-segment-button value="1">Button 1</ion-segment-button>
35+
<ion-segment-button value="2">Button 3</ion-segment-button>
36+
<ion-segment-button value="3">Button 3</ion-segment-button>
37+
<ion-segment-button value="4">Button 4</ion-segment-button>
38+
<ion-segment-button value="5">Button 5</ion-segment-button>
39+
<ion-segment-button value="6">Button 6</ion-segment-button>
40+
</ion-segment>
41+
</ion-toolbar>
42+
3243
<ion-toolbar>
3344
<ion-segment name="dynamicPropDisable" disabled color="danger">
3445
<ion-segment-button value="Bookmarks"> Bookmarks </ion-segment-button>

0 commit comments

Comments
 (0)
Please sign in to comment.