question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option for menu not to return focus to the assoicated button

See original GitHub issue

Feature request:

An @Input() option for not returning focus to the associated button when the menu closes.

What is the expected behavior?

When the menu closes, not to return the focus to the button. This could be an optional behavior.

What is the current behavior?

When the menu closes after clicking a menu item, the focus returns to the associated button. However, if the command of the menu item is to focus on an input element, current behavior just breaks the intended feature which for example enables editing in the input box right away.

What are the steps to reproduce?

With the tutorial link at https://stackblitz.com/angular/pbndqaomepr?file=app%2Fmenu-overview-example.html

Copy and paste the following:

<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
  <button mat-menu-item (click)="focusInput()">Focus Input</button>
  <button mat-menu-item>Item 2</button>
</mat-menu>
<p>a</p>b<p></p>
<button mat-raised-button (click)="focusInput()">Focus Input</button>
<p>c</p><p></p>d<p></p>

    <mat-form-field fxFlexFill>
        <textarea matInput placeholde="MyInput" #textDetail name="note" ></textarea>
    </mat-form-field>
import {Component, ElementRef,ViewChild} from '@angular/core';

/**
 * @title Basic menu
 */
@Component({
  selector: 'menu-overview-example',
  templateUrl: 'menu-overview-example.html',
  styleUrls: ['menu-overview-example.css'],
})
export class MenuOverviewExample {
  	@ViewChild('textDetail') noteInput: ElementRef;


focusInput(){
  (this.noteInput.nativeElement as HTMLTextAreaElement).focus();
}
}

In contrast, when running the same command focusInput() in the button, the input box gets focused as expected.

What is the use-case or motivation for changing an existing behavior?

Menu command to focus on any element that can be focused. For example, adding or editing some text in an input box.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 6.0.4, Angular Material 6.2.1, TypeScript 2.7.2

Chrome and other browsers

Is there anything else we should know?

At the moment, the workaround is to use

focusInput(){
  setTimeout(()=> (this.noteInput.nativeElement as HTMLTextAreaElement).focus(), 
       280);
}

280ms is for Chrome, but for Edge, the delay has to be around 400 ms. Anyway, people with eagle eyes could see the blinking of the focus changing.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
VVKotcommented, Feb 8, 2019

Hi @crisbeto, can I reopen this? It would be great to have such an option. Maybe we can mark the menu element to not return focus it is not feasible for the menu as a whole?

We have some use cases when we want to set focus on our own, for example, menu option to rename element(double-click is reserved for other interaction).

3reactions
vijaynet10commented, Sep 14, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Actions Menu Button Example Using element.focus() - W3C
Menu ; Closes the menu. Sets focus to the menu button. · Moves focus to the previous menu item. If focus is on...
Read more >
on clicking "/" render a menu and focus on the menu options
1 Answer 1 · I changed the element to a button and set tabIndex to "0" but it's still not focusing on the...
Read more >
Turn a Focus on or off on Mac - Apple Support
In the menu bar on your Mac, click Control Center , then click anywhere in the Focus section. If you set the option...
Read more >
HTMLElement.focus() - Web APIs | MDN
Select the first button to set focus and scroll to the off-screen button. Selecting the second button set's the focus, but scrolling is...
Read more >
Building Accessible Menu Systems - Smashing Magazine
Dropdowns which constitute a set of options are often called "menus", ... Menu buttons which take the aria-haspopup="true" property do not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found