Skip to content

Commit 584265c

Browse files
committed
filter to menus in overlays
1 parent ef73bff commit 584265c

File tree

6 files changed

+29
-190
lines changed

6 files changed

+29
-190
lines changed

src/cdk-experimental/menu/background-click-service.ts

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/cdk-experimental/menu/context-menu.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {isClickInsideMenuOverlay} from '@angular/cdk-experimental/menu/menu';
910
import {
1011
Directive,
1112
Input,
@@ -34,7 +35,6 @@ import {takeUntil} from 'rxjs/operators';
3435
import {CdkMenuPanel} from './menu-panel';
3536
import {MenuStack, MenuStackItem} from './menu-stack';
3637
import {throwExistingMenuStackError} from './menu-errors';
37-
import {BackgroundClickService} from './background-click-service';
3838

3939
/**
4040
* Whether the context menu should close when some element is clicked.
@@ -157,7 +157,6 @@ export class CdkContextMenuTrigger implements OnDestroy {
157157
private readonly _overlay: Overlay,
158158
private readonly _contextMenuTracker: ContextMenuTracker,
159159
@Inject(CDK_CONTEXT_MENU_DEFAULT_OPTIONS) private readonly _options: ContextMenuOptions,
160-
private readonly _closeService: BackgroundClickService,
161160
@Optional() private readonly _directionality?: Directionality
162161
) {
163162
this._setMenuStackListener();
@@ -190,8 +189,12 @@ export class CdkContextMenuTrigger implements OnDestroy {
190189
}
191190

192191
this._overlayRef.attach(this._getMenuContent());
193-
194-
this._closeService.startListener(shouldCloseMenu, this._menuStack);
192+
this._overlayRef.outsidePointerEvents().subscribe(event => {
193+
if (!isClickInsideMenuOverlay(event.target as Element)) {
194+
this._overlayRef?.dispose();
195+
this._overlayRef = null;
196+
}
197+
});
195198
}
196199
}
197200

src/cdk-experimental/menu/menu-bar.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,7 @@ import {CDK_MENU, Menu} from './menu-interface';
2828
import {CdkMenuItem} from './menu-item';
2929
import {MenuStack, MenuStackItem, FocusNext} from './menu-stack';
3030
import {getItemPointerEntries} from './item-pointer-entries';
31-
import {CLOSE_DECIDER, CloseDecider} from './background-click-service';
32-
33-
/** Whether the menu stack should be closed when target is clicked. */
34-
export function shouldCloseMenu(target: Element | null) {
35-
while (target) {
36-
if (
37-
target.classList.contains('cdk-menu-bar') ||
38-
(target.classList.contains('cdk-menu') && !target.classList.contains('cdk-menu-inline'))
39-
) {
40-
return false;
41-
}
42-
target = target.parentElement;
43-
}
44-
return true;
45-
}
4631

47-
/** Provider for the background click decider function for the MenuBar. */
48-
export const MENUBAR_CLOSE_PROVIDER: {
49-
provide: InjectionToken<CloseDecider>;
50-
useValue: (target: Element | null) => boolean;
51-
} = {provide: CLOSE_DECIDER, useValue: shouldCloseMenu};
5232

5333
/**
5434
* Directive applied to an element which configures it as a MenuBar by setting the appropriate
@@ -69,7 +49,6 @@ export const MENUBAR_CLOSE_PROVIDER: {
6949
{provide: CdkMenuGroup, useExisting: CdkMenuBar},
7050
{provide: CDK_MENU, useExisting: CdkMenuBar},
7151
{provide: MenuStack, useClass: MenuStack},
72-
MENUBAR_CLOSE_PROVIDER,
7352
],
7453
})
7554
export class CdkMenuBar extends CdkMenuGroup implements Menu, AfterContentInit, OnDestroy {

src/cdk-experimental/menu/menu-item-trigger.ts

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {isClickInsideMenuOverlay} from '@angular/cdk-experimental/menu/menu';
910
import {
1011
Directive,
1112
Input,
@@ -34,21 +35,7 @@ import {CdkMenuPanel} from './menu-panel';
3435
import {Menu, CDK_MENU} from './menu-interface';
3536
import {throwExistingMenuStackError} from './menu-errors';
3637
import {FocusNext, MenuStack} from './menu-stack';
37-
import {BackgroundClickService, CloseDecider, CLOSE_DECIDER} from './background-click-service';
38-
39-
/** Whether the standalone menu should be closed. */
40-
function shouldCloseMenu(target: Element | null) {
41-
while (target) {
42-
if (
43-
(target.classList.contains('cdk-menu') && !target.classList.contains('cdk-menu-inline')) ||
44-
target.classList.contains('cdk-standalone-menu-item')
45-
) {
46-
return false;
47-
}
48-
target = target.parentElement;
49-
}
50-
return true;
51-
}
38+
5239

5340
/**
5441
* A directive to be combined with CdkMenuItem which opens the Menu it is bound to. If the
@@ -64,7 +51,7 @@ function shouldCloseMenu(target: Element | null) {
6451
exportAs: 'cdkMenuTriggerFor',
6552
host: {
6653
'(keydown)': '_toggleOnKeydown($event)',
67-
'(mouseenter)': '_toggleOnMouseEnter()',
54+
//'(mouseenter)': '_toggleOnMouseEnter()',
6855
'(click)': 'toggle()',
6956
'aria-haspopup': 'menu',
7057
'[attr.aria-expanded]': 'isMenuOpen()',
@@ -116,22 +103,9 @@ export class CdkMenuItemTrigger implements OnDestroy {
116103
private readonly _elementRef: ElementRef<HTMLElement>,
117104
protected readonly _viewContainerRef: ViewContainerRef,
118105
private readonly _overlay: Overlay,
119-
private readonly _closeService: BackgroundClickService,
120-
@Optional()
121-
@Inject(CLOSE_DECIDER)
122-
private readonly _closeHandler?: CloseDecider,
123106
@Optional() @Inject(CDK_MENU) private readonly _parentMenu?: Menu,
124107
@Optional() private readonly _directionality?: Directionality
125-
) {
126-
this._registerCloseHandler();
127-
128-
// If there is no parent menu, this is a standalone trigger so we use the standalone trigger
129-
// close handler. If the parent is defined, the trigger should (optionally) inject the close
130-
// handler (sub-menus shouldn't inject).
131-
if (!_parentMenu) {
132-
this._closeHandler = shouldCloseMenu;
133-
}
134-
}
108+
) {}
135109

136110
/** Open/close the attached menu if the trigger has been configured with one */
137111
toggle() {
@@ -147,10 +121,12 @@ export class CdkMenuItemTrigger implements OnDestroy {
147121

148122
this._overlayRef = this._overlayRef || this._overlay.create(this._getOverlayConfig());
149123
this._overlayRef.attach(this._getPortal());
150-
151-
if (this._closeHandler) {
152-
this._closeService.startListener(this._closeHandler, this._getMenuStack());
153-
}
124+
this._overlayRef.outsidePointerEvents().subscribe(event => {
125+
if (!isClickInsideMenuOverlay(event.target as Element)) {
126+
this._overlayRef?.dispose();
127+
this._overlayRef = null;
128+
}
129+
});
154130
}
155131
}
156132

src/cdk-experimental/menu/menu.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,15 @@ export class CdkMenu extends CdkMenuGroup implements Menu, AfterContentInit, OnI
347347
this.closed.complete();
348348
}
349349
}
350+
351+
export function isClickInsideMenuOverlay(target: Element): boolean {
352+
while (target?.parentElement) {
353+
if (target.classList.contains('cdk-menu')) {
354+
return !!target?.parentElement?.classList?.contains('cdk-overlay-panel');
355+
}
356+
357+
target = target.parentElement;
358+
}
359+
360+
return false;
361+
}

src/material-experimental/menubar/menubar.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
CdkMenuGroup,
1313
CDK_MENU,
1414
MenuStack,
15-
MENUBAR_CLOSE_PROVIDER,
1615
} from '@angular/cdk-experimental/menu';
1716

1817
/**
@@ -37,7 +36,6 @@ import {
3736
{provide: CdkMenuBar, useExisting: MatMenuBar},
3837
{provide: CDK_MENU, useExisting: MatMenuBar},
3938
{provide: MenuStack, useClass: MenuStack},
40-
MENUBAR_CLOSE_PROVIDER,
4139
],
4240
})
4341
export class MatMenuBar extends CdkMenuBar {}

0 commit comments

Comments
 (0)