|
1 | 1 | import type { ComponentInterface, EventEmitter } from '@stencil/core'; |
2 | 2 | import { Component, Element, Event, Host, Listen, Method, Prop, State, h } from '@stencil/core'; |
| 3 | +import { isRTL } from '@utils/rtl'; |
3 | 4 |
|
4 | 5 | import type { SegmentViewScrollEvent } from './segment-view-interface'; |
5 | 6 |
|
@@ -39,8 +40,9 @@ export class SegmentView implements ComponentInterface { |
39 | 40 | @Listen('scroll') |
40 | 41 | handleScroll(ev: Event) { |
41 | 42 | const { scrollLeft, scrollWidth, clientWidth } = ev.target as HTMLElement; |
42 | | - const isRTL = window.getComputedStyle(this.el).direction === 'rtl'; |
43 | | - const scrollRatio = (isRTL ? -1 : 1) * (scrollLeft / (scrollWidth - clientWidth)); |
| 43 | + const max = scrollWidth - clientWidth; |
| 44 | + const scrollRatio = (isRTL(this.el) ? -1 : 1) * (scrollLeft / max); |
| 45 | + |
44 | 46 |
|
45 | 47 | this.ionSegmentViewScroll.emit({ |
46 | 48 | scrollRatio, |
@@ -119,17 +121,18 @@ export class SegmentView implements ComponentInterface { |
119 | 121 | async setContent(id: string, smoothScroll = true) { |
120 | 122 | const contents = this.getSegmentContents(); |
121 | 123 | const index = contents.findIndex((content) => content.id === id); |
122 | | - const isRTL = window.getComputedStyle(this.el).direction === 'rtl'; |
123 | 124 |
|
124 | 125 | if (index === -1) return; |
125 | 126 |
|
126 | 127 | this.isManualScroll = false; |
127 | 128 | this.resetScrollEndTimeout(); |
128 | 129 |
|
129 | 130 | const contentWidth = this.el.offsetWidth; |
| 131 | + const offset = index * contentWidth; |
| 132 | + |
130 | 133 | this.el.scrollTo({ |
131 | 134 | top: 0, |
132 | | - left: (isRTL ? -1 : 1) * index * contentWidth, |
| 135 | + left: (isRTL(this.el) ? -1 : 1 ) * offset, |
133 | 136 | behavior: smoothScroll ? 'smooth' : 'instant', |
134 | 137 | }); |
135 | 138 | } |
|
0 commit comments