-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarousel.jsx
More file actions
165 lines (159 loc) · 4.72 KB
/
Carousel.jsx
File metadata and controls
165 lines (159 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, {
useEffect,
useState,
forwardRef,
useImperativeHandle,
Children,
useMemo,
} from "react";
import { motion, AnimatePresence } from "framer-motion";
import { wrap } from "./helpers/wrap";
import { verticalVariants, variants } from "./helpers/variants";
import Controls from "./components/Controls";
import Counter from "./components/Counter";
import Navigation from "./components/Navigation";
//Calculations for swipe
const swipePower = (offset, velocity) => {
return Math.abs(offset) * velocity;
};
const Carousel = forwardRef(
(
{
range = 1000,
navigation = true,
onChange,
controls = true,
swipeConfindence = 1000,
intervalActive = true,
children,
className,
drag = true,
counter = [true, ''] ,
interval = [false, 0],
noExit = false,
type = "horizontal",
goToIndex,
},
ref
) => {
const [[page, direction], setPage] = useState([0, 0]);
const pageCount = useMemo(() => Children.count(children), [children]);
const pageIndex = wrap(0, pageCount, page);
if (!children || pageCount === 0) {
console.error(
"Carousel: No children provided. Please provide content or images to display within the carousel."
);
return (
<div className="carousel-error">
No content provided for the carousel.
</div>
);
}
if (interval && interval[0] < 0) {
console.error("Carousel: interval time is negative");
return <div className="carousel-error">Negative interval.</div>;
}
ref &&
useImperativeHandle(ref, () => ({
paginate,
}));
//automatic slider
useEffect(() => {
const intervalId =
intervalActive &&
interval[0] &&
setInterval(() => paginate(1), interval[1] * 1000);
return () => clearInterval(intervalId);
}, [intervalActive, interval]);
//goToPage external
useEffect(() => {
if (goToIndex && goToIndex <= pageCount && goToIndex >= 0) {
setPage((prevState) => [goToIndex, prevState[1]]);
} else {
console.error("invalid page");
}
}, [goToIndex]);
//goToPage internal
const goToPage = (number) => {
if (number <= pageCount && number >= 0) {
setPage((prevState) => [number, prevState[1]]);
} else {
console.error("invalid page");
}
};
//paginaton
const paginate = (newDirection) => {
setPage((prevState) => [prevState[0] + newDirection, newDirection]);
onChange && onChange(pageIndex);
};
return (
<>
<AnimatePresence mode="popLayout" initial={false} custom={direction}>
<motion.div
className={className}
style={{ position: "relative" }}
key={page}
custom={direction}
variants={
type === "vertical" ? verticalVariants(range) : variants(range)
}
initial="enter"
animate="center"
{...(!noExit
? {
exit: "exit",
}
: {})}
transition={{
x: { type: "spring", stiffness: 300, damping: 30 },
opacity: { duration: 0.2 },
}}
{...(drag
? {
drag: type === "vertical" ? "y" : "x",
dragConstraints:
type === "vertical"
? { top: 0, bottom: 0 }
: { left: 0, right: 0 },
dragElastic: 1,
onDragEnd: (e, { offset, velocity }) => {
const swipe = swipePower(
type === "vertical" ? offset.y : offset.x,
type === "vertical" ? velocity.y : velocity.x
);
if (swipe < -swipeConfindence) {
paginate(1);
} else if (swipe > swipeConfindence) {
paginate(-1);
}
},
}
: {})}
>
<div style={{ position: "absolute", top: 0, left: 0 }}>
{Children.toArray(children)[pageIndex]}
</div>
</motion.div>
</AnimatePresence>
{/* Optional components */}
{controls && <Controls paginate={paginate} />}
{counter && counter[0] && (
<Counter
counter={counter}
pageIndex={pageIndex}
pageCount={pageCount}
/>
)}
{navigation && (
<Navigation
pageCount={pageCount}
goToPage={goToPage}
pageIndex={pageIndex}
/>
)}
</>
);
}
);
Carousel.displayName = "Carousel";
export default Carousel;