Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup single touchmove event listener #130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions examples/app/FeatureTestConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,6 @@ export default class Main extends Component {
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
</div>
</Swipeable>
<SwipeableHook
{...boundSwipes}
{...swipeableDirProps}
delta={deltaNum}
preventDefaultTouchmoveEvent={preventDefaultTouchmoveEvent}
trackTouch={trackTouch}
trackMouse={trackMouse}
rotationAngle={rotationAngleNum}
className="callout"
style={swipeableStyle}>
<div onTouchStart={()=>this.resetState()}>
<h5>Hook - Swipe inside here to test</h5>
<p>See output below and check the console for 'onSwiping' and 'onSwiped' callback output(open dev tools)</p>
<span>You can also 'toggle' the swip(ed/ing) props being applied to this container below.</span>
</div>
</SwipeableHook>
<table>
<thead>
<tr><th>Applied?</th><th>Action</th><th>Output</th></tr>
Expand Down
53 changes: 50 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,52 @@ function rotateXYByAngle(pos, angle) {
}

const getTouchHandlerOption = props => {
if (props.touchHandlerOption) return props.touchHandlerOption
if (typeof props.touchHandlerOption !== 'undefined')
Copy link
Collaborator Author

@hartzis hartzis Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug Fix: should allow for false for touchHandlerOption prop. Specifically when options and passive aren't supported by the browser being used.

return props.touchHandlerOption
return props.preventDefaultTouchmoveEvent
? { passive: false }
: { passive: true }
}

const noop = () => {}
let touchMoveListenerSetup = false
let handlerToCall = noop
let touchMoveHandlerOption = {}
const touchMoveHandler = e => {
handlerToCall(e)
}
const setupTouchMoveListener = (newHandler, touchHandlerOption) => {
// cleanup and reset if preventDefaultTouchMoveEvent changed
if (typeof touchHandlerOption === 'object' && touchMoveListenerSetup) {
if (touchHandlerOption.passive !== touchMoveHandlerOption.passive) {
cleanUp()
}
}
// setup a single eventListener for 'touchmove'
if (!touchMoveListenerSetup) {
touchMoveHandlerOption = touchHandlerOption
document.addEventListener(
touchMove,
touchMoveHandler,
touchMoveHandlerOption
)
touchMoveListenerSetup = true
}
// assign single handler to call
handlerToCall = newHandler
}

// clean up the single 'touchmove' eventListener
const cleanUp = () => {
document.removeEventListener(
touchMove,
touchMoveHandler,
touchMoveHandlerOption
)
touchMoveListenerSetup = false
handlerToCall = noop
}

function getHandlers(set, props) {
const onStart = event => {
// if more than a single touch don't track, for now...
Expand Down Expand Up @@ -131,7 +171,6 @@ function getHandlers(set, props) {
}
if (props.trackTouch) {
const touchHandlerOption = getTouchHandlerOption(props)
document.addEventListener(touchMove, onMove, touchHandlerOption)
document.addEventListener(touchEnd, onUp, touchHandlerOption)
}
onStart(e)
Expand All @@ -144,7 +183,6 @@ function getHandlers(set, props) {
}
if (props.trackTouch) {
const touchHandlerOption = getTouchHandlerOption(props)
document.removeEventListener(touchMove, onMove, touchHandlerOption)
document.removeEventListener(touchEnd, onUp, touchHandlerOption)
}
}
Expand All @@ -154,6 +192,11 @@ function getHandlers(set, props) {
onEnd(e)
}

if (props.trackTouch) {
const touchHandlerOption = getTouchHandlerOption(props)
setupTouchMoveListener(onMove, touchHandlerOption)
}

const output = {}
if (props.trackMouse) {
output.onMouseDown = onDown
Expand Down Expand Up @@ -204,6 +247,10 @@ export class Swipeable extends React.PureComponent {
this._set = cb => (this._state = cb(this._state))
}

componentWillUnmount() {
cleanUp()
}

render() {
const {
className,
Expand Down