1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | var _excluded = ["as", "bsPrefix", "slide", "fade", "controls", "indicators", "activeIndex", "onSelect", "onSlide", "onSlid", "interval", "keyboard", "onKeyDown", "pause", "onMouseOver", "onMouseOut", "wrap", "touch", "onTouchStart", "onTouchMove", "onTouchEnd", "prevIcon", "prevLabel", "nextIcon", "nextLabel", "className", "children"];
|
4 | import useEventCallback from '@restart/hooks/useEventCallback';
|
5 | import useUpdateEffect from '@restart/hooks/useUpdateEffect';
|
6 | import useCommittedRef from '@restart/hooks/useCommittedRef';
|
7 | import useTimeout from '@restart/hooks/useTimeout';
|
8 | import classNames from 'classnames';
|
9 | import Transition from 'react-transition-group/Transition';
|
10 | import PropTypes from 'prop-types';
|
11 | import React, { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
12 | import { useUncontrolled } from 'uncontrollable';
|
13 | import CarouselCaption from './CarouselCaption';
|
14 | import CarouselItem from './CarouselItem';
|
15 | import { map, forEach } from './ElementChildren';
|
16 | import SafeAnchor from './SafeAnchor';
|
17 | import { useBootstrapPrefix } from './ThemeProvider';
|
18 | import transitionEndListener from './transitionEndListener';
|
19 | import triggerBrowserReflow from './triggerBrowserReflow';
|
20 | var SWIPE_THRESHOLD = 40;
|
21 | var propTypes = {
|
22 | |
23 |
|
24 |
|
25 | bsPrefix: PropTypes.string,
|
26 | as: PropTypes.elementType,
|
27 |
|
28 | |
29 |
|
30 |
|
31 | slide: PropTypes.bool,
|
32 |
|
33 |
|
34 | fade: PropTypes.bool,
|
35 |
|
36 | |
37 |
|
38 |
|
39 | controls: PropTypes.bool,
|
40 |
|
41 | |
42 |
|
43 |
|
44 | indicators: PropTypes.bool,
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | activeIndex: PropTypes.number,
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | onSelect: PropTypes.func,
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | onSlide: PropTypes.func,
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | onSlid: PropTypes.func,
|
79 |
|
80 | |
81 |
|
82 |
|
83 | interval: PropTypes.number,
|
84 |
|
85 |
|
86 | keyboard: PropTypes.bool,
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | pause: PropTypes.oneOf(['hover', false]),
|
94 |
|
95 |
|
96 | wrap: PropTypes.bool,
|
97 |
|
98 | |
99 |
|
100 |
|
101 | touch: PropTypes.bool,
|
102 |
|
103 |
|
104 | prevIcon: PropTypes.node,
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 | prevLabel: PropTypes.string,
|
112 |
|
113 |
|
114 | nextIcon: PropTypes.node,
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 | nextLabel: PropTypes.string
|
122 | };
|
123 | var defaultProps = {
|
124 | slide: true,
|
125 | fade: false,
|
126 | controls: true,
|
127 | indicators: true,
|
128 | defaultActiveIndex: 0,
|
129 | interval: 5000,
|
130 | keyboard: true,
|
131 | pause: 'hover',
|
132 | wrap: true,
|
133 | touch: true,
|
134 | prevIcon: React.createElement("span", {
|
135 | "aria-hidden": "true",
|
136 | className: "carousel-control-prev-icon"
|
137 | }),
|
138 | prevLabel: 'Previous',
|
139 | nextIcon: React.createElement("span", {
|
140 | "aria-hidden": "true",
|
141 | className: "carousel-control-next-icon"
|
142 | }),
|
143 | nextLabel: 'Next'
|
144 | };
|
145 |
|
146 | function isVisible(element) {
|
147 | if (!element || !element.style || !element.parentNode || !element.parentNode.style) {
|
148 | return false;
|
149 | }
|
150 |
|
151 | var elementStyle = getComputedStyle(element);
|
152 | return elementStyle.display !== 'none' && elementStyle.visibility !== 'hidden' && getComputedStyle(element.parentNode).display !== 'none';
|
153 | }
|
154 |
|
155 | function CarouselFunc(uncontrolledProps, ref) {
|
156 | var _useUncontrolled = useUncontrolled(uncontrolledProps, {
|
157 | activeIndex: 'onSelect'
|
158 | }),
|
159 | _useUncontrolled$as = _useUncontrolled.as,
|
160 | Component = _useUncontrolled$as === void 0 ? 'div' : _useUncontrolled$as,
|
161 | bsPrefix = _useUncontrolled.bsPrefix,
|
162 | slide = _useUncontrolled.slide,
|
163 | fade = _useUncontrolled.fade,
|
164 | controls = _useUncontrolled.controls,
|
165 | indicators = _useUncontrolled.indicators,
|
166 | activeIndex = _useUncontrolled.activeIndex,
|
167 | onSelect = _useUncontrolled.onSelect,
|
168 | onSlide = _useUncontrolled.onSlide,
|
169 | onSlid = _useUncontrolled.onSlid,
|
170 | interval = _useUncontrolled.interval,
|
171 | keyboard = _useUncontrolled.keyboard,
|
172 | onKeyDown = _useUncontrolled.onKeyDown,
|
173 | pause = _useUncontrolled.pause,
|
174 | onMouseOver = _useUncontrolled.onMouseOver,
|
175 | onMouseOut = _useUncontrolled.onMouseOut,
|
176 | wrap = _useUncontrolled.wrap,
|
177 | touch = _useUncontrolled.touch,
|
178 | onTouchStart = _useUncontrolled.onTouchStart,
|
179 | onTouchMove = _useUncontrolled.onTouchMove,
|
180 | onTouchEnd = _useUncontrolled.onTouchEnd,
|
181 | prevIcon = _useUncontrolled.prevIcon,
|
182 | prevLabel = _useUncontrolled.prevLabel,
|
183 | nextIcon = _useUncontrolled.nextIcon,
|
184 | nextLabel = _useUncontrolled.nextLabel,
|
185 | className = _useUncontrolled.className,
|
186 | children = _useUncontrolled.children,
|
187 | props = _objectWithoutPropertiesLoose(_useUncontrolled, _excluded);
|
188 |
|
189 | var prefix = useBootstrapPrefix(bsPrefix, 'carousel');
|
190 | var nextDirectionRef = useRef(null);
|
191 |
|
192 | var _useState = useState('next'),
|
193 | direction = _useState[0],
|
194 | setDirection = _useState[1];
|
195 |
|
196 | var _useState2 = useState(false),
|
197 | paused = _useState2[0],
|
198 | setPaused = _useState2[1];
|
199 |
|
200 | var _useState3 = useState(false),
|
201 | isSliding = _useState3[0],
|
202 | setIsSliding = _useState3[1];
|
203 |
|
204 | var _useState4 = useState(activeIndex || 0),
|
205 | renderedActiveIndex = _useState4[0],
|
206 | setRenderedActiveIndex = _useState4[1];
|
207 |
|
208 | if (!isSliding && activeIndex !== renderedActiveIndex) {
|
209 | if (nextDirectionRef.current) {
|
210 | setDirection(nextDirectionRef.current);
|
211 | } else {
|
212 | setDirection((activeIndex || 0) > renderedActiveIndex ? 'next' : 'prev');
|
213 | }
|
214 |
|
215 | if (slide) {
|
216 | setIsSliding(true);
|
217 | }
|
218 |
|
219 | setRenderedActiveIndex(activeIndex || 0);
|
220 | }
|
221 |
|
222 | useEffect(function () {
|
223 | if (nextDirectionRef.current) {
|
224 | nextDirectionRef.current = null;
|
225 | }
|
226 | });
|
227 | var numChildren = 0;
|
228 | var activeChildInterval;
|
229 |
|
230 |
|
231 | forEach(children, function (child, index) {
|
232 | ++numChildren;
|
233 |
|
234 | if (index === activeIndex) {
|
235 | activeChildInterval = child.props.interval;
|
236 | }
|
237 | });
|
238 | var activeChildIntervalRef = useCommittedRef(activeChildInterval);
|
239 | var prev = useCallback(function (event) {
|
240 | if (isSliding) {
|
241 | return;
|
242 | }
|
243 |
|
244 | var nextActiveIndex = renderedActiveIndex - 1;
|
245 |
|
246 | if (nextActiveIndex < 0) {
|
247 | if (!wrap) {
|
248 | return;
|
249 | }
|
250 |
|
251 | nextActiveIndex = numChildren - 1;
|
252 | }
|
253 |
|
254 | nextDirectionRef.current = 'prev';
|
255 |
|
256 | if (onSelect) {
|
257 | onSelect(nextActiveIndex, event);
|
258 | }
|
259 | }, [isSliding, renderedActiveIndex, onSelect, wrap, numChildren]);
|
260 |
|
261 | var next = useEventCallback(function (event) {
|
262 | if (isSliding) {
|
263 | return;
|
264 | }
|
265 |
|
266 | var nextActiveIndex = renderedActiveIndex + 1;
|
267 |
|
268 | if (nextActiveIndex >= numChildren) {
|
269 | if (!wrap) {
|
270 | return;
|
271 | }
|
272 |
|
273 | nextActiveIndex = 0;
|
274 | }
|
275 |
|
276 | nextDirectionRef.current = 'next';
|
277 |
|
278 | if (onSelect) {
|
279 | onSelect(nextActiveIndex, event);
|
280 | }
|
281 | });
|
282 | var elementRef = useRef();
|
283 | useImperativeHandle(ref, function () {
|
284 | return {
|
285 | element: elementRef.current,
|
286 | prev: prev,
|
287 | next: next
|
288 | };
|
289 | });
|
290 |
|
291 | var nextWhenVisible = useEventCallback(function () {
|
292 | if (!document.hidden && isVisible(elementRef.current)) {
|
293 | next();
|
294 | }
|
295 | });
|
296 | var slideDirection = direction === 'next' ? 'left' : 'right';
|
297 | useUpdateEffect(function () {
|
298 | if (slide) {
|
299 |
|
300 | return;
|
301 | }
|
302 |
|
303 | if (onSlide) {
|
304 | onSlide(renderedActiveIndex, slideDirection);
|
305 | }
|
306 |
|
307 | if (onSlid) {
|
308 | onSlid(renderedActiveIndex, slideDirection);
|
309 | }
|
310 | }, [renderedActiveIndex]);
|
311 | var orderClassName = prefix + "-item-" + direction;
|
312 | var directionalClassName = prefix + "-item-" + slideDirection;
|
313 | var handleEnter = useCallback(function (node) {
|
314 | triggerBrowserReflow(node);
|
315 |
|
316 | if (onSlide) {
|
317 | onSlide(renderedActiveIndex, slideDirection);
|
318 | }
|
319 | }, [onSlide, renderedActiveIndex, slideDirection]);
|
320 | var handleEntered = useCallback(function () {
|
321 | setIsSliding(false);
|
322 |
|
323 | if (onSlid) {
|
324 | onSlid(renderedActiveIndex, slideDirection);
|
325 | }
|
326 | }, [onSlid, renderedActiveIndex, slideDirection]);
|
327 | var handleKeyDown = useCallback(function (event) {
|
328 | if (keyboard && !/input|textarea/i.test(event.target.tagName)) {
|
329 | switch (event.key) {
|
330 | case 'ArrowLeft':
|
331 | event.preventDefault();
|
332 | prev(event);
|
333 | return;
|
334 |
|
335 | case 'ArrowRight':
|
336 | event.preventDefault();
|
337 | next(event);
|
338 | return;
|
339 |
|
340 | default:
|
341 | }
|
342 | }
|
343 |
|
344 | if (onKeyDown) {
|
345 | onKeyDown(event);
|
346 | }
|
347 | }, [keyboard, onKeyDown, prev, next]);
|
348 | var handleMouseOver = useCallback(function (event) {
|
349 | if (pause === 'hover') {
|
350 | setPaused(true);
|
351 | }
|
352 |
|
353 | if (onMouseOver) {
|
354 | onMouseOver(event);
|
355 | }
|
356 | }, [pause, onMouseOver]);
|
357 | var handleMouseOut = useCallback(function (event) {
|
358 | setPaused(false);
|
359 |
|
360 | if (onMouseOut) {
|
361 | onMouseOut(event);
|
362 | }
|
363 | }, [onMouseOut]);
|
364 | var touchStartXRef = useRef(0);
|
365 | var touchDeltaXRef = useRef(0);
|
366 | var touchUnpauseTimeout = useTimeout();
|
367 | var handleTouchStart = useCallback(function (event) {
|
368 | touchStartXRef.current = event.touches[0].clientX;
|
369 | touchDeltaXRef.current = 0;
|
370 |
|
371 | if (pause === 'hover') {
|
372 | setPaused(true);
|
373 | }
|
374 |
|
375 | if (onTouchStart) {
|
376 | onTouchStart(event);
|
377 | }
|
378 | }, [pause, onTouchStart]);
|
379 | var handleTouchMove = useCallback(function (event) {
|
380 | if (event.touches && event.touches.length > 1) {
|
381 | touchDeltaXRef.current = 0;
|
382 | } else {
|
383 | touchDeltaXRef.current = event.touches[0].clientX - touchStartXRef.current;
|
384 | }
|
385 |
|
386 | if (onTouchMove) {
|
387 | onTouchMove(event);
|
388 | }
|
389 | }, [onTouchMove]);
|
390 | var handleTouchEnd = useCallback(function (event) {
|
391 | if (touch) {
|
392 | var touchDeltaX = touchDeltaXRef.current;
|
393 |
|
394 | if (Math.abs(touchDeltaX) > SWIPE_THRESHOLD) {
|
395 | if (touchDeltaX > 0) {
|
396 | prev(event);
|
397 | } else {
|
398 | next(event);
|
399 | }
|
400 | }
|
401 | }
|
402 |
|
403 | if (pause === 'hover') {
|
404 | touchUnpauseTimeout.set(function () {
|
405 | setPaused(false);
|
406 | }, interval || undefined);
|
407 | }
|
408 |
|
409 | if (onTouchEnd) {
|
410 | onTouchEnd(event);
|
411 | }
|
412 | }, [touch, pause, prev, next, touchUnpauseTimeout, interval, onTouchEnd]);
|
413 | var shouldPlay = interval != null && !paused && !isSliding;
|
414 | var intervalHandleRef = useRef();
|
415 | useEffect(function () {
|
416 | var _ref, _activeChildIntervalR;
|
417 |
|
418 | if (!shouldPlay) {
|
419 | return undefined;
|
420 | }
|
421 |
|
422 | intervalHandleRef.current = window.setInterval(document.visibilityState ? nextWhenVisible : next, (_ref = (_activeChildIntervalR = activeChildIntervalRef.current) != null ? _activeChildIntervalR : interval) != null ? _ref : undefined);
|
423 | return function () {
|
424 | if (intervalHandleRef.current !== null) {
|
425 | clearInterval(intervalHandleRef.current);
|
426 | }
|
427 | };
|
428 | }, [shouldPlay, next, activeChildIntervalRef, interval, nextWhenVisible]);
|
429 | var indicatorOnClicks = useMemo(function () {
|
430 | return indicators && Array.from({
|
431 | length: numChildren
|
432 | }, function (_, index) {
|
433 | return function (event) {
|
434 | if (onSelect) {
|
435 | onSelect(index, event);
|
436 | }
|
437 | };
|
438 | });
|
439 | }, [indicators, numChildren, onSelect]);
|
440 | return React.createElement(Component, _extends({
|
441 | ref: elementRef
|
442 | }, props, {
|
443 | onKeyDown: handleKeyDown,
|
444 | onMouseOver: handleMouseOver,
|
445 | onMouseOut: handleMouseOut,
|
446 | onTouchStart: handleTouchStart,
|
447 | onTouchMove: handleTouchMove,
|
448 | onTouchEnd: handleTouchEnd,
|
449 | className: classNames(className, prefix, slide && 'slide', fade && prefix + "-fade")
|
450 | }), indicators && React.createElement("ol", {
|
451 | className: prefix + "-indicators"
|
452 | }, map(children, function (_child, index) {
|
453 | return React.createElement("li", {
|
454 | key: index,
|
455 | className: index === renderedActiveIndex ? 'active' : undefined,
|
456 | onClick: indicatorOnClicks ? indicatorOnClicks[index] : undefined
|
457 | });
|
458 | })), React.createElement("div", {
|
459 | className: prefix + "-inner"
|
460 | }, map(children, function (child, index) {
|
461 | var isActive = index === renderedActiveIndex;
|
462 | return slide ? React.createElement(Transition, {
|
463 | in: isActive,
|
464 | onEnter: isActive ? handleEnter : undefined,
|
465 | onEntered: isActive ? handleEntered : undefined,
|
466 | addEndListener: transitionEndListener
|
467 | }, function (status) {
|
468 | return React.cloneElement(child, {
|
469 | className: classNames(child.props.className, isActive && status !== 'entered' && orderClassName, (status === 'entered' || status === 'exiting') && 'active', (status === 'entering' || status === 'exiting') && directionalClassName)
|
470 | });
|
471 | }) : React.cloneElement(child, {
|
472 | className: classNames(child.props.className, isActive && 'active')
|
473 | });
|
474 | })), controls && React.createElement(React.Fragment, null, (wrap || activeIndex !== 0) && React.createElement(SafeAnchor, {
|
475 | className: prefix + "-control-prev",
|
476 | onClick: prev
|
477 | }, prevIcon, prevLabel && React.createElement("span", {
|
478 | className: "sr-only"
|
479 | }, prevLabel)), (wrap || activeIndex !== numChildren - 1) && React.createElement(SafeAnchor, {
|
480 | className: prefix + "-control-next",
|
481 | onClick: next
|
482 | }, nextIcon, nextLabel && React.createElement("span", {
|
483 | className: "sr-only"
|
484 | }, nextLabel))));
|
485 | }
|
486 |
|
487 | var Carousel = React.forwardRef(CarouselFunc);
|
488 | Carousel.displayName = 'Carousel';
|
489 | Carousel.propTypes = propTypes;
|
490 | Carousel.defaultProps = defaultProps;
|
491 | Carousel.Caption = CarouselCaption;
|
492 | Carousel.Item = CarouselItem;
|
493 | export default Carousel; |
\ | No newline at end of file |