Skip to content

Commit

Permalink
feat(DraggableCore): apply scale property while dragging an element
Browse files Browse the repository at this point in the history
  • Loading branch information
metdockal committed Oct 15, 2019
1 parent f024205 commit 1ca0129
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/DraggableCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type DraggableCoreProps = {
onDrag: DraggableEventHandler,
onStop: DraggableEventHandler,
onMouseDown: (e: MouseEvent) => void,
scale: number,
};

//
Expand Down Expand Up @@ -185,6 +186,11 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
*/
onMouseDown: PropTypes.func,

/**
* `scale`, if set, applies scaling while dragging an element
*/
scale: PropTypes.number,

/**
* These properties should be defined on the child, not here.
*/
Expand All @@ -206,6 +212,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
onDrag: function(){},
onStop: function(){},
onMouseDown: function(){},
scale: 1,
};

state = {
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/domFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ export function innerWidth(node: HTMLElement): number {
}

// Get from offsetParent
export function offsetXYFromParent(evt: {clientX: number, clientY: number}, offsetParent: HTMLElement): ControlPosition {
export function offsetXYFromParent(evt: {clientX: number, clientY: number}, offsetParent: HTMLElement, scale: number): ControlPosition {
const isBody = offsetParent === offsetParent.ownerDocument.body;
const offsetParentRect = isBody ? {left: 0, top: 0} : offsetParent.getBoundingClientRect();

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

return {x, y};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/positionFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function getControlPosition(e: MouseTouchEvent, touchIdentifier: ?number,
const node = findDOMNode(draggableCore);
// User can provide an offsetParent if desired.
const offsetParent = draggableCore.props.offsetParent || node.offsetParent || node.ownerDocument.body;
return offsetXYFromParent(touchObj || e, offsetParent);
return offsetXYFromParent(touchObj || e, offsetParent, draggableCore.props.scale);
}

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

0 comments on commit 1ca0129

Please sign in to comment.