UNPKG

15.6 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3var _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"];
4import useEventCallback from '@restart/hooks/useEventCallback';
5import useUpdateEffect from '@restart/hooks/useUpdateEffect';
6import useCommittedRef from '@restart/hooks/useCommittedRef';
7import useTimeout from '@restart/hooks/useTimeout';
8import classNames from 'classnames';
9import Transition from 'react-transition-group/Transition';
10import PropTypes from 'prop-types';
11import React, { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
12import { useUncontrolled } from 'uncontrollable';
13import CarouselCaption from './CarouselCaption';
14import CarouselItem from './CarouselItem';
15import { map, forEach } from './ElementChildren';
16import SafeAnchor from './SafeAnchor';
17import { useBootstrapPrefix } from './ThemeProvider';
18import transitionEndListener from './transitionEndListener';
19import triggerBrowserReflow from './triggerBrowserReflow';
20var SWIPE_THRESHOLD = 40;
21var propTypes = {
22 /**
23 * @default 'carousel'
24 */
25 bsPrefix: PropTypes.string,
26 as: PropTypes.elementType,
27
28 /**
29 * Enables animation on the Carousel as it transitions between slides.
30 */
31 slide: PropTypes.bool,
32
33 /** Animates slides with a crossfade animation instead of the default slide animation */
34 fade: PropTypes.bool,
35
36 /**
37 * Show the Carousel previous and next arrows for changing the current slide
38 */
39 controls: PropTypes.bool,
40
41 /**
42 * Show a set of slide position indicators
43 */
44 indicators: PropTypes.bool,
45
46 /**
47 * Controls the current visible slide
48 *
49 * @controllable onSelect
50 */
51 activeIndex: PropTypes.number,
52
53 /**
54 * Callback fired when the active item changes.
55 *
56 * ```js
57 * (eventKey: number, event: Object | null) => void
58 * ```
59 *
60 * @controllable activeIndex
61 */
62 onSelect: PropTypes.func,
63
64 /**
65 * Callback fired when a slide transition starts.
66 *
67 * ```js
68 * (eventKey: number, direction: 'left' | 'right') => void
69 */
70 onSlide: PropTypes.func,
71
72 /**
73 * Callback fired when a slide transition ends.
74 *
75 * ```js
76 * (eventKey: number, direction: 'left' | 'right') => void
77 */
78 onSlid: PropTypes.func,
79
80 /**
81 * The amount of time to delay between automatically cycling an item. If `null`, carousel will not automatically cycle.
82 */
83 interval: PropTypes.number,
84
85 /** Whether the carousel should react to keyboard events. */
86 keyboard: PropTypes.bool,
87
88 /**
89 * If set to `"hover"`, pauses the cycling of the carousel on `mouseenter` and resumes the cycling of the carousel on `mouseleave`. If set to `false`, hovering over the carousel won't pause it.
90 *
91 * On touch-enabled devices, when set to `"hover"`, cycling will pause on `touchend` (once the user finished interacting with the carousel) for two intervals, before automatically resuming. Note that this is in addition to the above mouse behavior.
92 */
93 pause: PropTypes.oneOf(['hover', false]),
94
95 /** Whether the carousel should cycle continuously or have hard stops. */
96 wrap: PropTypes.bool,
97
98 /**
99 * Whether the carousel should support left/right swipe interactions on touchscreen devices.
100 */
101 touch: PropTypes.bool,
102
103 /** Override the default button icon for the "previous" control */
104 prevIcon: PropTypes.node,
105
106 /**
107 * Label shown to screen readers only, can be used to show the previous element
108 * in the carousel.
109 * Set to null to deactivate.
110 */
111 prevLabel: PropTypes.string,
112
113 /** Override the default button icon for the "next" control */
114 nextIcon: PropTypes.node,
115
116 /**
117 * Label shown to screen readers only, can be used to show the next element
118 * in the carousel.
119 * Set to null to deactivate.
120 */
121 nextLabel: PropTypes.string
122};
123var 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: /*#__PURE__*/React.createElement("span", {
135 "aria-hidden": "true",
136 className: "carousel-control-prev-icon"
137 }),
138 prevLabel: 'Previous',
139 nextIcon: /*#__PURE__*/React.createElement("span", {
140 "aria-hidden": "true",
141 className: "carousel-control-next-icon"
142 }),
143 nextLabel: 'Next'
144};
145
146function 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
155function 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; // Iterate to grab all of the children's interval values
229 // (and count them, too)
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]); // This is used in the setInterval, so it should not invalidate.
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 }); // This is used in the setInterval, so it should not invalidate.
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 // These callbacks will be handled by the <Transition> callbacks.
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 /*#__PURE__*/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 && /*#__PURE__*/React.createElement("ol", {
451 className: prefix + "-indicators"
452 }, map(children, function (_child, index) {
453 return /*#__PURE__*/React.createElement("li", {
454 key: index,
455 className: index === renderedActiveIndex ? 'active' : undefined,
456 onClick: indicatorOnClicks ? indicatorOnClicks[index] : undefined
457 });
458 })), /*#__PURE__*/React.createElement("div", {
459 className: prefix + "-inner"
460 }, map(children, function (child, index) {
461 var isActive = index === renderedActiveIndex;
462 return slide ? /*#__PURE__*/React.createElement(Transition, {
463 in: isActive,
464 onEnter: isActive ? handleEnter : undefined,
465 onEntered: isActive ? handleEntered : undefined,
466 addEndListener: transitionEndListener
467 }, function (status) {
468 return /*#__PURE__*/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 }) : /*#__PURE__*/React.cloneElement(child, {
472 className: classNames(child.props.className, isActive && 'active')
473 });
474 })), controls && /*#__PURE__*/React.createElement(React.Fragment, null, (wrap || activeIndex !== 0) && /*#__PURE__*/React.createElement(SafeAnchor, {
475 className: prefix + "-control-prev",
476 onClick: prev
477 }, prevIcon, prevLabel && /*#__PURE__*/React.createElement("span", {
478 className: "sr-only"
479 }, prevLabel)), (wrap || activeIndex !== numChildren - 1) && /*#__PURE__*/React.createElement(SafeAnchor, {
480 className: prefix + "-control-next",
481 onClick: next
482 }, nextIcon, nextLabel && /*#__PURE__*/React.createElement("span", {
483 className: "sr-only"
484 }, nextLabel))));
485}
486
487var Carousel = /*#__PURE__*/React.forwardRef(CarouselFunc);
488Carousel.displayName = 'Carousel';
489Carousel.propTypes = propTypes;
490Carousel.defaultProps = defaultProps;
491Carousel.Caption = CarouselCaption;
492Carousel.Item = CarouselItem;
493export default Carousel;
\No newline at end of file