Skip to content

Commit 676413c

Browse files
committed
enhance Carousel to AutoCarousel and button layout
1 parent cae2ca1 commit 676413c

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

src/components/testimonial.js

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ const testimonials = [
4747
];
4848

4949
const Testimonial = ({ testimonial }) => (
50-
<div className="bg-black p-4 rounded-xl delay-150 hover:-translate-y-1 duration-300 hover:scale-110 md:m-6 m-2 mt-8 h-100 border-2 border-white">
50+
<div className=" bg-black p-4 rounded-xl delay-150 hover:-translate-y-1 duration-300 hover:scale-110 md:m-6 m-2 mt-8 h-80 w-96 border-2 border-white text-lg text-center justify-center items-center">
5151
<p className="text-white">{testimonial.text}</p>
52-
<div className="flex items-center mt-4">
52+
<div className="flex justify-center mt-8">
5353
<div className="flex-shrink-0">
5454
<img
5555
className="w-12 h-12 rounded-full"
@@ -67,71 +67,77 @@ const Testimonial = ({ testimonial }) => (
6767
const TestimonialCarousel = () => {
6868
const [current, setCurrent] = useState(0);
6969
const [numVisible, setNumVisible] = useState(3);
70-
70+
const [intervalId, setIntervalId] = useState(null);
7171
useEffect(() => {
7272
const handleResize = () => {
7373
const screenWidth = window.innerWidth;
74-
if (screenWidth < 640) {
74+
if (screenWidth <= 640) {
7575
setNumVisible(1);
76-
} else if (screenWidth < 768) {
76+
} else if (screenWidth >= 640 && screenWidth < 768) {
77+
setNumVisible(2);
78+
} else if (screenWidth >= 768 && screenWidth < 1024) {
7779
setNumVisible(2);
7880
} else {
7981
setNumVisible(3);
8082
}
8183
};
8284

85+
const startAutoCarousel = () => {
86+
const id = setInterval(() => {
87+
goToNext();
88+
}, 5000);
89+
90+
return id;
91+
};
92+
8393
if (typeof window !== "undefined") {
8494
window.addEventListener("resize", handleResize);
8595
}
86-
87-
return () => {
88-
if (typeof window !== "undefined") {
89-
window.removeEventListener("resize", handleResize);
90-
}
91-
};
96+
const intervalId = startAutoCarousel();
97+
return () => clearInterval(intervalId);
9298
}, []);
9399

94100
const visibleTestimonials = testimonials.slice(current, current + numVisible);
95101

96102
const goToPrev = () => {
97-
if (current === 0) {
98-
setCurrent(testimonials.length - numVisible);
99-
} else {
100-
setCurrent(current - 1);
101-
}
103+
setCurrent((prev) => {
104+
if (prev === 0) {
105+
return testimonials.length - numVisible;
106+
} else {
107+
return prev - 1;
108+
}
109+
});
102110
};
103111

104112
const goToNext = () => {
105-
if (current === testimonials.length - numVisible) {
106-
setCurrent(0);
107-
} else {
108-
setCurrent(current + 1);
109-
}
113+
setCurrent((prev) => {
114+
if (prev + numVisible === testimonials.length) {
115+
return 0;
116+
} else {
117+
return prev + 1;
118+
}
119+
});
110120
};
111121

112122
return (
113123
<div className="py-10">
114124
<Header name="What Our Members Say About Us" />
115125
<div className="flex flex-col items-center justify-center">
116126
<div className="flex items-center mb-6">
117-
<button
118-
onClick={goToPrev}
119-
className="bg-gray-800 text-white rounded-full p-3 flex justify-center items-center mr-2"
120-
>
121-
{"<"}
122-
</button>
123127
{visibleTestimonials.map((testimonial) => (
124128
<div key={testimonial.id}>
125129
<Testimonial testimonial={testimonial} />
126130
</div>
127131
))}
128-
<button
129-
onClick={goToNext}
130-
className="bg-gray-800 text-white rounded-full p-4 flex justify-center items-center ml-2"
131-
>
132-
{">"}
133-
</button>
134132
</div>
133+
</div>
134+
<div className="flex justify-center items-center gap-4">
135+
<button
136+
onClick={goToPrev}
137+
className="bg-gray-800 text-white rounded-full p-3 flex justify-center items-center mr-2"
138+
>
139+
{"<"}
140+
</button>
135141
<div className="flex justify-center">
136142
{testimonials.map((testimonial, index) => (
137143
<div
@@ -143,6 +149,12 @@ const TestimonialCarousel = () => {
143149
></div>
144150
))}
145151
</div>
152+
<button
153+
onClick={goToNext}
154+
className="bg-gray-800 text-white rounded-full p-3 flex justify-center items-center mr-2"
155+
>
156+
{">"}
157+
</button>
146158
</div>
147159
</div>
148160
);

0 commit comments

Comments
 (0)