Elements loose focus when moving to or from a portal
See original GitHub issueDo you want to request a feature or report a bug?
Perhaps a bug, but could also be seen as a feature
What is the current behavior?
When you move an element to a portal through ReactDOM.createPortal
the element looses focus if it had focus. If the element gains focus while in a portal, when moving out of the portal it looses focus.
What is the expected behavior?
I would have expected React to maintain focus of the element when moving in or out of a portal
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Tested with
React version: 16.2 Browsers: Latest chrome and firefox. I did not test others but expect the behaviour to be the same OS: Tested on Mac High Sierra
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top Results From Across the Web
javascript - Input element losing it's focus after key press when ...
I was inlining the close function, so the useEffect hook got triggered every time close changed when the component was rendered again due...
Read more >Implement a JavaScript when an element loses focus
Given a document, the task is to implement functionality when the element loses focus. We have 2 options, one is the onblur event...
Read more >How to use .focus() on a Field - Support and Troubleshooting
5. Enter "element.focus();" into the console and you will notice that the form does not move. This behavior can lead many developers to...
Read more >Portals - React
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. ......
Read more >Control focus with tabindex - web.dev
To focus an element, press the Tab key or call the element's focus() method. HTML; CSS. Result; Skip Results Iframe.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@naisergic In the first example the function is not a component, you use it and pass it’s return value to the portal. It works exactly the same as if the function didn’t exist.
i.e. The first example works as if you wrote this instead: https://codesandbox.io/s/portal-maintaining-focus-mk2gt
Take a look at this modification to your second example. https://codesandbox.io/s/portal-loosing-focus-b9b0k
Your ModalContainer component function is defined inside of your Portal component. So every time Portal renders a new ModalContainer function is created. Because the component is different this results in the previous component and your input being completely unmounnted and a new tree with the new ModelContainer component is rendered.