Skip to content

Commit 64c13cc

Browse files
authored
Merge pull request #10 from ItsMrAkhil/auto-slide
Add auto slider functionality
2 parents 6c13827 + 95e0ec1 commit 64c13cc

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ Props | Type | Default Value | Description
8484
`afterSlide` | `Function` | null | This function will be executed after every sliding is completed
8585
`beforeSlide` | `Function` | null | This function will be executed before starting every sliding
8686
`responsive` | `Array` | null | Use this for responsiveness [Documentation](#responsive)
87+
`autoSlide` | `Number` or `Boolean` | `2000` | Used to make carousel auto slide for every given time interval. Or for 2000ms if the prop value is true.
88+
`pauseOnMouseOver`| `Boolean` | `true` | Pause auto sliding on mouse over the carousel.
8789

8890
License
8991
----

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-styled-carousel",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "React styled-components carousel. No css is needed, fully configurable, SSR supported. Theming supported.",
55
"main": "build/Slider.js",
66
"scripts": {

src/components/Slider.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SliderWrapper from './SliderWrapper';
1111
import SliderList from './SliderList';
1212
import Dots from './Dots';
1313
import DefaultDot from './Dot';
14+
import Timer from '../utils/Timer';
1415

1516
if (typeof window === 'undefined') {
1617
global.window = {
@@ -39,14 +40,27 @@ class Slider extends React.Component {
3940
}
4041

4142
componentDidMount() {
42-
const { children, cardsToShow } = this.props;
43+
const { children, cardsToShow, autoSlide } = this.props;
4344
const childrenCount = cardsToShow || (children ? children.length || 1 : 1);
4445
const childWidth = 100 / childrenCount;
4546
this.setState({ // eslint-disable-line react/no-did-mount-set-state
4647
childWidth,
4748
cardsToShow: childrenCount,
4849
}, () => this.updateResponsiveView());
4950
window.addEventListener('resize', this.updateResponsiveView);
51+
if (autoSlide) {
52+
this.autoSlider = new Timer(() => {
53+
let updatedInitialCard = 0;
54+
const numberOfChildren = children ? children.length || 1 : 0;
55+
if (numberOfChildren - this.state.cardsToShow > this.state.initialCard) {
56+
updatedInitialCard = this.state.initialCard + 1;
57+
}
58+
this.setState({
59+
initialCard: updatedInitialCard,
60+
});
61+
}, (autoSlide === true ? 2000 : autoSlide));
62+
this.autoSlider.start();
63+
}
5064
}
5165

5266
componentWillUnmount() {
@@ -164,12 +178,17 @@ class Slider extends React.Component {
164178
const {
165179
children, cardsToShow,
166180
showDots, showArrows,
181+
pauseOnMouseOver,
167182
...otherProps
168183
} = this.props;
169184
const { initialCard, childWidth } = this.state;
170185
return (
171186
<React.Fragment>
172-
<SliderWrapper {...otherProps}>
187+
<SliderWrapper
188+
onMouseLeave={() => pauseOnMouseOver && this.autoSlider && this.autoSlider.resume()}
189+
onMouseEnter={() => pauseOnMouseOver && this.autoSlider && this.autoSlider.pause()}
190+
{...otherProps}
191+
>
173192
{showArrows && this.renderLeftArrow()}
174193
<SliderTrack>
175194
<SliderList translateX={initialCard * childWidth}>
@@ -197,6 +216,8 @@ Slider.defaultProps = {
197216
beforeSlide: null,
198217
infinite: true,
199218
responsive: null,
219+
autoSlide: 2000,
220+
pauseOnMouseOver: true,
200221
};
201222

202223
Slider.propTypes = {
@@ -211,9 +232,14 @@ Slider.propTypes = {
211232
beforeSlide: PropTypes.func,
212233
infinite: PropTypes.bool,
213234
responsive: PropTypes.arrayOf(PropTypes.shape({
214-
breakPoint: PropTypes.number.isRequired,
215-
cardsToShow: PropTypes.number.isRequired,
235+
breakPoint: PropTypes.number,
236+
cardsToShow: PropTypes.number,
216237
})),
238+
autoSlide: PropTypes.oneOfType([
239+
PropTypes.number,
240+
PropTypes.bool,
241+
]),
242+
pauseOnMouseOver: PropTypes.bool,
217243
};
218244

219245
export default Slider;

src/utils/Timer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Timer {
2+
constructor(callback, delay) {
3+
this.callback = callback;
4+
this.delay = delay;
5+
}
6+
7+
pause() {
8+
clearInterval(this.timerId);
9+
}
10+
11+
resume() {
12+
clearInterval(this.timerId);
13+
this.timerId = setInterval(this.callback, this.delay);
14+
}
15+
16+
start() {
17+
this.resume();
18+
}
19+
}
20+
21+
export default Timer;

0 commit comments

Comments
 (0)