A animated-testimonials component from Aceternity UI.
From Aceternity UI
1"use client";
2
3import { IconArrowLeft, IconArrowRight } from "@tabler/icons-react";
4import { motion, AnimatePresence } from "motion/react";
5
6import { useEffect, useState } from "react";
7
8type Testimonial = {
9 quote: string;
10 name: string;
11 designation: string;
12 src: string;
13};
14export const AnimatedTestimonials = ({
15 testimonials,
16 autoplay = false,
17}: {
18 testimonials: Testimonial[];
19 autoplay?: boolean;
20}) => {
21 const [active, setActive] = useState(0);
22
23 const handleNext = () => {
24 setActive((prev) => (prev + 1) % testimonials.length);
25 };
26
27 const handlePrev = () => {
28 setActive((prev) => (prev - 1 + testimonials.length) % testimonials.length);
29 };
30
31 const isActive = (index: number) => {
32 return index === active;
33 };
34
35 useEffect(() => {
36 if (autoplay) {
37 const interval = setInterval(handleNext, 5000);
38 return () => clearInterval(interval);
39 }
40 }, [autoplay]);
41
42 const randomRotateY = () => {
43 return Math.floor(Math.random() * 21) - 10;
44 };
45 return (
46 <div className="mx-auto max-w-sm px-4 py-20 font-sans antialiased md:max-w-4xl md:px-8 lg:px-12">
47 <div className="relative grid grid-cols-1 gap-20 md:grid-cols-2">
48 <div>
49 <div className="relative h-80 w-full">
50 <AnimatePresence>
51 {testimonials.map((testimonial, index) => (
52 <motion.div
53 key={testimonial.src}
54 initial={{
55 opacity: 0,
56 scale: 0.9,
57 z: -100,
58 rotate: randomRotateY(),
59 }}
60 animate={{
61 opacity: isActive(index) ? 1 : 0.7,
62 scale: isActive(index) ? 1 : 0.95,
63 z: isActive(index) ? 0 : -100,
64 rotate: isActive(index) ? 0 : randomRotateY(),
65 zIndex: isActive(index)
66 ? 40
67 : testimonials.length + 2 - index,
68 y: isActive(index) ? [0, -80, 0] : 0,
69 }}
70 exit={{
71 opacity: 0,
72 scale: 0.9,
73 z: 100,
74 rotate: randomRotateY(),
75 }}
76 transition={{
77 duration: 0.4,
78 ease: "easeInOut",
79 }}
80 className="absolute inset-0 origin-bottom"
81 >
82 <img
83 src={testimonial.src}
84 alt={testimonial.name}
85 width={500}
86 height={500}
87 draggable={false}
88 className="h-full w-full rounded-3xl object-cover object-center"
89 />
90 </motion.div>
91 ))}
92 </AnimatePresence>
93 </div>
94 </div>
95 <div className="flex flex-col justify-between py-4">
96 <motion.div
97 key={active}
98 initial={{
99 y: 20,
100 opacity: 0,
101 }}
102 animate={{
103 y: 0,
104 opacity: 1,
105 }}
106 exit={{
107 y: -20,
108 opacity: 0,
109 }}
110 transition={{
111 duration: 0.2,
112 ease: "easeInOut",
113 }}
114 >
115 <h3 className="text-2xl font-bold text-black dark:text-white">
116 {testimonials[active].name}
117 </h3>
118 <p className="text-sm text-gray-500 dark:text-neutral-500">
119 {testimonials[active].designation}
120 </p>
121 <motion.p className="mt-8 text-lg text-gray-500 dark:text-neutral-300">
122 {testimonials[active].quote.split(" ").map((word, index) => (
123 <motion.span
124 key={index}
125 initial={{
126 filter: "blur(10px)",
127 opacity: 0,
128 y: 5,
129 }}
130 animate={{
131 filter: "blur(0px)",
132 opacity: 1,
133 y: 0,
134 }}
135 transition={{
136 duration: 0.2,
137 ease: "easeInOut",
138 delay: 0.02 * index,
139 }}
140 className="inline-block"
141 >
142 {word}
143 </motion.span>
144 ))}
145 </motion.p>
146 </motion.div>
147 <div className="flex gap-4 pt-12 md:pt-0">
148 <button
149 onClick={handlePrev}
150 className="group/button flex h-7 w-7 items-center justify-center rounded-full bg-gray-100 dark:bg-neutral-800"
151 >
152 <IconArrowLeft className="h-5 w-5 text-black transition-transform duration-300 group-hover/button:rotate-12 dark:text-neutral-400" />
153 </button>
154 <button
155 onClick={handleNext}
156 className="group/button flex h-7 w-7 items-center justify-center rounded-full bg-gray-100 dark:bg-neutral-800"
157 >
158 <IconArrowRight className="h-5 w-5 text-black transition-transform duration-300 group-hover/button:-rotate-12 dark:text-neutral-400" />
159 </button>
160 </div>
161 </div>
162 </div>
163 </div>
164 );
165};
166