Created
January 8, 2022 07:30
-
-
Save Dromediansk/aaf9b6598524b3892e5d2ffed0f7f03c to your computer and use it in GitHub Desktop.
Image carousel component for nextJS and tailwind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image from "next/image"; | |
import { useState } from "react"; | |
import Swipe from "react-easy-swipe"; | |
import { AiOutlineLeft, AiOutlineRight } from "react-icons/ai"; | |
/** | |
* Carousel component for nextJS and Tailwind. | |
* Using external library react-easy-swipe for swipe gestures on mobile devices (optional) | |
* | |
* @param images - Array of images with src and alt attributes | |
* @returns React component | |
*/ | |
export default function Carousel({ images }) { | |
const [currentSlide, setCurrentSlide] = useState(0); | |
const handleNextSlide = () => { | |
let newSlide = currentSlide === images.length - 1 ? 0 : currentSlide + 1; | |
setCurrentSlide(newSlide); | |
}; | |
const handlePrevSlide = () => { | |
let newSlide = currentSlide === 0 ? images.length - 1 : currentSlide - 1; | |
setCurrentSlide(newSlide); | |
}; | |
return ( | |
<div className="relative"> | |
<AiOutlineLeft | |
onClick={handlePrevSlide} | |
className="absolute left-0 m-auto text-5xl inset-y-1/2 cursor-pointer text-gray-400 z-20" | |
/> | |
<div className="w-full h-[50vh] flex overflow-hidden relative m-auto"> | |
<Swipe | |
onSwipeLeft={handleNextSlide} | |
onSwipeRight={handlePrevSlide} | |
className="relative z-10 w-full h-full" | |
> | |
{images.map((image, index) => { | |
if (index === currentSlide) { | |
return ( | |
<Image | |
key={image.id} | |
image={image} | |
layout="fill" | |
objectFit="contain" | |
className="animate-fadeIn" | |
/> | |
); | |
} | |
})} | |
</Swipe> | |
</div> | |
<AiOutlineRight | |
onClick={handleNextSlide} | |
className="absolute right-0 m-auto text-5xl inset-y-1/2 cursor-pointer text-gray-400 z-20" | |
/> | |
<div className="relative flex justify-center p-2"> | |
{images.map((_, index) => { | |
return ( | |
<div | |
className={ | |
index === currentSlide | |
? "h-4 w-4 bg-gray-700 rounded-full mx-2 mb-2 cursor-pointer" | |
: "h-4 w-4 bg-gray-300 rounded-full mx-2 mb-2 cursor-pointer" | |
} | |
key={index} | |
onClick={() => { | |
setCurrentSlide(index); | |
}} | |
/> | |
); | |
})} | |
</div> | |
</div> | |
); | |
} |
animate-fadeIn how to add
transiton , duration where
this is good.
Replacing line 43 from:
image={image}
to
src={image}
worked for me fine. Thanks!
Well done. Works for me
Replace image={image} with src={image} and
layout="fill"
objectFit="contain" are depreciated and replace with fill and object-fit='contain'
Thanks it help me a lot
any idea to make it responsive. the image's height is not change in mobile mode
It was good.
Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well done