Skip to content

Commit

Permalink
Resolves regression issue by introducing offsetParent to <Draggable…
Browse files Browse the repository at this point in the history
…Core>

Allows nodes to use an arbitrary node as origin instead of the parent. This is useful, when the parent's position is changing. When used, resolves react-grid-layout#170

This issue was introduced by a398097
  • Loading branch information
Aeneas Rekkas (arekkas) authored and aeneasr committed Jul 19, 2016
1 parent 7df4ecd commit 274f0ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ on itself and thus must have callbacks attached to be useful.
cancel: string,
disabled: boolean,
enableUserSelectHack: boolean,
offsetParent: HTMLElement,
grid: [number, number],
handle: string,
onStart: DraggableEventHandler,
Expand Down
11 changes: 11 additions & 0 deletions lib/DraggableCore.es6
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export default class DraggableCore extends React.Component {
*/
enableUserSelectHack: PropTypes.bool,

/**
* `offsetParent`, if set, uses the passed DOM node to compute drag offsets
* instead of using the parent node.
*/
offsetParent: function(props, propName) {
if (process.browser && props[propName] && props[propName].nodeType !== 1) {
throw new Error('Draggable\'s offsetParent must be a DOM Node.');
}
},

/**
* `grid` specifies the x and y that dragging should snap to.
*/
Expand Down Expand Up @@ -152,6 +162,7 @@ export default class DraggableCore extends React.Component {
cancel: null,
disabled: false,
enableUserSelectHack: true,
offsetParent: null,
handle: null,
grid: null,
transform: null,
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/domFns.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {findInArray, isFunction, int} from './shims';
import browserPrefix, {getPrefix, browserPrefixToStyle, browserPrefixToKey} from './getPrefix';

import type DraggableCore from '../DraggableCore';
import type {ControlPosition} from './types';

let matchesSelectorFunc = '';
Expand Down Expand Up @@ -95,11 +96,11 @@ export function innerWidth(node: HTMLElement): number {
}

// Get from offsetParent
export function offsetXYFromParentOf(evt: {clientX: number, clientY: number}, node: HTMLElement & {offsetParent: HTMLElement}): ControlPosition {
const offsetParent = node.offsetParent || node.ownerDocument.body;
const offsetParentRect = node.offsetParent === node.ownerDocument.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
export function offsetXYFromParentOf(evt: {clientX: number, clientY: number}, node: HTMLElement & {offsetParent: HTMLElement}, draggableCore: DraggableCore): ControlPosition {
const offsetParent = draggableCore.props.offsetParent || node.offsetParent || node.ownerDocument.body;
const offsetParentRect = offsetParent === node.ownerDocument.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();

const x = evt.clientX + offsetParent.scrollLeft - offsetParentRect.left;
const x = evt.clientX + offsetParent.scrollLeft - offsetParentRect.left;
const y = evt.clientY + offsetParent.scrollTop - offsetParentRect.top;

return {x, y};
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/positionFns.es6
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function canDragY(draggable: Draggable): boolean {
export function getControlPosition(e: MouseEvent, touchIdentifier: ?number, draggableCore: DraggableCore): ?ControlPosition {
const touchObj = typeof touchIdentifier === 'number' ? getTouch(e, touchIdentifier) : null;
if (typeof touchIdentifier === 'number' && !touchObj) return null; // not the right touch
return offsetXYFromParentOf(touchObj || e, ReactDOM.findDOMNode(draggableCore));
return offsetXYFromParentOf(touchObj || e, ReactDOM.findDOMNode(draggableCore), draggableCore);
}

// Create an data object exposed by <DraggableCore>'s events
Expand Down

0 comments on commit 274f0ac

Please sign in to comment.