Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help(matMenu): Auto focusing text box after clicking on a menu item #18750

Closed
chenxinyanc opened this issue Mar 7, 2020 · 6 comments
Closed
Labels
troubleshooting This issue is not reporting an issue, but just asking for help

Comments

@chenxinyanc
Copy link

chenxinyanc commented Mar 7, 2020

What are you trying to do?

  • I tried to move focus to a text box after user clicks the menu. This text box submits user input when it loses focus.
  • I want to know what is the correct / recommended approach to move focus to other control, we I'm inside a menu item click callback.
  • Is it possible for matMenu to stop moving focus to the triggering button when closing the menu, if user's focus is not inside matMenu at the time just before the menu closes? (something like Option for menu not to return focus to the assoicated button #11696, but with a pre-condition)

What troubleshooting steps have you tried?

I need to use setTimeout(..., 0) to defer moving focus to text box, so the focus will be moved to the menu-triggering button, then moved to the text box at last. However, this looks flaky to me and I'm not sure if it's the best practice.

Reproduction

https://stackblitz.com/edit/angular-component-mat-menu-focus?file=src%2Fapp%2Ftext-editor%2Ftext-editor.component.ts

Steps to reproduce:

  1. Open the sandbox link above, note the following code in text-editor.component.ts.
  ngAfterViewInit() {
    // If we remove setTimeout, textInput.blur event will be triggered immediately.
    window.setTimeout(() => {
      // Move focus to the text box automatically.
      this.textInput.nativeElement.focus();
    }, 0);
  }
  1. In the app, you can edit the text with "Edit" menu item. That's expected behavior.
    temp

  2. Remove setTimeout, calling focus directly instead

  ngAfterViewInit() {
    // Move focus to the text box automatically.
    this.textInput.nativeElement.focus();
  }
  1. In the app, the text box seems to get focus first, then lost focus almost immediately.
    temp

Environment

  • Angular: 9.0.2
  • CDK/Material: 9.1.0
  • Browser(s): Chrome 80.0.3987.132
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows
@chenxinyanc chenxinyanc added needs triage This issue needs to be triaged by the team troubleshooting This issue is not reporting an issue, but just asking for help labels Mar 7, 2020
@Totati
Copy link
Contributor

Totati commented Mar 8, 2020

Hi, there's any easy soulution for your problem. This is not a bug, it works as intended. MatMenu restores the focus by default, this is why you lost focus after focusing your input synchronously. There's an option to disable this beahaviour => matMenuTriggerRestoreFocus. With that your code works without setTimeout. StackBlitz

@chenxinyanc
Copy link
Author

I see. That solves my problem. Thanks @Totati ! Then there is actually a solution to #11696. Just nobody can post it under that issue.

Then I have one last question: is it a valid feature request to add another option to "move focus back to trigger button only when the focus is not inside the menu before menu closes"? I think that would be of great help for my scenario, without any extra code to take care of accessibility.

@Totati
Copy link
Contributor

Totati commented Mar 8, 2020

@chenxinyanc #18756

@chenxinyanc
Copy link
Author

chenxinyanc commented Mar 9, 2020

If I understand this PR correctly, while it enables consumer to move focus to elsewhere in menuClosed event, it won't help for the case of moving focus inside click event of menu items (buttons)...

However, that makes it possible for consumer to manually restore focus after the menu closes, without accessibility implications, so it helps anyway 😃

@jelbourn
Copy link
Member

Closing this since I think the combination of matMenuTriggerRestoreFocus and #18756 deals with this

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 11, 2020
@mmalerba mmalerba removed the needs triage This issue needs to be triaged by the team label Jun 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
troubleshooting This issue is not reporting an issue, but just asking for help
Projects
None yet
Development

No branches or pull requests

4 participants