@@ -129,6 +129,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
129
129
/** CSS `transform` that is applied to the element while it's being dragged. */
130
130
private _activeTransform : Point = { x : 0 , y : 0 } ;
131
131
132
+ /** Inline `transform` value that the element had before the first dragging sequence. */
133
+ private _initialTransform ?: string ;
134
+
132
135
/**
133
136
* Whether the dragging sequence has been started. Doesn't
134
137
* necessarily mean that the element has been moved.
@@ -325,6 +328,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
325
328
return ;
326
329
}
327
330
331
+ // Cache the previous transform amount only after the first drag sequence, because
332
+ // we don't want our own transforms to stack on top of each other.
333
+ if ( this . _initialTransform == null ) {
334
+ this . _initialTransform = this . _rootElement . style . transform || '' ;
335
+ }
336
+
328
337
this . _hasStartedDragging = this . _hasMoved = false ;
329
338
this . _initialContainer = this . dropContainer ;
330
339
this . _pointerMoveSubscription = this . _dragDropRegistry . pointerMove . subscribe ( this . _pointerMove ) ;
@@ -373,13 +382,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
373
382
if ( ! this . _hasStartedDragging ) {
374
383
const distanceX = Math . abs ( pointerPosition . x - this . _pickupPositionOnPage . x ) ;
375
384
const distanceY = Math . abs ( pointerPosition . y - this . _pickupPositionOnPage . y ) ;
376
- const minimumDistance = this . _config . dragStartThreshold ;
377
385
378
386
// Only start dragging after the user has moved more than the minimum distance in either
379
387
// direction. Note that this is preferrable over doing something like `skip(minimumDistance)`
380
388
// in the `pointerMove` subscription, because we're not guaranteed to have one move event
381
389
// per pixel of movement (e.g. if the user moves their pointer quickly).
382
- if ( distanceX + distanceY >= minimumDistance ) {
390
+ if ( distanceX + distanceY >= this . _config . dragStartThreshold ) {
383
391
this . _hasStartedDragging = true ;
384
392
this . _ngZone . run ( ( ) => this . _startDragSequence ( ) ) ;
385
393
}
@@ -399,7 +407,11 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
399
407
pointerPosition . x - this . _pickupPositionOnPage . x + this . _passiveTransform . x ;
400
408
activeTransform . y =
401
409
pointerPosition . y - this . _pickupPositionOnPage . y + this . _passiveTransform . y ;
402
- this . _setTransform ( this . _rootElement , activeTransform . x , activeTransform . y ) ;
410
+ const transform = getTransform ( activeTransform . x , activeTransform . y ) ;
411
+
412
+ // Preserve the previous `transform` value, if there was one.
413
+ this . _rootElement . style . transform = this . _initialTransform ?
414
+ this . _initialTransform + ' ' + transform : transform ;
403
415
}
404
416
405
417
// Since this event gets fired for every pixel while dragging, we only
@@ -507,9 +519,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
507
519
}
508
520
509
521
this . dropContainer . _sortItem ( this , x , y , this . _pointerDirectionDelta ) ;
510
- this . _setTransform ( this . _preview ,
511
- x - this . _pickupPositionInElement . x ,
512
- y - this . _pickupPositionInElement . y ) ;
522
+ this . _preview . style . transform =
523
+ getTransform ( x - this . _pickupPositionInElement . x , y - this . _pickupPositionInElement . y ) ;
513
524
}
514
525
515
526
/**
@@ -525,15 +536,16 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
525
536
526
537
preview = viewRef . rootNodes [ 0 ] ;
527
538
this . _previewRef = viewRef ;
528
- this . _setTransform ( preview , this . _pickupPositionOnPage . x , this . _pickupPositionOnPage . y ) ;
539
+ preview . style . transform =
540
+ getTransform ( this . _pickupPositionOnPage . x , this . _pickupPositionOnPage . y ) ;
529
541
} else {
530
542
const element = this . _rootElement ;
531
543
const elementRect = element . getBoundingClientRect ( ) ;
532
544
533
545
preview = element . cloneNode ( true ) as HTMLElement ;
534
546
preview . style . width = `${ elementRect . width } px` ;
535
547
preview . style . height = `${ elementRect . height } px` ;
536
- this . _setTransform ( preview , elementRect . left , elementRect . top ) ;
548
+ preview . style . transform = getTransform ( elementRect . left , elementRect . top ) ;
537
549
}
538
550
539
551
extendStyles ( preview . style , {
@@ -603,7 +615,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
603
615
this . _preview . classList . add ( 'cdk-drag-animating' ) ;
604
616
605
617
// Move the preview to the placeholder position.
606
- this . _setTransform ( this . _preview , placeholderRect . left , placeholderRect . top ) ;
618
+ this . _preview . style . transform = getTransform ( placeholderRect . left , placeholderRect . top ) ;
607
619
608
620
// If the element doesn't have a `transition`, the `transitionend` event won't fire. Since
609
621
// we need to trigger a style recalculation in order for the `cdk-drag-animating` class to
@@ -634,16 +646,6 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
634
646
} ) ;
635
647
}
636
648
637
- /**
638
- * Sets the `transform` style on an element.
639
- * @param element Element on which to set the transform.
640
- * @param x Desired position of the element along the X axis.
641
- * @param y Desired position of the element along the Y axis.
642
- */
643
- private _setTransform ( element : HTMLElement , x : number , y : number ) {
644
- element . style . transform = `translate3d(${ x } px, ${ y } px, 0)` ;
645
- }
646
-
647
649
/**
648
650
* Helper to remove an element from the DOM and to do all the necessary null checks.
649
651
* @param element Element to be removed.
@@ -768,3 +770,12 @@ interface Point {
768
770
x : number ;
769
771
y : number ;
770
772
}
773
+
774
+ /**
775
+ * Gets a 3d `transform` that can be applied to an element.
776
+ * @param x Desired position of the element along the X axis.
777
+ * @param y Desired position of the element along the Y axis.
778
+ */
779
+ function getTransform ( x : number , y : number ) : string {
780
+ return `translate3d(${ x } px, ${ y } px, 0)` ;
781
+ }
0 commit comments