Skip to content

Commit 7d3eac1

Browse files
author
wfbn8821
committed
add basics and component source code
1 parent e489f31 commit 7d3eac1

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
The MIT License (MIT)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "react-card-scroll",
3+
"version": "0.1.0",
4+
"description": "A React component to navigate horizontally between cards of same width",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"react-component",
11+
"navigation",
12+
"scroll",
13+
"horizontal",
14+
"cards"
15+
],
16+
"author": "Florian Bernard",
17+
"license": "MIT",
18+
"devDependencies": {
19+
"react": "^15.0.1",
20+
"webpack": "^1.13.0"
21+
},
22+
"dependencies": {
23+
"lodash": "^4.11.1",
24+
"react-motion": "^0.4.2"
25+
}
26+
}

src/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
import {throttle} from 'lodash'
3+
import {Motion, spring} from 'react-motion'
4+
5+
let CardScroll = React.createClass({
6+
getInitialState(){
7+
return {currentLeft: 0, currentCard: 0}
8+
},
9+
10+
componentDidMount() {
11+
window.addEventListener('resize', throttle(() => this.scrollCards({number: 0})));
12+
},
13+
14+
render() {
15+
const {currentLeft} = this.state
16+
const updateRow = c => {
17+
if (c) {
18+
this._row = c
19+
}
20+
}
21+
return (
22+
<div className="scroll-container flex-item">
23+
<div style={{position: "fixed", left: 0, top: "50%", zIndex: 1}}
24+
onClick={this.scrollCardsWrap({toLeft: true})}>{"<"}</div>
25+
<div style={{position: "fixed", right: 0, top: "50%", zIndex: 1}}
26+
onClick={this.scrollCardsWrap()}>{">"}</div>
27+
<Motion style={{left: spring(currentLeft)}}>
28+
{value => {
29+
Object.assign(value, {
30+
flexWrap: "nowrap",
31+
position: "relative"
32+
})
33+
return (
34+
<div className="row"
35+
style={value}
36+
ref={c => updateRow(c)}>
37+
{this.props.children}
38+
</div>)
39+
}
40+
}
41+
</Motion>
42+
</div>
43+
)
44+
},
45+
46+
scrollCardsWrap(params){
47+
return () => {
48+
this.scrollCards(params)
49+
}
50+
},
51+
//TODO write TESTS!!!
52+
scrollCards({toLeft=false, number=1}={}){
53+
if (toLeft && this.state.currentCard - number < 0) {
54+
// gonna scroll too much to the left so just scroll to the first card
55+
number = this.state.currentCard
56+
}
57+
const cardWidth = this.props.getCardWidth()
58+
const visibleCardCount = Math.floor(this._row.clientWidth / cardWidth)
59+
const cardCount = this.props.getCardCount()
60+
if (!toLeft && this.state.currentCard + visibleCardCount + number > cardCount) {
61+
// gonna scroll too much to the right so scroll so the last card is at the right
62+
number = -this.state.currentCard + cardCount - visibleCardCount
63+
}
64+
let currentCard = this.state.currentCard + (toLeft ? -number : number)
65+
if (currentCard < 0) {
66+
// case where we wanna display last card at the right but not enough cards
67+
currentCard = 0
68+
}
69+
const currentLeft = -currentCard * cardWidth
70+
this.setState({currentLeft, currentCard})
71+
}
72+
})
73+
74+
export default CardScroll;

0 commit comments

Comments
 (0)