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

Handle pointerdown #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
73 changes: 54 additions & 19 deletions src/OutsideClickHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ export default class OutsideClickHandler extends React.Component {

this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
this.onPointerDown = this.onPointerDown.bind(this);
this.onPointerUp = this.onPointerUp.bind(this);
this.downHandler = this.downHandler.bind(this);
this.upHandler = this.upHandler.bind(this);
this.setChildNodeRef = this.setChildNodeRef.bind(this);
}

componentDidMount() {
const { disabled, useCapture } = this.props;

if (!disabled) this.addMouseDownEventListener(useCapture);
if (!disabled) this.addDownEventListeners(useCapture);
}

componentWillReceiveProps({ disabled, useCapture }) {
Expand All @@ -49,7 +53,7 @@ export default class OutsideClickHandler extends React.Component {
if (disabled) {
this.removeEventListeners();
} else {
this.addMouseDownEventListener(useCapture);
this.addDownEventListeners(useCapture);
}
}
}
Expand All @@ -58,54 +62,85 @@ export default class OutsideClickHandler extends React.Component {
this.removeEventListeners();
}

// Use mousedown/mouseup to enforce that clicks remain outside the root's
// descendant tree, even when dragged. This should also get triggered on
// touch devices.
// Use mousedown/mouseup or pointerdown/pointerup to enforce that clicks remain
// outside the root's descendant tree, even when dragged. This should also get
// triggered on touch devices.
onMouseDown(e) {
this.downHandler(e, 'removeMouseUp', 'mouseup', this.onMouseUp);
}

onPointerDown(e) {
this.downHandler(e, 'removePointerUp', 'pointerup', this.onPointerUp);
}

// Use mousedown/mouseup or pointerdown/pointerup to enforce that clicks remain
// outside the root's descendant tree, even when dragged. This should also get
// triggered on touch devices.
onMouseUp(e) {
this.upHandler(e, 'removeMouseUp');
}

onPointerUp(e) {
this.upHandler(e, 'removePointerUp');
}

setChildNodeRef(ref) {
this.childNode = ref;
}

downHandler(e, removeUpHandlerName, eventName, callback) {
const { useCapture } = this.props;

const isDescendantOfRoot = this.childNode && this.childNode.contains(e.target);
if (!isDescendantOfRoot) {
this.removeMouseUp = addEventListener(
this[removeUpHandlerName] = addEventListener(
document,
'mouseup',
this.onMouseUp,
eventName,
callback,
{ capture: useCapture },
);
}
}

// Use mousedown/mouseup to enforce that clicks remain outside the root's
// descendant tree, even when dragged. This should also get triggered on
// touch devices.
onMouseUp(e) {
upHandler(e, removeUpHandlerName) {
const { onOutsideClick } = this.props;

const isDescendantOfRoot = this.childNode && this.childNode.contains(e.target);
if (this.removeMouseUp) this.removeMouseUp();
this.removeMouseUp = null;
if (this[removeUpHandlerName]) this[removeUpHandlerName]();
this[removeUpHandlerName] = null;

if (!isDescendantOfRoot) {
if (this.lastUpTimestamp === e.timeStamp
&& this.lastUpRemoveHandlerName === 'removePointerUp'
&& removeUpHandlerName === 'removeMouseUp') {
return;
}
onOutsideClick(e);
this.lastUpTimestamp = e.timeStamp;
this.lastUpRemoveHandlerName = removeUpHandlerName;
}
}

setChildNodeRef(ref) {
this.childNode = ref;
}

addMouseDownEventListener(useCapture) {
addDownEventListeners(useCapture) {
this.removeMouseDown = addEventListener(
document,
'mousedown',
this.onMouseDown,
{ capture: useCapture },
);
this.removePointerDown = addEventListener(
document,
'pointerdown',
this.onPointerDown,
{ capture: useCapture },
);
}

removeEventListeners() {
if (this.removeMouseDown) this.removeMouseDown();
if (this.removeMouseUp) this.removeMouseUp();
if (this.removePointerDown) this.removePointerDown();
if (this.removePointerUp) this.removePointerUp();
}

render() {
Expand Down