Skip to content

Commit

Permalink
fix(cdk/drag-drop): handle not working when it has a child inside sha…
Browse files Browse the repository at this point in the history
…dow DOM

Fixes that the children of the drag handle inside a shadow root weren't being detected. The problem was that we were using `_getEventTarget` to resolve the actual event target and using `contains` to verify that it's inside the handle. Since `contains` doesn't descend into shadow root, the call failed. These changes remove the `_getEventTarget` call since we can use the event `target` directly.

Fixes #23680.

(cherry picked from commit b9cda57)
  • Loading branch information
crisbeto authored and mmalerba committed Nov 15, 2021
1 parent a38fcc2 commit a24b1ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,26 @@ describe('CdkDrag', () => {
flush();
}).toThrowError(/^cdkDragHandle must be attached to an element node/);
}));

it('should be able to drag an element using a handle with a shadow DOM child', fakeAsync(() => {
if (!_supportsShadowDom()) {
return;
}

const fixture = createComponent(
StandaloneDraggableWithShadowInsideHandle,
undefined,
undefined,
[ShadowWrapper],
);
fixture.detectChanges();
const dragElement = fixture.componentInstance.dragElement.nativeElement;
const handleChild = fixture.componentInstance.handleChild.nativeElement;

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

describe('in a drop container', () => {
Expand Down Expand Up @@ -6461,6 +6481,29 @@ class StandaloneDraggableWithIndirectHandle {
@ViewChild('handleElement') handleElement: ElementRef<HTMLElement>;
}

@Component({
selector: 'shadow-wrapper',
template: '<ng-content></ng-content>',
encapsulation: ViewEncapsulation.ShadowDom,
})
class ShadowWrapper {}

@Component({
template: `
<div #dragElement cdkDrag style="width: 100px; height: 100px; background: red;">
<div cdkDragHandle style="width: 10px; height: 10px;">
<shadow-wrapper>
<div #handleChild style="width: 10px; height: 10px; background: green;"></div>
</shadow-wrapper>
</div>
</div>
`,
})
class StandaloneDraggableWithShadowInsideHandle {
@ViewChild('dragElement') dragElement: ElementRef<HTMLElement>;
@ViewChild('handleChild') handleChild: ElementRef<HTMLElement>;
}

@Component({
encapsulation: ViewEncapsulation.None,
styles: [
Expand Down
3 changes: 1 addition & 2 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,7 @@ export class DragRef<T = any> {
// Delegate the event based on whether it started from a handle or the element itself.
if (this._handles.length) {
const targetHandle = this._handles.find(handle => {
const target = _getEventTarget(event);
return !!target && (target === handle || handle.contains(target as HTMLElement));
return event.target && (event.target === handle || handle.contains(event.target as Node));
});

if (targetHandle && !this._disabledHandles.has(targetHandle) && !this.disabled) {
Expand Down

0 comments on commit a24b1ac

Please sign in to comment.