1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports["default"] = ReactSlidySlider;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _react = _interopRequireWildcard(require("react"));
|
15 |
|
16 | var _slidy = _interopRequireDefault(require("./slidy"));
|
17 |
|
18 | var _jsxRuntime = require("react/jsx-runtime");
|
19 |
|
20 | var __jsx = _react["default"].createElement;
|
21 |
|
22 | function noop(_) {}
|
23 |
|
24 | function convertToArrayFrom(children) {
|
25 | return Array.isArray(children) ? children : [children];
|
26 | }
|
27 |
|
28 | function getItemsToRender(_ref) {
|
29 | var index = _ref.index,
|
30 | maxIndex = _ref.maxIndex,
|
31 | items = _ref.items,
|
32 | itemsToPreload = _ref.itemsToPreload,
|
33 | numOfSlides = _ref.numOfSlides;
|
34 | var preload = Math.max(itemsToPreload, numOfSlides);
|
35 |
|
36 | if (index >= items.length - numOfSlides) {
|
37 | var addNewItems = items.length > numOfSlides ? items.slice(0, numOfSlides - 1) : [];
|
38 | return [].concat(items.slice(0, maxIndex + preload), addNewItems);
|
39 | } else {
|
40 | return items.slice(0, maxIndex + preload);
|
41 | }
|
42 | }
|
43 |
|
44 | function destroySlider(slidyInstance, doAfterDestroy) {
|
45 | slidyInstance && slidyInstance.clean() && slidyInstance.destroy();
|
46 | doAfterDestroy();
|
47 | }
|
48 |
|
49 | var renderItem = function renderItem(numOfSlides) {
|
50 | return function (item, index) {
|
51 | var inlineStyle = numOfSlides !== 1 ? {
|
52 | width: 100 / numOfSlides + "%"
|
53 | } : {};
|
54 | return (0, _jsxRuntime.jsx)("li", {
|
55 | style: inlineStyle,
|
56 | children: item
|
57 | }, index);
|
58 | };
|
59 | };
|
60 |
|
61 | function ReactSlidySlider(_ref2) {
|
62 | var ArrowLeft = _ref2.ArrowLeft,
|
63 | ArrowRight = _ref2.ArrowRight,
|
64 | children = _ref2.children,
|
65 | classNameBase = _ref2.classNameBase,
|
66 | doAfterDestroy = _ref2.doAfterDestroy,
|
67 | doAfterInit = _ref2.doAfterInit,
|
68 | doAfterSlide = _ref2.doAfterSlide,
|
69 | doBeforeSlide = _ref2.doBeforeSlide,
|
70 | ease = _ref2.ease,
|
71 | infiniteLoop = _ref2.infiniteLoop,
|
72 | initialSlide = _ref2.initialSlide,
|
73 | itemsToPreload = _ref2.itemsToPreload,
|
74 | keyboardNavigation = _ref2.keyboardNavigation,
|
75 | numOfSlides = _ref2.numOfSlides,
|
76 | showArrows = _ref2.showArrows,
|
77 | slide = _ref2.slide,
|
78 | slideSpeed = _ref2.slideSpeed;
|
79 |
|
80 | var _useState = (0, _react.useState)({
|
81 | goTo: noop,
|
82 | next: noop,
|
83 | prev: noop,
|
84 | updateItems: noop
|
85 | }),
|
86 | slidyInstance = _useState[0],
|
87 | setSlidyInstance = _useState[1];
|
88 |
|
89 | var _useState2 = (0, _react.useState)(initialSlide),
|
90 | index = _useState2[0],
|
91 | setIndex = _useState2[1];
|
92 |
|
93 | var _useState3 = (0, _react.useState)(initialSlide),
|
94 | maxIndex = _useState3[0],
|
95 | setMaxIndex = _useState3[1];
|
96 |
|
97 | var sliderContainerDOMEl = (0, _react.useRef)(null);
|
98 | var slidesDOMEl = (0, _react.useRef)(null);
|
99 | var items = convertToArrayFrom(children);
|
100 | (0, _react.useEffect)(function () {
|
101 | slide !== index && slidyInstance.goTo(slide);
|
102 | }, [slide]
|
103 | );
|
104 | (0, _react.useEffect)(function () {
|
105 | var handleKeyboard;
|
106 | var slidyInstance = (0, _slidy["default"])(sliderContainerDOMEl.current, {
|
107 | ease: ease,
|
108 | doAfterSlide: doAfterSlide,
|
109 | doBeforeSlide: doBeforeSlide,
|
110 | numOfSlides: numOfSlides,
|
111 | slideSpeed: slideSpeed,
|
112 | infiniteLoop: infiniteLoop,
|
113 | slidesDOMEl: slidesDOMEl.current,
|
114 | initialSlide: index,
|
115 | items: items.length,
|
116 | onNext: function onNext(nextIndex) {
|
117 | setIndex(nextIndex);
|
118 | nextIndex > maxIndex && setMaxIndex(nextIndex);
|
119 | return nextIndex;
|
120 | },
|
121 | onPrev: function onPrev(nextIndex) {
|
122 | setIndex(nextIndex);
|
123 | return nextIndex;
|
124 | }
|
125 | });
|
126 | setSlidyInstance(slidyInstance);
|
127 | doAfterInit();
|
128 |
|
129 | if (keyboardNavigation) {
|
130 | handleKeyboard = function handleKeyboard(e) {
|
131 | if (e.keyCode === 39) slidyInstance.next(e);else if (e.keyCode === 37) slidyInstance.prev(e);
|
132 | };
|
133 |
|
134 | document.addEventListener('keydown', handleKeyboard);
|
135 | }
|
136 |
|
137 | return function () {
|
138 | destroySlider(slidyInstance, doAfterDestroy);
|
139 |
|
140 | if (keyboardNavigation) {
|
141 | document.removeEventListener('keydown', handleKeyboard);
|
142 | }
|
143 | };
|
144 | }, []
|
145 | );
|
146 | (0, _react.useEffect)(function () {
|
147 | slidyInstance && slidyInstance.updateItems(items.length);
|
148 | });
|
149 | var itemsToRender = getItemsToRender({
|
150 | index: index,
|
151 | maxIndex: maxIndex,
|
152 | items: items,
|
153 | itemsToPreload: itemsToPreload,
|
154 | numOfSlides: numOfSlides
|
155 | });
|
156 |
|
157 | var handlePrev = function handlePrev(e) {
|
158 | return slidyInstance.prev(e);
|
159 | };
|
160 |
|
161 | var handleNext = function handleNext(e) {
|
162 | return items.length > numOfSlides && slidyInstance.next(e);
|
163 | };
|
164 |
|
165 | var renderLeftArrow = function renderLeftArrow() {
|
166 | var disabled = index === 0 && !infiniteLoop;
|
167 | var props = {
|
168 | disabled: disabled,
|
169 | onClick: handlePrev
|
170 | };
|
171 | var leftArrowClasses = classNameBase + "-arrow " + classNameBase + "-arrowLeft";
|
172 | if (ArrowLeft) return (0, _jsxRuntime.jsx)(ArrowLeft, (0, _extends2["default"])({}, props, {
|
173 | className: leftArrowClasses
|
174 | }));
|
175 | return (0, _jsxRuntime.jsx)("span", (0, _extends2["default"])({
|
176 | "aria-label": "Previous",
|
177 | className: leftArrowClasses + " " + classNameBase + "-prev",
|
178 | role: "button"
|
179 | }, props));
|
180 | };
|
181 |
|
182 | var renderRightArrow = function renderRightArrow() {
|
183 | var disabled = (items.length <= numOfSlides || index === items.length - 1) && !infiniteLoop;
|
184 | var props = {
|
185 | disabled: disabled,
|
186 | onClick: handleNext
|
187 | };
|
188 | var rightArrowClasses = classNameBase + "-arrow " + classNameBase + "-arrowRight";
|
189 | if (ArrowRight) return (0, _jsxRuntime.jsx)(ArrowRight, (0, _extends2["default"])({}, props, {
|
190 | className: rightArrowClasses
|
191 | }));
|
192 | return (0, _jsxRuntime.jsx)("span", (0, _extends2["default"])({
|
193 | "aria-label": "Next",
|
194 | className: rightArrowClasses + " " + classNameBase + "-next",
|
195 | role: "button"
|
196 | }, props));
|
197 | };
|
198 |
|
199 | return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
200 | children: [showArrows && (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
201 | children: [renderLeftArrow(), renderRightArrow()]
|
202 | }), (0, _jsxRuntime.jsx)("div", {
|
203 | ref: sliderContainerDOMEl,
|
204 | children: (0, _jsxRuntime.jsx)("ul", {
|
205 | ref: slidesDOMEl,
|
206 | children: itemsToRender.map(renderItem(numOfSlides))
|
207 | })
|
208 | })]
|
209 | });
|
210 | } |
\ | No newline at end of file |