Motivation
When opening a menu with keyboard as demonstrated in the repro steps below, focus moves to the first menu item (which is correct), but the focus origin is set to "program". This is a problem because some Angular Material consumers (in particular products internal to Google) have styles that depend upon the appropriate focus origin class. In my case, I have strong focus indicator styles that are only supposed to appear when the class .cdk-keyboard-focused is present. When opening a menu with a keyboard, I expect my strong focus indicator to render on the first menu item, but it does not.
Note that this is a problem in both the old Material and new MDC menus.
Problem & Solution
The problematic line is here. Menu only tracks whether it was opened by a "mouse" or "touch" focus origin, and if it is neither of those, it defaults to "program". The easiest solution seems to be to update menu to also track "keyboard" focus origin. The _handleKeydown event handler will need to update the _openedBy focus origin to "keyboard". I'm open to other ideas as well.
Reproduction
Steps to reproduce:
- Go to https://stackblitz.com/angular/xkpoabmbqpb?file=src/app/material-module.ts
- Open the menu with the "Enter" key.
- Observe that the class
.cdk-program-focused is present on the first menu item.
Expected Behavior
I expect the class .cdk-keyboard-focused to be present on the first menu item.
Actual Behavior
Observe that the class .cdk-program-focused is present on the first menu item.
Environment
- Angular: Latest
- CDK/Material: Latest
- Browser(s): All
- Operating System (e.g. Windows, macOS, Ubuntu): All
Motivation
When opening a menu with keyboard as demonstrated in the repro steps below, focus moves to the first menu item (which is correct), but the focus origin is set to "program". This is a problem because some Angular Material consumers (in particular products internal to Google) have styles that depend upon the appropriate focus origin class. In my case, I have strong focus indicator styles that are only supposed to appear when the class
.cdk-keyboard-focusedis present. When opening a menu with a keyboard, I expect my strong focus indicator to render on the first menu item, but it does not.Note that this is a problem in both the old Material and new MDC menus.
Problem & Solution
The problematic line is here. Menu only tracks whether it was opened by a "mouse" or "touch" focus origin, and if it is neither of those, it defaults to "program". The easiest solution seems to be to update menu to also track "keyboard" focus origin. The
_handleKeydownevent handler will need to update the_openedByfocus origin to "keyboard". I'm open to other ideas as well.Reproduction
Steps to reproduce:
.cdk-program-focusedis present on the first menu item.Expected Behavior
I expect the class
.cdk-keyboard-focusedto be present on the first menu item.Actual Behavior
Observe that the class
.cdk-program-focusedis present on the first menu item.Environment