Skip to content

Commit de05a67

Browse files
laisengmmalerba
authored andcommitted
fix(cdk/drag-drop): element not draggable when has initial transform none
1 parent 0742bab commit de05a67

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,51 @@ describe('CdkDrag', () => {
407407
}));
408408
});
409409

410+
describe('mouse dragging when initial transform is none', () => {
411+
it('should drag an element freely to a particular position', fakeAsync(() => {
412+
const fixture = createComponent(StandaloneDraggableWithInitialTransformNone);
413+
fixture.detectChanges();
414+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
415+
416+
expect(dragElement.style.transform).toBe('none');
417+
dragElementViaMouse(fixture, dragElement, 50, 100);
418+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
419+
}));
420+
421+
it('should drag an SVG element freely to a particular position', fakeAsync(() => {
422+
const fixture = createComponent(StandaloneDraggableSvgWithInitialTransformNone);
423+
fixture.detectChanges();
424+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
425+
426+
expect(dragElement.style.transform).toBe('none');
427+
dragElementViaMouse(fixture, dragElement, 50, 100);
428+
expect(dragElement.getAttribute('transform')).toBe('translate(50 100)');
429+
}));
430+
431+
it('should drag an SVG element freely to a particular position in SVG viewBox coordinates',
432+
fakeAsync(() => {
433+
const fixture = createComponent(StandaloneDraggableSvgWithViewBoxInitialTranformNone);
434+
fixture.detectChanges();
435+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
436+
437+
expect(dragElement.style.transform).toBe('none');
438+
dragElementViaMouse(fixture, dragElement, 50, 100);
439+
expect(dragElement.getAttribute('transform')).toBe('translate(100 200)');
440+
}));
441+
});
442+
443+
describe('touch dragging when initial transform is none', () => {
444+
it('should drag an element freely to a particular position', fakeAsync(() => {
445+
const fixture = createComponent(StandaloneDraggableWithInitialTransformNone);
446+
fixture.detectChanges();
447+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
448+
449+
expect(dragElement.style.transform).toBe('none');
450+
dragElementViaTouch(fixture, dragElement, 50, 100);
451+
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
452+
}));
453+
});
454+
410455
it('should dispatch an event when the user has started dragging', fakeAsync(() => {
411456
const fixture = createComponent(StandaloneDraggable);
412457
fixture.detectChanges();
@@ -5936,6 +5981,21 @@ class StandaloneDraggable {
59365981
freeDragPosition?: {x: number, y: number};
59375982
}
59385983

5984+
@Component({
5985+
template: `
5986+
<div class="wrapper" style="width: 200px; height: 200px; background: green;">
5987+
<div
5988+
cdkDrag
5989+
#dragElement
5990+
style="transform: none; width: 100px; height: 100px; background: red;"></div>
5991+
</div>
5992+
`
5993+
})
5994+
class StandaloneDraggableWithInitialTransformNone {
5995+
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
5996+
@ViewChild(CdkDrag) dragInstance: CdkDrag;
5997+
}
5998+
59395999
@Component({
59406000
changeDetection: ChangeDetectionStrategy.OnPush,
59416001
template: `
@@ -5960,6 +6020,34 @@ class StandaloneDraggableSvg {
59606020
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
59616021
}
59626022

6023+
@Component({
6024+
template: `
6025+
<svg><g
6026+
cdkDrag
6027+
style="transform: none"
6028+
#dragElement>
6029+
<circle fill="red" r="50" cx="50" cy="50"/>
6030+
</g></svg>
6031+
`
6032+
})
6033+
class StandaloneDraggableSvgWithInitialTransformNone {
6034+
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
6035+
}
6036+
6037+
@Component({
6038+
template: `
6039+
<svg width="400px" height="400px" viewBox="0 0 800 800"><g
6040+
cdkDrag
6041+
style="transform: none"
6042+
#dragElement>
6043+
<circle fill="red" r="50" cx="50" cy="50"/>
6044+
</g></svg>
6045+
`
6046+
})
6047+
class StandaloneDraggableSvgWithViewBoxInitialTranformNone {
6048+
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
6049+
}
6050+
59636051
@Component({
59646052
template: `
59656053
<svg width="400px" height="400px" viewBox="0 0 800 800"><g

src/cdk/drag-drop/drag-styling.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,7 @@ export function toggleVisibility(element: HTMLElement,
8282
* that exited before the base transform was applied.
8383
*/
8484
export function combineTransforms(transform: string, initialTransform?: string): string {
85-
return initialTransform ? (transform + ' ' + initialTransform) : transform;
85+
return initialTransform && initialTransform != 'none' ?
86+
(transform + ' ' + initialTransform) :
87+
transform;
8688
}

0 commit comments

Comments
 (0)