Skip to content

fix(drag-drop): add support for dragging svg elements in IE11 #14215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/cdk/drag-drop/drag.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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