Skip to content

[web] Keep focus when a semantics node is inserted above a text field - #196240

Open
Ortes wants to merge 5 commits into
flutter:masterfrom
Ortes:web-semantics-keep-focus-on-reparent
Open

Ortes wants to merge 5 commits into
flutter:masterfrom
Ortes:web-semantics-keep-focus-on-reparent

Conversation

@Ortes

@Ortes Ortes commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

With semantics enabled, a focused text field loses focus when the framework inserts a semantics node above it (for example when a tooltip or an OverlayPortal child appears next to a route on a nested navigator).

SemanticsObject.updateChildren moves the existing child's element into the new node with append/insertBefore. Chrome fires blur on a focused element removed from the document, so the text input connection is closed and the framework unfocuses the field. Typing stops, and a double-click in a search field with autocomplete options never shows the selected word.

This PR moves children that are already in the document with moveBefore when the browser supports it, which keeps the focus. Browsers without moveBefore (Safari) keep using insertBefore and get the focus back with focusWithoutScroll() after the move: Safari fires no blur but still moves document.activeElement to <body>, so keystrokes were lost there too.

Before/after, with the sample from the issue (type, wait for the node to be inserted after 5 s, type again):

  • before: field value abc, document.activeElement is <body>
  • after: field value abcdef, document.activeElement is the <input>

Tests: added does not blur a focused text field when a node is inserted above or removed from its ancestors and keeps the focus of a text field moved without moveBefore (deletes Element.prototype.moveBefore for the test) to semantics_test.dart. Both fail without the change and pass with it, in Chrome and Safari. test/engine/semantics/ and text_editing_test.dart pass locally in Chrome (dart2js, CanvasKit).

Fixes #195289

Pre-launch Checklist

When the framework inserts a semantics node above an existing one, the
engine moves the existing node's DOM element into the new node with
append/insertBefore. Chrome fires `blur` on a focused element that is
removed from the document, so a focused text field below it is blurred,
the text input connection is closed and the framework unfocuses the
field.

Move already connected children with `moveBefore` when the browser
supports it. It moves the element without blurring it.

Fixes flutter#195289
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) platform-web Web applications specifically team-web Owned by Web platform team labels Nov 9, 2026
@Ortes
Ortes marked this pull request as ready for review November 9, 2026 20:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the insertOrMoveBefore helper method to DomNode to allow moving DOM nodes without losing focus on their descendants, and integrates it into SemanticsObject child updates. It also adds a regression test to verify that inserting or removing ancestor nodes does not blur a focused text field. The review feedback suggests simplifying boolean checks in the new helper method to align with Effective Dart guidelines, and conditionally asserting the blur count in the test to handle environments that do not support moveBefore.

/// the browser supports it. Unlike a remove-and-insert, `moveBefore` keeps the
/// focus inside [node], so a focused descendant is not blurred.
void insertOrMoveBefore(DomNode node, DomNode? referenceNode) {
if (node.isConnected == true && isConnected == true && (this as JSObject).has('moveBefore')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

According to the Effective Dart style guide, boolean expressions should not be compared to true or false directly. Since isConnected is a non-nullable boolean, we can simplify this condition to improve readability and adhere to the style guide.

    if (node.isConnected && isConnected && (this as JSObject).has('moveBefore')) {
References
  1. Effective Dart: Style guide recommends not comparing boolean expressions to true or false. (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

isConnected is declared as external bool? get isConnected; on DomNode, so the == true is needed: node.isConnected && isConnected would not compile.

Comment on lines +3378 to +3390

// Insert node 3 between node 0 and node 1, as the framework does when a
// new sibling (e.g. an overlay or a tooltip) appears next to a route.
pumpSemantics(withNode3: true);

expect(blurCount, 0);
expect(domDocument.activeElement, input);

// Remove node 3, moving node 1 back under node 0.
pumpSemantics(withNode3: false);

expect(blurCount, 0);
expect(domDocument.activeElement, input);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure the test is robust and does not fail when run in environments or browsers that do not yet support moveBefore (where Chrome would naturally trigger a blur event), consider conditionally asserting blurCount == 0 only when moveBefore is supported.

    final bool supportsMoveBefore = (domDocument.body! as JSObject).has('moveBefore');

    // Insert node 3 between node 0 and node 1, as the framework does when a
    // new sibling (e.g. an overlay or a tooltip) appears next to a route.
    pumpSemantics(withNode3: true);

    if (supportsMoveBefore) {
      expect(blurCount, 0);
    }
    expect(domDocument.activeElement, input);

    // Remove node 3, moving node 1 back under node 0.
    pumpSemantics(withNode3: false);

    if (supportsMoveBefore) {
      expect(blurCount, 0);
    }
    expect(domDocument.activeElement, input);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would rather keep this assertion unconditional. The web tests pin Chrome 145 (dev/package_lock.yaml), which has moveBefore (since 133), and Chrome is the browser where insertBefore blurs the moved input, so this assertion is the regression check for #195289. Gating it on moveBefore support would make the test pass without checking anything on a browser that lacks it. If the Firefox or Safari runs disagree, I will handle them explicitly.

@flutter-zl
flutter-zl self-requested a review November 12, 2026 20:06
@flutter-zl flutter-zl added the CICD Run CI/CD label Nov 12, 2026
flutter-zl
flutter-zl previously approved these changes Nov 12, 2026

@flutter-zl flutter-zl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!

I verified it with a before/after demo of the issue's scenario, built against the PR's engine changes:
- Before: https://flutter-demo-97-before.web.app — the field blurs and typing stops when the node is inserted.
- After: https://flutter-demo-97-after.web.app — the field keeps focus, 0 blur events, typing continues

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 17, 2026
Safari has no moveBefore and drops the focus of a moved descendant without
firing blur, so keystrokes are lost. Restore the focus after insertBefore.
@Ortes

Ortes commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Update after Flutter 3.50.0, in a real app with semantics on.

The case that led me here, RawAutocomplete suggestions next to a route on a ShellRoute navigator, no longer reproduces on 3.50.0: no ancestor of the focused input is re-inserted any more, so nothing blurs. I did not bisect which change did it.

The sample in #195289 still reproduces on 3.50.0, so this PR is still needed. With Element.prototype.moveBefore deleted, to simulate a browser without it:

  • Chrome: the input blurs and document.activeElement is <body>. With this PR it keeps focus.
  • WebKit: no blur fires, but document.activeElement still moves to <body> and later keystrokes are lost.

So the description was wrong about Safari. 8f533f9 adds a fallback for browsers without moveBefore: it remembers the focused element inside the moved node and calls focusWithoutScroll() after insertBefore, like #192963 does after a role update. A new test, keeps the focus of a text field moved without moveBefore, deletes moveBefore and checks the focus.

Local runs of semantics_test.dart, chrome-dart2js-canvaskit-engine and safari-dart2js-canvaskit-engine: both focus tests fail without the change and pass with it. On Safari, platform view is reachable via a hit test fails with and without this PR. I updated the description.

@flutter-zl flutter-zl added the CICD Run CI/CD label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Web] With semantics enabled, a focused TextField loses focus when a semantics node is inserted above it

2 participants