Created
December 6, 2021 08:02
-
-
Save kursademirtas/037219649b300d0109f9e9bb8d997869 to your computer and use it in GitHub Desktop.
Slider Component
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 { useKeenSlider } from "keen-slider/react"; | |
| import {SliderProductCard} from "@components/ui"; | |
| import { ArrowFilterIcon } from "@components/icons"; | |
| import { colors } from "styles/variables"; | |
| const ProductSlider = ({ | |
| background, | |
| withDots, | |
| products, | |
| title, | |
| productDetailSlide, | |
| }) => { | |
| const [currentSlide, setCurrentSlide] = useState(0); | |
| const [sliderRef, slider] = useKeenSlider({ | |
| initial: 0, | |
| slideChanged(s) { | |
| setCurrentSlide(s.details().relativeSlide); | |
| }, | |
| slidesPerView: 5, | |
| breakpoints: { | |
| "(max-width: 1024px)": { | |
| slidesPerView: 3, | |
| }, | |
| }, | |
| mode: "snap", | |
| spacing: 10, | |
| loop: true, | |
| }); | |
| return ( | |
| <SliderWrapper | |
| backgroundColor={background} | |
| title={title} | |
| productDetailSlide={productDetailSlide} | |
| > | |
| <Title>{title}</Title> | |
| <Slider ref={sliderRef} className="keen-slider"> | |
| {products.map((product) => { | |
| return ( | |
| <SliderProductCard | |
| key={product.id} | |
| product={product} | |
| ></SliderProductCard> | |
| ); | |
| })} | |
| </Slider> | |
| {slider && ( | |
| <SliderArrowContainer> | |
| <ArrowFilterIcon | |
| onClick={(e) => e.stopPropagation() || slider.prev()} | |
| disabled={currentSlide === 0} | |
| style={{ | |
| width: "18px", | |
| height: "30px", | |
| position: "absolute", | |
| right: "95%", | |
| top: "50%", | |
| transform: "rotate(180deg)", | |
| opacity: "0.3", | |
| cursor: "pointer", | |
| }} | |
| /> | |
| <ArrowFilterIcon | |
| onClick={(e) => e.stopPropagation() || slider.next()} | |
| disabled={currentSlide === slider.details().size - 1} | |
| style={{ | |
| width: "18px", | |
| height: "30px", | |
| position: "absolute", | |
| right: "4%", | |
| top: "50%", | |
| opacity: "0.3", | |
| cursor: "pointer", | |
| }} | |
| /> | |
| </SliderArrowContainer> | |
| )} | |
| {slider && withDots && ( | |
| <Dots> | |
| {[...Array(slider.details().size).keys()].map((idx) => { | |
| return ( | |
| <Dot | |
| key={idx} | |
| onClick={() => { | |
| slider.moveToSlideRelative(idx); | |
| }} | |
| isActive={currentSlide === idx} | |
| /> | |
| ); | |
| })} | |
| </Dots> | |
| )} | |
| </SliderWrapper> | |
| ); | |
| }; | |
| const SliderWrapper = styled.div` | |
| position: relative; | |
| background-color: ${(props) => props.backgroundColor}; | |
| width: 100vw; | |
| padding: 16px 70px; | |
| margin: 20px 0; | |
| @media (max-width: 768px) { | |
| padding: 0; | |
| } | |
| `; | |
| const Slider = styled.div` | |
| margin-top: 20px; | |
| padding: 0 3px; | |
| `; | |
| const Title = styled.h3` | |
| font-family: "Futura Now Headline Medium"; | |
| font-size: 14px; | |
| line-height: 1; | |
| letter-spacing: 1px; | |
| position: relative; | |
| padding: 20px 35px 10px 0; | |
| :before { | |
| position: absolute; | |
| content: ""; | |
| width: 80px; | |
| height: 2px; | |
| background-color: black; | |
| bottom: -13px; | |
| } | |
| @media (max-width: 768px) { | |
| padding-left: 20px; | |
| } | |
| `; | |
| const Dots = styled.div` | |
| display: flex; | |
| padding: 30px 0 10px; | |
| justify-content: center; | |
| overflow: auto; | |
| ::-webkit-scrollbar { | |
| display: none; | |
| } | |
| `; | |
| const Dot = styled.button` | |
| border: none; | |
| width: 8px; | |
| height: 8px; | |
| background: ${(props) => (props.isActive ? "black" : "#c5c5c5")}; | |
| border-radius: 50%; | |
| margin: 0 5px; | |
| padding: 5px; | |
| cursor: pointer; | |
| position: relative; | |
| ::after { | |
| content: ""; | |
| position: absolute; | |
| top: -4px; | |
| left: -4px; | |
| width: 16px; | |
| height: 16px; | |
| border: ${(props) => | |
| props.isActive ? `1px solid ${colors.darkestGray}` : "#c5c5c5"}; | |
| border-radius: 50px; | |
| } | |
| `; | |
| const SliderArrowContainer = styled.div` | |
| @media (max-width: 1024px) { | |
| display: none; | |
| } | |
| `; | |
| export default ProductSlider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment