Building Interactive Testimonial Carousels/Swipers with react-multi-carousel
Step-by-Step guide/tutorial on how to make Swiper, with react-multi-carousel, including ready-to-use code snippets for Implementation in your project.
Introduction
Testimonials are a powerful tool to showcase the credibility and value of your products or services. Imagine being able to present them dynamically in an interactive carousel that adapts to different screen sizes. In this tutorial, we'll walk you through the process of creating an impressive testimonial carousel using the react-multi-carousel
library. Whether you're new to React or just looking to enhance your website's user experience, this step-by-step guide will get you up and running in no time.
Live example
Installation
The first step is to install the necessary dependencies. Open your terminal and execute either of the following commands based on your package manager preference:
npm install react-multi-carousel --save
or
yarn add react-multi-carousel --save
Setting Up index.js
Let's dive into the implementation. In your index.js
file, import the TestimonList
component and the testimonial data:
import TestimonList from "../components/TestimonList";
import records from "../data/testimonials.json"
export default function Home( ) {
return (
<main>
<TestimonList records={records} translate={translate}/>
</main>
);
}
Creating the TestimonList Component
In the TestimonList
component, you'll create a responsive testimonial carousel using react-multi-carousel
. We'll define the breakpoints and rendering options for different screen sizes:
import TestimonCard from "./TestimonCard";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
export interface Record {
image: string;
title: string;
description: string;
name: string;
}
interface Props {
records: Record[];
}
const TestimonList = ({ records}: Props) => {
const responsive = {
// Define breakpoints and items to show at each breakpoint
// Adjust these values based on your design and preferences
superLargeDesktop: { breakpoint: { max: 4000, min: 3000 }, items: 4, slidesToSlide: 1 },
desktop: { breakpoint: { max: 3000, min: 1024 }, items: 2, slidesToSlide: 1 },
tablet: { breakpoint: { max: 1024, min: 464 }, items: 1, slidesToSlide: 1 },
mobile: { breakpoint: { max: 464, min: 0 }, items: 1, slidesToSlide: 1 },
};
return (
<div className="w-full bg-slate-100">
{/* Carousel title */}
<h2 className="text-2xl sm:text-3xl font-medium text-center pb-2 pt-16">
What our associates say about us
</h2>
<div className="max-w-[1140px] mx-auto px- sm:px-6 text-center mt-10">
{/* Carousel component */}
{/* The word itself says what each prop means, play with the values */}
<Carousel
ssr={true}
className="z-[1]"
centerMode={true}
keyBoardControl={true}
infinite={true}
draggable={false}
responsive={responsive}
swipeable={false}
>
{/* Mapping over records to render TestimonCard components */}
{records.map((record, index) => (
<TestimonCard
key={index}
title={record.title}
image={record.image}
description={record.description}
name={record.name}
/>
))}
</Carousel>
</div>
</div>
);
};
export default TestimonList;
Testimonial Cards with TestimonCard Component
Each testimonial will be displayed as a card using the TestimonCard
component. This component encapsulates the layout and structure of a single testimonial card:
import Image from "next/image";
const TestimonCard = ({ title, image, name, description }: any) => {
return (
<div className="max-w-[400px] min-h-[296px] mb-10 border shadow-lg rounded bg-slate-50 mr-4">
<div className="sm:ml-4 mt-[-5px] w-[150px]">
<Image src={image} alt={title} width={250} height={180} />
</div>
<div className="mt-[-5px]">
<div className="text-start font-medium ml-2 sm:ml-4 text-lg whitespace-nowrap overflow-hidden text-ellipsis">
{title}
</div>
<div className="text-start font-medium ml-2 sm:ml-4 mb-2 whitespace-nowrap overflow-hidden text-ellipsis">
{name}
</div>
<div className="pb-2 text-start mx-2 sm:mx-4 text-sm sm:text-base mb-2 font-semibold text-gray-600">
{description}
</div>
</div>
</div>
);
};
export default TestimonCard;
Table of props
Here's a table listing the props you can use with the react-multi-carousel
component based on the documentation:
Name | Type | Default | Description |
responsive | object | {} | Numbers of slides to show at each breakpoint |
deviceType | string | '' | Only pass this when use for server-side rendering, what to pass can be found in the example folder |
ssr | boolean | false | Use in conjunction with responsive and deviceType prop |
slidesToSlide | Number | 1 | How many slides to slide. |
draggable | boolean | true | Optionally disable/enable dragging on desktop |
swipeable | boolean | true | Optionally disable/enable swiping on mobile |
arrows | boolean | true | Hide/Show the default arrows |
renderArrowsWhenDisabled | boolean | false | Allow for the arrows to have a disabled attribute instead of not showing them |
removeArrowOnDeviceType | string or array | '' | Hide the default arrows at different break point, should be used with responsive props. Value could be mobile or ['mobile', 'tablet'], can be a string or array |
customLeftArrow | jsx | null | Replace the default arrow with your own |
customRightArrow | jsx | null | Replace the default arrow with your own |
customDot | jsx | null | Replace the default dots with your own |
customButtonGroup | jsx | null | Fully customize your own control functionality if you don't want arrows or dots |
infinite | boolean | false | Enables infinite scrolling in both directions. Carousel items are cloned in the DOM to achieve this. |
minimumTouchDrag | number | 50 | The amount of distance to drag / swipe in order to move to the next slide. |
afterChange | function | null | A callback after sliding everytime. |
beforeChange | function | null | A callback before sliding everytime. |
sliderClass | string | 'react-multi-carousel-track' | CSS class for inner slider div, use this to style your own track list. |
itemClass | string | '' | CSS class for carousel item, use this to style your own Carousel item. For example add padding-left and padding-right |
containerClass | string | 'react-multi-carousel-list' | Use this to style the whole container. For example add padding to allow the "dots" or "arrows" to go to other places without being overflown. |
dotListClass | string | 'react-multi-carousel-dot-list' | Use this to style the dot list. |
keyBoardControl | boolean | true | Use keyboard to navigate to next/previous slide |
autoPlay | boolean | false | Auto play |
autoPlaySpeed | number | 3000 | The unit is ms |
showDots | boolean | false | Hide the default dot list |
renderDotsOutside | boolean | false | Show dots outside of the container |
partialVisible | boolean | string | false |
customTransition | string | transform 300ms ease-in-out | Configure your own anaimation when sliding |
transitionDuration | `number | 300 | The unit is ms, if you are using customTransition, make sure to put the duration here as this is needed for the resizing to work. |
focusOnSelect | boolean | false | Go to slide on click and make the slide a current slide. |
centerMode | boolean | false | Shows the next items and previous items partially. |
additionalTransfrom | number | 0 | additional transfrom to the current one. |
shouldResetAutoplay | boolean | true | resets autoplay when clicking next, previous button and the dots |
rewind | boolean | false | if infinite is not enabled and autoPlay explicitly is, this option rewinds the carousel when the end is reached (Lightweight infinite mode alternative without cloning). |
rewindWithAnimation | boolean | false | when rewinding the carousel back to the beginning, this decides if the rewind process should be instant or with transition. |
rtl | boolean | false | Sets the carousel direction to be right to left |
Conclusion
By following this tutorial, you've successfully created a responsive testimonial carousel using react-multi-carousel
. Testimonials are now elegantly displayed in an interactive format that adapts to various device sizes. As you continue to refine your web development skills, you can further customize and optimize the carousel's behavior and appearance to suit your project's unique requirements.
Remember, this tutorial provides a foundation that you can build upon. Feel free to experiment with the provided code and tweak the values to achieve the desired look and feel for your testimonial carousel.
Happy coding!