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.

Elements loose focus when moving to or from a portal

See original GitHub issue

Do 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dantmancommented, Apr 26, 2020

@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

2reactions
dantmancommented, Apr 26, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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