@kolserdav/swiper 2.2.2 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,101 +1,106 @@
1
- # swiper
2
-
3
- > Simple and powerfull dynamic react swiper component
4
-
5
- [![NPM](https://img.shields.io/npm/v/swiper.svg)](https://www.npmjs.com/package/swiper) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
-
7
- ## Install
8
-
9
- ```bash
10
- npm install --save @kolserdav/swiper
11
- ```
12
-
13
- ## Usage
14
-
15
- ```tsx
16
- import React, { useState, useEffect } from 'react';
17
- import { Swiper, GetSwipeHandler, Swipe } from 'swiper';
18
- import 'swiper/dist/index.css';
19
-
20
- const getNext: GetSwipeHandler = async (old) => {
21
- const id = old + 1;
22
- return {
23
- id: id < 7 ? id : null,
24
- children: id < 7 ? <h1>Test {id}</h1> : <div></div>,
25
- };
26
- };
27
-
28
- const getPrevios: GetSwipeHandler = async (old) => {
29
- const id = old - 1;
30
- return {
31
- id: id > 0 ? id : null,
32
- children: id > 0 ? <h1>Test {id}</h1> : <div></div>,
33
- };
34
- };
35
-
36
- const App = (): React.ReactElement => {
37
- const [current, setCurrent] = useState<Swipe>();
38
-
39
- useEffect(() => {
40
- if (!current) {
41
- (async () => {
42
- setCurrent(await getNext(1));
43
- })();
44
- }
45
- }, []);
46
- return (
47
- <div>
48
- {current && (
49
- <Swiper
50
- defaultCurrent={current}
51
- getNext={getNext}
52
- getPrev={getPrevios}
53
- invitationAnimation={true}
54
- />
55
- )}
56
- </div>
57
- );
58
- };
59
-
60
- export default App;
61
- ```
62
-
63
- ## Properties
64
-
65
- ```tsx
66
- interface SwiperProps {
67
- /**
68
- * Current card content
69
- */
70
- defaultCurrent: Swipe;
71
-
72
- /**
73
- * Get next card handler
74
- */
75
- getNext: GetSwipeHandler;
76
-
77
- /**
78
- * Get previous card handler
79
- */
80
- getPrev: GetSwipeHandler;
81
-
82
- /**
83
- * Class name for content block
84
- */
85
- className?: string;
86
-
87
- /**
88
- * invitation animation
89
- */
90
- invitationAnimation?: boolean;
91
-
92
- /**
93
- * On swipe callback
94
- */
95
- onSwipe?: (currentId: number) => void;
96
- }
97
- ```
98
-
99
- ## License
100
-
101
- MIT © [kolserdav](https://github.com/kolserdav)
1
+ # swiper
2
+
3
+ > Simple and powerfull dynamic react swiper component
4
+
5
+ [![NPM](https://img.shields.io/npm/v/swiper.svg)](https://www.npmjs.com/package/swiper) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install --save @kolserdav/swiper
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```tsx
16
+ import React, { useState, useEffect } from 'react';
17
+ import { Swiper, GetSwipeHandler, Swipe } from '@kolserdav/swiper';
18
+
19
+ const COUNT = 4;
20
+
21
+ const getNext: GetSwipeHandler = async (old) => {
22
+ let id = old + 1;
23
+ id = id <= COUNT ? id : 0;
24
+ return {
25
+ id,
26
+ children: <h1>Test {id}</h1>,
27
+ };
28
+ };
29
+
30
+ const getPrevios: GetSwipeHandler = async (old) => {
31
+ let id = old - 1;
32
+ id = id >= 0 ? id : COUNT;
33
+ return {
34
+ id,
35
+ children: <h1>Test {id}</h1>,
36
+ };
37
+ };
38
+
39
+ const App = (): React.ReactElement => {
40
+ const [current, setCurrent] = useState<Swipe>();
41
+ useEffect(() => {
42
+ if (!current) {
43
+ (async () => {
44
+ setCurrent(await getNext(0));
45
+ })();
46
+ }
47
+ }, []);
48
+
49
+ return (
50
+ <div>
51
+ {current && (
52
+ <Swiper
53
+ defaultCurrent={current}
54
+ getNext={getNext}
55
+ getPrev={getPrevios}
56
+ invitationAnimation={true}
57
+ durationAnimation={1000}
58
+ />
59
+ )}
60
+ </div>
61
+ );
62
+ };
63
+
64
+ export default App;
65
+
66
+ ```
67
+
68
+ ## Properties
69
+
70
+ ```tsx
71
+ interface SwiperProps {
72
+ /**
73
+ * Current card content
74
+ */
75
+ defaultCurrent: Swipe;
76
+
77
+ /**
78
+ * Get next card handler
79
+ */
80
+ getNext: GetSwipeHandler;
81
+
82
+ /**
83
+ * Get previous card handler
84
+ */
85
+ getPrev: GetSwipeHandler;
86
+
87
+ /**
88
+ * Class name for content block
89
+ */
90
+ className?: string;
91
+
92
+ /**
93
+ * invitation animation
94
+ */
95
+ invitationAnimation?: boolean;
96
+
97
+ /**
98
+ * On swipe callback
99
+ */
100
+ onSwipe?: (currentId: number) => void;
101
+ }
102
+ ```
103
+
104
+ ## License
105
+
106
+ MIT © [kolserdav](https://github.com/kolserdav)
package/dist/index.d.ts CHANGED
@@ -52,7 +52,11 @@ interface SwiperProps {
52
52
  /**
53
53
  * On swipe callback
54
54
  */
55
- onSwipe?: (currentId: number) => void;
55
+ onSwipe?: (currentId: number | null | undefined) => void;
56
+ /**
57
+ * Auto slide if provieded
58
+ */
59
+ durationAnimation?: number;
56
60
  }
57
61
  /**
58
62
  * Swiper component
package/dist/index.js CHANGED
@@ -300,7 +300,8 @@ var Swiper = function Swiper(props) {
300
300
  getPrev = props.getPrev,
301
301
  onSwipe = props.onSwipe,
302
302
  className = props.className,
303
- invitationAnimation = props.invitationAnimation;
303
+ invitationAnimation = props.invitationAnimation,
304
+ durationAnimation = props.durationAnimation;
304
305
 
305
306
  var _useStyles = useStyles(),
306
307
  go = _useStyles.go,
@@ -452,7 +453,7 @@ var Swiper = function Swiper(props) {
452
453
  });
453
454
  };
454
455
 
455
- if (!(next !== null && next !== void 0 && next.id)) {
456
+ if ((next === null || next === void 0 ? void 0 : next.id) === null) {
456
457
  setBackClassHandler();
457
458
  setLeft(0);
458
459
  setLoad(false);
@@ -498,7 +499,7 @@ var Swiper = function Swiper(props) {
498
499
  });
499
500
  };
500
501
 
501
- if (!(prev !== null && prev !== void 0 && prev.id)) {
502
+ if ((prev === null || prev === void 0 ? void 0 : prev.id) === null) {
502
503
  setBackClassHandler();
503
504
  setLeft(0);
504
505
  setLoad(false);
@@ -506,7 +507,7 @@ var Swiper = function Swiper(props) {
506
507
  return 1;
507
508
  }
508
509
 
509
- currentId = prev.id;
510
+ currentId = prev === null || prev === void 0 ? void 0 : prev.id;
510
511
 
511
512
  if (onSwipe !== undefined) {
512
513
  onSwipe(currentId);
@@ -671,6 +672,14 @@ var Swiper = function Swiper(props) {
671
672
  React.useEffect(function () {
672
673
  var _containerRef$current10, _containerRef$current11, _containerRef$current12, _containerRef$current13, _containerRef$current14, _containerRef$current15, _containerRef$current16, _containerRef$current17, _containerRef$current18, _current$nextRef, _current$closeRef;
673
674
 
675
+ var clearAnimate;
676
+
677
+ if (durationAnimation) {
678
+ clearAnimate = setInterval(function () {
679
+ clickNextHandler();
680
+ }, durationAnimation);
681
+ }
682
+
674
683
  var _width = containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current10 = containerRef.current) === null || _containerRef$current10 === void 0 ? void 0 : (_containerRef$current11 = _containerRef$current10.parentElement) === null || _containerRef$current11 === void 0 ? void 0 : (_containerRef$current12 = _containerRef$current11.getBoundingClientRect()) === null || _containerRef$current12 === void 0 ? void 0 : _containerRef$current12.width;
675
684
 
676
685
  var _height = containerRef === null || containerRef === void 0 ? void 0 : (_containerRef$current13 = containerRef.current) === null || _containerRef$current13 === void 0 ? void 0 : (_containerRef$current14 = _containerRef$current13.parentElement) === null || _containerRef$current14 === void 0 ? void 0 : (_containerRef$current15 = _containerRef$current14.getBoundingClientRect()) === null || _containerRef$current15 === void 0 ? void 0 : _containerRef$current15.height;
@@ -755,6 +764,10 @@ var Swiper = function Swiper(props) {
755
764
  setPreValues(prev, next);
756
765
  window.addEventListener('resize', resizeHandler);
757
766
  return function () {
767
+ if (clearAnimate) {
768
+ clearInterval(clearAnimate);
769
+ }
770
+
758
771
  window.removeEventListener('resize', resizeHandler);
759
772
  buttonNext === null || buttonNext === void 0 ? void 0 : buttonNext.removeEventListener('click', clickNextHandler);
760
773
  buttonClose === null || buttonClose === void 0 ? void 0 : buttonClose.removeEventListener('click', clickNextHandler);
@@ -768,7 +781,7 @@ var Swiper = function Swiper(props) {
768
781
 
769
782
  return React__default.createElement("div", {
770
783
  key: item.id
771
- }, React__default.createElement(React.Fragment, null, item.id && width && React__default.createElement("div", {
784
+ }, React__default.createElement(React.Fragment, null, item.id !== null && width && React__default.createElement("div", {
772
785
  onTouchMove: onTouchWrapper('onTouchMove'),
773
786
  onTouchStart: onTouchWrapper('onTouchStart'),
774
787
  onTouchEnd: onTouchWrapper('onTouchEnd'),
@@ -790,7 +803,7 @@ var Swiper = function Swiper(props) {
790
803
  ref: getRef(item.id)
791
804
  }, React__default.createElement("div", {
792
805
  className: clsx(content, className)
793
- }, item.children)), !item.id && React__default.createElement("div", {
806
+ }, item.children)), item.id === null && React__default.createElement("div", {
794
807
  style: item.type === 'current' ? {
795
808
  width: width,
796
809
  height: height,
@@ -808,12 +821,12 @@ var Swiper = function Swiper(props) {
808
821
  }, React__default.createElement(material.IconButton, {
809
822
  disabled: true
810
823
  }, React__default.createElement(StopIcon, null)))));
811
- }), typeof isMobile !== 'undefined' && !isMobile && (prev === null || prev === void 0 ? void 0 : prev.id) && React__default.createElement("div", {
824
+ }), typeof isMobile !== 'undefined' && !isMobile && (prev === null || prev === void 0 ? void 0 : prev.id) !== null && React__default.createElement("div", {
812
825
  className: clsx(button, button__prev)
813
826
  }, React__default.createElement(material.IconButton, {
814
827
  disabled: load || isMobile,
815
828
  onClick: clickPrevHandler
816
- }, React__default.createElement(NavigateNextIcon, null))), typeof isMobile !== 'undefined' && !isMobile && (next === null || next === void 0 ? void 0 : next.id) && React__default.createElement("div", {
829
+ }, React__default.createElement(NavigateNextIcon, null))), typeof isMobile !== 'undefined' && !isMobile && (next === null || next === void 0 ? void 0 : next.id) !== null && React__default.createElement("div", {
817
830
  className: clsx(button, button__next)
818
831
  }, React__default.createElement(material.IconButton, {
819
832
  disabled: load || isMobile,