@@ -198,6 +198,7 @@ export class Rnd extends React.PureComponent<Props, State> {
198198 draggable ! : $TODO ; // Draggable;
199199 resizing = false ;
200200 resizingPosition = { x : 0 , y : 0 } ;
201+ offsetFromParent = { left : 0 , top : 0 } ;
201202
202203 constructor ( props : Props ) {
203204 super ( props ) ;
@@ -226,7 +227,8 @@ export class Rnd extends React.PureComponent<Props, State> {
226227 }
227228
228229 componentDidMount ( ) {
229- const { left, top } = this . getOffsetFromParent ( ) ;
230+ this . updateOffsetFromParent ( ) ;
231+ const { left, top } = this . offsetFromParent ;
230232 const { x, y } = this . getDraggablePosition ( ) ;
231233 this . draggable . setState ( {
232234 x : x - left ,
@@ -329,7 +331,8 @@ export class Rnd extends React.PureComponent<Props, State> {
329331 const left = ( boundaryLeft - parentLeft ) / scale ;
330332 const top = boundaryTop - parentTop ;
331333 if ( ! this . resizable ) return ;
332- const offset = this . getOffsetFromParent ( ) ;
334+ this . updateOffsetFromParent ( ) ;
335+ const offset = this . offsetFromParent ;
333336 this . setState ( {
334337 bounds : {
335338 top : top - offset . top ,
@@ -342,14 +345,14 @@ export class Rnd extends React.PureComponent<Props, State> {
342345
343346 onDrag ( e : RndDragEvent , data : DraggableData ) {
344347 if ( this . props . onDrag ) {
345- const offset = this . getOffsetFromParent ( ) ;
348+ const offset = this . offsetFromParent ;
346349 this . props . onDrag ( e , { ...data , x : data . x - offset . left , y : data . y - offset . top } ) ;
347350 }
348351 }
349352
350353 onDragStop ( e : RndDragEvent , data : DraggableData ) {
351354 if ( this . props . onDragStop ) {
352- const { left, top } = this . getOffsetFromParent ( ) ;
355+ const { left, top } = this . offsetFromParent ;
353356 return this . props . onDragStop ( e , { ...data , x : data . x + left , y : data . y + top } ) ;
354357 }
355358 }
@@ -363,7 +366,7 @@ export class Rnd extends React.PureComponent<Props, State> {
363366 this . resizing = true ;
364367
365368 const scale = this . props . scale as number ;
366- const offset = this . getOffsetFromParent ( ) ;
369+ const offset = this . offsetFromParent ;
367370 const pos = this . getDraggablePosition ( ) ;
368371 this . resizingPosition = { x : pos . x + offset . left , y : pos . y + offset . top } ;
369372 this . setState ( {
@@ -458,27 +461,22 @@ export class Rnd extends React.PureComponent<Props, State> {
458461 elementRef : HTMLDivElement ,
459462 delta : { height : number ; width : number } ,
460463 ) {
461- let x ;
462- let y ;
463- const offset = this . getOffsetFromParent ( ) ;
464464 if ( / l e f t / i. test ( direction ) ) {
465- x = this . state . original . x - delta . width ;
465+ const x = this . state . original . x - delta . width ;
466466 // INFO: Apply x position by resize to draggable.
467467 this . draggable . setState ( { x } ) ;
468- x += offset . left ;
469468 }
470469 if ( / t o p / i. test ( direction ) ) {
471- y = this . state . original . y - delta . height ;
470+ const y = this . state . original . y - delta . height ;
472471 // INFO: Apply x position by resize to draggable.
473472 this . draggable . setState ( { y } ) ;
474- y += offset . top ;
475- }
476- if ( typeof x === "undefined" ) {
477- x = this . getDraggablePosition ( ) . x + offset . left ;
478- }
479- if ( typeof y === "undefined" ) {
480- y = this . getDraggablePosition ( ) . y + offset . top ;
481473 }
474+
475+ this . updateOffsetFromParent ( ) ;
476+ const offset = this . offsetFromParent ;
477+ const x = this . getDraggablePosition ( ) . x + offset . left ;
478+ const y = this . getDraggablePosition ( ) . y + offset . top ;
479+
482480 this . resizingPosition = { x, y } ;
483481 if ( ! this . props . onResize ) return ;
484482 this . props . onResize ( e , direction , elementRef , delta , {
@@ -510,7 +508,7 @@ export class Rnd extends React.PureComponent<Props, State> {
510508 this . draggable . setState ( position ) ;
511509 }
512510
513- getOffsetFromParent ( ) : { top : number ; left : number } {
511+ updateOffsetFromParent ( ) {
514512 const scale = this . props . scale as number ;
515513 const parent = this . getParent ( ) ;
516514 const self = this . getSelfElement ( ) ;
@@ -525,7 +523,7 @@ export class Rnd extends React.PureComponent<Props, State> {
525523 const parentTop = parentRect . top ;
526524 const selfRect = self . getBoundingClientRect ( ) ;
527525 const position = this . getDraggablePosition ( ) ;
528- return {
526+ this . offsetFromParent = {
529527 left : selfRect . left - parentLeft - position . x * scale ,
530528 top : selfRect . top - parentTop - position . y * scale ,
531529 } ;
@@ -581,7 +579,7 @@ export class Rnd extends React.PureComponent<Props, State> {
581579 ...cursorStyle ,
582580 ...style ,
583581 } ;
584- const { left, top } = this . getOffsetFromParent ( ) ;
582+ const { left, top } = this . offsetFromParent ;
585583 let draggablePosition ;
586584 if ( position ) {
587585 draggablePosition = {
0 commit comments