UNPKG

16.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _react = _interopRequireWildcard(require("react"));
9
10var _cssClasses = _interopRequireDefault(require("../cssClasses"));
11
12var _dimensions = require("../dimensions");
13
14var _CSSTranslate = _interopRequireDefault(require("../CSSTranslate"));
15
16var _reactEasySwipe = _interopRequireDefault(require("react-easy-swipe"));
17
18var _window = _interopRequireDefault(require("../shims/window"));
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
23
24function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
26function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
27
28function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
29
30function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
32function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
33
34function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
35
36function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
37
38function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
39
40function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
41
42function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
43
44function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
45
46function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
47
48function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
49
50function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
51
52var isKeyboardEvent = function isKeyboardEvent(e) {
53 return e.hasOwnProperty('key');
54};
55
56var Thumbs = /*#__PURE__*/function (_Component) {
57 _inherits(Thumbs, _Component);
58
59 var _super = _createSuper(Thumbs);
60
61 function Thumbs(_props) {
62 var _this;
63
64 _classCallCheck(this, Thumbs);
65
66 _this = _super.call(this, _props);
67
68 _defineProperty(_assertThisInitialized(_this), "itemsWrapperRef", void 0);
69
70 _defineProperty(_assertThisInitialized(_this), "itemsListRef", void 0);
71
72 _defineProperty(_assertThisInitialized(_this), "thumbsRef", void 0);
73
74 _defineProperty(_assertThisInitialized(_this), "setItemsWrapperRef", function (node) {
75 _this.itemsWrapperRef = node;
76 });
77
78 _defineProperty(_assertThisInitialized(_this), "setItemsListRef", function (node) {
79 _this.itemsListRef = node;
80 });
81
82 _defineProperty(_assertThisInitialized(_this), "setThumbsRef", function (node, index) {
83 if (!_this.thumbsRef) {
84 _this.thumbsRef = [];
85 }
86
87 _this.thumbsRef[index] = node;
88 });
89
90 _defineProperty(_assertThisInitialized(_this), "updateSizes", function () {
91 if (!_this.props.children || !_this.itemsWrapperRef || !_this.thumbsRef) {
92 return;
93 }
94
95 var total = _react.Children.count(_this.props.children);
96
97 var wrapperSize = _this.itemsWrapperRef.clientWidth;
98 var itemSize = _this.props.thumbWidth ? _this.props.thumbWidth : (0, _dimensions.outerWidth)(_this.thumbsRef[0]);
99 var visibleItems = Math.floor(wrapperSize / itemSize);
100 var showArrows = visibleItems < total;
101 var lastPosition = showArrows ? total - visibleItems : 0;
102
103 _this.setState(function (_state, props) {
104 return {
105 itemSize: itemSize,
106 visibleItems: visibleItems,
107 firstItem: showArrows ? _this.getFirstItem(props.selectedItem) : 0,
108 lastPosition: lastPosition,
109 showArrows: showArrows
110 };
111 });
112 });
113
114 _defineProperty(_assertThisInitialized(_this), "handleClickItem", function (index, item, e) {
115 if (!isKeyboardEvent(e) || e.key === 'Enter') {
116 var handler = _this.props.onSelectItem;
117
118 if (typeof handler === 'function') {
119 handler(index, item);
120 }
121 }
122 });
123
124 _defineProperty(_assertThisInitialized(_this), "onSwipeStart", function () {
125 _this.setState({
126 swiping: true
127 });
128 });
129
130 _defineProperty(_assertThisInitialized(_this), "onSwipeEnd", function () {
131 _this.setState({
132 swiping: false
133 });
134 });
135
136 _defineProperty(_assertThisInitialized(_this), "onSwipeMove", function (delta) {
137 var deltaX = delta.x;
138
139 if (!_this.state.itemSize || !_this.itemsWrapperRef || !_this.state.visibleItems) {
140 return false;
141 }
142
143 var leftBoundary = 0;
144
145 var childrenLength = _react.Children.count(_this.props.children);
146
147 var currentPosition = -(_this.state.firstItem * 100) / _this.state.visibleItems;
148 var lastLeftItem = Math.max(childrenLength - _this.state.visibleItems, 0);
149 var lastLeftBoundary = -lastLeftItem * 100 / _this.state.visibleItems; // prevent user from swiping left out of boundaries
150
151 if (currentPosition === leftBoundary && deltaX > 0) {
152 deltaX = 0;
153 } // prevent user from swiping right out of boundaries
154
155
156 if (currentPosition === lastLeftBoundary && deltaX < 0) {
157 deltaX = 0;
158 }
159
160 var wrapperSize = _this.itemsWrapperRef.clientWidth;
161 var position = currentPosition + 100 / (wrapperSize / deltaX); // if 3d isn't available we will use left to move
162
163 if (_this.itemsListRef) {
164 ['WebkitTransform', 'MozTransform', 'MsTransform', 'OTransform', 'transform', 'msTransform'].forEach(function (prop) {
165 _this.itemsListRef.style[prop] = (0, _CSSTranslate.default)(position, '%', _this.props.axis);
166 });
167 }
168
169 return true;
170 });
171
172 _defineProperty(_assertThisInitialized(_this), "slideRight", function (positions) {
173 _this.moveTo(_this.state.firstItem - (typeof positions === 'number' ? positions : 1));
174 });
175
176 _defineProperty(_assertThisInitialized(_this), "slideLeft", function (positions) {
177 _this.moveTo(_this.state.firstItem + (typeof positions === 'number' ? positions : 1));
178 });
179
180 _defineProperty(_assertThisInitialized(_this), "moveTo", function (position) {
181 // position can't be lower than 0
182 position = position < 0 ? 0 : position; // position can't be higher than last postion
183
184 position = position >= _this.state.lastPosition ? _this.state.lastPosition : position;
185
186 _this.setState({
187 firstItem: position
188 });
189 });
190
191 _this.state = {
192 selectedItem: _props.selectedItem,
193 swiping: false,
194 showArrows: false,
195 firstItem: 0,
196 visibleItems: 0,
197 lastPosition: 0
198 };
199 return _this;
200 }
201
202 _createClass(Thumbs, [{
203 key: "componentDidMount",
204 value: function componentDidMount() {
205 this.setupThumbs();
206 }
207 }, {
208 key: "componentDidUpdate",
209 value: function componentDidUpdate(prevProps) {
210 if (this.props.selectedItem !== this.state.selectedItem) {
211 this.setState({
212 selectedItem: this.props.selectedItem,
213 firstItem: this.getFirstItem(this.props.selectedItem)
214 });
215 }
216
217 if (this.props.children === prevProps.children) {
218 return;
219 } // This will capture any size changes for arrow adjustments etc.
220 // usually in the same render cycle so we don't see any flickers
221
222
223 this.updateSizes();
224 }
225 }, {
226 key: "componentWillUnmount",
227 value: function componentWillUnmount() {
228 this.destroyThumbs();
229 }
230 }, {
231 key: "setupThumbs",
232 value: function setupThumbs() {
233 // as the widths are calculated, we need to resize
234 // the carousel when the window is resized
235 (0, _window.default)().addEventListener('resize', this.updateSizes); // issue #2 - image loading smaller
236
237 (0, _window.default)().addEventListener('DOMContentLoaded', this.updateSizes); // when the component is rendered we need to calculate
238 // the container size to adjust the responsive behaviour
239
240 this.updateSizes();
241 }
242 }, {
243 key: "destroyThumbs",
244 value: function destroyThumbs() {
245 // removing listeners
246 (0, _window.default)().removeEventListener('resize', this.updateSizes);
247 (0, _window.default)().removeEventListener('DOMContentLoaded', this.updateSizes);
248 }
249 }, {
250 key: "getFirstItem",
251 value: function getFirstItem(selectedItem) {
252 var firstItem = selectedItem;
253
254 if (selectedItem >= this.state.lastPosition) {
255 firstItem = this.state.lastPosition;
256 }
257
258 if (selectedItem < this.state.firstItem + this.state.visibleItems) {
259 firstItem = this.state.firstItem;
260 }
261
262 if (selectedItem < this.state.firstItem) {
263 firstItem = selectedItem;
264 }
265
266 return firstItem;
267 }
268 }, {
269 key: "renderItems",
270 value: function renderItems() {
271 var _this2 = this;
272
273 return this.props.children.map(function (img, index) {
274 var itemClass = _cssClasses.default.ITEM(false, index === _this2.state.selectedItem);
275
276 var thumbProps = {
277 key: index,
278 ref: function ref(e) {
279 return _this2.setThumbsRef(e, index);
280 },
281 className: itemClass,
282 onClick: _this2.handleClickItem.bind(_this2, index, _this2.props.children[index]),
283 onKeyDown: _this2.handleClickItem.bind(_this2, index, _this2.props.children[index]),
284 'aria-label': "".concat(_this2.props.labels.item, " ").concat(index + 1),
285 style: {
286 width: _this2.props.thumbWidth
287 }
288 };
289 return /*#__PURE__*/_react.default.createElement("li", _extends({}, thumbProps, {
290 role: "button",
291 tabIndex: 0
292 }), img);
293 });
294 }
295 }, {
296 key: "render",
297 value: function render() {
298 var _this3 = this;
299
300 if (!this.props.children) {
301 return null;
302 }
303
304 var isSwipeable = _react.Children.count(this.props.children) > 1; // show left arrow?
305
306 var hasPrev = this.state.showArrows && this.state.firstItem > 0; // show right arrow
307
308 var hasNext = this.state.showArrows && this.state.firstItem < this.state.lastPosition; // obj to hold the transformations and styles
309
310 var itemListStyles = {};
311 var currentPosition = -this.state.firstItem * (this.state.itemSize || 0);
312 var transformProp = (0, _CSSTranslate.default)(currentPosition, 'px', this.props.axis);
313 var transitionTime = this.props.transitionTime + 'ms';
314 itemListStyles = {
315 WebkitTransform: transformProp,
316 MozTransform: transformProp,
317 MsTransform: transformProp,
318 OTransform: transformProp,
319 transform: transformProp,
320 msTransform: transformProp,
321 WebkitTransitionDuration: transitionTime,
322 MozTransitionDuration: transitionTime,
323 MsTransitionDuration: transitionTime,
324 OTransitionDuration: transitionTime,
325 transitionDuration: transitionTime,
326 msTransitionDuration: transitionTime
327 };
328 return /*#__PURE__*/_react.default.createElement("div", {
329 className: _cssClasses.default.CAROUSEL(false)
330 }, /*#__PURE__*/_react.default.createElement("div", {
331 className: _cssClasses.default.WRAPPER(false),
332 ref: this.setItemsWrapperRef
333 }, /*#__PURE__*/_react.default.createElement("button", {
334 type: "button",
335 className: _cssClasses.default.ARROW_PREV(!hasPrev),
336 onClick: function onClick() {
337 return _this3.slideRight();
338 },
339 "aria-label": this.props.labels.leftArrow
340 }), isSwipeable ? /*#__PURE__*/_react.default.createElement(_reactEasySwipe.default, {
341 tagName: "ul",
342 className: _cssClasses.default.SLIDER(false, this.state.swiping),
343 onSwipeLeft: this.slideLeft,
344 onSwipeRight: this.slideRight,
345 onSwipeMove: this.onSwipeMove,
346 onSwipeStart: this.onSwipeStart,
347 onSwipeEnd: this.onSwipeEnd,
348 style: itemListStyles,
349 innerRef: this.setItemsListRef,
350 allowMouseEvents: this.props.emulateTouch
351 }, this.renderItems()) : /*#__PURE__*/_react.default.createElement("ul", {
352 className: _cssClasses.default.SLIDER(false, this.state.swiping),
353 ref: function ref(node) {
354 return _this3.setItemsListRef(node);
355 },
356 style: itemListStyles
357 }, this.renderItems()), /*#__PURE__*/_react.default.createElement("button", {
358 type: "button",
359 className: _cssClasses.default.ARROW_NEXT(!hasNext),
360 onClick: function onClick() {
361 return _this3.slideLeft();
362 },
363 "aria-label": this.props.labels.rightArrow
364 })));
365 }
366 }]);
367
368 return Thumbs;
369}(_react.Component);
370
371exports.default = Thumbs;
372
373_defineProperty(Thumbs, "displayName", 'Thumbs');
374
375_defineProperty(Thumbs, "defaultProps", {
376 axis: 'horizontal',
377 labels: {
378 leftArrow: 'previous slide / item',
379 rightArrow: 'next slide / item',
380 item: 'slide item'
381 },
382 selectedItem: 0,
383 thumbWidth: 80,
384 transitionTime: 350
385});
\No newline at end of file