Created
April 4, 2021 22:40
-
-
Save DonHuskini/ce1c350fd59e7014c726703318fb2726 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { useState } from "react"; | |
import styled from "styled-components"; | |
import { useSpringCarousel } from "react-spring-carousel-js"; | |
import CarouselItem from "./CarouselItem"; | |
import ArrowButton from "./ArrowButton"; | |
import { S as StyledArrowButton } from "./ArrowButton"; | |
const S = {}; | |
S.Carousel = styled.div` | |
display: flex; | |
position: relative; | |
width: 100%; | |
height: 200px; | |
&:hover ${StyledArrowButton.ArrowButton} { | |
opacity: 1; | |
} | |
`; | |
const Carousel = ({ items }) => { | |
const [isFirst, setFirst] = useState(true); | |
const [isLast, setLast] = useState(false); | |
const { | |
carouselFragment, | |
slideToNextItem, | |
slideToPrevItem, | |
useListenToCustomEvent | |
} = useSpringCarousel({ | |
items: items.map((item, index) => ({ | |
id: index, | |
renderItem: ( | |
<CarouselItem> | |
<img src={item} /> | |
</CarouselItem> | |
) | |
})) | |
}); | |
useListenToCustomEvent("onSlideChange", (data) => { | |
setFirst(data.currentItem === 0); | |
setLast(data.currentItem === items.length - 1); | |
}); | |
return ( | |
<S.Carousel> | |
<ArrowButton type="prev" isVisible={!isFirst} onClick={slideToPrevItem} /> | |
{carouselFragment} | |
<ArrowButton type="next" isVisible={!isLast} onClick={slideToNextItem} /> | |
</S.Carousel> | |
); | |
}; | |
export default Carousel; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment