Skip to content

Commit 6c13827

Browse files
authored
Merge pull request #9 from ItsMrAkhil/responsive
Add responsiveness to the carousel
2 parents ecced0d + 25807b0 commit 6c13827

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ const ExampleSlider = () => (
4343
ReactDOM.render(<ExampleSlider />, document.getElementById('root'));
4444
```
4545

46+
#### Responsive
47+
`react-styled-carousel` is responsive. Below is the example configuration of responsiveness.
48+
49+
> **Note:** If `responsive` prop is provided then, it'll override the cardsToShow prop. In other words cardsToShow will not work.
50+
51+
```js
52+
import React from 'react';
53+
import ReactDOM from 'react-dom'
54+
import Slider from 'react-styled-carousel';
55+
56+
const responsive = [
57+
{ breakPoint: 1280, cardsToShow: 4 }, // this will be applied if screen size is greater than 1280px. cardsToShow will become 4.
58+
{ breakPoint: 760, cardsToShow: 2 },
59+
];
60+
61+
const ExampleSlider = () => (
62+
<Slider>
63+
<h1>1</h1>
64+
<h1>2</h1>
65+
<h1>3</h1>
66+
<h1>4</h1>
67+
</Slider>
68+
);
69+
70+
ReactDOM.render(<ExampleSlider />, document.getElementById('root'));
71+
```
72+
4673
#### Configurable Props
4774
Props | Type | Default Value | Description
4875
----- | ---- | ------------- | -----------
@@ -56,6 +83,7 @@ Props | Type | Default Value | Description
5683
`cardsToShow` | `Number` | Children length | How many cards to be shown for a single slide.
5784
`afterSlide` | `Function` | null | This function will be executed after every sliding is completed
5885
`beforeSlide` | `Function` | null | This function will be executed before starting every sliding
86+
`responsive` | `Array` | null | Use this for responsiveness [Documentation](#responsive)
5987

6088
License
6189
----

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-styled-carousel",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"description": "React styled-components carousel. No css is needed, fully configurable, SSR supported. Theming supported.",
55
"main": "build/Slider.js",
66
"scripts": {
@@ -53,6 +53,7 @@
5353
"webpack-dev-server": "^2.11.1"
5454
},
5555
"dependencies": {
56+
"lodash.sortby": "^4.7.0",
5657
"react": "^16.2.0",
5758
"styled-components": "^3.1.6"
5859
},

src/Root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Root = () => (
1010
<Container>
1111
<h1>Styled Carousel Example</h1>
1212
<Slider
13-
cardsToShow={2}
13+
cardsToShow={4}
1414
>
1515
<ExampleCard>1</ExampleCard>
1616
<ExampleCard>2</ExampleCard>

src/components/Slider.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/* global window */
12
import React from 'react';
23
import PropTypes from 'prop-types';
4+
import sortBy from 'lodash.sortby';
35

46
import SliderTrack from './SliderTrack';
57
import CardWrapper from './CardWrapper';
@@ -10,6 +12,14 @@ import SliderList from './SliderList';
1012
import Dots from './Dots';
1113
import DefaultDot from './Dot';
1214

15+
if (typeof window === 'undefined') {
16+
global.window = {
17+
addEventListener: () => null,
18+
removeEventListener: () => null,
19+
innerWidth: 1280,
20+
};
21+
}
22+
1323
class Slider extends React.Component {
1424
constructor(props) {
1525
super(props);
@@ -20,7 +30,12 @@ class Slider extends React.Component {
2030
this.renderDots = this.renderDots.bind(this);
2131
this.renderLeftArrow = this.renderLeftArrow.bind(this);
2232
this.renderRightArrow = this.renderRightArrow.bind(this);
23-
this.state = { initialCard: 0, childWidth: 0, cardsToShow: 0 };
33+
this.updateResponsiveView = this.updateResponsiveView.bind(this);
34+
this.state = {
35+
initialCard: 0,
36+
childWidth: 0,
37+
cardsToShow: 0,
38+
};
2439
}
2540

2641
componentDidMount() {
@@ -30,7 +45,33 @@ class Slider extends React.Component {
3045
this.setState({ // eslint-disable-line react/no-did-mount-set-state
3146
childWidth,
3247
cardsToShow: childrenCount,
33-
});
48+
}, () => this.updateResponsiveView());
49+
window.addEventListener('resize', this.updateResponsiveView);
50+
}
51+
52+
componentWillUnmount() {
53+
window.removeEventListener('resize', this.updateResponsiveView);
54+
}
55+
56+
updateResponsiveView() {
57+
const { children } = this.props;
58+
let { responsive } = this.props;
59+
const numberOfChildren = children ? children.length || 1 : 0;
60+
if (responsive) {
61+
responsive = sortBy(responsive, 'breakPoint');
62+
let updatedCardsToShow = this.state.cardsToShow;
63+
responsive.forEach(({ breakPoint, cardsToShow }) => {
64+
if (breakPoint <= window.innerWidth) {
65+
updatedCardsToShow = cardsToShow;
66+
}
67+
});
68+
const updatedInitialCard = (numberOfChildren - updatedCardsToShow) < this.state.initialCard ? (numberOfChildren - updatedCardsToShow) : this.state.initialCard;
69+
this.setState({
70+
cardsToShow: updatedCardsToShow,
71+
childWidth: 100 / updatedCardsToShow,
72+
initialCard: updatedInitialCard,
73+
});
74+
}
3475
}
3576

3677
changeInitialCard(initialCard) {
@@ -155,6 +196,7 @@ Slider.defaultProps = {
155196
afterSlide: null,
156197
beforeSlide: null,
157198
infinite: true,
199+
responsive: null,
158200
};
159201

160202
Slider.propTypes = {
@@ -168,6 +210,10 @@ Slider.propTypes = {
168210
afterSlide: PropTypes.func,
169211
beforeSlide: PropTypes.func,
170212
infinite: PropTypes.bool,
213+
responsive: PropTypes.arrayOf(PropTypes.shape({
214+
breakPoint: PropTypes.number.isRequired,
215+
cardsToShow: PropTypes.number.isRequired,
216+
})),
171217
};
172218

173219
export default Slider;

0 commit comments

Comments
 (0)