Skip to content

Commit

Permalink
Feature: Implement shouldStopTouchMovePropagation
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanselzer committed Jan 16, 2018
1 parent d709c61 commit a03cb52
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -5,8 +5,6 @@ In the mouse environment it supports scroll position changes during a hover sess

react-cursor-position re-renders child components with new position props when the cursor or touch position changes.

It is safe for server rendering and single page applications.

## Status

[![CircleCI](https://img.shields.io/circleci/project/github/ethanselzer/react-cursor-position.svg)](https://circleci.com/gh/ethanselzer/react-cursor-position)
Expand Down
8 changes: 7 additions & 1 deletion src/ReactCursorPosition.js
Expand Up @@ -60,6 +60,7 @@ export default class extends React.Component {
pressDuration: PropTypes.number,
pressMoveThreshold: PropTypes.number,
shouldDecorateChildren: PropTypes.bool,
shouldStopTouchMovePropagation: PropTypes.bool,
style: PropTypes.object
};

Expand All @@ -74,7 +75,8 @@ export default class extends React.Component {
onDetectedEnvironmentChanged: noop,
pressDuration: 500,
pressMoveThreshold: 5,
shouldDecorateChildren: true
shouldDecorateChildren: true,
shouldStopTouchMovePropagation: false
};

onTouchStart(e) {
Expand Down Expand Up @@ -105,6 +107,10 @@ export default class extends React.Component {

this.setPositionState(position);
e.preventDefault();

if (this.props.shouldStopTouchMovePropagation) {
e.stopPropagation();
}
}

onTouchEnd() {
Expand Down
42 changes: 42 additions & 0 deletions test/ReactCursorPosition.spec.js
Expand Up @@ -154,6 +154,18 @@ describe('ReactCursorPosition', () => {
expect(instance.clearTimers.calledOnce).to.be.true;
});

it('prevents default on touch move, when activated', () => {
const tree = getMountedComponentTree({ isActivatedOnTouch: true });
const touchEvent = getTouchEvent();
sinon.spy(touchEvent, 'preventDefault');

tree.instance().onTouchStart(touchEvent);
tree.instance().onTouchMove(touchEvent);
tree.update();

expect(touchEvent.preventDefault.called).to.be.true;
});

describe('Add Touch and Mouse Event Listeners', () => {
it('fills touch event listeners collection', () => {
positionObserver = getMountedComponentTree();
Expand Down Expand Up @@ -643,6 +655,35 @@ describe('ReactCursorPosition', () => {
expect(childComponent.props()).to.be.empty;
});

describe('support for shouldStopTouchMovePropagation', () => {
it('is unset by default', () => {
const tree = getMountedComponentTree({ isActivatedOnTouch: true });
const touchEvent = getTouchEvent();
sinon.spy(touchEvent, 'stopPropagation');

tree.instance().onTouchStart(touchEvent);
tree.instance().onTouchMove(touchEvent);
tree.update();

expect(touchEvent.stopPropagation.called).to.be.false;
});

it('can be set', () => {
const tree = getMountedComponentTree({
isActivatedOnTouch: true,
shouldStopTouchMovePropagation: true
});
const touchEvent = getTouchEvent();
sinon.spy(touchEvent, 'stopPropagation');

tree.instance().onTouchStart(touchEvent);
tree.instance().onTouchMove(touchEvent);
tree.update();

expect(touchEvent.stopPropagation.called).to.be.true;
});
})

describe('Support for pressDuration', (done) => {
it('sets isActive if pressThreshold is not exceeded for duration', () => {
const clock = sinon.useFakeTimers();
Expand Down Expand Up @@ -874,6 +915,7 @@ describe('ReactCursorPosition', () => {
}
},
preventDefault: () => {},
stopPropagation: () => {},
touches: [{
pageX,
pageY
Expand Down

0 comments on commit a03cb52

Please sign in to comment.