Skip to content

Commit

Permalink
Forced offsets are computed in getDragPreviewOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schupp authored and Paul Schupp committed Jun 12, 2017
1 parent a9f62bc commit 39ed688
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/react-dnd-html5-backend/src/HTML5Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,19 @@ export default class HTML5Backend {
const dragPreview = this.sourcePreviewNodes[sourceId] || sourceNode;
const { anchorX, anchorY, offsetX, offsetY } = this.getCurrentSourcePreviewNodeOptions();
const anchorPoint = { anchorX, anchorY };
const offsetPoint = { offsetX, offsetY };
const dragPreviewOffset = getDragPreviewOffset(
sourceNode,
dragPreview,
clientOffset,
anchorPoint,
offsetPoint,
);
const forceOffsetX = offsetX === 0 || offsetX;
const forceOffsetY = offsetY === 0 || offsetY;

dataTransfer.setDragImage(
dragPreview,
forceOffsetX ? offsetX : dragPreviewOffset.x,
forceOffsetY ? offsetY : dragPreviewOffset.y,
dragPreviewOffset.x,
dragPreviewOffset.y,
);
}

Expand Down
11 changes: 9 additions & 2 deletions packages/react-dnd-html5-backend/src/OffsetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getEventClientOffset(e) {
};
}

export function getDragPreviewOffset(sourceNode, dragPreview, clientOffset, anchorPoint) {
export function getDragPreviewOffset(sourceNode, dragPreview, clientOffset, anchorPoint, offsetPoint) {
// The browsers will use the image intrinsic size under different conditions.
// Firefox only cares if it's an image, but WebKit also wants it to be detached.
const isImage = dragPreview.nodeName === 'IMG' && (
Expand Down Expand Up @@ -71,7 +71,7 @@ export function getDragPreviewOffset(sourceNode, dragPreview, clientOffset, anch
// Dock to the bottom
offsetFromDragPreview.y + dragPreviewHeight - sourceHeight,
]);
const x = interpolantX.interpolate(anchorX);
let x = interpolantX.interpolate(anchorX);
let y = interpolantY.interpolate(anchorY);

// Work around Safari 8 positioning bug
Expand All @@ -80,5 +80,12 @@ export function getDragPreviewOffset(sourceNode, dragPreview, clientOffset, anch
y += (window.devicePixelRatio - 1) * dragPreviewHeight;
}

// Force offsets if specified in the options.
const { offsetX, offsetY } = offsetPoint;
const forceOffsetX = offsetX === 0 || offsetX;
const forceOffsetY = offsetY === 0 || offsetY;
x = forceOffsetX ? offsetX : x;
y = forceOffsetY ? offsetY : y;

return { x, y };
}

0 comments on commit 39ed688

Please sign in to comment.