Created
March 1, 2020 21:45
-
-
Save DZuz14/785c7a5d96ce56153e0d1cde3adfe152 to your computer and use it in GitHub Desktop.
Slider Final Part 2
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
/** @jsx jsx */ | |
import React, { useState, useEffect, useRef } from 'react' | |
import { css, jsx } from '@emotion/core' | |
import SliderContent from './SliderContent' | |
import Slide from './Slide' | |
import Arrow from './Arrow' | |
import Dots from './Dots' | |
const getWidth = () => window.innerWidth | |
/** | |
* @function Slider | |
*/ | |
const Slider = props => { | |
const [state, setState] = useState({ | |
activeIndex: 0, | |
translate: 0, | |
transition: 0.45 | |
}) | |
const { translate, transition, activeIndex } = state | |
const autoPlayRef = useRef() | |
useEffect(() => { | |
autoPlayRef.current = nextSlide | |
}) | |
useEffect(() => { | |
const play = () => { | |
autoPlayRef.current() | |
} | |
const interval = setInterval(play, props.autoPlay * 1000) | |
return () => clearInterval(interval) | |
}, []) | |
const nextSlide = () => { | |
if (activeIndex === props.slides.length - 1) { | |
return setState({ | |
...state, | |
translate: 0, | |
activeIndex: 0 | |
}) | |
} | |
setState({ | |
...state, | |
activeIndex: activeIndex + 1, | |
translate: (activeIndex + 1) * getWidth() | |
}) | |
} | |
const prevSlide = () => { | |
if (activeIndex === 0) { | |
return setState({ | |
...state, | |
translate: (props.slides.length - 1) * getWidth(), | |
activeIndex: props.slides.length - 1 | |
}) | |
} | |
setState({ | |
...state, | |
activeIndex: activeIndex - 1, | |
translate: (activeIndex - 1) * getWidth() | |
}) | |
} | |
return ( | |
<div css={SliderCSS}> | |
<SliderContent | |
translate={translate} | |
transition={transition} | |
width={getWidth() * props.slides.length} | |
> | |
{props.slides.map((slide, i) => ( | |
<Slide key={slide + i} content={slide} /> | |
))} | |
</SliderContent> | |
{!props.autoPlay && ( | |
<> | |
<Arrow direction="left" handleClick={prevSlide} /> | |
<Arrow direction="right" handleClick={nextSlide} /> | |
</> | |
)} | |
<Dots slides={props.slides} activeIndex={activeIndex} /> | |
</div> | |
) | |
} | |
Slider.defaultProps = { | |
slides: [], | |
autoPlay: null | |
} | |
const SliderCSS = css` | |
position: relative; | |
height: 100vh; | |
width: 100vw; | |
margin: 0 auto; | |
overflow: hidden; | |
` | |
export default Slider |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment