✅ – ✅ – dmikester1 – 16-36 Sep 26
I have an
OutsideClickTrigger
hook I have found that I am utilizing to hide a filter menu whenever the user clicks outside the menu. There is a filters button that I am using to show the filters menu like this: onClick={() => setShowFilters(!showFilters)}
. Now the issue is that when the filters menu is open, and I click on the filters button again to close it, it does not work or maybe it closes it quickly and then OutsideClickTrigger
gets run and it shows it again. Last time I was able to fix this by wrapping the button and the menu in the hook component. That won't work this time because of the structure of my html. I'm thinking I somehow need to also pass the button element into that hook so it checks if that was the element clicked.34 Replies
Here is my
OutsideClickTrigger
hook component:
In this section: if (ref.current && !ref.current.contains(event.target)) {
onClick();
}
I think I also need to check if the button was clicked.Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I thought this was kind of a "click away component". I am wrapping my stuff in it like this:
<OutsideTrigger onClick={() => setShowFilters(false)}>
I can remove the prop types 🙂Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
So you are saying rewrite the logic in
useOutsideTrigger
?Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
OK, so I would wrap my menu and my button with that component?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
Cool, I'll give it a try and report back!
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I renamed it from modal, but this is how I wrapped my filters menu:
<OutsideClickHook onClickAway={() => setShowFilters(false)}>
when I click outside the menu, it does not dissapearUnknown User•16mo ago
Message Not Public
Sign In & Join Server To View
and above that section:
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
are those bootstrap classes or something different?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
ok
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
i'd ideally like to fade it in/out
Solution
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
oh i see
i'll try that
Is this div meant to take up the entire page?
<div
onClick={onClickAway}
className="flex h-full w-full flex-col items-center justify-center bg-black/50"
>
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
so wherever i click, i will be clicking that div?
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
I think that is my issue
gonna be tricky to figure out the positioning of my menu with that though
The filters menu is under the Filter button
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
not finding one, but something like this?
oh i wasn't gonna use children
something like that?
think I got it!!!
I had to set
position: fixed
on that overlay and now it is going full width and height.This question has an answer! Thank you for helping 😄
If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/902647189120118794/1156268044486705274
This question has an answer! Thank you for helping 😄
If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/902647189120118794/1156268044486705274
Thank you so much igaul! That was a tricky one!
Never would have thought of that
stopPropagation
trick.Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
This thread hasn’t had any activity in 36 hours, so it’s now locked.
Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/902647189120118794/1156268044486705274