Skip to content

Commit c74744b

Browse files
author
wfbn8821
committed
fix override of css classes, rcs class is now a props of cardScroll
1 parent 6a8e56d commit c74744b

File tree

6 files changed

+88
-107
lines changed

6 files changed

+88
-107
lines changed

README.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Visually you have a left stack of cards, visible cards in the middle, and a righ
88

99
## New in 2.x
1010
Using stacks instead of sliding cards out of the screen
11+
Pass children CSS class as props
1112
Doesn't use react-motion anymore
1213

1314
## Installation
@@ -19,34 +20,35 @@ npm i -S react-card-scroll
1920

2021
Import css either in sass ```~react-card-scroll/lib/assets/styles.css``` or in javascript with webpack
2122

22-
Use bootstrap style to decide how many cards will be visible (just add ```rcs-```): ```rcs-col-*-*```. You don't have to include bootstrap.
23+
Use bootstrap style to decide how many cards will be visible at a give screen size (just add ```rcs-```): ```rcs-col-*-*```. You don't have to include bootstrap.
2324

2425
Choose how you want to implement the navigation by using ```scrollCards({toLeft: true/false, number:*})```.For example, you can scroll when you click on a stack or when you wheel your mouse with the pointer on a card title.
25-
See the example: ```npm run example``` and open localhost:8081. And get inspiration from the source code.
26+
See the example: ```npm run example``` and open ```localhost:8081```. And get inspiration from the source code.
2627

2728
You can know where a card is with the ```getCardOffset``` function in JavaScript, or with the CSS classes ```rcs-left-stack rcs-center rcs-right-stack```
2829

29-
Use arrows with props ```showArrows={true}```
30+
Use default navigation arrows with props ```showArrows={true}```
3031

