Skip to content

Commit 7b27283

Browse files
author
Aeneas Rekkas (arekkas)
committed
Resolves regression issue by introducing useBodyAsOrigin to <DraggableCore>
Allows nodes to use the body as origin instead of the parent. This is useful, when the parent's position is changing. When used, resolves #170 This issue was introduced by a398097
1 parent 35f9ae5 commit 7b27283

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ on itself and thus must have callbacks attached to be useful.
249249
cancel: string,
250250
disabled: boolean,
251251
enableUserSelectHack: boolean,
252+
useBodyAsOrigin: boolean,
252253
grid: [number, number],
253254
handle: string,
254255
onStart: DraggableEventHandler,

lib/DraggableCore.es6

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export default class DraggableCore extends React.Component {
6666
*/
6767
enableUserSelectHack: PropTypes.bool,
6868

69+
/**
70+
* `useBodyAsOrigin`, if true, uses the <body> node to compute offsets.
71+
* If false, the <Draggable>'s parent node will be used instead.
72+
*
73+
* Using this attribute will break <Draggable> inside of scrollable
74+
* elements, except <body>, but makes sense, if <Draggable>'s parent
75+
* node is moving around.
76+
*
77+
* Defaults to `false`.
78+
*/
79+
useBodyAsOrigin: PropTypes.bool,
80+
6981
/**
7082
* `grid` specifies the x and y that dragging should snap to.
7183
*/
@@ -152,6 +164,7 @@ export default class DraggableCore extends React.Component {
152164
cancel: null,
153165
disabled: false,
154166
enableUserSelectHack: true,
167+
useBodyAsOrigin: false,
155168
handle: null,
156169
grid: null,
157170
transform: null,

lib/utils/domFns.es6

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ export function innerWidth(node: HTMLElement): number {
9595
}
9696

9797
// Get from offsetParent
98-
export function offsetXYFromParentOf(evt: {clientX: number, clientY: number}, node: HTMLElement & {offsetParent: HTMLElement}): ControlPosition {
99-
const offsetParent = node.offsetParent || document.body;
100-
const offsetParentRect = node.offsetParent === document.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
98+
export function offsetXYFromParentOf(evt: {clientX: number, clientY: number}, node: HTMLElement & {offsetParent: HTMLElement}, draggableCore: DraggableCore): ControlPosition {
99+
const offsetParent = draggableCore.props.useBodyAsOrigin ? document.body : node.offsetParent || document.body;
100+
const offsetParentRect = offsetParent === document.body ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();
101101

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

lib/utils/positionFns.es6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function canDragY(draggable: Draggable): boolean {
6666
export function getControlPosition(e: MouseEvent, touchIdentifier: ?number, draggableCore: DraggableCore): ?ControlPosition {
6767
const touchObj = typeof touchIdentifier === 'number' ? getTouch(e, touchIdentifier) : null;
6868
if (typeof touchIdentifier === 'number' && !touchObj) return null; // not the right touch
69-
return offsetXYFromParentOf(touchObj || e, ReactDOM.findDOMNode(draggableCore));
69+
return offsetXYFromParentOf(touchObj || e, ReactDOM.findDOMNode(draggableCore), draggableCore);
7070
}
7171

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

0 commit comments

Comments
 (0)