Skip to content

fix(cdk/overlay): ensure OverlayOutsideClickDispatcher listens to contextmenu events #20373

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 1 commit into from
Aug 21, 2020
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {TestBed, inject} from '@angular/core/testing';
import {Component, NgModule} from '@angular/core';
import {dispatchMouseEvent} from '@angular/cdk/testing/private';
import {OverlayModule, OverlayContainer, Overlay} from '../index';
import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher';
import {ComponentPortal} from '@angular/cdk/portal';


describe('OverlayOutsideClickDispatcher', () => {
let outsideClickDispatcher: OverlayOutsideClickDispatcher;
let overlay: Overlay;
Expand Down Expand Up @@ -155,6 +155,37 @@ describe('OverlayOutsideClickDispatcher', () => {

overlayRef.dispose();
});

it('should dispatch an event when a context menu is triggered outside the overlay', () => {
const portal = new ComponentPortal(TestComponent);
const overlayRef = overlay.create();
overlayRef.attach(portal);
const context = document.createElement('div');
document.body.appendChild(context);

const spy = jasmine.createSpy('overlay contextmenu spy');
overlayRef.outsidePointerEvents().subscribe(spy);

dispatchMouseEvent(context, 'contextmenu');
expect(spy).toHaveBeenCalled();

context.parentNode!.removeChild(context);
overlayRef.dispose();
});

it('should not dispatch an event when a context menu is triggered inside the overlay', () => {
const portal = new ComponentPortal(TestComponent);
const overlayRef = overlay.create();
overlayRef.attach(portal);

const spy = jasmine.createSpy('overlay contextmenu spy');
overlayRef.outsidePointerEvents().subscribe(spy);

dispatchMouseEvent(overlayRef.overlayElement, 'contextmenu');
expect(spy).not.toHaveBeenCalled();

overlayRef.dispose();
});
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
// tslint:enable: max-line-length
if (!this._isAttached) {
this._document.body.addEventListener('click', this._clickListener, true);
this._document.body.addEventListener('contextmenu', this._clickListener, true);

// click event is not fired on iOS. To make element "clickable" we are
// setting the cursor to pointer
Expand All @@ -57,6 +58,7 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
protected detach() {
if (this._isAttached) {
this._document.body.removeEventListener('click', this._clickListener, true);
this._document.body.removeEventListener('contextmenu', this._clickListener, true);
if (this._platform.IOS && this._cursorStyleIsSet) {
this._document.body.style.cursor = this._cursorOriginalValue;
this._cursorStyleIsSet = false;
Expand Down