Skip to content

Commit 4b268c5

Browse files
authored
Fix dragging and resizing on mobile (#727)
* Fix dragging and resizing on mobile * Use indexOf for checking resize direction
1 parent 4d9c024 commit 4b268c5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/index.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ export interface Props {
160160
cancel?: string;
161161
enableUserSelectHack?: boolean;
162162
allowAnyClick?: boolean;
163-
nodeRef?: React.Ref<typeof React.Component>;
164163
scale?: number;
165164
[key: string]: any;
166165
}
@@ -214,6 +213,7 @@ export class Rnd extends React.PureComponent<Props, State> {
214213
resizing = false;
215214
resizingPosition = { x: 0, y: 0 };
216215
offsetFromParent = { left: 0, top: 0 };
216+
resizableElement: { current: HTMLDivElement | null } = { current: null };
217217

218218
constructor(props: Props) {
219219
super(props);
@@ -476,15 +476,25 @@ export class Rnd extends React.PureComponent<Props, State> {
476476
elementRef: HTMLDivElement,
477477
delta: { height: number; width: number },
478478
) {
479-
if (/left/i.test(direction)) {
480-
const x = this.state.original.x - delta.width;
481-
// INFO: Apply x position by resize to draggable.
482-
this.draggable.setState({ x });
479+
// INFO: Apply x and y position adjustments caused by resizing to draggable
480+
const newPos = { x: this.state.original.x, y: this.state.original.y };
481+
const left = -delta.width;
482+
const top = -delta.height;
483+
const directions = ["top", "left", "topLeft", "bottomLeft", "topRight"];
484+
485+
if (directions.indexOf(direction) !== -1) {
486+
if (direction === "bottomLeft") {
487+
newPos.x += left;
488+
} else if (direction === "topRight") {
489+
newPos.y += top;
490+
} else {
491+
newPos.x += left;
492+
newPos.y += top;
493+
}
483494
}
484-
if (/top/i.test(direction)) {
485-
const y = this.state.original.y - delta.height;
486-
// INFO: Apply x position by resize to draggable.
487-
this.draggable.setState({ y });
495+
496+
if (newPos.x !== this.draggable.state.x || newPos.y !== this.draggable.state.y) {
497+
this.draggable.setState(newPos);
488498
}
489499

490500
this.updateOffsetFromParent();
@@ -552,6 +562,7 @@ export class Rnd extends React.PureComponent<Props, State> {
552562
refResizable = (c: Resizable | null) => {
553563
if (!c) return;
554564
this.resizable = c;
565+
this.resizableElement.current = c.resizable;
555566
};
556567

557568
render() {
@@ -583,7 +594,6 @@ export class Rnd extends React.PureComponent<Props, State> {
583594
resizeHandleWrapperStyle,
584595
scale,
585596
allowAnyClick,
586-
nodeRef,
587597
...resizableProps
588598
} = this.props;
589599
const defaultValue = this.props.default ? { ...this.props.default } : undefined;
@@ -626,7 +636,7 @@ export class Rnd extends React.PureComponent<Props, State> {
626636
cancel={cancel}
627637
scale={scale}
628638
allowAnyClick={allowAnyClick}
629-
nodeRef={{ current: this.getSelfElement() }}
639+
nodeRef={this.resizableElement}
630640
>
631641
<Resizable
632642
{...resizableProps}

0 commit comments

Comments
 (0)