Skip to content

Commit 6e1f1d2

Browse files
thetaPCrostislavcz
andcommitted
refactor(segment): switch to scrollTo
Co-authored-by: rostislavcz <[email protected]>
1 parent f0ae0fa commit 6e1f1d2

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

core/src/components/segment/segment.tsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -340,39 +340,38 @@ export class Segment implements ComponentInterface {
340340
* scroll container.
341341
*/
342342
const centeredX = activeButtonLeft - scrollContainerBox.width / 2 + activeButtonBox.width / 2;
343-
console.log(centeredX);
344343

345344
/**
346-
* 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
347355
* to avoid a WebKit bug where accelerated animations break
348356
* when using scrollIntoView. Using scrollIntoView will cause the
349357
* segment container to jump during the transition and then snap into place.
350358
* This is because scrollIntoView can potentially cause parent element
351-
* 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
352360
* we use this API instead.
353361
*
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+
*
354367
* Note that if there is not enough scrolling space to center the element
355368
* within the scroll container, the browser will attempt
356369
* to center by as much as it can.
357370
*/
358-
// el.scrollBy({
359-
// top: 0,
360-
// left: centeredX,
361-
// behavior: smoothScroll ? 'smooth' : 'instant',
362-
// });
363-
364-
activeButton.scrollIntoView({
371+
el.scrollTo({
372+
top: 0,
373+
left: newScrollPosition,
365374
behavior: smoothScroll ? 'smooth' : 'instant',
366-
inline: 'center',
367-
368-
/**
369-
* Segment should scroll on the
370-
* horizontal axis. `block: 'nearest'`
371-
* ensures that the vertical axis
372-
* does not scroll if the segment
373-
* as a whole is already in view.
374-
*/
375-
block: 'nearest',
376375
});
377376
}
378377
}

0 commit comments

Comments
 (0)