react-magic-card 2.0.5 → 2.0.6

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.
@@ -45,6 +45,8 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
45
45
  const [hasDelayed, setHasDelayed] = (0, react_1.useState)(true);
46
46
  const [select, setSelect] = (0, react_1.useState)(start);
47
47
  const [hasDetail, setHasDetail] = (0, react_1.useState)(false);
48
+ const [lastMoveTime, setLastMoveTime] = (0, react_1.useState)(Date.now());
49
+ const moveDelay = 140;
48
50
  const refOuter = (0, react_1.useRef)(null);
49
51
  const refDetail = (0, react_1.useRef)(null);
50
52
  const centralAngle = ((2 * Math.PI) / images.length) * (180 / Math.PI);
@@ -54,21 +56,26 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
54
56
  setSelect(select == 0 ? images.length - 1 : select - 1);
55
57
  setHasDelayed(false);
56
58
  setCount(count + centralAngle);
59
+ setLastMoveTime(Date.now());
57
60
  }, [centralAngle, count, images.length, select]);
58
61
  // turn right
59
62
  const shiftRight = (0, react_1.useCallback)(() => {
60
63
  setSelect(select == images.length - 1 ? 0 : select + 1);
61
64
  setHasDelayed(false);
62
65
  setCount(count - centralAngle);
66
+ setLastMoveTime(Date.now());
63
67
  }, [centralAngle, count, images.length, select]);
64
68
  // Function of the desktop.
65
69
  const handleScroll = (0, react_1.useCallback)((e) => {
70
+ const currentTime = Date.now();
71
+ if (currentTime - lastMoveTime < moveDelay)
72
+ return;
66
73
  const delta = e.deltaY;
67
74
  if (delta < 0)
68
75
  shiftLeft();
69
76
  if (delta > 0)
70
77
  shiftRight();
71
- }, [shiftLeft, shiftRight]);
78
+ }, [lastMoveTime, shiftLeft, shiftRight]);
72
79
  // Get a start y and x position in touchStart Y and X.
73
80
  const handleTouchStart = (e) => {
74
81
  const touch = e.touches[0];
@@ -77,6 +84,9 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
77
84
  };
78
85
  // Function of the mobile.
79
86
  const handleTouchMove = (0, react_1.useCallback)((event) => {
87
+ const currentTime = Date.now();
88
+ if (currentTime - lastMoveTime < moveDelay)
89
+ return;
80
90
  const touch = event.touches[0];
81
91
  const outer = refOuter.current;
82
92
  const outerHalfWidth = outer.clientWidth / 2;
@@ -116,7 +126,8 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
116
126
  }
117
127
  setTouchStartX(deltaX);
118
128
  setTouchStartY(deltaY);
119
- }, [shiftLeft, shiftRight, touchStartX, touchStartY]);
129
+ setLastMoveTime(Date.now());
130
+ }, [shiftLeft, shiftRight, touchStartX, touchStartY, lastMoveTime]);
120
131
  const handleMouseDown = (0, react_1.useCallback)((e) => {
121
132
  if (hasDetail)
122
133
  return;
@@ -125,7 +136,8 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
125
136
  setInitialY(e.clientY);
126
137
  }, [hasDetail]);
127
138
  const handleMouseMove = (0, react_1.useCallback)((e) => {
128
- if (isDragging) {
139
+ const currentTime = Date.now();
140
+ if (isDragging && currentTime - lastMoveTime >= moveDelay) {
129
141
  const outer = refOuter.current;
130
142
  const outerHalfWidth = outer.clientWidth / 2;
131
143
  const outerHalfHeight = outer.clientHeight / 2;
@@ -165,8 +177,9 @@ const CircleRotation = ({ images, start, height, width, radius, controller, offs
165
177
  setInitialX(e.clientX);
166
178
  setInitialY(e.clientY);
167
179
  setHasMove(true);
180
+ setLastMoveTime(Date.now());
168
181
  }
169
- }, [initialX, initialY, isDragging, shiftLeft, shiftRight]);
182
+ }, [initialX, initialY, isDragging, shiftLeft, shiftRight, lastMoveTime]);
170
183
  const handleMouseUp = (0, react_1.useCallback)(() => {
171
184
  setIsDragging(false);
172
185
  setHasMove(false);
@@ -58,6 +58,9 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
58
58
  const refOuter = (0, react_1.useRef)(null);
59
59
  const refDetail = (0, react_1.useRef)(null);
60
60
  const [count, setCount] = (0, react_1.useState)(0);
61
+ // Adding variables for speed limiting
62
+ const [lastMoveTime, setLastMoveTime] = (0, react_1.useState)(Date.now());
63
+ const moveDelay = 140; // Minimum delay between moves in milliseconds.
61
64
  // Functions of the swipe and wheel a shifting.
62
65
  // turn left
63
66
  const shiftLeft = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
@@ -79,12 +82,16 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
79
82
  }), [count, images.length, margin, select, edge, width]);
