A carousel component from Aceternity UI.
From Aceternity UI
1"use client";
2import { IconArrowNarrowRight } from "@tabler/icons-react";
3import { useState, useRef, useId, useEffect } from "react";
4
5interface SlideData {
6 title: string;
7 button: string;
8 src: string;
9}
10
11interface SlideProps {
12 slide: SlideData;
13 index: number;
14 current: number;
15 handleSlideClick: (index: number) => void;
16}
17
18const Slide = ({ slide, index, current, handleSlideClick }: SlideProps) => {
19 const slideRef = useRef<HTMLLIElement>(null);
20
21 const xRef = useRef(0);
22 const yRef = useRef(0);
23 const frameRef = useRef<number>();
24
25 useEffect(() => {
26 const animate = () => {
27 if (!slideRef.current) return;
28
29 const x = xRef.current;
30 const y = yRef.current;
31
32 slideRef.current.style.setProperty("--x", `${x}px`);
33 slideRef.current.style.setProperty("--y", `${y}px`);
34
35 frameRef.current = requestAnimationFrame(animate);
36 };
37
38 frameRef.current = requestAnimationFrame(animate);
39
40 return () => {
41 if (frameRef.current) {
42 cancelAnimationFrame(frameRef.current);
43 }
44 };
45 }, []);
46
47 const handleMouseMove = (event: React.MouseEvent) => {
48 const el = slideRef.current;
49 if (!el) return;
50
51 const r = el.getBoundingClientRect();
52 xRef.current = event.clientX - (r.left + Math.floor(r.width / 2));
53 yRef.current = event.clientY - (r.top + Math.floor(r.height / 2));
54 };
55
56 const handleMouseLeave = () => {
57 xRef.current = 0;
58 yRef.current = 0;
59 };
60
61 const imageLoaded = (event: React.SyntheticEvent<HTMLImageElement>) => {
62 event.currentTarget.style.opacity = "1";
63 };
64
65 const { src, button, title } = slide;
66
67 return (
68 <div className="[perspective:1200px] [transform-style:preserve-3d]">
69 <li
70 ref={slideRef}
71 className="flex flex-1 flex-col items-center justify-center relative text-center text-white opacity-100 transition-all duration-300 ease-in-out w-[70vmin] h-[70vmin] mx-[4vmin] z-10 "
72 onClick={() => handleSlideClick(index)}
73 onMouseMove={handleMouseMove}
74 onMouseLeave={handleMouseLeave}
75 style={{
76 transform:
77 current !== index
78 ? "scale(0.98) rotateX(8deg)"
79 : "scale(1) rotateX(0deg)",
80 transition: "transform 0.5s cubic-bezier(0.4, 0, 0.2, 1)",
81 transformOrigin: "bottom",
82 }}
83 >
84 <div
85 className="absolute top-0 left-0 w-full h-full bg-[#1D1F2F] rounded-[1%] overflow-hidden transition-all duration-150 ease-out"
86 style={{
87 transform:
88 current === index
89 ? "translate3d(calc(var(--x) / 30), calc(var(--y) / 30), 0)"
90 : "none",
91 }}
92 >
93 <img
94 className="absolute inset-0 w-[120%] h-[120%] object-cover opacity-100 transition-opacity duration-600 ease-in-out"
95 style={{
96 opacity: current === index ? 1 : 0.5,
97 }}
98 alt={title}
99 src={src}
100 onLoad={imageLoaded}
101 loading="eager"
102 decoding="sync"
103 />
104 {current === index && (
105 <div className="absolute inset-0 bg-black/30 transition-all duration-1000" />
106 )}
107 </div>
108
109 <article
110 className={`relative p-[4vmin] transition-opacity duration-1000 ease-in-out ${
111 current === index ? "opacity-100 visible" : "opacity-0 invisible"
112 }`}
113 >
114 <h2 className="text-lg md:text-2xl lg:text-4xl font-semibold relative">
115 {title}
116 </h2>
117 <div className="flex justify-center">
118 <button className="mt-6 px-4 py-2 w-fit mx-auto sm:text-sm text-black bg-white h-12 border border-transparent text-xs flex justify-center items-center rounded-2xl hover:shadow-lg transition duration-200 shadow-[0px_2px_3px_-1px_rgba(0,0,0,0.1),0px_1px_0px_0px_rgba(25,28,33,0.02),0px_0px_0px_1px_rgba(25,28,33,0.08)]">
119 {button}
120 </button>
121 </div>
122 </article>
123 </li>
124 </div>
125 );
126};
127
128interface CarouselControlProps {
129 type: string;
130 title: string;
131 handleClick: () => void;
132}
133
134const CarouselControl = ({
135 type,
136 title,
137 handleClick,
138}: CarouselControlProps) => {
139 return (
140 <button
141 className={`w-10 h-10 flex items-center mx-2 justify-center bg-neutral-200 dark:bg-neutral-800 border-3 border-transparent rounded-full focus:border-[#6D64F7] focus:outline-none hover:-translate-y-0.5 active:translate-y-0.5 transition duration-200 ${
142 type === "previous" ? "rotate-180" : ""
143 }`}
144 title={title}
145 onClick={handleClick}
146 >
147 <IconArrowNarrowRight className="text-neutral-600 dark:text-neutral-200" />
148 </button>
149 );
150};
151
152interface CarouselProps {
153 slides: SlideData[];
154}
155
156export default function Carousel({ slides }: CarouselProps) {
157 const [current, setCurrent] = useState(0);
158
159 const handlePreviousClick = () => {
160 const previous = current - 1;
161 setCurrent(previous < 0 ? slides.length - 1 : previous);
162 };
163
164 const handleNextClick = () => {
165 const next = current + 1;
166 setCurrent(next === slides.length ? 0 : next);
167 };
168
169 const handleSlideClick = (index: number) => {
170 if (current !== index) {
171 setCurrent(index);
172 }
173 };
174
175 const id = useId();
176
177 return (
178 <div
179 className="relative w-[70vmin] h-[70vmin] mx-auto"
180 aria-labelledby={`carousel-heading-${id}`}
181 >
182 <ul
183 className="absolute flex mx-[-4vmin] transition-transform duration-1000 ease-in-out"
184 style={{
185 transform: `translateX(-${current * (100 / slides.length)}%)`,
186 }}
187 >
188 {slides.map((slide, index) => (
189 <Slide
190 key={index}
191 slide={slide}
192 index={index}
193 current={current}
194 handleSlideClick={handleSlideClick}
195 />
196 ))}
197 </ul>
198
199 <div className="absolute flex justify-center w-full top-[calc(100%+1rem)]">
200 <CarouselControl
201 type="previous"
202 title="Go to previous slide"
203 handleClick={handlePreviousClick}
204 />
205
206 <CarouselControl
207 type="next"
208 title="Go to next slide"
209 handleClick={handleNextClick}
210 />
211 </div>
212 </div>
213 );
214}
215