diff --git a/08-ImageCarousel/chan-byeong/index.tsx b/08-ImageCarousel/chan-byeong/index.tsx new file mode 100644 index 0000000..a53dba1 --- /dev/null +++ b/08-ImageCarousel/chan-byeong/index.tsx @@ -0,0 +1,105 @@ +import { useState } from "react"; + +export default function ImageCarousel({ + images, +}: Readonly<{ + images: ReadonlyArray<{ src: string; alt: string }>; +}>) { + const [currentIndex, setCurrentIndex] = useState(1); + const [isTransition, setIsTransition] = useState(true); + const clonedImage = formatImageArr(images); + + const handlePrev = () => { + setIsTransition(true); + setCurrentIndex((prev) => prev - 1); + }; + + const handleNext = () => { + setIsTransition(true); + setCurrentIndex((prev) => prev + 1); + }; + + const handleTransitionEnd = () => { + setIsTransition(false); + if (currentIndex === 0) { + setCurrentIndex(clonedImage.length - 2); + } else if (currentIndex === clonedImage.length - 1) { + setCurrentIndex(1); + } + }; + + return ( +