react-carousel
Feature-rich, react-way carousel component that does not suck.
Why?
There are some great carousels (like slick) that do not have real React implementations. This library provides you with carousel that is not merely a wrapper for some jQuery solution, can be used as controlled or uncontrolled element (similar to inputs), and has tons of useful features.
Installation
npm i @brainhubeu/react-carousel
Demo
You can access a clickable demo with many examples and a live code editor here.
Typescript
npm i @types/brainhubeu__react-carousel -D
Usage
By default the component does not need anything except children to render simple carousel.
Remember that styles does not have to be imported everytime you use carousel, you can do it once in an entry point of your bundle.
Carousel as controlled element
You can control which slides are being shown by providing Carousel with value and onChange props
Where:
value
is the current slide's index (zero based, in the example above imageOne has index 0, imageTwo has index 1 and so on)onChange
handler triggered when carousel wants to change current slide (e.g. on arrow click or on swipe)
Default arrows
You can turn default arrows on with arrows
prop
Where:
arrows
is boolean flag that determines usage of default arrows
Custom arrows
You can define custom components for arrows using arrowLeft
and arrowRight
props. If you don't need to change default click listeners, add addArrowClickHandler
prop. Otherwise, add click event handlers to your custom components.
Where:
arrowLeft
andarrowRight
are React components for arrowsaddArrowClickHandler
is boolean flag - default click event handlers are used when it's set to true
Passing slides as a prop
Where:
slides
is an array of slides (it must be an array, if you want to use a component as an only slide you have to pass it as a child)
Additional options
Where:
slidesPerPage
is the number of slides visible at onceslidesPerScroll
is the number by which value will change on autoPlay and when arrow is clickedanimationSpeed
(number) transition duration in millisecondsautoPlay
(number) slide change interval in millisecondsstopAutoPlayOnHover
(boolean) determines if autoPlay should stop when mouse hover over carouseloffset
(number) padding between itemsitemWidth
(number) determines custom width for each slide in carouselclickToChange
boolean indicating if clicking on a slide should trigger changing the current valuecentered
boolean indicating if the current active slide should be aligned to the center or to the left of a carousel
Infinite
Responsive
All props (except value, onChange, responsive, children) can be set to different values on different screen resolution
Note: In the example above you cannot leave clickToChange value out in 500 breakpoint. In that case value from 1000 breakpoint will not be applied as the options are not inherited from higher to lower resolutions, only from default to current resolution.
Showing dots or thumbnails
There is a separate Dots component that can be used to show navigation dots.
It can also show thumbnails instead of dots
Unit tests
npm run test-unit
Regression tests
Install selenium if it is not installed
npm run selenium-install
1. npm run selenium-start
2. npm run example
3. npm run test-regression