Skip to content

Commit

Permalink
fix(cdk/drag-drop): element not draggable when has initial transform … (
Browse files Browse the repository at this point in the history
#22458)

* fix(cdk/drag-drop): element not draggable when has initial transform none

* fix(cdk/drag-drop): fix for recommended changes to code and tests cases

(cherry picked from commit 83d82b9)
  • Loading branch information
laiseng authored and mmalerba committed Jul 28, 2021
1 parent cdd6663 commit b8b8919
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ describe('CdkDrag', () => {
}));
});

describe('mouse dragging when initial transform is none', () => {
it('should drag an element freely to a particular position', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;
dragElement.style.transform = 'none';

dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
}));
});

it('should dispatch an event when the user has started dragging', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
fixture.detectChanges();
Expand Down
6 changes: 5 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,12 @@ export class DragRef<T = any> {

// Cache the previous transform amount only after the first drag sequence, because
// we don't want our own transforms to stack on top of each other.
// Should be excluded none because none + translate3d(x, y, x) is invalid css
if (this._initialTransform == null) {
this._initialTransform = this._rootElement.style.transform || '';
this._initialTransform = this._rootElement.style.transform
&& this._rootElement.style.transform != 'none'
? this._rootElement.style.transform
: '';
}

// Preserve the previous `transform` value, if there was one. Note that we apply our own
Expand Down
4 changes: 3 additions & 1 deletion src/cdk/drag-drop/drag-styling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ export function toggleVisibility(element: HTMLElement,
* that exited before the base transform was applied.
*/
export function combineTransforms(transform: string, initialTransform?: string): string {
return initialTransform ? (transform + ' ' + initialTransform) : transform;
return initialTransform && initialTransform != 'none' ?
(transform + ' ' + initialTransform) :
transform;
}

0 comments on commit b8b8919

Please sign in to comment.