-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(menu): unable to open same sub-menu from different triggers and not picking up indirect descendant items #10132
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ import { | |
OnDestroy, | ||
OnInit, | ||
Output, | ||
QueryList, | ||
TemplateRef, | ||
QueryList, | ||
ViewChild, | ||
ViewEncapsulation, | ||
} from '@angular/core'; | ||
|
@@ -36,7 +36,7 @@ import {matMenuAnimations} from './menu-animations'; | |
import {MatMenuContent} from './menu-content'; | ||
import {throwMatMenuInvalidPositionX, throwMatMenuInvalidPositionY} from './menu-errors'; | ||
import {MatMenuItem} from './menu-item'; | ||
import {MatMenuPanel} from './menu-panel'; | ||
import {MAT_MENU_PANEL, MatMenuPanel} from './menu-panel'; | ||
import {MenuPositionX, MenuPositionY} from './menu-positions'; | ||
|
||
|
||
|
@@ -84,18 +84,27 @@ const MAT_MENU_BASE_ELEVATION = 2; | |
styleUrls: ['menu.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
exportAs: 'matMenu', | ||
animations: [ | ||
matMenuAnimations.transformMenu, | ||
matMenuAnimations.fadeInItems | ||
], | ||
exportAs: 'matMenu' | ||
providers: [ | ||
{provide: MAT_MENU_PANEL, useExisting: MatMenu} | ||
] | ||
}) | ||
export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestroy { | ||
export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel<MatMenuItem>, OnDestroy { | ||
private _keyManager: FocusKeyManager<MatMenuItem>; | ||
private _xPosition: MenuPositionX = this._defaultOptions.xPosition; | ||
private _yPosition: MenuPositionY = this._defaultOptions.yPosition; | ||
private _previousElevation: string; | ||
|
||
/** Menu items inside the current menu. */ | ||
private _items: MatMenuItem[] = []; | ||
|
||
/** Emits whenever the amount of menu items changes. */ | ||
private _itemChanges = new Subject<MatMenuItem[]>(); | ||
|
||
/** Subscription to tab events on the menu panel */ | ||
private _tabSubscription = Subscription.EMPTY; | ||
|
||
|
@@ -106,7 +115,10 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
_panelAnimationState: 'void' | 'enter' = 'void'; | ||
|
||
/** Emits whenever an animation on the menu completes. */ | ||
_animationDone = new Subject<void>(); | ||
_animationDone = new Subject<AnimationEvent>(); | ||
|
||
/** Whether the menu is animating. */ | ||
_isAnimating: boolean; | ||
|
||
/** Parent menu of the current menu panel. */ | ||
parentMenu: MatMenuPanel | undefined; | ||
|
@@ -142,7 +154,11 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
/** @docs-private */ | ||
@ViewChild(TemplateRef) templateRef: TemplateRef<any>; | ||
|
||
/** List of the items inside of a menu. */ | ||
/** | ||
* List of the items inside of a menu. | ||
* @deprecated | ||
* @deletion-target 7.0.0 | ||
*/ | ||
@ContentChildren(MatMenuItem) items: QueryList<MatMenuItem>; | ||
|
||
/** | ||
|
@@ -218,7 +234,7 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
} | ||
|
||
ngAfterContentInit() { | ||
this._keyManager = new FocusKeyManager<MatMenuItem>(this.items).withWrap().withTypeAhead(); | ||
this._keyManager = new FocusKeyManager<MatMenuItem>(this._items).withWrap().withTypeAhead(); | ||
this._tabSubscription = this._keyManager.tabOut.subscribe(() => this.close.emit('tab')); | ||
} | ||
|
||
|
@@ -229,16 +245,10 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
|
||
/** Stream that emits whenever the hovered menu item changes. */ | ||
_hovered(): Observable<MatMenuItem> { | ||
if (this.items) { | ||
return this.items.changes.pipe( | ||
startWith(this.items), | ||
switchMap(items => merge(...items.map(item => item._hovered))) | ||
); | ||
} | ||
|
||
return this._ngZone.onStable | ||
.asObservable() | ||
.pipe(take(1), switchMap(() => this._hovered())); | ||
return this._itemChanges.pipe( | ||
startWith(this._items), | ||
switchMap(items => merge(...items.map(item => item._hovered))) | ||
); | ||
} | ||
|
||
/** Handle a keyboard event from the menu, delegating to the appropriate action. */ | ||
|
@@ -322,6 +332,35 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
} | ||
} | ||
|
||
/** | ||
* Registers a menu item with the menu. | ||
* @docs-private | ||
*/ | ||
addItem(item: MatMenuItem) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does needing to call these methods constitute a breaking change for people implementing their own menu panel? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. We never required the |
||
// We register the items through this method, rather than picking them up through | ||
// `ContentChildren`, because we need the items to be picked up by their closest | ||
// `mat-menu` ancestor. If we used `@ContentChildren(MatMenuItem, {descendants: true})`, | ||
// all descendant items will bleed into the top-level menu in the case where the consumer | ||
// has `mat-menu` instances nested inside each other. | ||
if (this._items.indexOf(item) === -1) { | ||
this._items.push(item); | ||
this._itemChanges.next(this._items); | ||
} | ||
} | ||
|
||
/** | ||
* Removes an item from the menu. | ||
* @docs-private | ||
*/ | ||
removeItem(item: MatMenuItem) { | ||
const index = this._items.indexOf(item); | ||
|
||
if (this._items.indexOf(item) > -1) { | ||
this._items.splice(index, 1); | ||
this._itemChanges.next(this._items); | ||
} | ||
} | ||
|
||
/** Starts the enter animation. */ | ||
_startAnimation() { | ||
// @deletion-target 7.0.0 Combine with _resetAnimation. | ||
|
@@ -335,7 +374,8 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro | |
} | ||
|
||
/** Callback that is invoked when the panel animation completes. */ | ||
_onAnimationDone() { | ||
this._animationDone.next(); | ||
_onAnimationDone(event: AnimationEvent) { | ||
this._animationDone.next(event); | ||
this._isAnimating = false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expand to talk about why this registration is necessary?