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

Fix issues with distance and pressThreshold props on mobile #378

Merged
merged 1 commit into from
May 23, 2018
Merged
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
164 changes: 50 additions & 114 deletions src/SortableContainer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
events,
vendorPrefix,
limit,
getEdgeOffset,
getElementMargin,
getLockPixelOffset,
getPosition,
provideDisplayName,
omit,
} from '../utils';
Expand Down Expand Up @@ -145,20 +148,17 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
}
}

handleStart = e => {
handleStart = event => {
const {distance, shouldCancelStart} = this.props;

if (e.button === 2 || shouldCancelStart(e)) {
if (event.button === 2 || shouldCancelStart(event)) {
return false;
}

this._touched = true;
this._pos = {
x: e.pageX,
y: e.pageY,
};
this._pos = getPosition(event);

const node = closest(e.target, el => el.sortableInfo != null);
const node = closest(event.target, el => el.sortableInfo != null);

if (
node &&
Expand All @@ -170,7 +170,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
const {index, collection} = node.sortableInfo;

if (
useDragHandle && !closest(e.target, el => el.sortableHandle != null)
useDragHandle && !closest(event.target, el => el.sortableHandle != null)
)
return;

Expand All @@ -181,16 +181,16 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
* prevent subsequent 'mousemove' events from being fired
* (see https://github.com/clauderic/react-sortable-hoc/issues/118)
*/
if (e.target.tagName.toLowerCase() === 'a') {
e.preventDefault();
if (event.target.tagName.toLowerCase() === 'a') {
event.preventDefault();
}

if (!distance) {
if (this.props.pressDelay === 0) {
this.handlePress(e);
this.handlePress(event);
} else {
this.pressTimer = setTimeout(
() => this.handlePress(e),
() => this.handlePress(event),
this.props.pressDelay
);
}
Expand All @@ -202,21 +202,22 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
return node.sortableInfo.manager === this.manager;
};

handleMove = e => {
handleMove = event => {
const {distance, pressThreshold} = this.props;

if (!this.state.sorting && this._touched) {
this._delta = {
x: this._pos.x - e.pageX,
y: this._pos.y - e.pageY,
const position = getPosition(event);
const delta = this._delta = {
x: this._pos.x - position.x,
y: this._pos.y - position.y,
};
const delta = Math.abs(this._delta.x) + Math.abs(this._delta.y);
const combinedDelta = Math.abs(delta.x) + Math.abs(delta.y);

if (!distance && (!pressThreshold || pressThreshold && delta >= pressThreshold)) {
if (!distance && (!pressThreshold || pressThreshold && combinedDelta >= pressThreshold)) {
clearTimeout(this.cancelTimer);
this.cancelTimer = setTimeout(this.cancel, 0);
} else if (distance && delta >= distance && this.manager.isActive()) {
this.handlePress(e);
} else if (distance && combinedDelta >= distance && this.manager.isActive()) {
this.handlePress(event);
}
}
};
Expand All @@ -238,7 +239,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
}
};

handlePress = e => {
handlePress = event => {
const active = this.manager.getActive();

if (active) {
Expand Down Expand Up @@ -274,8 +275,8 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
x: axis.indexOf('x') >= 0,
y: axis.indexOf('y') >= 0,
};
this.offsetEdge = this.getEdgeOffset(node);
this.initialOffset = this.getOffset(e);
this.offsetEdge = getEdgeOffset(node, this.container);
this.initialOffset = getPosition(event);
this.initialScroll = {
top: this.container.scrollTop,
left: this.container.scrollLeft,
Expand Down Expand Up @@ -345,7 +346,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
this.helper.classList.add(...helperClass.split(' '));
}

this.listenerNode = e.touches ? node : this.contentWindow;
this.listenerNode = event.touches ? node : this.contentWindow;
events.move.forEach(eventName =>
this.listenerNode.addEventListener(
eventName,
Expand All @@ -364,22 +365,26 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
sortingIndex: index,
});

if (onSortStart) onSortStart({node, index, collection}, e);
if (onSortStart) {
onSortStart({node, index, collection}, event);
}
}
};

handleSortMove = e => {
handleSortMove = event => {
const {onSortMove} = this.props;
e.preventDefault(); // Prevent scrolling on mobile
event.preventDefault(); // Prevent scrolling on mobile

this.updatePosition(e);
this.updatePosition(event);
this.animateNodes();
this.autoscroll();

if (onSortMove) onSortMove(e);
if (onSortMove) {
onSortMove(event);
}
};

handleSortEnd = e => {
handleSortEnd = event => {
const {hideSortableGhost, onSortEnd} = this.props;
const {collection} = this.manager.active;

Expand Down Expand Up @@ -434,113 +439,44 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
newIndex: this.newIndex,
collection,
},
e
event
);
}

this._touched = false;
};

getEdgeOffset(node, offset = {top: 0, left: 0}) {
// Get the actual offsetTop / offsetLeft value, no matter how deep the node is nested
if (node) {
const nodeOffset = {
top: offset.top + node.offsetTop,
left: offset.left + node.offsetLeft,
};
if (node.parentNode !== this.container) {
return this.getEdgeOffset(node.parentNode, nodeOffset);
} else {
return nodeOffset;
}
}
}

getOffset(e) {
if (e.touches && e.touches.length) {
return {
x: e.touches[0].pageX,
y: e.touches[0].pageY,
};
} else if (e.changedTouches && e.changedTouches.length) {
return {
x: e.changedTouches[0].pageX,
y: e.changedTouches[0].pageY,
};
} else {
return {
x: e.pageX,
y: e.pageY,
};
}
}

getLockPixelOffsets() {
let {lockOffset} = this.props;

if (!Array.isArray(lockOffset)) {
lockOffset = [lockOffset, lockOffset];
}
const {width, height} = this;
const {lockOffset} = this.props;
const offsets = Array.isArray(lockOffset)
? lockOffset
: [lockOffset, lockOffset];

invariant(
lockOffset.length === 2,
offsets.length === 2,
'lockOffset prop of SortableContainer should be a single ' +
'value or an array of exactly two values. Given %s',
lockOffset
);

const [minLockOffset, maxLockOffset] = lockOffset;
const [minLockOffset, maxLockOffset] = offsets;

return [
this.getLockPixelOffset(minLockOffset),
this.getLockPixelOffset(maxLockOffset),
getLockPixelOffset({offset: minLockOffset, width, height}),
getLockPixelOffset({offset: maxLockOffset, width, height}),
];
}

getLockPixelOffset(lockOffset) {
let offsetX = lockOffset;
let offsetY = lockOffset;
let unit = 'px';

if (typeof lockOffset === 'string') {
const match = /^[+-]?\d*(?:\.\d*)?(px|%)$/.exec(lockOffset);

invariant(
match !== null,
'lockOffset value should be a number or a string of a ' +
'number followed by "px" or "%". Given %s',
lockOffset
);

offsetX = (offsetY = parseFloat(lockOffset));
unit = match[1];
}

invariant(
isFinite(offsetX) && isFinite(offsetY),
'lockOffset value should be a finite. Given %s',
lockOffset
);

if (unit === '%') {
offsetX = offsetX * this.width / 100;
offsetY = offsetY * this.height / 100;
}

return {
x: offsetX,
y: offsetY,
};
}

updatePosition(e) {
updatePosition(event) {
const {lockAxis, lockToContainerEdges} = this.props;

const offset = this.getOffset(e);
const offset = getPosition(event);
const translate = {
x: offset.x - this.initialOffset.x,
y: offset.y - this.initialOffset.y,
};

// Adjust for window scroll
translate.y -= (window.pageYOffset - this.initialWindowScroll.top);
translate.x -= (window.pageXOffset - this.initialWindowScroll.left);
Expand Down Expand Up @@ -617,7 +553,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f

// If we haven't cached the node's offsetTop / offsetLeft value
if (!edgeOffset) {
nodes[i].edgeOffset = (edgeOffset = this.getEdgeOffset(node));
nodes[i].edgeOffset = (edgeOffset = getEdgeOffset(node, this.container));
}

// Get a reference to the next and previous node
Expand All @@ -627,7 +563,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
// Also cache the next node's edge offset if needed.
// We need this for calculating the animation in a grid setup
if (nextNode && !nextNode.edgeOffset) {
nextNode.edgeOffset = this.getEdgeOffset(nextNode.node);
nextNode.edgeOffset = getEdgeOffset(nextNode.node, this.container);
}

// If the node is the one we're currently animating, skip it
Expand Down
74 changes: 74 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import invariant from 'invariant';

export function arrayMove(arr, previousIndex, newIndex) {
const array = arr.slice(0);
if (newIndex >= array.length) {
Expand Down Expand Up @@ -80,3 +82,75 @@ export function provideDisplayName(prefix, Component) {

return componentName ? `${prefix}(${componentName})` : prefix;
}

export function getPosition(event) {
if (event.touches && event.touches.length) {
return {
x: event.touches[0].pageX,
y: event.touches[0].pageY,
};
} else if (event.changedTouches && event.changedTouches.length) {
return {
x: event.changedTouches[0].pageX,
y: event.changedTouches[0].pageY,
};
} else {
return {
x: event.pageX,
y: event.pageY,
};
}
}

export function getEdgeOffset(node, parent, offset = {top: 0, left: 0}) {
// Get the actual offsetTop / offsetLeft value, no matter how deep the node is nested
if (node) {
const nodeOffset = {
top: offset.top + node.offsetTop,
left: offset.left + node.offsetLeft,
};

if (node.parentNode !== parent) {
return getEdgeOffset(node.parentNode, parent, nodeOffset);
} else {
return nodeOffset;
}
}
}


export function getLockPixelOffset({lockOffset, width, height}) {
let offsetX = lockOffset;
let offsetY = lockOffset;
let unit = 'px';

if (typeof lockOffset === 'string') {
const match = /^[+-]?\d*(?:\.\d*)?(px|%)$/.exec(lockOffset);

invariant(
match !== null,
'lockOffset value should be a number or a string of a ' +
'number followed by "px" or "%". Given %s',
lockOffset
);

offsetX = (offsetY = parseFloat(lockOffset));
unit = match[1];
}

invariant(
isFinite(offsetX) && isFinite(offsetY),
'lockOffset value should be a finite. Given %s',
lockOffset
);

if (unit === '%') {
offsetX = offsetX * width / 100;
offsetY = offsetY * height / 100;
}

return {
x: offsetX,
y: offsetY,
};
}