question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change in v10.11.1 causes inputs to lose focus on DOM changes

See original GitHub issue
  • Check if updating to the latest Preact version resolves the issue (no)

Describe the bug Up to preact v10.11.0 changes in the DOM did not cause a focused input to lose focus. Starting with v10.11.1 the input loses focus.

To Reproduce

Steps to reproduce the behavior:

  1. Check out https://github.com/jcestibariz/bug-preact-focus
  2. Notice preact is locked to v10.11.0 which does not contain the bug
  3. Run npm i and then npm start
  4. In the browser click on the input and press backspace
  5. Notice the last “tag” is removed but the input keeps focus.
  6. Go back to the console and press Ctrl+C to stop the server
  7. Run npm i preact@10.11.1 and then npm start
  8. Go back to the browser and repeat step 4
  9. Notice now the input loses focus
  10. You can also check with the latest version (v10.11.3 as of this moment) the bug is still present

Expected behavior The input should not lose focus when DOM changes.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, Dec 5, 2022

Reverting it would not be the right course of action as the PR solves an issue with unmounting fragments, dissecting the issue though points at a bigger one 😅 so first off, the reproduction isn’t using keys for a changing list of elements, which does hint that these remounts are intended. Going further, when we add the keys, in this reproduction, we can see that the focus is lost when we remove the first element but not the second time around which does point that we are dealing with a matching issue of or dom or vnodes.

PR for reference https://github.com/preactjs/preact/pull/3738 - we might need to tweak this to better account for the two cases.

1reaction
jcestibarizcommented, Nov 26, 2022

I ran a git bisect and looks like the commit that caused this bug is:

284a8b0d969f8ef3a3eeff0107b0104a52f774ad is the first bad commit
commit 284a8b0d969f8ef3a3eeff0107b0104a52f774ad
Author: Jovi De Croock <decroockjovi@gmail.com>
Date:   Thu Sep 29 10:42:24 2022 +0200

    remove _nextDom resetting as it collides with nested fragment switching (#3738)

 src/diff/children.js           | 11 ------
 test/browser/fragments.test.js | 78 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 73 insertions(+), 16 deletions(-)

I reverted this change on top of the current master (d4089df1263faab9b980a3493a4c7e986f254f8e) and I can confirm that reverting this change fixes the bug. Since I don’t have enough knowledge of preact’s internals to tell @JoviDeCroock 's intention, I won’t submit this revert as the solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Routing causes inputs to lose focus - Stack Overflow
This causes the input field to lose focus. Avoid using <Redirect /> and use history.replace after updating the state.
Read more >
Issues · preactjs/preact - GitHub
#2618 opened on Jul 11, 2020 by marvinhagemeister. Open 26 · The road to Preact 11 ... Change in v10.11.1 causes inputs to...
Read more >
onfocusout Event - W3Schools
The onfocusout event occurs when an element looses focus. The onfocusout event is often used on input fields. The onfocosout event is often...
Read more >
Adding Html element makes input-field lose focus - Learn - Elm
With the following code, when input text reaches 5 characters, the input field will lose focus. If I change else branch to [...
Read more >
React.js loses input focus on typing
This meant that when the user typed one character the recipients[i] value changed (via recipientEmailChangedHandler ).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found