Skip to content

Commit 248e09e

Browse files
author
Florian Bernard
authored
Merge pull request #4 from webcom-components/stackParameters
Allow stack parameters to be changed
2 parents 86566ff + 3eaca65 commit 248e09e

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ You decide how many cards will be simultaneously visible depending on screen siz
77
Visually you have a left stack of cards, visible cards in the middle, and a right stack.
88

99
## New in 2.x
10-
Using stacks instead of sliding cards out of the screen
11-
Pass children CSS class as props
12-
Doesn't use react-motion anymore
10+
11+
**New in 2.1:** expose stacks parameters
12+
Using stacks instead of sliding cards out of the screen
13+
Pass children CSS class as props
14+
Doesn't use react-motion anymore
1315

1416
## Installation
1517
```bash
@@ -29,12 +31,18 @@ You can know where a card is with the ```getCardOffset``` function in JavaScript
2931

3032
Use default navigation arrows with props ```showArrows={true}```
3133

34+
You can change the default parameters ```visibleStack``` et ```stackSpace```, which determine how many cards of the stacks are shifted on the sides, and by how many pixels.
35+
3236
## Example demo
3337
![react-card-scroll](https://cloud.githubusercontent.com/assets/11945259/15610699/db52c656-2426-11e6-9228-dd622dadfb86.gif)
3438

3539
## Some (very) basic usage
3640
```jsx
37-
<CardScroll ref="cardScroll" childrenClass="rcs-col-sm-6 rcs-col-md-4">
41+
<CardScroll
42+
ref="cardScroll"
43+
childrenClass="rcs-col-sm-6 rcs-col-md-4"
44+
visibleStack={1}
45+
stackSpace={25}>
3846
<div>
3947
Hello
4048
</div>

