-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Focus lost when moving child with key
#4235
Comments
i don't think there's much preact can do - the DOM does not provide a method to reorder nodes without removing them from the parent, and if you do remove an element from the parent it inherently looses focus. the only thing that may be possible is to avoid removing an element that is focused from the parent completely and only ever remove other nodes when reordering, but that sounds... complicated. |
Thanks for the info! For anyone interested, I made a glitch playground to experiment with focus loss and It looks like react solved this by recording the focus and selection range before a commit, and resetting it afterwards. They also intercept the blur event emitted by the browser. |
We used to have this in the beta phase of Preact X but the performance hit is extremely large when this has to be done on every EDIT: see PR for a valid comment about i.e. this not preventing pitfalls with |
Describe the bug
Moving children with a
key
attribute can cause a focused element in the child DOM to lose focus.To Reproduce
The following app demonstrates the issue on https://preactjs.com/repl
Click on one of the
<input>
fields and start typing. Notice that when the element gets moved, you might lose focus. (I noticed that it only happens when the node is moved down the DOM, not up. You can tell that the DOM node was not re-rendered: theinput
field keeps its value.Schermopname.2023-12-18.om.18.23.23.mov
Expected behavior
The element should maintain focus. In
react-dom
, this works correctly. Try the following app in https://playcode.io/reactSchermopname.2023-12-18.om.18.15.31.mov
The text was updated successfully, but these errors were encountered: