Skip to content

Commit 0d8799c

Browse files
Fix bug on slider when dragging in rtl mode (#457)
* fix: honor direction when calculating drag offsets * fix: tweak demo to set rtl in a way that's compatible with the implementation * fix: add defensive checks
1 parent 6f2a181 commit 0d8799c

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/App/examples/Example14/Example14.jsx

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ import
1515
import s from '../../style.scss';
1616

1717
export default () => (
18-
<CarouselProvider
19-
visibleSlides={2}
20-
totalSlides={8}
21-
step={1}
22-
naturalSlideWidth={400}
23-
naturalSlideHeight={500}
24-
>
25-
<h2 className={s.headline}>RTL</h2>
26-
<p>
18+
<div dir="rtl">
19+
<CarouselProvider
20+
visibleSlides={2}
21+
totalSlides={8}
22+
step={1}
23+
naturalSlideWidth={400}
24+
naturalSlideHeight={500}
25+
>
26+
<h2 className={s.headline}>RTL</h2>
27+
<p>
2728
A carousel wrapped in an element with
28-
{' '}
29-
<code>dir=&quot;rtl&quot;</code>
29+
{' '}
30+
<code>dir=&quot;rtl&quot;</code>
3031
, demonstrating support for use with right-to-left languages.
31-
</p>
32-
<div dir="rtl">
32+
</p>
3333
<Slider className={s.slider}>
3434
<Slide index={0}>
3535
<ImageWithZoom src="./media/img01.jpeg" />
@@ -61,6 +61,6 @@ export default () => (
6161
<ButtonNext>Next</ButtonNext>
6262
<ButtonLast>Last</ButtonLast>
6363
<DotGroup dotNumbers />
64-
</div>
65-
</CarouselProvider>
64+
</CarouselProvider>
65+
</div>
6666
);

src/Slider/Slider.jsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ const Slider = class Slider extends React.Component {
201201

202202
getSliderRef(el) {
203203
this.sliderTrayElement = el;
204+
if (el && window) {
205+
// NOTE: we can't rely on the element itself to detect direction
206+
// as the direction of the slider is currently flipped to ltr
207+
const carouselElement = el.closest('.carousel');
208+
if (carouselElement) {
209+
this.rtl = window.getComputedStyle(carouselElement, null).getPropertyValue('direction') === 'rtl';
210+
}
211+
}
204212
}
205213

206214

@@ -234,7 +242,7 @@ const Slider = class Slider extends React.Component {
234242
window.cancelAnimationFrame.call(window, this.moveTimer);
235243
this.moveTimer = window.requestAnimationFrame.call(window, () => {
236244
this.setState(state => ({
237-
deltaX: screenX - state.startX,
245+
deltaX: (screenX - state.startX) * (this.rtl ? -1 : 1),
238246
deltaY: screenY - state.startY,
239247
preventingVerticalScroll: Math.abs(screenY - state.startY)
240248
<= this.props.verticalPixelThreshold

0 commit comments

Comments
 (0)