Skip to content

A11y: Elements lose visible focus when using a screenreader and hitting Enter to activate #11314

Closed

Description

@ericenns

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

When navigating a docusaurus site via keyboard while using a screen reader (e.g. NVDA), tabbing to a button and hitting enter causes visible focus to be lost. This is due to https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-common/src/hooks/useKeyboardNavigation.ts, which removes navigation-with-keyboard class when clicking, this works for keyboard navigation while not using a screen reader but when using a screen reader hitting enter instead activates mousedown events.

Adding the following css appears to fix the issue:

body:not(.navigation-with-keyboard) *:not(input):focus-visible {
  outline: auto !important;
}

Which as you can see uses the :focus-visible selector which keeps the outline even if the element sees a mousedown event it will maintain focus if the element was already focused by tabbing to it. If a user clicks on a button not focused it will not have a visible focus ring.

Reproducible demo

The live docusaurus site exhibits this behaviour so it can be used.

Steps to reproduce

  1. On windows activate NVDA
  2. Open your browser to https://docusaurus.io/docs
  3. Tab to the collapse button beside Getting Started in the sidebar
  4. Hit the enter key

Expected behavior

Visible focus should be retained

Actual behavior

Visible focus is lost

Your environment

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Jul 8, 2025
  2. added
    domain: a11y
    and removed
    status: needs triage
    on Jul 10, 2025
  3. Huzaif-nabi commented on Oct 17, 2025

    @Huzaif-nabi

    I can reproduce this and would like to submit a pull request with the suggested fix.

  4. slorber commented on Oct 23, 2025

    @slorber
    Collaborator

    Can this be reproduced only on Windows with NVDA?

    On macOS with VoiceOver I still see the focus ring on the sidebar item collapse buttons.

    Is it related to using the screen reader, or can you reproduce in Windows by simply tab/enter without the screen reader being enabled?

    Can you tell us which browser you are using and which version?

  5. ericenns commented on Jan 8, 2026

    @ericenns
    Author

    Can this be reproduced only on Windows with NVDA?

    On macOS with VoiceOver I still see the focus ring on the sidebar item collapse buttons.

    Is it related to using the screen reader, or can you reproduce in Windows by simply tab/enter without the screen reader being enabled?

    Can you tell us which browser you are using and which version?

    This can also be reproduced on Windows with JAWS

    The issue only occurs when using the screen reader, as both NVDA and JAWS through the accesibility api trigger a click when hitting Enter key.

    This occurs on all versions of Chrome and Edge, I haven't tested with Firefox.

  6. nmggithub commented on Feb 1, 2026

    @nmggithub
    Contributor

    TL;DR:

    I believe the useKeyboardNavigation hook is poorly designed for the modern web. At best, it provides an aesthetic benefit by removing Chrome's own focus ring when users click non-input focused elements that don't navigate away. At worst, it negatively affect downstream styles (see: electron/website#1001) as well as screen readers that cause mousedown events to be emitted.

    I see two options:

    1. Remove the hook entirely and let downstream users decide how they want to style focus rings.
    2. Use something like :focus-visible (perhaps instead of listening to mouse events).

    Existing behavior

    With VoiceOver

    Below is a video of me using VoiceOver around the button discussed in the initial issue comment. As can be seen, both Chrome and VoiceOver itself add focus indicators. The first toggle is achieved by pressing Enter. The second toggle is achieved by clicking. The click removes Chrome's focus ring (due to hook's CSS).

    Screen.Recording.2026-02-01.at.16.45.27.mov

    Without VoiceOver (and removing the hook's styles)

    If I remove the hook's styles, navigate to the same button with the keyboard, and click it, there does appear to be two focus indicators (blue and white) for different states:

    Screen.Recording.2026-02-01.at.17.08.22.mov

    Removing the hook's styles and testing out tabs (one of the original motivating components for these style changes), the look is less ugly than before. To note, in the below video, I clicked right outside the tabs, pressed the Tab key to select the first tab, and then click that first tab.

    Screen.Recording.2026-02-01.at.17.37.33.mov

    Looking to the future

    I think the useKeyboardNavigation implementation needs to be rethought. CSS pseudo-selectors like :focus-visible provide a much better API surface with which to handle focus rings. While listening to mousedown events may seem like a good indication that the user is now using a mouse, it's not foolproof. Some screen readers / assistive technologies simulate mouse actions, thus causing events like mousedown to be fired even when the user actually isn't using a keyboard. That previously-linked article explicitly calls out how the Enter key resulted in a mousedown event firing.

    The hook appears to have been made before :focus-visible was widely supported (although there does appear to have been discussion about :focus-visible in the issue that prompted the creation of what would eventually become the hook). Regardless, we're kind of fighting the browser (and sometimes downstream devs) with these style changes. A more thoughtful approach is warranted.

  7. slorber commented on Feb 5, 2026

    @slorber
    Collaborator

    I agree with you @nmggithub, this hook is not ideal and was written at a time where :focus-visible was early and afaik not widely supported.

    In #3626 I even proposed to use :focus-visible but it was too early

    Can you confirm that moving to :focus-within would fix the current windows-only problem? Do you want to submit a PR?


    Note: here's the original blog post because the link in code is broken: https://david-gilbertson.medium.com/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2

  8. nmggithub commented on Feb 5, 2026

    @nmggithub
    Contributor

    @slorber Thanks for following up (and confirming that change might be needed)! I'll do some testing myself and see if I can come up with a better approach by using :focus-visible (or maybe :focus-within, as you mentioned, but that may have been a typo).

  9. slorber commented on Feb 6, 2026

    @slorber
    Collaborator

    @ericenns can you please test on our website docusaurus.io if you still encounter the windows screen reader problem above?

  10. ericenns commented on Feb 6, 2026

    @ericenns
    Author

    @slorber I tested docusuarus.io and I can confirm that visible focus is no longer lost while using either NVDA or JAWS screen readers on Windows with Chrome.

  11. slorber commented on Feb 6, 2026

    @slorber
    Collaborator

    awesome, thanks for confirming 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdomain: a11y

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions