Skip to content

Support more meaningful options for dialog autoFocus #22678

Closed

Description

@jelbourn

Currently, the autoFocus option of MatDialogConfig is a boolean, with the values meaning:

  • false - default value, focused the role="dialog" element
  • true - focuses the first tabbable element inside the dialog

This API is kind of confusing, since the dialog automatically focuses something regardless of what you do, so autoFocus: false doesn't really make sense.

Instead, we should change autoFocus to accept these values:

  • dialog - focus the role="dialog" element
  • first-tabbable - focus the first tabbable element
  • first-header - focus the first header (h1 - h6) element in the dialog. It's still an open question whether this should force tabIndex = -1.
  • A CSS selector- arbitrary CSS selector, the dialog would focus the first matching element (vis querySelector). Again an open question if this should force tabIndex = -1.

Activity

  1. added
    feature
    P3
    Accessibility
    on May 12, 2021
  2. zelliott commented on May 12, 2021

    @zelliott
    Contributor

    I might call it first-heading instead of first-header (https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements) (wouldn't want it to be confused with <header>).

    I'll throw a vote in for forcing the element to be focusable. I feel like forgetting to add tabindex will otherwise be a super common gotcha and result in bug reports that these values aren't working as expected.

    The logic could be something like:

    1. If tabindex is specified, do nothing.
    2. Else, set tabindex to -1, and add self-removing event listeners to remove the tabindex once the element is blurred or mousedown-ed. You don't want to keep the element focusable because it will mess with a user setting the sequential focus starting point via "click to move focus" (https://googlechrome.github.io/samples/focus-navigation-start-point/).

    The logic to "force-focus" an element and revert it back to normal afterward could be pulled out into a common helper function.

  3. added 7 commits that reference this issue on Jun 2, 2021
    ac27351
    6e0c06e
    2946196
    38a48e5
    c12cb53
    8fcac0d
    76beab4
  4. added a commit that references this issue on Jul 26, 2021
    059dbde
  5. added a commit that references this issue on Jul 27, 2021
    bce7b34
  6. added a commit that references this issue on Jul 28, 2021
    2e7b61b
  7. angular-automatic-lock-bot commented on Aug 30, 2021

    @angular-automatic-lock-bot

    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.

  8. locked and limited conversation to collaborators on Aug 30, 2021
  9. added a commit that references this issue on Jun 4, 2024
    769996e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

AccessibilityP3feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions