This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fernando Via Canel
committed
Jun 14, 2017
1 parent
e6114da
commit 585e172
Showing
10 changed files
with
183 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>withSwipe - @klarna/higher-order-components examples</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> | ||
<style>body {margin: 0; background-color: #f0f0f0;}</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react' | ||
import { render } from 'react-dom' | ||
import { withSwipe } from '../' | ||
|
||
const WithSwipe = withSwipe({ velocityThreshold: 0.2 })(function Swipable({ ...props }) { | ||
return ( | ||
<article | ||
style={{ backgroundColor: 'grey', width: 100, height: 100, alignSelf: 'center' }} | ||
{...props} | ||
> | ||
<h1>withSwipe</h1> | ||
</article> | ||
) | ||
}) | ||
|
||
render( | ||
<main | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
width: '100vw', | ||
height: '100vh', | ||
}} | ||
> | ||
<WithSwipe | ||
onSwipeLeft={() => console.log('SWIPED_LEFT')} | ||
onSwipeRight={() => console.log('SWIPED_RIGHT')} | ||
onSwipeUp={() => console.log('SWIPED_UP')} | ||
onSwipeDown={() => console.log('SWIPED_DOWN')} | ||
/> | ||
</main>, | ||
document.getElementById('root') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import { withUncontrolledProp } from '../' | ||
|
||
export default withUncontrolledProp({ | ||
prop: 'counter', | ||
defaultProp: 'defaultCounter', | ||
handlers: { | ||
onClick: ({ counter }) => () => counter + 1, | ||
}, | ||
})(function StatelessCounter({ counter, onClick }) { | ||
return ( | ||
<article> | ||
<h1>withUncontrolledProp</h1> | ||
<p> | ||
<button onClick={onClick}>Add one</button> | ||
Counter: {counter} | ||
</p> | ||
</article> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { Component } from 'react' | ||
|
||
const initialState = { | ||
position: { | ||
startX: '', | ||
startY: '', | ||
deltaDistance: 0, | ||
previousDeltaY: 0, | ||
}, | ||
time: { | ||
startTime: null, | ||
}, | ||
} | ||
|
||
export default ({ velocityThreshold }) => Target => | ||
class App extends Component { | ||
constructor() { | ||
super() | ||
|
||
this.state = initialState | ||
} | ||
TouchStart(e) { | ||
this.setState({ | ||
time: { | ||
...this.state.time, | ||
startTime: new Date().getTime(), | ||
}, | ||
position: { | ||
...this.state.position, | ||
mouseStartX: e.touches[0].clientX, | ||
mouseStartY: e.touches[0].clientY, | ||
boxX: e.target.getBoundingClientRect().left, | ||
boxY: e.target.getBoundingClientRect().top, | ||
deltaDistance: 0, | ||
}, | ||
}) | ||
} | ||
TouchMove(e) { | ||
e.persist() | ||
console.log('touchY', e.touches[0].clientY) | ||
console.log('mouseStartY', this.state.position.mouseStartY) | ||
console.log('boxY', this.state.position.boxY) | ||
console.log('deltaY', e.touches[0].clientY - this.state.position.mouseStartY) | ||
const changeX = e.touches[0].clientX - this.state.position.mouseStartX, | ||
changeY = e.touches[0].clientY - this.state.position.mouseStartY | ||
|
||
this.setState({ | ||
position: { | ||
...this.state.position, | ||
deltaDistance: e.touches[0].clientY - this.state.position.mouseStartY, | ||
}, | ||
style: { | ||
...this.state.style, | ||
transform: `translate( ${0}px, ${this.state.position.previousDeltaY + changeY}px)`, | ||
}, | ||
}) | ||
} | ||
TouchEnd(e) { | ||
const deltaTime = new Date().getTime() - this.state.time.startTime | ||
const velocity = this.state.position.deltaDistance / deltaTime | ||
this.gestureHandler(velocity) | ||
console.log('new boxY', this.state.position.boxY + this.state.position.deltaDistance) | ||
this.setState({ | ||
position: { | ||
...this.state.position, | ||
previousDeltaY: this.state.position.previousDeltaY + this.state.position.deltaDistance, | ||
}, | ||
}) | ||
} | ||
|
||
gestureHandler(velocity) { | ||
const THRESHOLD = velocityThreshold | ||
|
||
switch (true) { | ||
case velocity < -THRESHOLD: | ||
this.props.onSwipeUp && this.props.onSwipeUp() | ||
break | ||
case velocity > THRESHOLD: | ||
this.props.onSwipeUp && this.props.onSwipeDown() | ||
break | ||
default: | ||
console.log('INVALID_SWIPE') | ||
} | ||
} | ||
render() { | ||
const { onSwipeUp, onSwipeDown, ...props } = this.props | ||
return ( | ||
<div | ||
style={this.state.style} | ||
onTouchStart={e => this.TouchStart(e)} | ||
onTouchEnd={e => this.TouchEnd(e)} | ||
onTouchMove={e => this.TouchMove(e)} | ||
> | ||
<Target {...props} /> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters