onBlur event handler firing when focusing on child components
Hi everyone!
I'm having trouble with my
onFocus and onBlur events.
I have this div, which is a container for search bar:
On focus, it sets searchPopUpOpened to true and does the opposite on blur.
<SearchBarPopUp /> is a child of this search bar container with onFocus and onBlur event handlers, but for some reason, if you click inside the <SearchBarPopUp />, focus is lostonBlur event handler fires, setting searchPopUpOpened to false.
How to fix this behaviour? I need my main div to have focus while the user is clicking on its children, but for some reason, it doesn't work with my search bar pop-up.6 Replies
I usually used a click handler on the document to close dorpdowns, in there you can check if the click was on or inside your search dropdown.
CSS has
:focus-within maybe there is an event for that, too?You mean, add
onClick on the document itself to make sure if user clicks anywhere but search bar, the searchPopUpOpened will be set to false?It does not feel like an elegant solution, but I am not aware of a better solution atm.
I looked it up, the
:focus-within equivalent is onBlur, which is the one I use 🥹
Perhaps I'll have to use it like a crutch, thanks though!onBlur uses :focus
(if i am not mistaken)
A common thing, thats pretty close to your case are dropdowns (for selects or similar), maybe that helps your search for ideas.Thanks. I've named it wrongly, it's definitely a dropdown, not a pop-up. I'll to rename it