-
Notifications
You must be signed in to change notification settings - Fork 16
/
carousel.js
164 lines (159 loc) · 5.49 KB
/
carousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, {Component, Children} from 'react';
import PropTypes from 'prop-types';
class Carousel extends Component {
constructor(props) {
super(props);
this.state = {slide: props.initialSlide, dragging: null, sliding: false, offset: 0};
this.setTimer = this.setTimer.bind(this);
this.clearTimer = this.clearTimer.bind(this);
this.events = {
onTouchStart: this.onDraggingStart.bind(this),
onTouchMove: this.onDraggingMove.bind(this),
onTouchEnd: this.onDraggingEnd.bind(this),
onTouchCancel: this.onDraggingEnd.bind(this),
onClick: this.onClick.bind(this),
onTransitionEnd: this.onTransitionEnd.bind(this)
};
}
componentDidMount() {
this.setTimer();
}
componentWillUnmount() {
this.clearTimer();
}
onTransitionEnd() { // this will not be triggered when document.hidden
let {slide} = this.state;
const count = Children.count(this.props.children);
if (slide == count + 1) slide = 1;
if (slide == 0) slide = count;
this.setState({slide, sliding: false}, () => {
this.setTimer();
this.props.slideDidChange && this.props.slideDidChange(slide);
});
}
setTimer() {
const interval = this.props.autoPlayInterval;
if (Children.count(this.props.children) > 1 && interval > 0) {
this.clearTimer();
this.timer = window.setInterval(this.changeSlide.bind(this, this.state.slide + 1), interval);
}
}
clearTimer() {
window.clearInterval(this.timer);
}
changeSlide(slide) {
if (document.hidden) return; // run only when page is visible
if (this.props.slideWillChange && !this.props.slideWillChange(slide, this.state.slide)) return;
if (slide >= 0 && slide <= React.Children.count(this.props.children) + 1)
this.setState({slide, sliding: true, dragging: null, offset: 0}, this.setTimer);
}
onDraggingStart(event) {
if (event.touches)
this.setState({dragging: {
x: event.touches[0].pageX,
y: event.touches[0].pageY
}, offset: 0});
}
onDraggingMove(event) {
const {sliding, dragging} = this.state;
if (sliding || !dragging || !event.touches) return;
const x = event.touches[0].pageX;
const y = event.touches[0].pageY;
const offset = x - dragging.x;
if (Math.abs(y - dragging.y) < Math.abs(offset)) event.preventDefault();
this.setState({offset});
}
onDraggingEnd(event) {
const sliderWidth = event.currentTarget.clientWidth;
const {slide, offset, dragging} = this.state;
if (!dragging) return;
const target = Math.abs(offset) > sliderWidth / 5 ? (offset > 0 ? slide - 1 : slide + 1) : slide;
this.setState({dragging: null}, this.changeSlide.bind(this, target));
}
onClick(event) {
if (Math.abs(this.state.offset) < 25) return; // trigger click in a small distance
event.preventDefault();
event.stopPropagation();
event.nativeEvent.stopPropagation();
}
render() {
const {children, switcher, indicator, transitionDuration, transitionTimingFunction, slideWillChange, slideDidChange} = this.props;
const props = Object.assign({}, this.props); // rest parameters is not available before node 8
delete props.children;
delete props.autoPlayInterval;
delete props.switcher;
delete props.indicator;
delete props.transitionDuration;
delete props.transitionTimingFunction;
delete props.slideWillChange;
delete props.slideDidChange;
delete props.initialSlide;
const {slide, sliding, dragging, offset} = this.state;
const slides = Children.map(children, (child) => React.cloneElement(child, {key: child.key + '_clone'}));
const count = Children.count(children);
const enabled = count > 1;
const goPrevSlide = this.changeSlide.bind(this, slide - 1);
const goNextSlide = this.changeSlide.bind(this, slide + 1);
const slideStyle = {
flexBasis: '100%',
flexShrink: 0
};
return (
<div {...props} style={Object.assign({}, props.style, {
position: 'relative',
overflowX: 'hidden',
touchAction: 'pan-y pinch-zoom'
})}>
<ul style={{
listStyleType: 'none',
padding: 0,
margin: 0,
display: 'flex',
transitionProperty: sliding ? 'transform' : 'none',
transform: enabled ? (offset !== 0 ? 'translateX(calc(' + (offset * 1) + 'px - ' + slide * 100 + '%))' : 'translateX(-' + slide * 100 + '%)') : null,
transitionDuration,
transitionTimingFunction,
contain: 'layout',
willChange: 'transform'
}} {...this.events}>
{enabled && Children.map(slides.slice(-1).concat(children, slides.slice(0, 1)),
(item, index) => <li aria-current={slide === index} style={slideStyle}>{item}</li>) || <li>{children}</li>
}
</ul>
{enabled && indicator && <ol>
{Children.map(children, (item, index) =>
<li aria-current={slide === index + 1} onClick={this.changeSlide.bind(this, index + 1)}>
{index + 1}
</li>
)}
</ol>}
{enabled && switcher && <div>
<button type="button" className="prev" onClick={goPrevSlide}></button>
<button type="button" className="next" onClick={goNextSlide}></button>
</div>}
</div>
);
}
}
Carousel.propTypes = {
className: PropTypes.string,
autoPlayInterval: PropTypes.number,
transitionDuration: PropTypes.string,
transitionTimingFunction: PropTypes.string,
switcher: PropTypes.bool,
indicator: PropTypes.bool,
slideWillChange: PropTypes.func,
slideDidChange: PropTypes.func,
initialSlide: PropTypes.number,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired
};
Carousel.defaultProps = {
className: 'slider',
transitionDuration: '.8s',
transitionTimingFunction: 'ease-in-out',
initialSlide: 1 // slide index start from 1
};
export default Carousel;