react-magic-card 0.0.4 → 0.0.5

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.
@@ -32,7 +32,7 @@ type TransitionProperty = {
32
32
  restDelta?: number;
33
33
  };
34
34
  type MagicCircleProps = {
35
- data: ImageProperty[];
35
+ images: ImageProperty[];
36
36
  width: number;
37
37
  height: number;
38
38
  radius: number;
@@ -47,5 +47,5 @@ type MagicCircleProps = {
47
47
  classImage?: string;
48
48
  classImageUnique?: string;
49
49
  };
50
- export declare const MagicCircle: ({ data, width, height, radius, controller, start, dynamic, wheelDelay, animate, initial, transition, className, classImage, classImageUnique }: MagicCircleProps) => React.JSX.Element;
50
+ export declare const MagicCircle: ({ images, width, height, radius, controller, start, dynamic, wheelDelay, animate, initial, transition, className, classImage, classImageUnique }: MagicCircleProps) => React.JSX.Element;
51
51
  export {};
@@ -27,80 +27,100 @@ exports.MagicCircle = void 0;
27
27
  const react_1 = __importStar(require("react"));
28
28
  const framer_motion_1 = require("framer-motion");
29
29
  require("./main.css");
30
- const MagicCircle = ({ data, width, height, radius, controller, start, dynamic, wheelDelay, animate, initial, transition, className, classImage, classImageUnique }) => {
30
+ const MagicCircle = ({ images, width, height, radius, controller, start, dynamic, wheelDelay, animate, initial, transition, className, classImage, classImageUnique }) => {
31
31
  const [count, setCount] = (0, react_1.useState)(0);
32
+ const [touchStartY, setTouchStartY] = (0, react_1.useState)(0);
32
33
  const [hasDelayed, setHasDelayed] = (0, react_1.useState)(true);
33
- const [images, setImages] = (0, react_1.useState)(data);
34
34
  const [select, setSelect] = (0, react_1.useState)(start);
35
35
  const ref = (0, react_1.useRef)(null);
36
- const shiftRight = (0, react_1.useCallback)(() => {
37
- setImages(arr => {
38
- const lastElement = arr.pop();
39
- arr.unshift(lastElement);
40
- setSelect(select == images.length - 1 ? 0 : select + 1);
41
- return arr;
42
- });
43
- }, [images.length, select]);
44
- const shiftLeft = (0, react_1.useCallback)(() => {
45
- setImages(arr => {
46
- const firstElement = arr.shift();
47
- arr.push(firstElement);
48
- setSelect(select == 0 ? images.length - 1 : select - 1);
49
- return arr;
50
- });
51
- }, [images.length, select]);
52
36
  const centralAngle = ((2 * Math.PI) / images.length) * (180 / Math.PI);
53
- const controlRotation = (0, react_1.useCallback)((e) => {
37
+ // Functions of the rotation and select and delay.
38
+ // turn left
39
+ const shiftLeft = (0, react_1.useCallback)(() => {
40
+ setHasDelayed(false);
41
+ setSelect(select == images.length - 1 ? 0 : select + 1);
42
+ if (dynamic)
43
+ return setCount(count - centralAngle);
44
+ }, [centralAngle, count, dynamic, images.length, select]);
45
+ // turn right
46
+ const shiftRight = (0, react_1.useCallback)(() => {
47
+ setHasDelayed(false);
48
+ setSelect(select == 0 ? images.length - 1 : select - 1);
49
+ if (dynamic)
50
+ return setCount(count + centralAngle);
51
+ }, [centralAngle, count, dynamic, images.length, select]);
52
+ // Function of the desktop.
53
+ const handleWheel = (0, react_1.useCallback)((e) => {
54
54
  const delta = e.deltaY;
55
- if (delta > 0) {
56
- setHasDelayed(false);
57
- for (let i = 0; i < images.length; i++)
58
- shiftRight();
59
- if (dynamic)
60
- return setCount(count - centralAngle);
61
- }
62
- if (delta < 0) {
63
- setHasDelayed(false);
64
- for (let i = 0; i < images.length; i++)
65
- shiftLeft();
66
- if (dynamic)
67
- return setCount(count + centralAngle);
68
- }
69
- }, [dynamic, count, centralAngle, images.length, shiftRight, shiftLeft]);
55
+ if (delta > 0)
56
+ shiftLeft();
57
+ if (delta < 0)
58
+ shiftRight();
59
+ }, [shiftLeft, shiftRight]);
60
+ // Get a start y position in touchStartY.
61
+ const handleTouchStart = (event) => {
62
+ const touch = event.touches[0];
63
+ setTouchStartY(touch.clientY);
64
+ };
65
+ // Function of the mobile.
66
+ const handleTouchMove = (0, react_1.useCallback)((event) => {
67
+ const touch = event.touches[0];
68
+ const deltaY = touch.clientY - touchStartY;
69
+ if (deltaY > 0)
70
+ shiftLeft();
71
+ if (deltaY < 0)
72
+ shiftRight();
73
+ }, [shiftLeft, shiftRight, touchStartY]);
74
+ // Main functional, exit function if ref and hasDelayed does not exist.
70
75
  (0, react_1.useEffect)(() => {
71
76
  const range = ref.current;
77
+ if (!range || !hasDelayed)
78
+ return;
72
79
  const timeId = setTimeout(() => {
73
80
  setHasDelayed(true);
74
81
  }, wheelDelay);
75
- if (!range || !hasDelayed)
76
- return;
77
- range.addEventListener('wheel', controlRotation, { passive: false });
82
+ // Add handle event when component mount and deps update.
83
+ range.addEventListener('wheel', handleWheel);
84
+ range.addEventListener('touchstart', handleTouchStart);
85
+ range.addEventListener('touchmove', handleTouchMove);
86
+ // Clean up event and timeId when component is unmount.
78
87
  return () => {
79
- range.removeEventListener('wheel', controlRotation);
88
+ range.removeEventListener('wheel', handleWheel);
89
+ range.removeEventListener('touchstart', handleTouchStart);
90
+ range.removeEventListener('touchmove', handleTouchMove);
80
91
  clearTimeout(timeId);
81
92
  };
82
- }, [count, hasDelayed, controlRotation, wheelDelay]);
83
- const enterControll = () => {
93
+ }, [handleTouchMove, handleWheel, hasDelayed, wheelDelay]);
94
+ // Functions of the controller.
95
+ // entry ref area.
96
+ const enterControll = (e) => {
97
+ e.preventDefault();
84
98
  document.body.style.overflow = 'hidden';
85
99
  };
100
+ // leave ref area.
86
101
  const leaveControll = () => {
87
102
  document.body.style.overflow = 'auto';
88
103
  };
104
+ // Added event when component is mounted.
89
105
  (0, react_1.useEffect)(() => {
90
106
  const range = ref.current;
91
107
  if (!range)
92
108
  return;
93
- range.addEventListener('mouseover', enterControll);
109
+ // Add event.
110
+ range.addEventListener('mouseover', enterControll, { passive: false });
94
111
  range.addEventListener('mouseout', leaveControll);
95
- range.addEventListener('touchmove', leaveControll, { passive: false });
112
+ range.addEventListener('touchstart', enterControll, { passive: false });
113
+ range.addEventListener('touchout', leaveControll);
114
+ // Clean up event when component is unmount.
96
115
  return () => {
97
116
  range.removeEventListener('mouseover', enterControll);
98
117
  range.removeEventListener('mouseout', leaveControll);
99
- range.removeEventListener('touchmove', leaveControll);
118
+ range.removeEventListener('touchstart', enterControll);
119
+ range.removeEventListener('touchout', leaveControll);
100
120
  };
101
121
  }, []);
102
122
  // angle between images.
103
- const angle = parseFloat((2 * Math.PI).toFixed(15)) / data.length;
123
+ const angle = parseFloat((2 * Math.PI).toFixed(15)) / images.length;
104
124
  return (react_1.default.createElement(framer_motion_1.LazyMotion, { features: framer_motion_1.domAnimation },
105
125
  react_1.default.createElement(framer_motion_1.m.div, { ref: ref, className: className + ' ' + 'outer', style: {
106
126
  width: radius * 2 + controller + 'px',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-magic-card",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "image gallery component with framer motion",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
package/readme.md CHANGED
@@ -15,23 +15,23 @@ npm install react-magic-card framer-motion
15
15
  ## Usage
16
16
 
17
17
  ```tsx
18
- import { MagicCircle } from 'react-magic-card'
18
+ import { MagicCircle } from "react-magic-card";
19
19
 
20
20
  function MyComponent() {
21
21
  const images = [
22
22
  {
23
- src: '/1.jpeg',
24
- alt: '1image'
23
+ src: "/1.jpeg",
24
+ alt: "1image",
25
25
  },
26
26
  {
27
- src: '/2.jpeg',
28
- alt: '2image'
27
+ src: "/2.jpeg",
28
+ alt: "2image",
29
29
  },
30
30
  {
31
- src: '/3.jpeg',
32
- alt: '3image'
33
- }
34
- ]
31
+ src: "/3.jpeg",
32
+ alt: "3image",
33
+ },
34
+ ];
35
35
 
36
36
  return (
37
37
  <MagicCircle
@@ -51,29 +51,29 @@ function MyComponent() {
51
51
  selectScale: 1.6,
52
52
  selectRotateX: 40,
53
53
  selectRotateY: 40,
54
- selectRotateZ: -10
54
+ selectRotateZ: -10,
55
55
  }}
56
56
  transition={{ duration: 0.2 }}
57
57
  />
58
- )
58
+ );
59
59
  }
60
60
  ```
61
61
 
62
- ## Properties
63
-
64
- - data: An array a images property objects for src and alt.
65
- - width: The width of single image.
66
- - height: The height a single image.
67
- - dynamic: A flag indicating whether dynamic rotation is allow.
68
- - start: The Index number, start scale position
69
- - radius: The radius of the circle.
70
- - controller: The diameter plus the value of the controller is the size of the controller.
71
- - className: The class name of the component.
72
- - classImage: The class name of the images.
73
- - classImageUnique: An index number is assigned to the end of the class name set for this.
74
- - animate: An object in the animation property.
75
- - initial: An object in the animation property.
76
- - transition: An object in the transition property.
62
+ | Property | Explanation |
63
+ | ---------------- | ------------------------------------------------------------------------ |
64
+ | images | array of images and objects containing the src and alt properties. |
65
+ | width | width of a single image. |
66
+ | height | height of a single image. |
67
+ | dynamic | indicating whether dynamic rotation is allowed. |
68
+ | start | index number of the starting scale position. |
69
+ | radius | radius of the circle. |
70
+ | controller | diameter plus the value of the controller is the size of the controller. |
71
+ | className | class name of the component. |
72
+ | classImage | class name of the images. |
73
+ | classImageUnique | index number is assigned to the end of the class name set for this. |
74
+ | animate | object in the animation property. |
75
+ | initial | object in the animation property. |
76
+ | transition | object in the transition property. |
77
77
 
78
78
  ## Animation property
79
79