Skip to content

Commit 1565fa3

Browse files
authored
refactor(cdk/overlay): remove logic which tracks excluded elements from outside clicks (#19887)
This removes the ability to track excluded elements from the overlay and only stops outside click event notifications if the click event occurred within an overlay element itself. The overlay should not track elements to be excluded from outside click - this should be up to the developer. This allows for custom closeout logic to be implemented by the developer leaving the cdk to be unopinionated.
1 parent 3b3b55f commit 1565fa3

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.spec.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,34 @@ describe('OverlayOutsideClickDispatcher', () => {
125125
overlayTwo.dispose();
126126
});
127127

128-
it(`should not dispatch click event when click on element
129-
included in excludeFromOutsideClick array`, () => {
128+
it('should dispatch the click event when click is on an element outside the overlay', () => {
129+
const portal = new ComponentPortal(TestComponent);
130130
const overlayRef = overlay.create();
131-
const spy = jasmine.createSpy('overlay mouse click event spy');
131+
overlayRef.attach(portal);
132+
const button = document.createElement('button');
133+
document.body.appendChild(button);
134+
135+
const spy = jasmine.createSpy('overlay mouse click spy');
132136
overlayRef.outsidePointerEvents().subscribe(spy);
133137

134-
const overlayConfig = overlayRef.getConfig();
135-
expect(overlayConfig.excludeFromOutsideClick).toBeDefined();
136-
expect(overlayConfig.excludeFromOutsideClick!.length).toBe(0);
138+
button.click();
139+
expect(spy).toHaveBeenCalled();
137140

138-
overlayRef.attach(new ComponentPortal(TestComponent));
141+
button.parentNode!.removeChild(button);
142+
overlayRef.dispose();
143+
});
144+
145+
it('should not dispatch the click event when click is on an element inside the overlay', () => {
146+
const portal = new ComponentPortal(TestComponent);
147+
const overlayRef = overlay.create();
148+
overlayRef.attach(portal);
139149

140-
const buttonShouldNotDetach = document.createElement('button');
141-
document.body.appendChild(buttonShouldNotDetach);
142-
overlayConfig.excludeFromOutsideClick!.push(buttonShouldNotDetach);
143-
buttonShouldNotDetach.click();
150+
const spy = jasmine.createSpy('overlay mouse click event spy');
151+
overlayRef.outsidePointerEvents().subscribe(spy);
144152

153+
overlayRef.overlayElement.click();
145154
expect(spy).not.toHaveBeenCalled();
146155

147-
buttonShouldNotDetach.parentNode!.removeChild(buttonShouldNotDetach);
148156
overlayRef.dispose();
149157
});
150158
});

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
8181
continue;
8282
}
8383

84-
const config = overlayRef.getConfig();
85-
const excludeElements = [...config.excludeFromOutsideClick!, overlayRef.overlayElement];
86-
const isInsideClick: boolean = excludeElements.some(e => e.contains(target));
87-
88-
// If it is inside click just break - we should do nothing
89-
// If it is outside click dispatch the mouse event, and proceed with the next overlay
90-
if (isInsideClick) {
84+
// If it's a click inside the overlay, just break - we should do nothing
85+
// If it's an outside click dispatch the mouse event, and proceed with the next overlay
86+
if (overlayRef.overlayElement.contains(target as Node)) {
9187
break;
9288
}
9389

src/cdk/overlay/overlay-config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ export class OverlayConfig {
5959
*/
6060
disposeOnNavigation?: boolean = false;
6161

62-
/**
63-
* Array of HTML elements clicking on which should not be considered as outside click
64-
*/
65-
excludeFromOutsideClick?: HTMLElement[] = [];
66-
6762
constructor(config?: OverlayConfig) {
6863
if (config) {
6964
// Use `Iterable` instead of `Array` because TypeScript, as of 3.6.3,

tools/public_api_guard/cdk/overlay.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ export declare class OverlayConfig {
204204
backdropClass?: string | string[];
205205
direction?: Direction | Directionality;
206206
disposeOnNavigation?: boolean;
207-
excludeFromOutsideClick?: HTMLElement[];
208207
hasBackdrop?: boolean;
209208
height?: number | string;
210209
maxHeight?: number | string;

0 commit comments

Comments
 (0)