example/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ const Example = React.createClass({
8787
<div>
8888
<button onClick={this.addCard}>Add card</button>
8989
<button onClick={this.removeCard}>Remove last card</button>
90-
<CardScroll ref="cardScroll" childrenClass="col rcs-col-sm-6 rcs-col-md-4 rcs-col-lg-3">
90+
<CardScroll
91+
ref="cardScroll"
92+
childrenClass="col rcs-col-sm-6 rcs-col-md-4 rcs-col-lg-3"
93+
visibleStack={1}
94+
stackSpace={25}>
9195
{this.state.cards.map((el, index) => React.cloneElement(el, {
9296
scrollCards,
9397
getCardOffset: getCardOffset(index)

lib/assets/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
._1H7C65wd8WJqBtCRmAfokb {
22
position: relative;
3+
z-index: 0;
34
box-sizing: border-box; }
45
._1H7C65wd8WJqBtCRmAfokb .rcs-center, ._1H7C65wd8WJqBtCRmAfokb .rcs-left-stack, ._1H7C65wd8WJqBtCRmAfokb .rcs-right-stack {
56
position: absolute;

lib/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ var defaultWidths = {
3434

3535
var CardScroll = _react2.default.createClass({
3636
displayName: 'CardScroll',
37+
getDefaultProps: function getDefaultProps() {
38+
return {
39+
visibleStack: 3,
40+
stackSpace: 5
41+
};
42+
},
3743
getInitialState: function getInitialState() {
3844
return { currentCard: 0 };
3945
},
4046
componentWillMount: function componentWillMount() {
41-
this.maxOffset = (0, _offset.getMaxOffset)();
47+
this.maxOffset = (0, _offset.getMaxOffset)({ visibleStack: this.props.visibleStack, stackSpace: this.props.stackSpace });
4248
this.widths = defaultWidths;
4349
this.children = {};
4450
},
@@ -73,9 +79,9 @@ var CardScroll = _react2.default.createClass({
7379
return defaultWidths;
7480
}
7581
// anticipate the margin that we will add
76-
var container = this._container.getBoundingClientRect().width - this.maxOffset * 2;
82+
var container = this._container.getBoundingClientRect().width;
7783
// get first child as all children should be of equal width
78-
var card = _reactDom2.default.findDOMNode(this._child).getBoundingClientRect().width * container / this._container.getBoundingClientRect().width;
84+
var card = _reactDom2.default.findDOMNode(this._child).getBoundingClientRect().width;
7985
return {
8086
card: card,
8187
container: container
@@ -115,7 +121,13 @@ var CardScroll = _react2.default.createClass({
115121
style: { marginLeft: this.maxOffset, marginRight: this.maxOffset },
116122
ref: updateContainer },
117123
_react2.default.Children.map(this.props.children, function (child, index) {
118-
var offset = (0, _offset.getOffset)({ index: index, firstVisibleIndex: currentCard, lastVisibleIndex: lastCard });
124+
var offset = (0, _offset.getOffset)({
125+
index: index,
126+
firstVisibleIndex: currentCard,
127+
lastVisibleIndex: lastCard,
128+
visibleStack: _this2.props.visibleStack,
129+
stackSpace: _this2.props.stackSpace
130+
});
119131
var position = offset;
120132
var zIndex = 0;
121133
var className = "rcs-left-stack";
@@ -197,7 +209,13 @@ var CardScroll = _react2.default.createClass({
197209
* return negative number if card is on left stack, 0 if visible, positive number if card is on right stack
198210
*/
199211
getCardOffset: function getCardOffset(index) {
200-
return (0, _offset.getOffset)({ index: index, firstVisibleIndex: this.state.currentCard, lastVisibleIndex: this.lastVisibleCardIndex() });
212+
return (0, _offset.getOffset)({
213+
index: index,
214+
firstVisibleIndex: this.state.currentCard,
215+
lastVisibleIndex: this.lastVisibleCardIndex(),
216+
visibleStack: this.props.visibleStack,
217+
stackSpace: this.props.stackSpace
218+
});
201219
}
202220
});
203221

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-card-scroll",
3-
"version": "2.0.5",
3+
"version": "2.1.0",
44
"description": "A React component to navigate horizontally between cards of same width",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ const defaultWidths = {
1010
}
1111

1212
let CardScroll = React.createClass({
13+
14+
getDefaultProps(){
15+
return {
16+
visibleStack: 3,
17+
stackSpace: 5
18+
}
19+
},
20+
1321
getInitialState(){
1422
return {currentCard: 0}
1523
},
1624

1725
componentWillMount() {
18-
this.maxOffset = getMaxOffset()
26+
this.maxOffset = getMaxOffset({visibleStack: this.props.visibleStack, stackSpace: this.props.stackSpace})
1927
this.widths = defaultWidths
2028
this.children = {}
2129
},
@@ -53,10 +61,9 @@ let CardScroll = React.createClass({
5361
if(childrenCount==0){
5462
return defaultWidths
5563
}
56-
// anticipate the margin that we will add
57-
const container = this._container.getBoundingClientRect().width-this.maxOffset*2
64+
const container = this._container.getBoundingClientRect().width
5865
// get first child as all children should be of equal width
59-
const card = ReactDOM.findDOMNode(this._child).getBoundingClientRect().width*container/this._container.getBoundingClientRect().width
66+
const card = ReactDOM.findDOMNode(this._child).getBoundingClientRect().width
6067
return {
6168
card,
6269
container
@@ -87,7 +94,13 @@ let CardScroll = React.createClass({
8794
style={{marginLeft: this.maxOffset, marginRight: this.maxOffset}}
8895
ref={updateContainer}>
8996
{React.Children.map(this.props.children, (child, index) => {
90-
const offset = getOffset({index, firstVisibleIndex:currentCard, lastVisibleIndex:lastCard})
97+
const offset = getOffset({
98+
index,
99+
firstVisibleIndex:currentCard,
100+
lastVisibleIndex:lastCard,
101+
visibleStack: this.props.visibleStack,
102+
stackSpace: this.props.stackSpace
103+
})
91104
let position = offset
92105
let zIndex = 0
93106
let className = "rcs-left-stack"
@@ -165,7 +178,13 @@ let CardScroll = React.createClass({
165178
* return negative number if card is on left stack, 0 if visible, positive number if card is on right stack
166179
*/
167180
getCardOffset(index){
168-
return getOffset({index, firstVisibleIndex:this.state.currentCard, lastVisibleIndex:this.lastVisibleCardIndex()})
181+
return getOffset({
182+
index,
183+
firstVisibleIndex:this.state.currentCard,
184+
lastVisibleIndex:this.lastVisibleCardIndex(),
185+
visibleStack: this.props.visibleStack,
186+
stackSpace: this.props.stackSpace
187+
})
169188
}
170189
})
171190

0 commit comments

Comments
 (0)