Carousel component for React
npm install @brainhubeu/react-carousel
A pure extendable React carousel, powered by Brainhub (craftsmen who ❤️ JS)
Live code demo |
v1 migration guide |
Hire us
npm i @brainhubeu/react-carousel
`$3
`
npm i @types/brainhubeu__react-carousel -D
`$3
If you don't use any bundler like Webpack, you can add these scripts to your HTML file, body section:
`html
`
Make sure to use a version ending with -cdn.Then, you can use the following global variables:
-
BrainhubeuReactCarousel
- BrainhubeuReactCarouselDots
- BrainhubeuReactCarouselItem
- BrainhubeuReactCarouselWrapper$3
When using @brainhubeu/react-carousel with SSR (Server-side Rendering), we recommend Next.js as @brainhubeu/react-carousel currently doesn't work on the server side so it must be rendered on the client side (maybe we'll provide server-side working in the future).
`js
import dynamic from 'next/dynamic';const { default: Carousel, Dots } = dynamic(
() => require('@brainhubeu/react-carousel'),
{ ssr: false },
);
`Usage
By default, the component does not need anything except children to render a simple carousel.
Remember that styles do not have to be imported every time you use carousel, you can do it once in an entry point of your bundle.
`javascript
import React from 'react';
import Carousel from '@brainhubeu/react-carousel';
import '@brainhubeu/react-carousel/lib/style.css';const MyCarousel = () => (



);
export default MyCarousel;
`
$3
There is a separate Dots component that can be used to fully control navigation dots or add thumbnails.
`javascript
import Carousel, { Dots } from '@brainhubeu/react-carousel';
import '@brainhubeu/react-carousel/lib/style.css'; import { useState } from 'react';const MyCarouselWithDots = () => {
const [value, setValue] = useState(0);
const onChange = value => {
setValue(value);
}
return (
value={value}
onChange={onChange}
>

...

value={this.state.value}
onChange={this.onChange}
thumbnails={[
(
),
...
(
),
]}
/>
);
};export default MyCarouselWithDots;
`
Props
You can access a clickable demo with many examples and a live code editor by clicking on a Prop name.$3
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | Number |
undefined | Current slide's index (zero based, depends on the elements order) |
| onChange | Function | undefined | Handler triggered when current slide is about to change (e.g. on arrow click or on swipe) |
| slides | Array | undefined | Alternative way to pass slides. This prop expects an array of JSX
elements |
| itemWidth | Number | undefined | Determines custom width for every slide in the carousel |
| offset | Number | 0 | Padding between items |
| animationSpeed | Number | 500 | Determines transition duration in milliseconds |
| draggable | Boolean | true | Makes it possible to drag to the next slide with mouse cursor |
| breakpoints | Object | undefined | All props can be set to different values on different screen resolutions |$3
You can extend react-carousel default behavior by applying plugins shipped within carousel or by creating and applying your own ones$3
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | Number | slide position in the slides Array | Current
Carousel value |
| onChange | Function | undefined | onChange callback (works the same way as onChange in Carousel component) |
| number | Number | Amount of slides | Number of slides in the carousel you want to control |
| thumbnails | Array of ReactElements | undefined | Array of thumbnails to show. If not provided, default dots will be shown |
| rtl | Boolean | false | Indicating if the dots should have direction from Right to Left |$3
- git clone https://github.com/brainhubeu/react-carousel
- cd react-carousel
- yarn
- yarn start-demo
- open http://localhost:8000/$3
Each test command should be run from the root directory.#### Unit tests
`
yarn test:unit:coverage
`#### E2E tests
`
yarn test:e2e
``react-carousel is copyright © 2018-2020 Brainhub. It is free software and may be redistributed under the terms specified in the license.
react-carousel is maintained by the Brainhub development team. It is funded by Brainhub and the names and logos for Brainhub are trademarks of Brainhub Sp. z o.o.. You can check other open-source projects supported/developed by our teammates here.

We love open-source JavaScript software! See our other projects or hire us to build your next web, desktop and mobile application with JavaScript.