Last active
April 25, 2018 17:13
-
-
Save charlesbodman/88a58f67fb12fa0ff81acc59322ff15b to your computer and use it in GitHub Desktop.
AutoSlide component of the Nuka Carousel
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 React, {Component} from 'react'; | |
| import Carousel from 'nuka-carousel'; | |
| import shallowCompare from 'react-addons-shallow-compare'; | |
| class AutoCarousel extends Component { | |
| constructor() | |
| { | |
| super(); | |
| this.autoPlayTimeout = false; | |
| } | |
| componentWillUnmount() { | |
| this.stopAutoSlide(); | |
| } | |
| componentDidMount() { | |
| this.autoSlide(); | |
| } | |
| shouldComponentUpdate(nextProps, nextState) { | |
| return shallowCompare(this, nextProps, nextState); | |
| } | |
| stopAutoSlide() | |
| { | |
| clearTimeout(this.autoPlayTimeout); | |
| } | |
| autoSlide() | |
| { | |
| if(this.props.autoTime) | |
| { | |
| var carousel = this.carousel; | |
| var carouselState = carousel.state; | |
| var nextSlide = carouselState.currentSlide + 1; | |
| if(nextSlide >= carouselState.slideCount) | |
| { | |
| nextSlide = 0; | |
| } | |
| this.stopAutoSlide(); | |
| this.autoPlayTimeout = setTimeout(carousel.goToSlide.bind(null,nextSlide),this.props.autoTime); | |
| } | |
| } | |
| afterSlide() | |
| { | |
| this.autoSlide(); | |
| } | |
| render() | |
| { | |
| return (<Carousel ref={(c)=>this.carousel = c} {...this.props } afterSlide={this.afterSlide.bind(this)} > | |
| {this.props.children} | |
| </Carousel>); | |
| } | |
| } | |
| module.exports = AutoCarousel; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage