Skip to content

Commit

Permalink
fix(drag-drop): add support for dragging svg elements in IE11 (#14215)
Browse files Browse the repository at this point in the history
Added check for when _rootElement is an SVGElement and set the transform attribute as well. IE11 does not acknowledge the style.transform property, but does allow the `transform` attribute

Closes #14214
  • Loading branch information
evanfuture authored and jelbourn committed Dec 3, 2018
1 parent ad75cad commit 81db16c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Expand Up @@ -66,6 +66,16 @@ describe('CdkDrag', () => {
expect(dragElement.style.transform).toBe('translate3d(50px, 100px, 0px)');
}));

it('should drag an SVG element freely to a particular position', fakeAsync(() => {
const fixture = createComponent(StandaloneDraggableSvg);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;

expect(dragElement.getAttribute('transform')).toBeFalsy();
dragElementViaMouse(fixture, dragElement, 50, 100);
expect(dragElement.getAttribute('transform')).toBe('translate(50 100)');
}));

it('should drag an element freely to a particular position when the page is scrolled',
fakeAsync(() => {
const fixture = createComponent(StandaloneDraggable);
Expand Down Expand Up @@ -2229,6 +2239,19 @@ class StandaloneDraggable {
endedSpy = jasmine.createSpy('ended spy');
}

@Component({
template: `
<svg><g
cdkDrag
#dragElement>
<circle fill="red" r="50" cx="50" cy="50"/>
</g></svg>
`
})
class StandaloneDraggableSvg {
@ViewChild('dragElement') dragElement: ElementRef<SVGElement>;
}

@Component({
template: `
<div #dragElement cdkDrag
Expand Down
6 changes: 6 additions & 0 deletions src/cdk/drag-drop/drag.ts
Expand Up @@ -491,6 +491,12 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
// Preserve the previous `transform` value, if there was one.
this._rootElement.style.transform = this._initialTransform ?
this._initialTransform + ' ' + transform : transform;

// Apply transform as attribute if dragging and svg element to work for IE
if (typeof SVGElement !== 'undefined' && this._rootElement instanceof SVGElement) {
const appliedTransform = `translate(${activeTransform.x} ${activeTransform.y})`;
this._rootElement.setAttribute('transform', appliedTransform);
}
}

// Since this event gets fired for every pixel while dragging, we only
Expand Down

0 comments on commit 81db16c

Please sign in to comment.