MCPcopy Index your code
hub / github.com/angular/components / _openMenu

Function _openMenu

src/material/menu/menu-trigger-base.ts:196–258  ·  view source on GitHub ↗

Internal method to open menu providing option to auto focus on first item.

(autoFocus: boolean)

Source from the content-addressed store, hash-verified

194
195 /** Internal method to open menu providing option to auto focus on first item. */
196 protected _openMenu(autoFocus: boolean): void {
197 if (this._triggerIsAriaDisabled()) {
198 return;
199 }
200
201 const menu = this._menu;
202
203 if (this._menuOpen || !menu) {
204 return;
205 }
206
207 this._pendingRemoval?.unsubscribe();
208 const previousTrigger = PANELS_TO_TRIGGERS.get(menu);
209 PANELS_TO_TRIGGERS.set(menu, this);
210
211 // If the same menu is currently attached to another trigger,
212 // we need to close it so it doesn't end up in a broken state.
213 if (previousTrigger && previousTrigger !== this) {
214 previousTrigger._closeMenu();
215 }
216
217 const overlayRef = this._createOverlay(menu);
218 const overlayConfig = overlayRef.getConfig();
219 const positionStrategy = overlayConfig.positionStrategy as FlexibleConnectedPositionStrategy;
220
221 this._setPosition(menu, positionStrategy);
222
223 if (this._canHaveBackdrop) {
224 overlayConfig.hasBackdrop =
225 menu.hasBackdrop == null ? !this._triggersSubmenu() : menu.hasBackdrop;
226 } else {
227 overlayConfig.hasBackdrop = menu.hasBackdrop ?? false;
228 }
229
230 // We need the `hasAttached` check for the case where the user kicked off a removal animation,
231 // but re-entered the menu. Re-attaching the same portal will trigger an error otherwise.
232 if (!overlayRef.hasAttached()) {
233 overlayRef.attach(this._getPortal(menu));
234 menu.lazyContent?.attach(this.menuData);
235 }
236
237 this._closingActionsSubscription = this._menuClosingActions().subscribe(() =>
238 this._closeMenu(),
239 );
240 menu.parentMenu = this._triggersSubmenu() ? this._parentMaterialMenu : undefined;
241 menu.direction = this.dir;
242
243 if (autoFocus) {
244 menu.focusFirstItem(this._openedBy || 'program');
245 }
246
247 this._setIsMenuOpen(true);
248
249 if (menu instanceof MatMenu) {
250 menu._setIsOpen(true);
251 menu._directDescendantItems.changes.pipe(takeUntil(menu.close)).subscribe(() => {
252 // Re-adjust the position without locking when the amount of items
253 // changes so that the overlay is allowed to pick a new optimal position.

Callers

nothing calls this directly

Calls 10

setMethod · 0.80
getConfigMethod · 0.80
_setIsOpenMethod · 0.80
reapplyLastPositionMethod · 0.80
withLockedPositionMethod · 0.80
getMethod · 0.65
hasAttachedMethod · 0.65
attachMethod · 0.65
focusFirstItemMethod · 0.65
_createOverlayMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…