Skip to content

feat(web): Add navigation focus handler for assistive technology focus restoration #170046

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

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

flutter-zl
Copy link
Contributor

@flutter-zl flutter-zl commented Jun 5, 2025

Description
This pull request adds a navigation focus handler to the Flutter web engine that bridges assistive technology activations with Flutter's focus tracking system. The listener intercepts screen reader activations (VoiceOver, NVDA, JAWS, etc.) and forces DOM focus on the activated elements, ensuring they integrate properly with Flutter's navigation focus restoration.

Before
When using VoiceOver or other screen readers to navigate between pages in a Flutter web app, focus restoration would fail because assistive technology activations don't naturally trigger the DOM focus events that Flutter's navigation system expects. Users would lose their navigation context, with focus jumping to default elements instead of returning to the previously activated button.

Before behavior demo
https://focus-demo-0529-before.web.app
On mac os, Use command + F5 to activate voice over.
Use control + option + arrow right to focus on "Go to page two" button.
Use control + option + space to click the "Go to page two" button.
Then in page two, use control + option + arrow right to focus on "Back to page one" button.
The focus will be on "Page one" heading instead of "Go to page two" button. This is not expected

After
Screen reader users can now navigate between pages and have their focus properly restored to the previously activated element (e.g., "Go to Page Two" button) when returning to a previous page, providing a consistent and accessible navigation experience across all assistive technologies.

After behavior demo
https://focus-demo-0529-after.web.app
On mac os, Use command + F5 to activate voice over.
Use control + option + arrow right to focus on "Go to page two" button.
Use control + option + space to click the "Go to page two" button.
Then in page two, use control + option + arrow right to focus on "Back to page one" button.
The focus will be on "Go to page two" button. This is expected.

Issue Fixed
This PR addresses GitHub Issue #140483, which reports that VoiceOver focus restoration doesn't work in Flutter web applications during navigation.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added engine flutter/engine repository. See also e: labels. platform-web Web applications specifically labels Jun 5, 2025
@flutter-zl
Copy link
Contributor Author

Difference between TAB navigation and VoiceOver navigation:

Using Tab + Space (keyboard-only user):
If you use the keyboard to navigate:

Press Tab to focus on the "Go to page two" button in https://focus-demo-0529-before.web.app/

Press Space to activate it

On Page Two, press Tab to focus on the "Back to page one" button

Press Space to activate it

Back on Page One, the focus is restored to the "Go to page two" button (correct behavior for keyboard users)

Because DOM focus was used, and frameworks like Flutter handle focus restoration for keyboard users

Using VoiceOver on macOS:
Activate VoiceOver: Command + F5

Use Control + Option + Right Arrow to navigate to the "Go to page two" button

Use Control + Option + Space to activate it

On Page Two, use Control + Option + Right Arrow to focus on the "Back to page one" button

Activate it

Back on Page One, VoiceOver virtual cursor lands on the "Page one" heading, not the "Go to page two" button

Because VoiceOver’s virtual cursor is separate from DOM focus, and it doesn’t trigger or track DOM focus events by default

@flutter-zl flutter-zl requested review from mdebbar and hannah-hyj June 5, 2025 15:21
@flutter-zl flutter-zl marked this pull request as ready for review June 5, 2025 15:22
@flutter-zl flutter-zl requested a review from yjbanov June 5, 2025 15:23
@flutter-zl
Copy link
Contributor Author

flutter-zl commented Jun 5, 2025

Difference between TAB navigation and VoiceOver navigation:

Using Tab + Space (keyboard-only user): If you use the keyboard to navigate:

Press Tab to focus on the "Go to page two" button in https://focus-demo-0529-before.web.app/

Press Space to activate it

On Page Two, press Tab to focus on the "Back to page one" button

Press Space to activate it

Back on Page One, the focus is restored to the "Go to page two" button (correct behavior for keyboard users)

Because DOM focus was used, and frameworks like Flutter handle focus restoration for keyboard users

Using VoiceOver on macOS: Activate VoiceOver: Command + F5