3132
## Example demo
3233
![react-card-scroll](https://cloud.githubusercontent.com/assets/11945259/15610699/db52c656-2426-11e6-9228-dd622dadfb86.gif)
3334

3435
## Some (very) basic usage
3536
```jsx
36-
<CardScroll ref="cardScroll">
37-
<div className="rcs-col-sm-6 rcs-col-md-4">
37+
<CardScroll ref="cardScroll" childrenClass="rcs-col-sm-6 rcs-col-md-4">
38+
<div>
39+
Hello
3840
</div>
3941

40-
<div className="rcs-col-sm-6 rcs-col-md-4">
42+
<div>
43+
World
4144
</div>
4245

43-
<div className="rcs-col-sm-6 rcs-col-md-4">
46+
<div>
47+
Foo
4448
</div>
4549

46-
<div className="rcs-col-sm-6 rcs-col-md-4">
47-
</div>
48-
49-
<div className="rcs-col-sm-6 rcs-col-md-4">
50+
<div>
51+
Bar
5052
</div>
5153
</CardScroll>
5254
```
@@ -58,15 +60,4 @@ this.refs.cardScroll.scrollCards({toLeft: true, number:1})
5860
```
5961

6062
## TODO
61-
- [x] Tests
62-
- [x] Example
63-
- Features
64-
- [x] CSS arrows
65-
- [x] Disable arrows if unable to navigate
66-
- [ ] ~~Mouse scroll triggers horizontal scroll~~ (it is up to the user, see example)
67-
- [x] Stacks
68-
- Fix
69-
- [x] Remove horizontal scroll bar
70-
- Technical
71-
- [x] CSS loader
72-
- [x] Improve packaging (babel + css)
63+
- [ ] Improve margin stack algorithm

example/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const ScrollableTitleCard = React.createClass({
1515
})
1616
},
1717
render() {
18-
const {title, children, className, style} = this.props
18+
const {title, children} = this.props
1919
const onClick = ev => {
2020
const offset = this.props.getCardOffset()
2121
if (offset != 0) {
@@ -24,14 +24,12 @@ const ScrollableTitleCard = React.createClass({
2424
}
2525
}
2626
return (
27-
<div className={"col rcs-col-sm-6 rcs-col-md-4 rcs-col-lg-3 "+className} style={style}>
28-
<div onClick={onClick} className="card">
29-
<div ref="title" className="card-header">
30-
{title}
31-
</div>
32-
<div className="card-block">
33-
{children}
34-
</div>
27+
<div onClick={onClick} className="card">
28+
<div ref="title" className="card-header">
29+
{title}
30+
</div>
31+
<div className="card-block">
32+
{children}
3533
</div>
3634
</div>
3735
)
@@ -89,7 +87,7 @@ const Example = React.createClass({
8987
<div>
9088
<button onClick={this.addCard}>Add card</button>
9189
<button onClick={this.removeCard}>Remove last card</button>
92-
<CardScroll ref="cardScroll">
90+
<CardScroll ref="cardScroll" childrenClass="col rcs-col-sm-6 rcs-col-md-4 rcs-col-lg-3">
9391
{this.state.cards.map((el, index) => React.cloneElement(el, {
9492
scrollCards,
9593
getCardOffset: getCardOffset(index)

lib/index.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var _lodash = require('lodash');
1616

1717
var _styles = {
1818
"container": "_1H7C65wd8WJqBtCRmAfokb",
19-
"stack": "_358enaiolY7BZPuXLuxdRA",
2019
"rightArrow": "_22yQcZhJvmCSA52vE9VrUe",
2120
"arrow": "_1y88-b9gQ_tGRfPBQpxMfo",
2221
"leftArrow": "_1bIeR72ojeoWY-MxcIhjT-"
@@ -77,6 +76,7 @@ var CardScroll = _react2.default.createClass({
7776
var container = this._container.getBoundingClientRect().width - this.maxOffset * 2;
7877
// get first child as all children should be of equal width
7978
var card = _reactDom2.default.findDOMNode(this._child).getBoundingClientRect().width * container / this._container.getBoundingClientRect().width;
79+
console.log({ card: card, container: container });
8080
return {
8181
card: card,
8282
container: container
@@ -86,6 +86,7 @@ var CardScroll = _react2.default.createClass({
8686
var _this2 = this;
8787

8888
var currentCard = this.state.currentCard;
89+
var childrenClass = this.props.childrenClass;
8990

9091
var lastCard = this.lastVisibleCardIndex();
9192
var updateContainer = function updateContainer(c) {
@@ -94,17 +95,16 @@ var CardScroll = _react2.default.createClass({
9495
var updateChild = function updateChild(c) {
9596
return c && (_this2._child = c);
9697
};
97-
var first = true;
9898
return _react2.default.createElement(
9999
'div',
100100
null,
101-
this.canScrollLeft() ? _react2.default.createElement(
101+
this.showArrows() && this.canScrollLeft() ? _react2.default.createElement(
102102
'div',
103103
{ className: _styles2.default.leftArrow,
104104
onClick: this.scrollCardsWrap({ toLeft: true }) },
105105
_react2.default.createElement('div', { className: _styles2.default.arrow })
106106
) : null,
107-
this.canScrollRight() ? _react2.default.createElement(
107+
this.showArrows() && this.canScrollRight() ? _react2.default.createElement(
108108
'div',
109109
{ className: _styles2.default.rightArrow,
110110
onClick: this.scrollCardsWrap() },
@@ -116,13 +116,6 @@ var CardScroll = _react2.default.createClass({
116116
style: { marginLeft: this.maxOffset, marginRight: this.maxOffset },
117117
ref: updateContainer },
118118
_react2.default.Children.map(this.props.children, function (child, index) {
119-
var props = { className: _styles2.default.stack };
120-
121-
if (first) {
122-
first = false;
123-
props.ref = updateChild;
124-
}
125-
126119
var offset = (0, _offset.getOffset)({ index: index, firstVisibleIndex: currentCard, lastVisibleIndex: lastCard });
127120
var position = offset;
128121
var zIndex = 0;
@@ -138,10 +131,12 @@ var CardScroll = _react2.default.createClass({
138131
zIndex = lastCard - index - 1;
139132
className = "rcs-right-stack";
140133
}
141-
props.style = { left: position, zIndex: zIndex };
142-
props.className = className + ' ' + _styles2.default.stack;
143-
144-
return _react2.default.cloneElement(child, props);
134+
var style = { left: position, zIndex: zIndex };
135+
return _react2.default.createElement(
136+
'div',
137+
{ className: className + ' ' + childrenClass, style: style, ref: index == 0 && updateChild },
138+
child
139+
);
145140
})
146141
)
147142
);
@@ -187,10 +182,13 @@ var CardScroll = _react2.default.createClass({
187182
return this.state.currentCard + this.getVisibleCardCount() - 1;
188183
},
189184
canScrollRight: function canScrollRight() {
190-
return this.props.showArrows && this.lastVisibleCardIndex() + 1 < _react2.default.Children.count(this.props.children);
185+
return this.lastVisibleCardIndex() + 1 < _react2.default.Children.count(this.props.children);
191186
},
192187
canScrollLeft: function canScrollLeft() {
193-
return this.props.showArrows && this.state.currentCard > 0;
188+
return this.state.currentCard > 0;
189+
},
190+
showArrows: function showArrows() {
191+
return this.props.showArrows;
194192
},
195193

196194

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.1",
3+
"version": "2.0.2",
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: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ let CardScroll = React.createClass({
5757
const container = this._container.getBoundingClientRect().width-this.maxOffset*2
5858
// get first child as all children should be of equal width
5959
const card = ReactDOM.findDOMNode(this._child).getBoundingClientRect().width*container/this._container.getBoundingClientRect().width
60+
console.log({card, container})
6061
return {
6162
card,
6263
container
6364
}
6465
},
65-
66+
6667
render() {
6768
const {currentCard} = this.state
69+
const {childrenClass} = this.props
6870
const lastCard = this.lastVisibleCardIndex()
6971
const updateContainer = c => c && (this._container = c)
7072
const updateChild = c => c && (this._child= c)
71-
let first = true
7273
return (
7374
<div>
7475
{this.showArrows() && this.canScrollLeft()?
@@ -87,13 +88,6 @@ let CardScroll = React.createClass({
8788
style={{marginLeft: this.maxOffset, marginRight: this.maxOffset}}
8889
ref={updateContainer}>
8990
{React.Children.map(this.props.children, (child, index) => {
90-
let props = {className: s.stack}
91-
92-
if(first){
93-
first = false
94-
props.ref= updateChild
95-
}
96-
9791
const offset = getOffset({index, firstVisibleIndex:currentCard, lastVisibleIndex:lastCard})
9892
let position = offset
9993
let zIndex = 0
@@ -109,10 +103,12 @@ let CardScroll = React.createClass({
109103
zIndex = lastCard-index-1
110104
className = "rcs-right-stack"
111105
}
112-
props.style = {left: position, zIndex}
113-
props.className = `${className} ${s.stack}`
114-
115-
return React.cloneElement(child, props);
106+
const style = {left: position, zIndex}
107+
return (
108+
<div className={`${className} ${childrenClass}`} style={style} ref={index == 0 && updateChild}>
109+
{child}
110+
</div>
111+
);
116112
})}
117113
</div>
118114
</div>

src/styles.scss

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
.container {
2-
position: relative;
3-
box-sizing: border-box;
4-
}
5-
6-
.container .stack{
7-
position: absolute;
8-
transition: left 1s cubic-bezier(0,.7,.7,1);
9-
box-sizing: border-box;
10-
}
11-
121
$border-color: #ccc;
132
$padding: 4px;
143
$outMargin: -15px;
@@ -27,42 +16,51 @@ $outPadding: $padding - $outMargin;
2716
transition: margin 0.5s;
2817
}
2918

30-
.rightArrow {
31-
@include arrow;
32-
background: linear-gradient(to right, white, #eee);
33-
right: 0;
34-
border-left: $border-color solid 1px;
35-
border-radius: 2px 0 0 2px;
36-
margin-right: $outMargin;
37-
padding-right: $outPadding;
38-
cursor: pointer;
39-
.arrow{
40-
&::after{
41-
content: ">"
42-
}
19+
.container {
20+
position: relative;
21+
box-sizing: border-box;
22+
:global(.rcs-center), :global(.rcs-left-stack), :global(.rcs-right-stack){
23+
position: absolute;
24+
transition: left 1s cubic-bezier(0,.7,.7,1);
25+
box-sizing: border-box;
4326
}
44-
&:hover{
45-
background: linear-gradient(to right, #ddd, #ccc);
46-
margin-right: 0;
27+
.rightArrow {
28+
@include arrow;
29+
background: linear-gradient(to right, white, #eee);
30+
right: 0;
31+
border-left: $border-color solid 1px;
32+
border-radius: 2px 0 0 2px;
33+
margin-right: $outMargin;
34+
padding-right: $outPadding;
35+
cursor: pointer;
36+
.arrow{
37+
&::after{
38+
content: ">"
39+
}
40+
}
41+
&:hover{
42+
background: linear-gradient(to right, #ddd, #ccc);
43+
margin-right: 0;
44+
}
4745
}
48-
}
4946

50-
.leftArrow {
51-
@include arrow;
52-
background: linear-gradient(to left, white, #eee);
53-
left: 0;
54-
border-right: $border-color solid 1px;
55-
border-radius: 0 2px 2px 0;
56-
margin-left: $outMargin;
57-
padding-left: $outPadding;
58-
.arrow{
59-
&::after{
60-
content: "<"
47+
.leftArrow {
48+
@include arrow;
49+
background: linear-gradient(to left, white, #eee);
50+
left: 0;
51+
border-right: $border-color solid 1px;
52+
border-radius: 0 2px 2px 0;
53+
margin-left: $outMargin;
54+
padding-left: $outPadding;
55+
.arrow{
56+
&::after{
57+
content: "<"
58+
}
59+
}
60+
&:hover{
61+
background: linear-gradient(to left, #ddd, #ccc);
62+
margin-left: 0;
6163
}
62-
}
63-
&:hover{
64-
background: linear-gradient(to left, #ddd, #ccc);
65-
margin-left: 0;
6664
}
6765
}
6866

0 commit comments

Comments
 (0)