|
1 | 1 | import type { ComponentInterface, EventEmitter } from '@stencil/core';
|
2 | 2 | import { Component, Element, Event, Host, Listen, Prop, State, Watch, h, writeTask } from '@stencil/core';
|
3 | 3 | import type { Gesture, GestureDetail } from '@utils/gesture';
|
| 4 | +import { raf } from '@utils/helpers'; |
4 | 5 | import { isRTL } from '@utils/rtl';
|
5 | 6 | import { createColorClasses, hostContext } from '@utils/theme';
|
6 | 7 |
|
@@ -83,31 +84,7 @@ export class Segment implements ComponentInterface {
|
83 | 84 | * Used by `ion-segment-button` to determine if the button should be checked.
|
84 | 85 | */
|
85 | 86 | this.ionSelect.emit({ value });
|
86 |
| - |
87 |
| - if (this.scrollable) { |
88 |
| - const buttons = this.getButtons(); |
89 |
| - const activeButton = buttons.find((button) => button.value === value); |
90 |
| - if (activeButton !== undefined) { |
91 |
| - /** |
92 |
| - * Scrollable segment buttons should be |
93 |
| - * centered within the view including |
94 |
| - * buttons that are partially offscreen. |
95 |
| - */ |
96 |
| - activeButton.scrollIntoView({ |
97 |
| - behavior: 'smooth', |
98 |
| - inline: 'center', |
99 |
| - |
100 |
| - /** |
101 |
| - * Segment should scroll on the |
102 |
| - * horizontal axis. `block: 'nearest'` |
103 |
| - * ensures that the vertical axis |
104 |
| - * does not scroll if the segment |
105 |
| - * as a whole is already in view. |
106 |
| - */ |
107 |
| - block: 'nearest', |
108 |
| - }); |
109 |
| - } |
110 |
| - } |
| 87 | + this.scrollActiveButtonIntoView(); |
111 | 88 | }
|
112 | 89 |
|
113 | 90 | /**
|
@@ -163,6 +140,14 @@ export class Segment implements ComponentInterface {
|
163 | 140 | async componentDidLoad() {
|
164 | 141 | this.setCheckedClasses();
|
165 | 142 |
|
| 143 | + /** |
| 144 | + * We need to wait for the buttons to all be rendered |
| 145 | + * before we can scroll. |
| 146 | + */ |
| 147 | + raf(() => { |
| 148 | + this.scrollActiveButtonIntoView(); |
| 149 | + }); |
| 150 | + |
166 | 151 | this.gesture = (await import('../../utils/gesture')).createGesture({
|
167 | 152 | el: this.el,
|
168 | 153 | gestureName: 'segment',
|
@@ -320,6 +305,35 @@ export class Segment implements ComponentInterface {
|
320 | 305 | }
|
321 | 306 | }
|
322 | 307 |
|
| 308 | + private scrollActiveButtonIntoView() { |
| 309 | + const { scrollable, value } = this; |
| 310 | + |
| 311 | + if (scrollable) { |
| 312 | + const buttons = this.getButtons(); |
| 313 | + const activeButton = buttons.find((button) => button.value === value); |
| 314 | + if (activeButton !== undefined) { |
| 315 | + /** |
| 316 | + * Scrollable segment buttons should be |
| 317 | + * centered within the view including |
| 318 | + * buttons that are partially offscreen. |
| 319 | + */ |
| 320 | + activeButton.scrollIntoView({ |
| 321 | + behavior: 'smooth', |
| 322 | + inline: 'center', |
| 323 | + |
| 324 | + /** |
| 325 | + * Segment should scroll on the |
| 326 | + * horizontal axis. `block: 'nearest'` |
| 327 | + * ensures that the vertical axis |
| 328 | + * does not scroll if the segment |
| 329 | + * as a whole is already in view. |
| 330 | + */ |
| 331 | + block: 'nearest', |
| 332 | + }); |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + |
323 | 337 | private setNextIndex(detail: GestureDetail, isEnd = false) {
|
324 | 338 | const rtl = isRTL(this.el);
|
325 | 339 | const activated = this.activated;
|
|
0 commit comments