Copy import { Carousel } from "react-hartan"
function App () {
return (
<>
< Carousel ></ Carousel >
</>
)
}
export default App
Copy import { Carousel } from "react-hartan"
const list = [
"https://dummyimage.com/601x300" ,
"https://dummyimage.com/602x300" ,
"https://dummyimage.com/603x300" ,
"https://dummyimage.com/604x300" ,
"https://dummyimage.com/605x300" ,
];
function App () {
return (
<>
< Carousel images = {list}></ Carousel >
</>
)
}
export default App
This is the code for the Carousel component.
Carousel.jsx Carousel.module.css
Copy import useCarousel from "../../Hooks/Carousel"
import carouselStyle from "./Carousel.module.css"
import PropTypes from "prop-types"
const imageList = [
"https://dummyimage.com/601x300" ,
"https://dummyimage.com/602x300" ,
"https://dummyimage.com/603x300" ,
"https://dummyimage.com/604x300" ,
"https://dummyimage.com/605x300" ,
];
export default function Carousel ({ images = imageList , id , imgLoad , userCarouselStyle , userImagesStyle }) {
const [ currentSlide , handleLeftArrow , handleRightArrow ] = useCarousel ( 0 , images . length );
return (
< section className = { ` ${ carouselStyle .carousel } ${ userCarouselStyle } ` }>
< div className = { ` ${ carouselStyle .arrows } ` }>
< span className = { ` ${ carouselStyle .leftArrow } ` } onClick = {handleLeftArrow}><</ span >
< span className = { ` ${ carouselStyle .rightArrow } ` } onClick = {handleRightArrow}>></ span >
</ div >
< div className = { ` ${ carouselStyle .images } ${ userImagesStyle } ` }>
{
images .map ((image , index) => {
return (
< figure key = {index} className = { ` ${ carouselStyle .slides } ` } style = {{ display : currentSlide === index ? "block" : "none" }}>
< img src = {image} alt = { `Slide ${ index + 1 } ` } loading = {imgLoad}/>
</ figure >
)
})
}
</ div >
</ section >
)
}
Carousel .propTypes = {
images : PropTypes .arrayOf ( PropTypes .string) ,
id : PropTypes .string ,
userCarouselStyle : PropTypes .string ,
userImagesStyle : PropTypes .string
};
Copy /* Carousel Styling */
/* Carousel container */
.carousel {
display : flex ;
align-items : center ;
overflow : hidden ;
border : 2 px solid rgb (179 , 175 , 175) ;
border-radius : .5 rem ;
position : relative ;
width : 50 rem ;
height : 25 rem ;
}
/* Arrows container */
.arrows {
cursor : pointer ;
display : flex ;
justify-content : center ;
}
.leftArrow ,
.rightArrow {
position : absolute ;
font-size : 3 rem ;
top : 50 % ;
transform : translateY (-50 % ) ;
color : gray ;
}
.leftArrow {
left : 1 % ;
}
.rightArrow {
right : 1 % ;
}
/* images container */
.images {
display : flex ;
gap : 1 rem ;
overflow : hidden ;
}
.slides {
width : 100 % ;
}
.slides img {
width : 100 % ;
height : 100 % ;
}
/* responsive design */
@media ( width < 768 px ){
.carousel {
width : 40 rem ;
height : 20 rem ;
}
}
@media ( width < 471 px ){
.carousel {
width : 32 rem ;
height : 16 rem ;
}
}