Skip to content

Commit 078ed0b

Browse files
thetaPCrostislavcz
andauthored
fix(segment): prevent flickering for scrollable on iOS (#29884)
Issue number: resolves #29523 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> The scrollable segment flickers on iOS physical devices or simulators when the active button is near the edge of the screen. The jump is due to the button being scrolled to the center and snaps back to the edge since the button was scrolled past the container. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Switched to `scrollTo` provides for a smoother transition. - Gave co author credit to the original reporter since they provided part of the solution - No new tests were created since functionality stays the same and testing on Playwright would be impossible to recreate ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: 8.3.2-dev.11726779768.16e1f1d2 How to test: 1. Create a new app through any starter 2. Add a scrollable segment with at least 6 buttons (code snippet example below) 3. Recommended to change the segment mode to `md` since it's easier to see the flicker 4. Build the app and open it in an iOS or simulator (if more instructions on how to do this is needed, reach out to me) 5. Click on the third button 6. Click on the first button 7. Notice the flicker 8. Click over to the third to last button 9. Click on either the last two buttons 10. Notice the flicker 11. Install the dev build 12. Verify the load does not flicker 13. Repeat steps 4 and 5 14. Verify the flicker is no longer there 15. Repeat steps 7 and 8 16. Verify the flicker is no longer there ```js <ion-segment value="2" scrollable="true" mode="md"> <ion-segment-button value="1"> <ion-label>Button 1</ion-label> </ion-segment-button> <ion-segment-button value="2"> <ion-label>Button 2</ion-label> </ion-segment-button> <ion-segment-button value="3"> <ion-label>Button 3</ion-label> </ion-segment-button> <ion-segment-button value="4"> <ion-label>Button 4</ion-label> </ion-segment-button> <ion-segment-button value="5"> <ion-label>Button 5</ion-label> </ion-segment-button> <ion-segment-button value="6"> <ion-label>Button 6</ion-label> </ion-segment-button> </ion-segment> ``` --------- Co-authored-by: rostislavcz <58735164+rostislavcz@users.noreply.github.com>
1 parent 221bb42 commit 078ed0b

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

core/src/components/segment/segment.tsx

+18-4
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,35 @@ export class Segment implements ComponentInterface {
342342
const centeredX = activeButtonLeft - scrollContainerBox.width / 2 + activeButtonBox.width / 2;
343343

344344
/**
345-
* We intentionally use scrollBy here instead of scrollIntoView
345+
* newScrollPosition is the absolute scroll position that the
346+
* container needs to move to in order to center the active button.
347+
* It is calculated by adding the current scroll position
348+
* (scrollLeft) to the offset needed to center the button
349+
* (centeredX).
350+
*/
351+
const newScrollPosition = el.scrollLeft + centeredX;
352+
353+
/**
354+
* We intentionally use scrollTo here instead of scrollIntoView
346355
* to avoid a WebKit bug where accelerated animations break
347356
* when using scrollIntoView. Using scrollIntoView will cause the
348357
* segment container to jump during the transition and then snap into place.
349358
* This is because scrollIntoView can potentially cause parent element
350-
* containers to also scroll. scrollBy does not have this same behavior, so
359+
* containers to also scroll. scrollTo does not have this same behavior, so
351360
* we use this API instead.
352361
*
362+
* scrollTo is used instead of scrollBy because there is a
363+
* Webkit bug that causes scrollBy to not work smoothly when
364+
* the active button is near the edge of the scroll container.
365+
* This leads to the buttons to jump around during the transition.
366+
*
353367
* Note that if there is not enough scrolling space to center the element
354368
* within the scroll container, the browser will attempt
355369
* to center by as much as it can.
356370
*/
357-
el.scrollBy({
371+
el.scrollTo({
358372
top: 0,
359-
left: centeredX,
373+
left: newScrollPosition,
360374
behavior: smoothScroll ? 'smooth' : 'instant',
361375
});
362376
}

0 commit comments

Comments
 (0)