This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added swipe actions to the transition pages component
- Loading branch information
Showing
9 changed files
with
167 additions
and
33 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
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
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 |
---|---|---|
@@ -1,24 +1,142 @@ | ||
import styled, { lightTheme } from '@slup/theming' | ||
import Inferno, { linkEvent } from 'inferno' | ||
import Component from 'inferno-component' | ||
import styled from '@slup/theming' | ||
|
||
const Scroll = styled.div` | ||
overflow-x: hidden; | ||
height: 100%; | ||
` | ||
|
||
export const Pages = styled.div` | ||
height: 100%; | ||
width: auto; | ||
display: flex; | ||
transition: transform 300ms cubic-bezier(0.4, 0.0, 0.2, 1); | ||
transform: ${props => `translateX(-${props.selected * 100}%)`} | ||
transition: transform 200ms ${props => !props.touched | ||
? 'cubic-bezier(0.4, 0.0, 0.2, 1)' | ||
: 'none' | ||
}; | ||
` | ||
|
||
export const Page = styled.div` | ||
flex-shrink: 0; | ||
height: 100%; | ||
width: 100%; | ||
` | ||
|
||
export const TransitionPages = props => | ||
<Scroll> | ||
<Pages {...props} /> | ||
</Scroll> | ||
interface IProps { | ||
selected: number | ||
onSwipe: (index: number) => void | ||
} | ||
|
||
interface IState { | ||
touched: boolean | ||
startX: number | ||
translate: number | ||
} | ||
|
||
export class TransitionPages extends Component<IProps, IState> { | ||
private container: HTMLDivElement | ||
|
||
public state: IState = { | ||
touched: false, | ||
startX: 0, | ||
translate: 0 | ||
} | ||
|
||
public componentWillReceiveProps(nextProps: IProps) { | ||
const stateSelection = Math.round(this.state.translate / 100) | ||
const DID_PROP_CHANGE = this.props.selected !== nextProps.selected | ||
|
||
if(nextProps.selected !== stateSelection && DID_PROP_CHANGE) { | ||
this.setState({ translate: nextProps.selected * 100 }) | ||
this.emitSwipe(nextProps.selected) | ||
} | ||
} | ||
|
||
/** | ||
* Emit the new selected index to a possible onSwipe listener | ||
* | ||
* @param index the new index | ||
*/ | ||
private emitSwipe(index: number) { | ||
if(this.props.onSwipe && typeof this.props.onSwipe == 'function') { | ||
this.props.onSwipe(index) | ||
} | ||
} | ||
|
||
/** | ||
* Saves the latest touch on the x axis | ||
* | ||
* @param self The local class | ||
* @param event The data from the fired event | ||
*/ | ||
private handleTouchStart(self, { targetTouches }) { | ||
const { translate } = self.state | ||
const { clientX } = targetTouches[0] | ||
const { clientWidth } = self.container | ||
|
||
const increment = (translate * clientWidth) / 100 | ||
|
||
self.setState({ | ||
touched: true, | ||
startX: clientX + increment | ||
}) | ||
} | ||
|
||
/** | ||
* Creates a percentage to move the container | ||
* using the current touch and the saved one | ||
* on the x axis | ||
* | ||
* @param self The local class | ||
* @param event The data from the fired event | ||
*/ | ||
private handleTouchMove(self, { targetTouches }) { | ||
const { startX } = self.state | ||
const { clientX } = targetTouches[0] | ||
const { clientWidth } = self.container | ||
const childrenCount = self.container.children[0].children.length | ||
|
||
let percentage = ((startX - clientX) / clientWidth) * 100 | ||
|
||
if (percentage < 0) | ||
percentage = 0 | ||
else if (percentage > ((childrenCount - 1) * 100)) | ||
percentage = (childrenCount - 1) * 100 | ||
|
||
self.setState({ translate: percentage }) | ||
} | ||
|
||
/** | ||
* Rounds the percentage | ||
* | ||
* @param self The local class | ||
*/ | ||
private handleTouchEnd(self) { | ||
self.setState({ | ||
touched: false, | ||
translate: Math.round(self.state.translate / 100) * 100 | ||
}) | ||
|
||
self.emitSwipe(Math.round(self.state.translate / 100)) | ||
} | ||
|
||
public render(props, { translate, touched }) { | ||
return( | ||
<Scroll | ||
{...props} | ||
innerRef={e => this.container = e} | ||
onTouchStart={linkEvent(this, this.handleTouchStart)} | ||
onTouchMove={linkEvent(this, this.handleTouchMove)} | ||
onTouchEnd={linkEvent(this, this.handleTouchEnd)} | ||
> | ||
<Pages | ||
selected={props.selected} | ||
touched={touched} | ||
style={`transform: translateX(-${translate}%)`} | ||
> | ||
{props.children} | ||
</Pages> | ||
</Scroll> | ||
) | ||
} | ||
} |