80
83
  // Wheel function of the desktop.
81
84
  const handleScroll = (0, react_1.useCallback)((e) => __awaiter(void 0, void 0, void 0, function* () {
85
+ const now = Date.now();
86
+ if (now - lastMoveTime < moveDelay)
87
+ return;
88
+ setLastMoveTime(now);
82
89
  const delta = e.deltaY;
83
90
  if (delta < 0)
84
91
  shiftLeft();
85
92
  if (delta > 0)
86
93
  shiftRight();
87
- }), [shiftLeft, shiftRight]);
94
+ }), [shiftLeft, shiftRight, lastMoveTime, moveDelay]);
88
95
  // Get a start y and x position in touchStart Y and X.
89
96
  const handleTouchStart = (e) => {
90
97
  const touch = e.touches[0];
@@ -93,6 +100,10 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
93
100
  };
94
101
  // Swipe Function of the mobile.
95
102
  const handleTouchMove = (0, react_1.useCallback)((e) => {
103
+ const now = Date.now();
104
+ if (now - lastMoveTime < moveDelay)
105
+ return;
106
+ setLastMoveTime(now);
96
107
  const touch = e.touches[0];
97
108
  const delta = hasDetail
98
109
  ? touch.clientX
@@ -116,7 +127,16 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
116
127
  : !vertical
117
128
  ? setTouchStartX(delta)
118
129
  : setTouchStartY(delta);
119
- }, [hasDetail, shiftLeft, shiftRight, touchStartX, touchStartY, vertical]);
130
+ }, [
131
+ hasDetail,
132
+ shiftLeft,
133
+ shiftRight,
134
+ touchStartX,
135
+ touchStartY,
136
+ vertical,
137
+ lastMoveTime,
138
+ moveDelay
139
+ ]);
120
140
  const handleMouseDown = (0, react_1.useCallback)((e) => {
121
141
  setIsDragging(true);
122
142
  setInitialX(e.clientX);
@@ -124,6 +144,10 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
124
144
  }, []);
125
145
  const handleMouseMove = (0, react_1.useCallback)((e) => {
126
146
  if (isDragging) {
147
+ const now = Date.now();
148
+ if (now - lastMoveTime < moveDelay)
149
+ return;
150
+ setLastMoveTime(now);
127
151
  const deltaX = e.clientX - initialX;
128
152
  const deltaY = e.clientY - initialY;
129
153
  if (!vertical ? deltaX < 0 : deltaY < 0)
@@ -133,7 +157,16 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
133
157
  !vertical ? setInitialX(e.clientX) : setInitialY(e.clientY);
134
158
  setHasMove(true);
135
159
  }
136
- }, [initialX, initialY, isDragging, shiftLeft, shiftRight, vertical]);
160
+ }, [
161
+ initialX,
162
+ initialY,
163
+ isDragging,
164
+ shiftLeft,
165
+ shiftRight,
166
+ vertical,
167
+ lastMoveTime,
168
+ moveDelay
169
+ ]);
137
170
  const handleMouseUp = (0, react_1.useCallback)(() => {
138
171
  setIsDragging(false);
139
172
  setHasMove(false);
@@ -144,7 +177,7 @@ const StraightInfinity = ({ images, start, height, width, vertical = false, marg
144
177
  const detail = refDetail.current;
145
178
  const timeId = setTimeout(() => {
146
179
  setHasDelayed(true);
147
- }, ((transition === null || transition === void 0 ? void 0 : transition.duration) / 2) * 1000);
180
+ }, (transition === null || transition === void 0 ? void 0 : transition.duration) * 1000);
148
181
  // Add handle event when component mount and deps update.
149
182
  document.addEventListener('mouseup', handleMouseUp);
150
183
  if (!hasDelayed)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-magic-card",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "React Component library for images gallery using the framer motion animation.",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",