-
Notifications
You must be signed in to change notification settings - Fork 28.7k
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
base: master
Are you sure you want to change the base?
Conversation
Difference between TAB navigation and VoiceOver navigation: Using Tab + Space (keyboard-only user): 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: 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. |
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 Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
final NavigationTarget? target = _findNavigationTarget(event); | ||
if (target != null && !_isAlreadyFocused(target.element)) { | ||
final DomElement? focusableElement = _findFocusableElement(target.element); | ||
focusableElement?.focusWithoutScroll(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 aFocusSemanticEvent
, 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 occurredDoes 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)
There was a problem hiding this comment.
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
@@ -1290,6 +1298,141 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { | |||
|
|||
@override | |||
double scaleFontSize(double unscaledFontSize) => unscaledFontSize * textScaleFactor; | |||
|
|||
static const String _flutterSemanticNodePrefix = 'flt-semantic-node-'; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
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
///
).