MCPcopy Create free account
hub / github.com/angular/components / _onKeydown

Method _onKeydown

src/cdk/menu/menu-item.ts:201–243  ·  view source on GitHub ↗

* Handles keyboard events for the menu item, specifically either triggering the user defined * callback or opening/closing the current menu based on whether the left or right arrow key was * pressed. * @param event the keyboard event to handle

(event: KeyboardEvent)

Source from the content-addressed store, hash-verified

199 * @param event the keyboard event to handle
200 */
201 _onKeydown(event: KeyboardEvent) {
202 switch (event.keyCode) {
203 case SPACE:
204 case ENTER:
205 // Skip events that will trigger clicks so the handler doesn't get triggered twice.
206 if (!hasModifierKey(event) && !eventDispatchesNativeClick(this._elementRef, event)) {
207 const nodeName = this._elementRef.nativeElement.nodeName;
208
209 // Avoid repeat events on non-native elements (see #30250). Note that we don't do this
210 // on the native elements so we don't interfere with their behavior (see #26296).
211 if (nodeName !== 'A' && nodeName !== 'BUTTON') {
212 event.preventDefault();
213 }
214
215 this.trigger({keepOpen: event.keyCode === SPACE && !this.closeOnSpacebarTrigger});
216 }
217 break;
218
219 case RIGHT_ARROW:
220 if (!hasModifierKey(event)) {
221 if (this._parentMenu && this._isParentVertical()) {
222 if (this._dir?.value !== 'rtl') {
223 this._forwardArrowPressed(event);
224 } else {
225 this._backArrowPressed(event);
226 }
227 }
228 }
229 break;
230
231 case LEFT_ARROW:
232 if (!hasModifierKey(event)) {
233 if (this._parentMenu && this._isParentVertical()) {
234 if (this._dir?.value !== 'rtl') {
235 this._backArrowPressed(event);
236 } else {
237 this._forwardArrowPressed(event);
238 }
239 }
240 }
241 break;
242 }
243 }
244
245 /** Whether this menu item is standalone or within a menu or menu bar. */
246 private _isStandaloneItem() {

Callers

nothing calls this directly

Calls 6

triggerMethod · 0.95
_isParentVerticalMethod · 0.95
_forwardArrowPressedMethod · 0.95
_backArrowPressedMethod · 0.95
hasModifierKeyFunction · 0.90

Tested by

no test coverage detected