parent
ad4a5fef22
commit
42830bd355
2 changed files with 79 additions and 1 deletions
@ -0,0 +1,76 @@ |
||||
import React, { useState } from "react"; |
||||
|
||||
import rightArrow from "./black-right-arrow.svg"; |
||||
import leftArrow from "./black-left-arrow.svg"; |
||||
|
||||
const ImageSlider = ({ slides }) => { |
||||
const [current, setCurrent] = useState(0); |
||||
const length = slides.length; |
||||
|
||||
const nextSlide = () => { |
||||
setCurrent(current === length - 1 ? 0 : current + 1); |
||||
}; |
||||
|
||||
const prevSlide = () => { |
||||
setCurrent(current === 0 ? length - 1 : current - 1); |
||||
}; |
||||
|
||||
console.log(current); |
||||
|
||||
if (!Array.isArray(slides) || slides.length <= 0) { |
||||
return null; |
||||
} |
||||
|
||||
return ( |
||||
<section className="bg-red-100"> |
||||
<div className="group relative bg-pink-400"> |
||||
{slides.map((slide, index) => { |
||||
return ( |
||||
<div |
||||
className={index === current ? "slide active" : "slide"} |
||||
key={index} |
||||
> |
||||
{index === current && ( |
||||
<img |
||||
src={slide.image} |
||||
alt="mahdi" |
||||
className="w-full" |
||||
style={{ height: "100%" }} |
||||
/> |
||||
)} |
||||
</div> |
||||
); |
||||
})} |
||||
<Tabbar nextSlide={nextSlide} prevSlide={prevSlide} /> |
||||
</div> |
||||
</section> |
||||
); |
||||
}; |
||||
|
||||
export default ImageSlider; |
||||
|
||||
function Tabbar({ nextSlide, prevSlide }) { |
||||
const tabs = [ |
||||
{ |
||||
icon: rightArrow, |
||||
click: nextSlide, |
||||
}, |
||||
{ |
||||
icon: leftArrow, |
||||
click: prevSlide, |
||||
}, |
||||
]; |
||||
return ( |
||||
<div className="flex flex-row items-center absolute bottom-7 right-10"> |
||||
{tabs.map((tab, index) => ( |
||||
<button |
||||
onClick={tab.click} |
||||
key={index} |
||||
className="bg-white border border-gray-300 rounded-full mx-1 p-2" |
||||
> |
||||
<img src={tab.icon} className="w-4" /> |
||||
</button> |
||||
))} |
||||
</div> |
||||
); |
||||
} |
Loading…
Reference in new issue