-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathmenu-item.ts
175 lines (147 loc) · 5.52 KB
/
menu-item.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
ChangeDetectionStrategy,
Component,
ElementRef,
OnDestroy,
ViewEncapsulation,
Input,
AfterViewInit,
ChangeDetectorRef,
booleanAttribute,
inject,
} from '@angular/core';
import {FocusableOption, FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Subject} from 'rxjs';
import {DOCUMENT} from '@angular/common';
import {MatMenuPanel, MAT_MENU_PANEL} from './menu-panel';
import {_StructuralStylesLoader, MatRipple} from '../core';
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
/**
* Single item inside a `mat-menu`. Provides the menu item styling and accessibility treatment.
*/
@Component({
selector: '[mat-menu-item]',
exportAs: 'matMenuItem',
host: {
'[attr.role]': 'role',
'class': 'mat-mdc-menu-item mat-focus-indicator',
'[class.mat-mdc-menu-item-highlighted]': '_highlighted',
'[class.mat-mdc-menu-item-submenu-trigger]': '_triggersSubmenu',
'[attr.tabindex]': '_getTabIndex()',
'[attr.aria-disabled]': 'disabled && disabledInteractive ? "true" : null',
'[attr.disabled]': 'disabled || null',
'[class.mat-mdc-menu-item-disabled-interactive]': 'disabledInteractive',
'(click)': '_checkDisabled($event)',
'(mouseenter)': '_handleMouseEnter()',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
templateUrl: 'menu-item.html',
imports: [MatRipple],
})
export class MatMenuItem implements FocusableOption, AfterViewInit, OnDestroy {
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
private _document = inject(DOCUMENT);
private _focusMonitor = inject(FocusMonitor);
_parentMenu? = inject<MatMenuPanel<MatMenuItem>>(MAT_MENU_PANEL, {optional: true});
private _changeDetectorRef = inject(ChangeDetectorRef);
/** ARIA role for the menu item. */
@Input() role: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem';
/** Whether the menu item is disabled. */
@Input({transform: booleanAttribute}) disabled: boolean = false;
/** Whether ripples are disabled on the menu item. */
@Input({transform: booleanAttribute}) disableRipple: boolean = false;
/** Whether the menu item should remain interactive when it is disabled. */
@Input({transform: booleanAttribute})
disabledInteractive: boolean = false;
/** Stream that emits when the menu item is hovered. */
readonly _hovered: Subject<MatMenuItem> = new Subject<MatMenuItem>();
/** Stream that emits when the menu item is focused. */
readonly _focused = new Subject<MatMenuItem>();
/** Whether the menu item is highlighted. */
_highlighted: boolean = false;
/** Whether the menu item acts as a trigger for a sub-menu. */
_triggersSubmenu: boolean = false;
constructor(...args: unknown[]);
constructor() {
inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader);
this._parentMenu?.addItem?.(this);
}
/** Focuses the menu item. */
focus(origin?: FocusOrigin, options?: FocusOptions): void {
if (this._focusMonitor && origin) {
this._focusMonitor.focusVia(this._getHostElement(), origin, options);
} else {
this._getHostElement().focus(options);
}
this._focused.next(this);
}
ngAfterViewInit() {
if (this._focusMonitor) {
// Start monitoring the element, so it gets the appropriate focused classes. We want
// to show the focus style for menu items only when the focus was not caused by a
// mouse or touch interaction.
this._focusMonitor.monitor(this._elementRef, false);
}
}
ngOnDestroy() {
if (this._focusMonitor) {
this._focusMonitor.stopMonitoring(this._elementRef);
}
if (this._parentMenu && this._parentMenu.removeItem) {
this._parentMenu.removeItem(this);
}
this._hovered.complete();
this._focused.complete();
}
/** Used to set the `tabindex`. */
_getTabIndex(): string {
return this.disabled && !this.disabledInteractive ? '-1' : '0';
}
/** Returns the host DOM element. */
_getHostElement(): HTMLElement {
return this._elementRef.nativeElement;
}
/** Prevents the default element actions if it is disabled. */
_checkDisabled(event: Event): void {
if (this.disabled) {
event.preventDefault();
event.stopPropagation();
}
}
/** Emits to the hover stream. */
_handleMouseEnter() {
this._hovered.next(this);
}
/** Gets the label to be used when determining whether the option should be focused. */
getLabel(): string {
const clone = this._elementRef.nativeElement.cloneNode(true) as HTMLElement;
const icons = clone.querySelectorAll('mat-icon, .material-icons');
// Strip away icons, so they don't show up in the text.
for (let i = 0; i < icons.length; i++) {
icons[i].remove();
}
return clone.textContent?.trim() || '';
}
_setHighlighted(isHighlighted: boolean) {
// We need to mark this for check for the case where the content is coming from a
// `matMenuContent` whose change detection tree is at the declaration position,
// not the insertion position. See #23175.
this._highlighted = isHighlighted;
this._changeDetectorRef.markForCheck();
}
_setTriggersSubmenu(triggersSubmenu: boolean) {
this._triggersSubmenu = triggersSubmenu;
this._changeDetectorRef.markForCheck();
}
_hasFocus(): boolean {
return this._document && this._document.activeElement === this._getHostElement();
}
}