UNPKG

20.4 kBMarkdownView Raw
1# React Responsive Carousel
2
3[![npm version](https://badge.fury.io/js/react-responsive-carousel.svg)](https://badge.fury.io/js/react-responsive-carousel)
4[![Build Status](https://travis-ci.org/leandrowd/react-responsive-carousel.svg?branch=master)](https://travis-ci.org/leandrowd/react-responsive-carousel)
5[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fleandrowd%2Freact-responsive-carousel.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fleandrowd%2Freact-responsive-carousel?ref=badge_shield)
6
7Powerful, lightweight and fully customizable carousel component for React apps.
8
9### Important
10
11I don't have any time available to keep maintaining this package. If you have any request, try to sort it within the community. I'm able to merge pull requests that look safe from time to time but no commitment on timelines here. Feel free to fork it and publish under other name if you are in a hurry or to use another component.
12
13### Features
14
15- Responsive
16- Mobile friendly
17- Swipe to slide
18- Mouse emulating touch
19- Server side rendering compatible
20- Keyboard navigation
21- Custom animation duration
22- Auto play w/ custom interval
23- Infinite loop
24- Horizontal or Vertical directions
25- Supports images, videos, text content or anything you want. Each direct child represents one slide!
26- Supports external controls
27- Highly customizable:
28 - Custom thumbs
29 - Custom arrows
30 - Custom indicators
31 - Custom status
32 - Custom animation handlers
33
34### Important links:
35
36- [Codesandbox playground](https://codesandbox.io/s/github/leandrowd/react-responsive-carousel/tree/master/codesandbox/default)
37- [Storybook](http://react-responsive-carousel.js.org/storybook/)
38- [Changelog](https://github.com/leandrowd/react-responsive-carousel/blob/master/CHANGELOG.md)
39- [Before contributing](https://github.com/leandrowd/react-responsive-carousel/blob/master/CONTRIBUTING.md)
40- [Troubleshooting](https://github.com/leandrowd/react-responsive-carousel/blob/master/TROUBLESHOOTING.md)
41
42### Demo
43
44<http://leandrowd.github.io/react-responsive-carousel/>
45
46Check it out these [cool demos](http://react-responsive-carousel.js.org/storybook/index.html) created using [storybook](https://storybook.js.org/). The source code for each example is available [here](https://github.com/leandrowd/react-responsive-carousel/blob/master/stories/)
47
48Customize it yourself:
49
50- Codesandbox: <https://codesandbox.io/s/lp602ljjj7>
51
52### Installing as a package
53
54`yarn add react-responsive-carousel`
55
56### Usage
57
58```javascript
59import React, { Component } from 'react';
60import ReactDOM from 'react-dom';
61import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader
62import { Carousel } from 'react-responsive-carousel';
63
64class DemoCarousel extends Component {
65 render() {
66 return (
67 <Carousel>
68 <div>
69 <img src="assets/1.jpeg" />
70 <p className="legend">Legend 1</p>
71 </div>
72 <div>
73 <img src="assets/2.jpeg" />
74 <p className="legend">Legend 2</p>
75 </div>
76 <div>
77 <img src="assets/3.jpeg" />
78 <p className="legend">Legend 3</p>
79 </div>
80 </Carousel>
81 );
82 }
83});
84
85ReactDOM.render(<DemoCarousel />, document.querySelector('.demo-carousel'));
86
87// Don't forget to include the css in your page
88
89// Using webpack or parcel with a style loader
90// import styles from 'react-responsive-carousel/lib/styles/carousel.min.css';
91
92// Using html tag:
93// <link rel="stylesheet" href="<NODE_MODULES_FOLDER>/react-responsive-carousel/lib/styles/carousel.min.css"/>
94```
95
96### Props
97
98| Name | Value | Description |
99| ---------------------------------------- | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
100| ariaLabel | `string` | Define the `aria-label` attribute for the root carousel element. The default is `undefined`, skipping the attribute from markup. |
101| axis | `'horizontal'`, `'vertical'` | Define the direction of the slider, defaults to `'horizontal'`. |
102| autoFocus | `boolean` | Force focus on the carousel when it renders. |
103| autoPlay | `boolean` | Change the slide automatically based on `interval` prop. |
104| centerMode | `boolean` | Center the current item and set the slide width based on `centerSlidePercentage`. |
105| centerSlidePercentage | `number` | Define the width percentage relative to the carousel width when `centerMode` is `true`. |
106| dynamicHeight | `boolean` | The height of the items will not be fixed. |
107| emulateTouch | `boolean` | Enable swipe on non-touch screens when `swipeable` is `true`. |
108| infiniteLoop | `boolean` | Going after the last item will move back to the first slide. |
109| interval | `number` | Interval in milliseconds to automatically go to the next item when `autoPlay` is true, defaults to `3000`. |
110| labels | `object` | Apply `aria-label` on carousel with an `object` with the properties `leftArrow`, `rightArrow` and `item`. The default is `{leftArrow: 'previous slide / item', rightArrow: 'next slide / item', item: 'slide item'}`. |
111| onClickItem | `function` | Callback to handle a click event on a slide, receives the current index and item as arguments. |
112| onClickThumb | `function` | Callback to handle a click event on a thumb, receives the current index and item as arguments. |
113| onChange | `function` | Callback to handle every time the selected item changes, receives the current index and item as arguments. |
114| onSwipeStart | `function` | Callback to handle when the swipe starts, receives a touch event as argument. |
115| onSwipeEnd | `function` | Callback to handle when the swipe ends, receives a touch event as argument. |
116| onSwipeMove | `function` | Callback triggered on every movement while swiping, receives a touch event as argument. |
117| preventMovementUntilSwipeScrollTolerance | `boolean` | Don't let the carousel scroll until the user swipe to the value specified on `swipeScrollTolerance`. |
118| renderArrowPrev | `function` | Render custom previous arrow. Receives a click handler, a `boolean` that shows if there's a previous item, and the accessibility label as arguments. |
119| renderArrowNext | `function` | Render custom previous arrow. Receives a click handler, a `boolean` that shows if there's a next item, and the accessibility label as arguments. |
120| renderIndicator | `function` | Render custom indicator. Receives a click handler, a `boolean` that shows if the item is selected, the item index, and the accessibility label as arguments. |
121| renderItem | `function` | Render a custom item. Receives an item of the carousel, and an `object` with the `isSelected` property as arguments. |
122| renderThumbs | `function` | Render prop to show the thumbs, receives the carousel items as argument. Get the `img` tag of each item of the slider, and render it by default. |
123| selectedItem | `number` | Set the selected item, defaults to `0`. |
124| showArrows | `boolean` | Enable previous and next arrow, defaults to `true`. |
125| showStatus | `boolean` | Enable status of the current item to the total, defaults to `true`. |
126| showIndicators | `boolean` | Enable indicators to select items, defaults to `true`. |
127| showThumbs | `boolean` | Enable thumbs, defaults to `true`. |
128| statusFormatter | `function` | Formatter that returns the status as a `string`, receives the current item and the total count as arguments. Defaults to `{currentItem} of {total}` format. |
129| stopOnHover | `boolean` | The slide will not change by `autoPlay` on hover, defaults to `true`. |
130| swipeable | `boolean` | Enable the user to swipe the carousel, defaults to `true`. |
131| swipeScrollTolerance | `number` | How many pixels it's needed to change the slide when swiping, defaults to `5`. |
132| thumbWidth | `number` | Width of the thumb, defaults to `80`. |
133| transitionTime | `number` | Duration of the animation of changing slides. |
134| useKeyboardArrows | `boolean` | Enable the arrows to move the slider when focused. |
135| verticalSwipe | `'natural'`, `'standard'` | Set the mode of swipe when the axis is `'vertical'`. The default is `'standard'`. |
136| width | `number` or `string` | The width of the carousel, defaults to `100%`. |
137
138### Customizing
139
140#### Items (Slides)
141
142By default, each slide will be rendered as passed as children. If you need to customize them, use the prop `renderItem`.
143
144```
145renderItem: (item: React.ReactNode, options?: { isSelected: boolean }) => React.ReactNode;
146```
147
148#### Thumbs
149
150By default, thumbs are generated extracting the images in each slide. If you don't have images on your slides or if you prefer a different thumbnail, use the method `renderThumbs` to return a new list of images to be used as thumbs.
151
152```
153renderThumbs: (children: React.ReactChild[]) => React.ReactChild[]
154```
155
156#### Arrows
157
158By default, simple arrows are rendered on each side. If you need to customize them and the css is not enough, use the `renderArrowPrev` and `renderArrowNext`. The click handler is passed as argument to the prop and needs to be added as click handler in the custom arrow.
159
160```
161renderArrowPrev: (clickHandler: () => void, hasPrev: boolean, label: string) => React.ReactNode;
162renderArrowNext: (clickHandler: () => void, hasNext: boolean, label: string) => React.ReactNode;
163```
164
165#### Indicators
166
167By default, indicators will be rendered as those small little dots in the bottom part of the carousel. To customize them, use the `renderIndicator` prop.
168
169```
170renderIndicator: (
171 clickHandler: (e: React.MouseEvent | React.KeyboardEvent) => void,
172 isSelected: boolean,
173 index: number,
174 label: string
175) => React.ReactNode;
176```
177
178#### Take full control of the carousel
179
180If none of the previous options are enough, you can build your own controls for the carousel. Check an example at http://react-responsive-carousel.js.org/storybook/?path=/story/02-advanced--with-external-controls
181
182#### Custom Animations
183
184By default, the carousel uses the traditional 'slide' style animation. There is also a built in fade animation, which can be used by passing `'fade'` to the `animationHandler` prop. \*note: the 'fade' animation does not support swiping animations, so you may want to set `swipeable` to `false`
185
186If you would like something completely custom, you can pass custom animation handler functions to `animationHandler`, `swipeAnimationHandler`, and `stopSwipingHandler`. The animation handler functions accept props and state, and return styles for the contain list, default slide style, selected slide style, and previous slide style. Take a look at the fade animation handler for an idea of how they work:
187
188```javascript
189const fadeAnimationHandler: AnimationHandler = (props, state): AnimationHandlerResponse => {
190 const transitionTime = props.transitionTime + 'ms';
191 const transitionTimingFunction = 'ease-in-out';
192
193 let slideStyle: React.CSSProperties = {
194 position: 'absolute',
195 display: 'block',
196 zIndex: -2,
197 minHeight: '100%',
198 opacity: 0,
199 top: 0,
200 right: 0,
201 left: 0,
202 bottom: 0,
203 transitionTimingFunction: transitionTimingFunction,
204 msTransitionTimingFunction: transitionTimingFunction,
205 MozTransitionTimingFunction: transitionTimingFunction,
206 WebkitTransitionTimingFunction: transitionTimingFunction,
207 OTransitionTimingFunction: transitionTimingFunction,
208 };
209
210 if (!state.swiping) {
211 slideStyle = {
212 ...slideStyle,
213 WebkitTransitionDuration: transitionTime,
214 MozTransitionDuration: transitionTime,
215 OTransitionDuration: transitionTime,
216 transitionDuration: transitionTime,
217 msTransitionDuration: transitionTime,
218 };
219 }
220
221 return {
222 slideStyle,
223 selectedStyle: { ...slideStyle, opacity: 1, position: 'relative' },
224 prevStyle: { ...slideStyle },
225 };
226};
227```
228
229### Videos
230
231If your carousel is about videos, keep in mind that it's up to you to control which videos will play. Using the `renderItem` prop, you will get information saying if the slide is selected or not and can use that to change the video state. Only play videos on selected slides to avoid issues. Check an example at http://react-responsive-carousel.js.org/storybook/?path=/story/02-advanced--youtube-autoplay-with-custom-thumbs
232
233=======================
234
235### Contributing
236
237The [contributing guide](https://github.com/leandrowd/react-responsive-carousel/blob/master/CONTRIBUTING.md) contains details on how to create pull requests and setup your dev environment. Please read it before contributing!
238
239=======================
240
241### Raising issues
242
243When raising an issue, please add as much details as possible. Screenshots, video recordings, or anything else that can make it easier to reproduce the bug you are reporting.
244
245- A new option is to create an example with the code that causes the bug. Fork this [example from codesandbox](https://codesandbox.io/s/github/leandrowd/react-responsive-carousel/tree/master/codesandbox/default) and add your code there. Don't forget to fork, save and add the link for the example to the issue.
246
247=======================
248
249## License
250
251[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fleandrowd%2Freact-responsive-carousel.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fleandrowd%2Freact-responsive-carousel?ref=badge_large)
252
253```
254
255```