A apple-cards-carousel component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, {
3 useEffect,
4 useRef,
5 useState,
6 createContext,
7 useContext,
8} from "react";
9import {
10 IconArrowNarrowLeft,
11 IconArrowNarrowRight,
12 IconX,
13} from "@tabler/icons-react";
14import { cn } from "@/lib/utils";
15import { AnimatePresence, motion } from "motion/react";
16import Image, { ImageProps } from "next/image";
17import { useOutsideClick } from "@/hooks/use-outside-click";
18
19interface CarouselProps {
20 items: JSX.Element[];
21 initialScroll?: number;
22}
23
24type Card = {
25 src: string;
26 title: string;
27 category: string;
28 content: React.ReactNode;
29};
30
31export const CarouselContext = createContext<{
32 onCardClose: (index: number) => void;
33 currentIndex: number;
34}>({
35 onCardClose: () => {},
36 currentIndex: 0,
37});
38
39export const Carousel = ({ items, initialScroll = 0 }: CarouselProps) => {
40 const carouselRef = React.useRef<HTMLDivElement>(null);
41 const [canScrollLeft, setCanScrollLeft] = React.useState(false);
42 const [canScrollRight, setCanScrollRight] = React.useState(true);
43 const [currentIndex, setCurrentIndex] = useState(0);
44
45 useEffect(() => {
46 if (carouselRef.current) {
47 carouselRef.current.scrollLeft = initialScroll;
48 checkScrollability();
49 }
50 }, [initialScroll]);
51
52 const checkScrollability = () => {
53 if (carouselRef.current) {
54 const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current;
55 setCanScrollLeft(scrollLeft > 0);
56 setCanScrollRight(scrollLeft < scrollWidth - clientWidth);
57 }
58 };
59
60 const scrollLeft = () => {
61 if (carouselRef.current) {
62 carouselRef.current.scrollBy({ left: -300, behavior: "smooth" });
63 }
64 };
65
66 const scrollRight = () => {
67 if (carouselRef.current) {
68 carouselRef.current.scrollBy({ left: 300, behavior: "smooth" });
69 }
70 };
71
72 const handleCardClose = (index: number) => {
73 if (carouselRef.current) {
74 const cardWidth = isMobile() ? 230 : 384; // (md:w-96)
75 const gap = isMobile() ? 4 : 8;
76 const scrollPosition = (cardWidth + gap) * (index + 1);
77 carouselRef.current.scrollTo({
78 left: scrollPosition,
79 behavior: "smooth",
80 });
81 setCurrentIndex(index);
82 }
83 };
84
85 const isMobile = () => {
86 return window && window.innerWidth < 768;
87 };
88
89 return (
90 <CarouselContext.Provider
91 value={{ onCardClose: handleCardClose, currentIndex }}
92 >
93 <div className="relative w-full">
94 <div
95 className="flex w-full overflow-x-scroll overscroll-x-auto scroll-smooth py-10 [scrollbar-width:none] md:py-20"
96 ref={carouselRef}
97 onScroll={checkScrollability}
98 >
99 <div
100 className={cn(
101 "absolute right-0 z-[1000] h-auto w-[5%] overflow-hidden bg-gradient-to-l",
102 )}
103 ></div>
104
105 <div
106 className={cn(
107 "flex flex-row justify-start gap-4 pl-4",
108 "mx-auto max-w-7xl", // remove max-w-4xl if you want the carousel to span the full width of its container
109 )}
110 >
111 {items.map((item, index) => (
112 <motion.div
113 initial={{
114 opacity: 0,
115 y: 20,
116 }}
117 animate={{
118 opacity: 1,
119 y: 0,
120 transition: {
121 duration: 0.5,
122 delay: 0.2 * index,
123 ease: "easeOut",
124 },
125 }}
126 key={"card" + index}
127 className="rounded-3xl last:pr-[5%] md:last:pr-[33%]"
128 >
129 {item}
130 </motion.div>
131 ))}
132 </div>
133 </div>
134 <div className="mr-10 flex justify-end gap-2">
135 <button
136 className="relative z-40 flex h-10 w-10 items-center justify-center rounded-full bg-gray-100 disabled:opacity-50"
137 onClick={scrollLeft}
138 disabled={!canScrollLeft}
139 >
140 <IconArrowNarrowLeft className="h-6 w-6 text-gray-500" />
141 </button>
142 <button
143 className="relative z-40 flex h-10 w-10 items-center justify-center rounded-full bg-gray-100 disabled:opacity-50"
144 onClick={scrollRight}
145 disabled={!canScrollRight}
146 >
147 <IconArrowNarrowRight className="h-6 w-6 text-gray-500" />
148 </button>
149 </div>
150 </div>
151 </CarouselContext.Provider>
152 );
153};
154
155export const Card = ({
156 card,
157 index,
158 layout = false,
159}: {
160 card: Card;
161 index: number;
162 layout?: boolean;
163}) => {
164 const [open, setOpen] = useState(false);
165 const containerRef = useRef<HTMLDivElement>(null);
166 const { onCardClose, currentIndex } = useContext(CarouselContext);
167
168 useEffect(() => {
169 function onKeyDown(event: KeyboardEvent) {
170 if (event.key === "Escape") {
171 handleClose();
172 }
173 }
174
175 if (open) {
176 document.body.style.overflow = "hidden";
177 } else {
178 document.body.style.overflow = "auto";
179 }
180
181 window.addEventListener("keydown", onKeyDown);
182 return () => window.removeEventListener("keydown", onKeyDown);
183 }, [open]);
184
185 useOutsideClick(containerRef, () => handleClose());
186
187 const handleOpen = () => {
188 setOpen(true);
189 };
190
191 const handleClose = () => {
192 setOpen(false);
193 onCardClose(index);
194 };
195
196 return (
197 <>
198 <AnimatePresence>
199 {open && (
200 <div className="fixed inset-0 z-50 h-screen overflow-auto">
201 <motion.div
202 initial={{ opacity: 0 }}
203 animate={{ opacity: 1 }}
204 exit={{ opacity: 0 }}
205 className="fixed inset-0 h-full w-full bg-black/80 backdrop-blur-lg"
206 />
207 <motion.div
208 initial={{ opacity: 0 }}
209 animate={{ opacity: 1 }}
210 exit={{ opacity: 0 }}
211 ref={containerRef}
212 layoutId={layout ? `card-${card.title}` : undefined}
213 className="relative z-[60] mx-auto my-10 h-fit max-w-5xl rounded-3xl bg-white p-4 font-sans md:p-10 dark:bg-neutral-900"
214 >
215 <button
216 className="sticky top-4 right-0 ml-auto flex h-8 w-8 items-center justify-center rounded-full bg-black dark:bg-white"
217 onClick={handleClose}
218 >
219 <IconX className="h-6 w-6 text-neutral-100 dark:text-neutral-900" />
220 </button>
221 <motion.p
222 layoutId={layout ? `category-${card.title}` : undefined}
223 className="text-base font-medium text-black dark:text-white"
224 >
225 {card.category}
226 </motion.p>
227 <motion.p
228 layoutId={layout ? `title-${card.title}` : undefined}
229 className="mt-4 text-2xl font-semibold text-neutral-700 md:text-5xl dark:text-white"
230 >
231 {card.title}
232 </motion.p>
233 <div className="py-10">{card.content}</div>
234 </motion.div>
235 </div>
236 )}
237 </AnimatePresence>
238 <motion.button
239 layoutId={layout ? `card-${card.title}` : undefined}
240 onClick={handleOpen}
241 className="relative z-10 flex h-80 w-56 flex-col items-start justify-start overflow-hidden rounded-3xl bg-gray-100 md:h-[40rem] md:w-96 dark:bg-neutral-900"
242 >
243 <div className="pointer-events-none absolute inset-x-0 top-0 z-30 h-full bg-gradient-to-b from-black/50 via-transparent to-transparent" />
244 <div className="relative z-40 p-8">
245 <motion.p
246 layoutId={layout ? `category-${card.category}` : undefined}
247 className="text-left font-sans text-sm font-medium text-white md:text-base"
248 >
249 {card.category}
250 </motion.p>
251 <motion.p
252 layoutId={layout ? `title-${card.title}` : undefined}
253 className="mt-2 max-w-xs text-left font-sans text-xl font-semibold [text-wrap:balance] text-white md:text-3xl"
254 >
255 {card.title}
256 </motion.p>
257 </div>
258 <BlurImage
259 src={card.src}
260 alt={card.title}
261 fill
262 className="absolute inset-0 z-10 object-cover"
263 />
264 </motion.button>
265 </>
266 );
267};
268
269export const BlurImage = ({
270 height,
271 width,
272 src,
273 className,
274 alt,
275 ...rest
276}: ImageProps) => {
277 const [isLoading, setLoading] = useState(true);
278 return (
279 <img
280 className={cn(
281 "h-full w-full transition duration-300",
282 isLoading ? "blur-sm" : "blur-0",
283 className,
284 )}
285 onLoad={() => setLoading(false)}
286 src={src as string}
287 width={width}
288 height={height}
289 loading="lazy"
290 decoding="async"
291 blurDataURL={typeof src === "string" ? src : undefined}
292 alt={alt ? alt : "Background of a beautiful view"}
293 {...rest}
294 />
295 );
296};
297