Use Control + Option + Right Arrow to navigate to the "Go to page two" button

Use Control + Option + Space to activate it

On Page Two, use Control + Option + Right Arrow to focus on the "Back to page one" button

Activate it

Back on Page One, VoiceOver virtual cursor lands on the "Page one" heading, not the "Go to page two" button

Because VoiceOver’s virtual cursor is separate from DOM focus, and it doesn’t trigger or track DOM focus events by default

@yjbanov , @mdebbar, @hannah-hyj , From above observation, we can conclude that we need to forces DOM focus on the activated elements when using screen reader activations. Hence this PR can fix the issue #140483.

@flutter-zl flutter-zl marked this pull request as draft June 6, 2025 16:16
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-zl flutter-zl changed the title feat(web): Add global click listener for assistive technology focus restoration feat(web): Add navigation focus handler for assistive technology focus restoration Jun 9, 2025
@flutter-zl flutter-zl marked this pull request as ready for review June 9, 2025 16:17
@mdebbar
Copy link
Contributor

mdebbar commented Jun 11, 2025

I don't think I'm the best person to review this, but maybe @chunhtai or @yjbanov ?

@mdebbar mdebbar requested a review from chunhtai June 11, 2025 14:17
final NavigationTarget? target = _findNavigationTarget(event);
if (target != null && !_isAlreadyFocused(target.element)) {
final DomElement? focusableElement = _findFocusableElement(target.element);
focusableElement?.focusWithoutScroll();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain a bit how this preserve focus when navigate back to the screen?

If I read the code correctly, this seems to attempt to focus the navigation target when it is clicked e.g. focuses the button when hitting the button that may or may not cause navigation?

Copy link
Contributor

@chunhtai chunhtai Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, framework actually send focus event to previously focused node when a page is popped.

void sendSemanticsEvent(SemanticsEvent semanticsEvent) {

This will send a message through flutter/accessibility channel with a FocusSemanticEvent, can the engine listen to this a switch focus instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right: framework actually send focus event to previously focused node when a page is popped. Please check https://github.com/flutter/flutter/pull/170046#issuecomment-2944993145. The assitive technology like macos voice over does not trigger or track DOM focus events. So the solution is to forces DOM focus on the activated elements when using screen reader activations. Hence this PR can fix the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain a bit how this preserve focus when navigate back to the screen?

If I read the code correctly, this seems to attempt to focus the navigation target when it is clicked e.g. focuses the button when hitting the button that may or may not cause navigation?

How does this preserve focus when navigating back?
This implementation works as a bridge between assistive technology and Flutter's focus tracking system. Here's the flow:
User navigates with AT: VoiceOver user uses Control+Option+Space to activate "Go to page two" button
Your handler detects AT activation: The click event has characteristics of AT usage (coordinates near origin, etc.)
Force DOM focus: This code calls focusableElement?.focusWithoutScroll() to ensure the button gets proper DOM focus
Flutter tracks the focus: Now Flutter's navigation system knows which element was focused when navigation occurred
Framework sends focus restoration: When popping back, framework sends FocusSemanticEvent to restore focus to the previously focused element
Focus is properly restored: The "Go to page two" button receives focus instead of defaulting to the page heading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, framework actually send focus event to previously focused node when a page is popped.

You are right: framework actually send focus event to previously focused node when a page is popped. Please check https://github.com/flutter/flutter/pull/170046#issuecomment-2944993145. The assistive technology like macos voice over does not trigger or track DOM focus events. So the solution is to forces DOM focus on the activated elements when using screen reader activations. Hence this PR can fix the issue.

void sendSemanticsEvent(SemanticsEvent semanticsEvent) {

This will send a message through flutter/accessibility channel with a FocusSemanticEvent, can the engine listen to this a switch focus instead?

Do you mean add switch case for platform web in https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/navigator.dart#L3256C1-L3280C6 ? I tried it, but the issue is that The assistive technology like macos voice over does not trigger or track DOM focus events and lastFocusNode will be null. We can not detect lastFocusNode when in Voice over.

Copy link
Contributor

@chunhtai chunhtai Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you are right, i forgot that web doesn't track last focus. i.e. onDidGainAccessibilityFocus.

The assistive technology like macos voice over does not trigger or track DOM focus events

Is this true even if the dom has a focus listener? I remembered @yjbanov and I had an idea to add onfocus to all dom objects so that we can still track a11y focus in web

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force DOM focus: This code calls focusableElement?.focusWithoutScroll() to ensure the button gets proper DOM focus
Flutter tracks the focus: Now Flutter's navigation system knows which element was focused when navigation occurred

Does this sends didGainFocus to framework through flutter/accessibility channel? I think the framework relies on this to record last focus.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah you are right, i forgot that web doesn't track last focus. i.e. onDidGainAccessibilityFocus.

The assistive technology like macos voice over does not trigger or track DOM focus events

Is this true even if the dom has a focus listener? I remembered @yjbanov and I had an idea to add onfocus to all dom objects so that we can still track a11y focus in web

Please check https://github.com/flutter/flutter/blob/master/engine/src/flutter/lib/ui/semantics.dart#L282-#L288.
VoiceOver navigation doesn't automatically trigger DOM focus events.
This PR solution detects AT activations and forces DOM focus, which then triggers the focus listeners.

Copy link
Contributor Author

@flutter-zl flutter-zl Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force DOM focus: This code calls focusableElement?.focusWithoutScroll() to ensure the button gets proper DOM focus
Flutter tracks the focus: Now Flutter's navigation system knows which element was focused when navigation occurred

Does this sends didGainFocus to framework through flutter/accessibility channel? I think the framework relies on this to record last focus.

Please check https://github.com/flutter/flutter/blob/master/engine/src/flutter/lib/ui/semantics.dart#L282-#L288.
focusWithoutScroll() call does NOT directly send didGainFocus
But it ensures the element has DOM focus when the framework later needs to track it.
Unlike Android/iOS which send didGainFocus messages through the accessibility channel.
The web engine doesn't implement this pattern! This PR solution fills this gap by ensuring DOM focus happens, which then enables the existing focus tracking to work.
Please check this observation: #170046 (comment)

Copy link
Contributor

@chunhtai chunhtai Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it ensures the element has DOM focus when the framework later needs to track it.

Framework has two focus system, on for keyboard focus and one for accessibility focus.

for a11y focus, it completely relies on onDidGainAccessibilityFocus to track last focus when navigate between routes

for keyboard focus, it go through a focus mananger and I think it has a completely different way to track and restore focus based on focus structure.

I think in this pr, the onFocus will notify the framework keyboard focus and then when popping route, the framework attempts to restore keyboard focus which trigger focus event in engine. since engine doesn't differentiate keyboard focus and a11y focus, so it move both focus based on the framework focus.

I think this approach is fine. I will take a full review today

@chunhtai chunhtai self-requested a review June 11, 2025 18:55
@@ -1290,6 +1298,141 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {

@override
double scaleFontSize(double unscaledFontSize) => unscaledFontSize * textScaleFactor;

static const String _flutterSemanticNodePrefix = 'flt-semantic-node-';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should define this somewhere as a const to share across web engine


final NavigationTarget? target = _findNavigationTarget(event);
if (target != null && !_isAlreadyFocused(target.element)) {
final DomElement? focusableElement = _findFocusableElement(target.element);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also find the one that has onFocus semantics action?

return true;
}

// Pattern 2: Integer coordinate navigation from sophisticated ATs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are some example of of sophisticated ATs? maybe add some more comment here to clarify

}
});

domDocument.addEventListener('click', navigationFocusListener, true.toJS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this makes the entire app a container that VO will focus? or will it cause additional tab to reach the item inside?

return false;
}

// Exclude test coordinates to prevent false positives in Flutter tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this, should we fix our test to not hit integer target or origin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine flutter/engine repository. See also e: labels. platform-web Web applications specifically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants