@@ -19,7 +19,6 @@ import { Pagination } from "./Pagination";
1919class Carousel extends React . Component {
2020 state = {
2121 rootHeight : 0 ,
22- rootWidth : 0 ,
2322 childWidth : 0 ,
2423 childHeight : 0 ,
2524 sliderPosition : 0 ,
@@ -97,10 +96,20 @@ class Carousel extends React.Component {
9796 this . ro = new ResizeObserver ( ( entries , observer ) => {
9897 for ( const entry of entries ) {
9998 if ( entry . target === this . sliderContainer ) {
100- this . onContainerResize ( entry ) ;
99+ // we are using rAF because it fixes the infinite refresh with gatsby (ssr?).
100+ // TBH, I'm not sure i fully understand why.
101+ // see https://github.com/sag1v/react-elastic-carousel/issues/107
102+ window . requestAnimationFrame ( ( ) => {
103+ this . onContainerResize ( entry ) ;
104+ } ) ;
101105 }
102106 if ( entry . target === this . slider ) {
103- this . onSliderResize ( entry ) ;
107+ // we are using rAF because it fixes the infinite refresh with gatsby (ssr?).
108+ // TBH, I'm not sure i fully understand why
109+ // see https://github.com/sag1v/react-elastic-carousel/issues/107
110+ window . requestAnimationFrame ( ( ) => {
111+ this . onSliderResize ( entry ) ;
112+ } ) ;
104113 }
105114 }
106115 } ) ;
@@ -156,8 +165,8 @@ class Carousel extends React.Component {
156165 . find ( bp => bp . width <= sliderContainerWidth ) ;
157166 if ( ! currentBreakPoint ) {
158167 /* in case we don't have a lower width than sliderContainerWidth
159- * this mostly happens in initilization when sliderContainerWidth is 0
160- */
168+ * this mostly happens in initilization when sliderContainerWidth is 0
169+ */
161170 currentBreakPoint = breakPoints [ 0 ] ;
162171 }
163172 }
@@ -232,8 +241,8 @@ class Carousel extends React.Component {
232241 } = this . getDerivedPropsFromBreakPoint ( ) ;
233242
234243 /* based on slider container's width, get num of items to show
235- * and calculate child's width (and update it in state)
236- */
244+ * and calculate child's width (and update it in state)
245+ */
237246 const childrenLength = Children . toArray ( children ) . length ;
238247 let childWidth = 0 ;
239248 if ( verticalMode ) {
@@ -250,10 +259,10 @@ class Carousel extends React.Component {
250259 state => ( { childWidth } ) ,
251260 ( ) => {
252261 /* Based on all of the above new data:
253- * update slider position
254- * get the new current breakpoint
255- * pass the current breakpoint to the consumer's callback
256- */
262+ * update slider position
263+ * get the new current breakpoint
264+ * pass the current breakpoint to the consumer's callback
265+ */
257266 this . updateSliderPosition ( ) ;
258267 const currentBreakPoint = this . getDerivedPropsFromBreakPoint ( ) ;
259268 onResize ( currentBreakPoint ) ;
@@ -381,10 +390,18 @@ class Carousel extends React.Component {
381390 // bail out of state update
382391 return ;
383392 }
393+ let swipedSliderPosition ;
394+ if ( horizontalSwipe ) {
395+ if ( isRTL ) {
396+ swipedSliderPosition = sliderPosition + deltaX ;
397+ } else {
398+ swipedSliderPosition = sliderPosition - deltaX ;
399+ }
400+ } else {
401+ swipedSliderPosition = sliderPosition - deltaY ;
402+ }
384403 return {
385- swipedSliderPosition : horizontalSwipe
386- ? sliderPosition - deltaX
387- : sliderPosition - deltaY ,
404+ swipedSliderPosition,
388405 isSwiping : true ,
389406 transitioning : true
390407 } ;
0 commit comments