Skip to content

Commit d8beca6

Browse files
authored
Merge pull request #99 from sag1v/next
Merge next to master
2 parents 8df1148 + 39172ff commit d8beca6

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-elastic-carousel",
3-
"version": "0.9.1",
3+
"version": "0.9.3",
44
"description": "A flexible and responsive carousel component for react",
55
"author": "sag1v (Sagiv Ben Giat)",
66
"license": "MIT",

src/react-elastic-carousel/components/Carousel.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Carousel extends React.Component {
2626
swipedSliderPosition: 0,
2727
isSwiping: false,
2828
transitioning: false,
29+
transitionMs: this.props.transitionMs,
2930
activeIndex: this.props.initialActiveIndex || this.props.initialFirstItem, // support deprecated initialFirstItem
3031
pages: [],
3132
activePage: 0,
@@ -168,7 +169,8 @@ class Carousel extends React.Component {
168169
const {
169170
children,
170171
verticalMode,
171-
itemsToShow
172+
itemsToShow,
173+
transitionMs
172174
} = this.getDerivedPropsFromBreakPoint();
173175
const { childWidth, childHeight, activeIndex } = state;
174176
const totalItems = children.length;
@@ -181,6 +183,10 @@ class Carousel extends React.Component {
181183
let sliderPosition = (verticalMode ? childHeight : childWidth) * moveBy;
182184
const newActiveIndex =
183185
emptySlots > 0 ? activeIndex - emptySlots : activeIndex;
186+
// go back from 0ms to whatever set by the user
187+
// We were at 0ms because we wanted to disable animation on resize
188+
// see https://github.com/sag1v/react-elastic-carousel/issues/94
189+
window.requestAnimationFrame(() => this.setState({ transitionMs }));
184190
return {
185191
sliderPosition,
186192
activeIndex: newActiveIndex < 0 ? 0 : newActiveIndex
@@ -209,7 +215,8 @@ class Carousel extends React.Component {
209215
onContainerResize = sliderContainerNode => {
210216
const { width } = sliderContainerNode.contentRect;
211217
// update slider container width
212-
this.setState({ sliderContainerWidth: width }, () => {
218+
// disable animation on resize see https://github.com/sag1v/react-elastic-carousel/issues/94
219+
this.setState({ sliderContainerWidth: width, transitionMs: 0 }, () => {
213220
// we must get these props inside setState (get future props because its async)
214221
const {
215222
onResize,
@@ -334,23 +341,28 @@ class Carousel extends React.Component {
334341
divider = largeDivider;
335342
}
336343

337-
let distanceSwipe = verticalMode
344+
const distanceSwipe = verticalMode
338345
? rootHeight / divider
339346
: sliderContainerWidth / divider;
340347

341-
const isHorizontalSwipe = dir === "Left" || dir === "Right";
348+
const horizontalSwipe = dir === "Left" || dir === "Right";
349+
const verticalSwipe = dir === "Up" || dir === "Down";
350+
const horizontalMode = !verticalMode;
342351

343352
const shouldHorizontalSkipUpdate =
344-
isHorizontalSwipe && (!verticalMode && absX > distanceSwipe);
353+
(horizontalMode && verticalSwipe) ||
354+
(horizontalMode && horizontalSwipe && absX > distanceSwipe);
355+
345356
const shouldVerticalSkipUpdate =
346-
!isHorizontalSwipe && (verticalMode && absY > distanceSwipe);
357+
(verticalMode && horizontalSwipe) ||
358+
(verticalMode && verticalSwipe && absY > distanceSwipe);
347359

348360
if (shouldHorizontalSkipUpdate || shouldVerticalSkipUpdate) {
349361
// bail out of state update
350362
return;
351363
}
352364
return {
353-
swipedSliderPosition: isHorizontalSwipe
365+
swipedSliderPosition: horizontalSwipe
354366
? sliderPosition - deltaX
355367
: sliderPosition - deltaY,
356368
isSwiping: true,
@@ -366,19 +378,24 @@ class Carousel extends React.Component {
366378
// 3. vertical mode - swipe up or down
367379

368380
const { absX, absY, dir } = data;
369-
const { childWidth } = this.state;
381+
const { childWidth, childHeight } = this.state;
370382
const { verticalMode, isRTL } = this.props;
371383
let func = this.resetSwipe;
372-
const minSwipeDistance = childWidth / 3;
384+
const minSwipeDistanceHorizontal = childWidth / 3;
385+
const minSwipeDistanceVertical = childHeight / 3;
373386
const swipedLeft = dir === "Left";
374387
const swipedRight = dir === "Right";
375388
const swipedUp = dir === "Up";
376389
const swipedDown = dir === "Down";
377390
const verticalGoSwipe =
378-
verticalMode && (swipedUp || swipedDown) && absY > minSwipeDistance;
391+
verticalMode &&
392+
(swipedUp || swipedDown) &&
393+
absY > minSwipeDistanceVertical;
379394

380395
const horizontalGoSwipe =
381-
!verticalMode && (swipedRight || swipedLeft) && absX > minSwipeDistance;
396+
!verticalMode &&
397+
(swipedRight || swipedLeft) &&
398+
absX > minSwipeDistanceHorizontal;
382399

383400
let goodToGo = false;
384401
if (verticalGoSwipe || horizontalGoSwipe) {
@@ -585,7 +602,8 @@ class Carousel extends React.Component {
585602
swipedSliderPosition,
586603
rootHeight,
587604
pages,
588-
activeIndex
605+
activeIndex,
606+
transitionMs
589607
} = this.state;
590608
const {
591609
className,
@@ -595,7 +613,6 @@ class Carousel extends React.Component {
595613
isRTL,
596614
easing,
597615
tiltEasing,
598-
transitionMs,
599616
children,
600617
focusOnSelect,
601618
autoTabIndexVisibleItems,
@@ -663,6 +680,7 @@ class Carousel extends React.Component {
663680
ref={this.setRef("slider")}
664681
>
665682
<Track
683+
verticalMode={verticalMode}
666684
children={children}
667685
childWidth={childWidth}
668686
currentItem={activeIndex}
@@ -742,6 +760,7 @@ Carousel.defaultProps = {
742760
autoPlaySpeed: 2000,
743761

744762
// callbacks
763+
onChange: noop,
745764
onNextEnd: noop,
746765
onPrevEnd: noop,
747766
onNextStart: noop,

src/react-elastic-carousel/components/Track.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const Track = ({
1717
itemPadding,
1818
onSwiped,
1919
onSwiping,
20+
verticalMode,
2021
onItemClick
2122
}) => {
2223
const width = `${childWidth}px`;
@@ -47,7 +48,10 @@ const Track = ({
4748
});
4849
const toRender = enableSwipe ? (
4950
<Swipeable
50-
style={{ display: "flex" }}
51+
style={{
52+
display: "flex",
53+
flexDirection: verticalMode ? "column" : "row"
54+
}}
5155
stopPropagation
5256
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}
5357
trackMouse={enableMouseSwipe}
@@ -71,6 +75,7 @@ Track.propTypes = {
7175
itemPosition: PropTypes.string,
7276
itemPadding: PropTypes.array,
7377
childWidth: PropTypes.number,
78+
verticalMode: PropTypes.bool,
7479
enableSwipe: PropTypes.bool,
7580
enableMouseSwipe: PropTypes.bool,
7681
preventDefaultTouchmoveEvent: PropTypes.bool,

src/react-elastic-carousel/components/styled/Slider.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ const calcTransition = ({ isSwiping, transitionMs, easing, tiltEasing }) => {
4747
return `all ${duration}ms ${effectiveEasing}`;
4848
};
4949

50-
export default styled.div`
50+
// We use attributes (style) to bypass multiple creation of classes (dynamic styling)
51+
export default styled.div.attrs(props => ({
52+
style: {
53+
transition: calcTransition(props),
54+
left: calcLeft(props),
55+
right: calcRight(props),
56+
top: calcTop(props)
57+
}
58+
}))`
5159
position: absolute;
5260
display: flex;
5361
flex-direction: ${({ verticalMode }) => (verticalMode ? "column" : "row")};
54-
${({ verticalMode }) => (verticalMode ? "min-height: 100%;" : "")}
55-
transition: ${calcTransition};
56-
left: ${calcLeft};
57-
right: ${calcRight};
58-
top: ${calcTop};
62+
${({ verticalMode }) => (verticalMode ? "min-height: 100%;" : "")};
5963
`;

src/react-elastic-carousel/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import * as React from "react";
22

3-
type RenderArrowProps = {
3+
export type RenderArrowProps = {
44
type: "PREV" | "NEXT";
55
onClick: () => void;
66
isEdge: boolean;
77
};
88

9-
type RenderPaginationProps = {
9+
export type RenderPaginationProps = {
1010
pages: number[];
1111
activePage: number;
1212
// The onClick event that sets the state of the carousel and sends
1313
// it to a specific page.
1414
onClick: (indicatorId: string) => void;
1515
};
1616

17-
type ItemObject = {
17+
export type ItemObject = {
1818
// Children's props
1919
object: any;
2020
index: number;
2121
};
2222

23-
type Breakpoint = {
23+
export type Breakpoint = {
2424
itemsToScroll: number;
2525
itemsToShow: number;
2626
};

0 commit comments

Comments
 (0)