Skip to content

bug(menu): When opening menu with keyboard, focus is set to the first item, but focus origin is set to "program". #21241

Description

@zelliott

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:

  1. Go to https://stackblitz.com/angular/xkpoabmbqpb?file=src/app/material-module.ts
  2. Open the menu with the "Enter" key.
  3. 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

Metadata

Metadata

Assignees

Labels

AccessibilityThis issue is related to accessibility (a11y)GThis is is related to a Google internal issueP3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: material/menu

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions