UNPKG

5.99 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard3 = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports["default"] = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _interopRequireWildcard2 = _interopRequireDefault(require("@babel/runtime/helpers/interopRequireWildcard"));
15
16var _react = _interopRequireWildcard3(require("react"));
17
18var _reactSlidySlider = _interopRequireDefault(require("./react-slidy-slider"));
19
20var _jsxRuntime = require("react/jsx-runtime");
21
22var __jsx = _react["default"].createElement;
23
24function noop() {}
25
26var CLASSNAMES = {
27 contain: 'react-Slidy--contain',
28 cover: 'react-Slidy--cover',
29 fullHeight: 'react-Slidy--fullHeight',
30 fullWidth: 'react-Slidy--fullWidth'
31};
32
33var ReactSlidy = function ReactSlidy(_ref) {
34 var ArrowLeft = _ref.ArrowLeft,
35 ArrowRight = _ref.ArrowRight,
36 children = _ref.children,
37 _ref$classNameBase = _ref.classNameBase,
38 classNameBase = _ref$classNameBase === void 0 ? 'react-Slidy' : _ref$classNameBase,
39 _ref$doAfterDestroy = _ref.doAfterDestroy,
40 doAfterDestroy = _ref$doAfterDestroy === void 0 ? noop : _ref$doAfterDestroy,
41 _ref$doAfterInit = _ref.doAfterInit,
42 doAfterInit = _ref$doAfterInit === void 0 ? noop : _ref$doAfterInit,
43 _ref$doAfterSlide = _ref.doAfterSlide,
44 doAfterSlide = _ref$doAfterSlide === void 0 ? noop : _ref$doAfterSlide,
45 _ref$doBeforeSlide = _ref.doBeforeSlide,
46 doBeforeSlide = _ref$doBeforeSlide === void 0 ? noop : _ref$doBeforeSlide,
47 imageObjectFit = _ref.imageObjectFit,
48 _ref$infiniteLoop = _ref.infiniteLoop,
49 infiniteLoop = _ref$infiniteLoop === void 0 ? false : _ref$infiniteLoop,
50 _ref$itemsToPreload = _ref.itemsToPreload,
51 itemsToPreload = _ref$itemsToPreload === void 0 ? 1 : _ref$itemsToPreload,
52 _ref$initialSlide = _ref.initialSlide,
53 initialSlide = _ref$initialSlide === void 0 ? 0 : _ref$initialSlide,
54 _ref$ease = _ref.ease,
55 ease = _ref$ease === void 0 ? 'ease' : _ref$ease,
56 _ref$lazyLoadSlider = _ref.lazyLoadSlider,
57 lazyLoadSlider = _ref$lazyLoadSlider === void 0 ? true : _ref$lazyLoadSlider,
58 _ref$lazyLoadConfig = _ref.lazyLoadConfig,
59 lazyLoadConfig = _ref$lazyLoadConfig === void 0 ? {
60 offset: 150
61 } : _ref$lazyLoadConfig,
62 _ref$keyboardNavigati = _ref.keyboardNavigation,
63 keyboardNavigation = _ref$keyboardNavigati === void 0 ? false : _ref$keyboardNavigati,
64 _ref$numOfSlides = _ref.numOfSlides,
65 numOfSlides = _ref$numOfSlides === void 0 ? 1 : _ref$numOfSlides,
66 _ref$sanitize = _ref.sanitize,
67 sanitize = _ref$sanitize === void 0 ? true : _ref$sanitize,
68 _ref$slide = _ref.slide,
69 slide = _ref$slide === void 0 ? 0 : _ref$slide,
70 _ref$slideSpeed = _ref.slideSpeed,
71 slideSpeed = _ref$slideSpeed === void 0 ? 500 : _ref$slideSpeed,
72 _ref$showArrows = _ref.showArrows,
73 showArrows = _ref$showArrows === void 0 ? true : _ref$showArrows,
74 _ref$useFullHeight = _ref.useFullHeight,
75 useFullHeight = _ref$useFullHeight === void 0 ? false : _ref$useFullHeight,
76 _ref$useFullWidth = _ref.useFullWidth,
77 useFullWidth = _ref$useFullWidth === void 0 ? true : _ref$useFullWidth;
78
79 var _useState = (0, _react.useState)(!lazyLoadSlider),
80 showSlider = _useState[0],
81 setShowSlider = _useState[1];
82
83 var nodeEl = (0, _react.useRef)(null);
84 (0, _react.useEffect)(function () {
85 var observer;
86
87 if (lazyLoadSlider) {
88 var initLazyLoadSlider = function initLazyLoadSlider() {
89 // if we support IntersectionObserver, let's use it
90 var _lazyLoadConfig$offse = lazyLoadConfig.offset,
91 offset = _lazyLoadConfig$offse === void 0 ? 0 : _lazyLoadConfig$offse;
92 observer = new window.IntersectionObserver(handleIntersection, {
93 rootMargin: offset + "px 0px 0px"
94 });
95 observer.observe(nodeEl.current);
96 };
97
98 if (!('IntersectionObserver' in window)) {
99 Promise.resolve().then(function () {
100 return (0, _interopRequireWildcard2["default"])(require('intersection-observer'));
101 }).then(initLazyLoadSlider);
102 } else {
103 initLazyLoadSlider();
104 }
105 }
106
107 return function () {
108 return observer && observer.disconnect();
109 };
110 }, [] // eslint-disable-line
111 );
112
113 var handleIntersection = function handleIntersection(_ref2, observer) {
114 var entry = _ref2[0];
115
116 if (entry.isIntersecting || entry.intersectionRatio > 0) {
117 observer.unobserve(entry.target);
118 setShowSlider(true);
119 }
120 };
121
122 var numOfSlidesSanitzed = Math.min(numOfSlides, _react["default"].Children.count(children));
123 var rootClassName = [classNameBase, useFullHeight && CLASSNAMES.fullHeight, useFullWidth && CLASSNAMES.fullWidth, imageObjectFit && CLASSNAMES[imageObjectFit]].filter(Boolean).join(' ');
124 var reactSlidySliderProps = {
125 ArrowLeft: ArrowLeft,
126 ArrowRight: ArrowRight,
127 children: children,
128 classNameBase: classNameBase,
129 doAfterDestroy: doAfterDestroy,
130 doAfterInit: doAfterInit,
131 doAfterSlide: doAfterSlide,
132 doBeforeSlide: doBeforeSlide,
133 ease: ease,
134 infiniteLoop: infiniteLoop,
135 initialSlide: initialSlide,
136 itemsToPreload: itemsToPreload,
137 keyboardNavigation: keyboardNavigation,
138 numOfSlides: numOfSlides,
139 showArrows: showArrows,
140 slide: slide,
141 slideSpeed: slideSpeed
142 };
143 return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
144 className: rootClassName,
145 ref: nodeEl,
146 children: showSlider && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSlidySlider["default"], (0, _extends2["default"])({}, reactSlidySliderProps, {
147 numOfSlides: sanitize ? numOfSlidesSanitzed : numOfSlides,
148 children: children
149 }))
150 });
151};
152
153var _default = ReactSlidy;
154exports["default"] = _default;
\No newline at end of file