-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
201 lines (180 loc) · 5.49 KB
/
Copy pathindex.js
File metadata and controls
201 lines (180 loc) · 5.49 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from 'react' // Can be aliased to Preact in your project
import Level from '@app-elements/level'
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Improving_scrolling_performance_with_passive_listeners
let passiveIfSupported = false
try {
window.addEventListener('test', null,
Object.defineProperty({}, 'passive', {
get: function () {
passiveIfSupported = { passive: true }
}
})
)
} catch (err) {}
export class Carousel extends React.Component {
static defaultProps = {
tolerance: 100,
mouseSupport: true
}
state = { active: this.props.active || 0, width: 0 }
gesture = { x: [], y: [], match: '' }
capture = (event) => {
event.preventDefault()
this.gesture.x.push(event.touches[0].clientX)
this.gesture.y.push(event.touches[0].clientY)
}
compute = (event) => {
const { tolerance } = this.props
const xStart = this.gesture.x[0]
const yStart = this.gesture.y[0]
const xEnd = this.gesture.x.pop()
const yEnd = this.gesture.y.pop()
const xTravel = xEnd - xStart
const yTravel = yEnd - yStart
if (Math.abs(xTravel) < tolerance && yTravel > tolerance) {
this.gesture.match = 'down'
} else if (
yTravel < tolerance &&
Math.abs(xTravel) < tolerance &&
Math.abs(yTravel) > tolerance
) {
this.gesture.match = 'up'
} else if (
xTravel < tolerance &&
Math.abs(xTravel) > tolerance &&
Math.abs(yTravel) < tolerance
) {
this.gesture.match = 'left'
} else if (xTravel > tolerance && Math.abs(yTravel) < tolerance) {
this.gesture.match = 'right'
}
if (this.gesture.match !== '') {
this.onSwipe(this.gesture.match)
}
this.gesture.x = []
this.gesture.y = []
this.gesture.match = ''
}
onSwipe = (direction) => {
if (this.props.onSwipe) {
this.props.onSwipe(direction)
}
this.setState({ swipe: direction }, () => {
if (direction === 'left') {
this.handleNext()
} else if (direction === 'right') {
this.handlePrev()
}
})
}
handleNext = (ev) => {
ev && ev.preventDefault()
const active = this.state.active + this.state.numFit
const n = active >= this.props.children.length - 1
? 0
: this.state.active + 1
this.setState({ active: n })
}
handlePrev = (ev) => {
ev && ev.preventDefault()
const n = this.state.active <= 0
? this.props.children.length - (this.state.numFit + 1)
: this.state.active - 1
this.setState({ active: n })
}
setActive = (active) => {
return ev => {
ev.preventDefault()
this.setState({ active })
}
}
getRef = (ref) => {
if (!ref || this.ref) return
this.ref = ref
window.requestAnimationFrame(() => {
const width = this.ref.offsetWidth
const parent = this.ref.parentNode
const parentWidth = parent.offsetWidth
const numFit = parent != null
? Math.max(0, Math.floor(parent.offsetWidth / width) - 1)
: 0
this.setState({ width, parentWidth, numFit })
})
}
getStyle = (idx, active) => {
const { parentWidth, width } = this.state
const style = parentWidth != null
? `width: ${parentWidth}px;`
: ''
if (active === 0 || idx >= active) {
return style
}
return `${style} margin-left: -${width}px;`
}
getSlidesStyle = () => {
return `width: ${this.state.parentWidth * this.props.children.length}px;`
}
componentDidMount () {
this.base.addEventListener('touchstart', this.capture, passiveIfSupported)
this.base.addEventListener('touchmove', this.capture, passiveIfSupported)
this.base.addEventListener('touchend', this.compute, passiveIfSupported)
}
componentWillUnmount () {
this.base.removeEventListener('touchstart', this.capture)
this.base.removeEventListener('touchmove', this.capture)
this.base.removeEventListener('touchend', this.compute)
}
render () {
const {
children,
className = 'carousel-slide',
noNav = false,
withDots = false,
wrapperClass = ''
} = this.props
const { active } = this.state
return (
<div className={`carousel ${wrapperClass}`}>
<div>
<div className='carousel-inner'>
{!noNav &&
<nav className='nav prev'>
<button onClick={this.handlePrev} />
</nav>}
<div className='slides-wrapper'>
<div className='slides' style={this.getSlidesStyle()}>
{children.map((c, idx) =>
<div
key={`caurosel-${idx}`}
ref={(ref) => idx === 0 && this.getRef(ref)}
style={this.getStyle(idx, active)}
class={`${className}${idx === active ? ' active' : ''}`}
>
{c}
</div>
)}
</div>
</div>
{!noNav &&
<nav className='nav next'>
<button onClick={this.handleNext} />
</nav>}
</div>
{withDots &&
<Level className='carousel-dots'>
{children.map((_, idx) =>
<button
onClick={this.setActive(idx)}
key={`dots-${idx}`}
className={`${idx === active ? 'active' : ''}`}
>
{idx}
</button>
)}
</Level>}
</div>
</div>
)
}
}
export default Carousel