Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
yarn.lock
.vscode/
26 changes: 18 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ var LongPressable = function (_React$PureComponent) {
args[_key] = arguments[_key];
}

return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = LongPressable.__proto__ || Object.getPrototypeOf(LongPressable)).call.apply(_ref, [this].concat(args))), _this), _this.isLongPressing = false, _this.startingPosition = { x: 0, y: 0 }, _this.onPointerUp = function (e) {
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = LongPressable.__proto__ || Object.getPrototypeOf(LongPressable)).call.apply(_ref, [this].concat(args))), _this), _this.isLongPressing = false, _this.startingPosition = { x: 0, y: 0 }, _this.cancelEvent = function (e) {
e.stopPropagation();
}, _this.onPointerUp = function (e) {
if (_this.timerID) {
_this.cancelLongPress();
}

var mousePosition = eventToPosition(e);

if (!_this.isLongPressing && !_this.exceedDragThreshold(mousePosition)) {
_this.props.onShortPress();
_this.props.onShortPress && _this.props.onShortPress();
} else {
_this.isLongPressing = false;
}
Expand All @@ -70,7 +72,7 @@ var LongPressable = function (_React$PureComponent) {

_this.timerID = setTimeout(function () {
_this.isLongPressing = true;
_this.props.onLongPress();
_this.props.onLongPress && _this.props.onLongPress();
}, _this.props.longPressTime);
}, _this.onPointerMove = function (e) {
var mousePosition = eventToPosition(e);
Expand Down Expand Up @@ -98,13 +100,19 @@ var LongPressable = function (_React$PureComponent) {
}, {
key: 'render',
value: function render() {
var disabled = this.props.disabled;

return _react2.default.createElement(
'div',
{
onPointerUp: this.onPointerUp,
onPointerDown: this.onPointerDown,
onPointerMove: this.onPointerMove,
onPointerLeave: this.onPointerLeave
onContextMenu: function onContextMenu(e) {
e.preventDefault();e.stopPropagation();
},
onClick: disabled ? this.cancelEvent : null,
onPointerUp: disabled ? null : this.onPointerUp,
onPointerDown: disabled ? null : this.onPointerDown,
onPointerMove: disabled ? null : this.onPointerMove,
onPointerLeave: disabled ? null : this.onPointerLeave
},
this.props.children
);
Expand All @@ -122,11 +130,13 @@ LongPressable.propTypes = {
// Maximum distance (pixels) user is allowed to drag before
// click is canceled
dragThreshold: _propTypes2.default.number,
disabled: _propTypes2.default.bool,
children: _propTypes2.default.node
};
LongPressable.defaultProps = {
longPressTime: 500,
primaryMouseButtonOnly: true,
dragThreshold: 100
dragThreshold: 100,
disabled: false
};
exports.default = LongPressable;
26 changes: 18 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ export default class LongPressable extends React.PureComponent {
// Maximum distance (pixels) user is allowed to drag before
// click is canceled
dragThreshold: PropType.number,
disabled: PropType.bool,
children: PropType.node
}

static defaultProps = {
longPressTime: 500,
primaryMouseButtonOnly: true,
dragThreshold: 100
dragThreshold: 100,
disabled: false
}

isLongPressing = false
startingPosition = { x: 0, y: 0 }

cancelEvent = (e) => {
e.stopPropagation();
}

onPointerUp = (e) => {
if (this.timerID) {
this.cancelLongPress()
Expand All @@ -45,7 +51,7 @@ export default class LongPressable extends React.PureComponent {

if (!this.isLongPressing &&
!this.exceedDragThreshold(mousePosition)) {
this.props.onShortPress()
this.props.onShortPress && this.props.onShortPress()
}
else {
this.isLongPressing = false
Expand All @@ -58,12 +64,12 @@ export default class LongPressable extends React.PureComponent {
return
}
}

this.startingPosition = eventToPosition(e)

this.timerID = setTimeout(() => {
this.isLongPressing = true
this.props.onLongPress()
this.props.onLongPress && this.props.onLongPress()
}, this.props.longPressTime)
}

Expand All @@ -90,12 +96,16 @@ export default class LongPressable extends React.PureComponent {
}

render() {
var disabled = this.props.disabled;

return (
<div
onPointerUp={this.onPointerUp}
onPointerDown={this.onPointerDown}
onPointerMove={this.onPointerMove}
onPointerLeave={this.onPointerLeave}
onContextMenu={e => { e.preventDefault(); e.stopPropagation(); }}
onClick={disabled ? this.cancelEvent : null}
onPointerUp={disabled ? null : this.onPointerUp}
onPointerDown={disabled ? null : this.onPointerDown}
onPointerMove={disabled ? null : this.onPointerMove}
onPointerLeave={disabled ? null : this.onPointerLeave}
>
{this.props.children}
</div>
Expand Down