After spending hours trying to find a React carousel that fit my extremely basic needs:
- No style sheets to force my way into to customize the carousel.
- Supply a component or markup to the carousel.
- Autosize the size of the slide to the parent container.
- Control the carousel with any button or component.
- Loop.
- Set initial slide index.
- Disable tabbing to next slide. This is the most important as I was using a form in each slide and didn't want the carousel to jump to the next slide until the current slide was validated.
I decided to roll yet another carousel because my basic needs couldn't be met by the masses.
1.1.0 Update: Added autoplay and swipe left/right to navigate through slides on mobile. Added autoplay.
npm i --save reactium-carousel
import React from 'react';
import { Carousel, Slide } from 'reactium-carousel';
// Your component
export default class Demo extends React.Component {
constructor(props) {
super(props);
// Create a reference to the carousel
// so we can control it with buttons
this.carousel = null;
}
render() {
return (
<div>
<div style={{width: 500, height: 500}}>
<Carousel speed={0.25} loop={true} startIndex={0} ref={elm => (this.carousel = elm)}>
<Slide>SLIDE - 1</Slide>
<Slide>SLIDE - 2</Slide>
<Slide>SLIDE - 3</Slide>
</Carousel>
</div>
<div>
<button type='button' onClick={() => this.carousel.prev()}>
back
</button>
<button type='button' onClick={() => this.carousel.next()}>
next
</button>
<button type='button' onClick={() => this.carousel.jumpTo(2)}>
Slide - 3
</button>
</div>
</div>
)
}
}
Boolean : Autoplay the slides and display for a fixed period of time: duration
.
Default : false
Number : Time in seconds to display a slide when autoplay
is true
.
Default : 10
Boolean : Loop back to the first slide when at the end of the slides list.
Default : false
Boolean : Pause autoplay
on mouse hover.
Default : true
Number : Time in seconds of the slide animation.
Default : 0.5
Number : Zero based integer that sets the initial slide displayed.
Default : 0
Object : Style object applied to the Carousel.container
DOM element.
Boolean : Enable/Disable swipe navigation when in a mobile view.
Default : true
Boolean : The animation status.
DOMElement : The Carousel wrapper <div>
element.
Number : The active slide index.
Boolean : The autoplay status.
Navigate to the next slide. If loop is true
, navigate to the first slide.
Navigate to the prev slide. If loop is true
, navigate to the last slide.
Navigate to the specified slide index.
Start the autoplay. This will reset the timer back to zero.
Pause the autoplay and sets the paused
property to: true.
Resume the autoplay and sets the paused
property to: false.
Stop the autoplay. This will reset the timer back to zero.
Triggered after the animation has completed.
Triggered after the animation has completed and state update.
Triggered before the next/previous animation.
Triggered when play()
function is called.
Triggered when the pause()
function is called.
Triggered when the resume()
function is called.
Triggered when the stop()
function is called.
These features we not apart of my initial release because I didn't need them at the time.
Autoplay. Added in 1.1.0Swipe next/prev. Added in 1.1.0
The src is built on Reactium.. learn that $#!+
No really PRs are more than welcome...
Clone the source repo from here.
Install dependencies and run locally:
$ cd /Your/Copy/of/repo
$ npm install && npm run local
Navigate to the ~/src/app/components/ReCarousel
directory.
Profit.