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

Method _takeFocus

src/material/sidenav/drawer.ts:435–468  ·  view source on GitHub ↗

* Moves focus into the drawer. Note that this works even if * the focus trap is disabled in `side` mode.

()

Source from the content-addressed store, hash-verified

433 * the focus trap is disabled in `side` mode.
434 */
435 private _takeFocus() {
436 if (!this._focusTrap) {
437 return;
438 }
439
440 const element = this._elementRef.nativeElement;
441
442 // When autoFocus is not on the sidenav, if the element cannot be focused or does
443 // not exist, focus the sidenav itself so the keyboard navigation still works.
444 // We need to check that `focus` is a function due to Universal.
445 switch (this.autoFocus) {
446 case false:
447 case 'dialog':
448 return;
449 case true:
450 case 'first-tabbable':
451 afterNextRender(
452 () => {
453 const hasMovedFocus = this._focusTrap!.focusInitialElement();
454 if (!hasMovedFocus && typeof element.focus === 'function') {
455 element.focus();
456 }
457 },
458 {injector: this._injector},
459 );
460 break;
461 case 'first-heading':
462 this._focusByCssSelector('h1, h2, h3, h4, h5, h6, [role="heading"]');
463 break;
464 default:
465 this._focusByCssSelector(this.autoFocus!);
466 break;
467 }
468 }
469
470 /**
471 * Restores focus to the element that was originally focused when the drawer opened.

Callers 1

constructorMethod · 0.95

Calls 3

_focusByCssSelectorMethod · 0.95
focusInitialElementMethod · 0.80
focusMethod · 0.65

Tested by

no test coverage detected