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

feat: scale support { w: number; h: number } #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Draggable.js
Expand Up @@ -29,7 +29,7 @@ export type DraggableDefaultProps = {
defaultClassNameDragging: string,
defaultClassNameDragged: string,
defaultPosition: ControlPosition,
scale: number,
scale: number| { w: number; h: number },
};

export type DraggableProps = {
Expand Down
10 changes: 8 additions & 2 deletions lib/DraggableCore.js
Expand Up @@ -55,7 +55,7 @@ export type DraggableCoreDefaultProps = {
onDrag: DraggableEventHandler,
onStop: DraggableEventHandler,
onMouseDown: (e: MouseEvent) => void,
scale: number,
scale: number| { w: number; h: number }
};

export type DraggableCoreProps = {
Expand Down Expand Up @@ -206,7 +206,13 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
/**
* `scale`, if set, applies scaling while dragging an element
*/
scale: PropTypes.number,
scale: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
w: PropTypes.number,
h: PropTypes.number
})
]),

/**
* These properties should be defined on the child, not here.
Expand Down
8 changes: 5 additions & 3 deletions lib/utils/domFns.js
Expand Up @@ -107,12 +107,14 @@ interface EventWithOffset {
}

// Get from offsetParent
export function offsetXYFromParent(evt: EventWithOffset, offsetParent: HTMLElement, scale: number): ControlPosition {
export function offsetXYFromParent(evt: EventWithOffset, offsetParent: HTMLElement, scale: number | { w: number; h: 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) / scale;
const y = (evt.clientY + offsetParent.scrollTop - offsetParentRect.top) / scale;
const scaleW = typeof scale === 'number' ? scale : scale.w;
const scaleH = typeof scale === 'number' ? scale : scale.h;
const x = (evt.clientX + offsetParent.scrollLeft - offsetParentRect.left) / scaleW;
const y = (evt.clientY + offsetParent.scrollTop - offsetParentRect.top) / scaleH;

return {x, y};
}
Expand Down
10 changes: 6 additions & 4 deletions lib/utils/positionFns.js
Expand Up @@ -104,12 +104,14 @@ export function createCoreData(draggable: DraggableCore, x: number, y: number):
// Create an data exposed by <Draggable>'s events
export function createDraggableData(draggable: Draggable, coreData: DraggableData): DraggableData {
const scale = draggable.props.scale;
const scaleW = typeof scale === 'number' ? scale : scale.w;
const scaleH = typeof scale === 'number' ? scale : scale.h;
return {
node: coreData.node,
x: draggable.state.x + (coreData.deltaX / scale),
y: draggable.state.y + (coreData.deltaY / scale),
deltaX: (coreData.deltaX / scale),
deltaY: (coreData.deltaY / scale),
x: draggable.state.x + (coreData.deltaX / scaleW),
y: draggable.state.y + (coreData.deltaY / scaleH),
deltaX: (coreData.deltaX / scaleW),
deltaY: (coreData.deltaY / scaleH),
lastX: draggable.state.x,
lastY: draggable.state.y
};
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Expand Up @@ -53,7 +53,7 @@ declare module 'react-draggable' {
onDrag: DraggableEventHandler,
onStop: DraggableEventHandler,
onMouseDown: (e: MouseEvent) => void,
scale: number
scale: number | { w: number; h: number }
}

export default class Draggable extends React.Component<Partial<DraggableProps>, {}> {
Expand Down