MCPcopy
hub / github.com/angular/components / MatMenuItem

Class MatMenuItem

src/material/menu/menu-item.ts:49–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47 imports: [MatRipple],
48})
49export class MatMenuItem implements FocusableOption, AfterViewInit, OnDestroy {
50 private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
51 private _document = inject(DOCUMENT);
52 private _focusMonitor = inject(FocusMonitor);
53 _parentMenu? = inject<MatMenuPanel<MatMenuItem>>(MAT_MENU_PANEL, {optional: true});
54 private _changeDetectorRef = inject(ChangeDetectorRef);
55
56 /** ARIA role for the menu item. */
57 @Input() role: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem';
58
59 /** Whether the menu item is disabled. */
60 @Input({transform: booleanAttribute}) disabled: boolean = false;
61
62 /** Whether ripples are disabled on the menu item. */
63 @Input({transform: booleanAttribute}) disableRipple: boolean = false;
64
65 /** Stream that emits when the menu item is hovered. */
66 readonly _hovered: Subject<MatMenuItem> = new Subject<MatMenuItem>();
67
68 /** Stream that emits when the menu item is focused. */
69 readonly _focused = new Subject<MatMenuItem>();
70
71 /** Whether the menu item is highlighted. */
72 _highlighted: boolean = false;
73
74 /** Whether the menu item acts as a trigger for a sub-menu. */
75 _triggersSubmenu: boolean = false;
76
77 constructor() {
78 inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader);
79 this._parentMenu?.addItem?.(this);
80 }
81
82 /** Focuses the menu item. */
83 focus(origin?: FocusOrigin, options?: FocusOptions): void {
84 if (this._focusMonitor && origin) {
85 this._focusMonitor.focusVia(this._getHostElement(), origin, options);
86 } else {
87 this._getHostElement().focus(options);
88 }
89
90 this._focused.next(this);
91 }
92
93 ngAfterViewInit() {
94 if (this._focusMonitor) {
95 // Start monitoring the element, so it gets the appropriate focused classes. We want
96 // to show the focus style for menu items only when the focus was not caused by a
97 // mouse or touch interaction.
98 this._focusMonitor.monitor(this._elementRef, false);
99 }
100 }
101
102 ngOnDestroy() {
103 if (this._focusMonitor) {
104 this._focusMonitor.stopMonitoring(this._elementRef);
105 }
106

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…