When I click outside the modal it doesnt close. Why doesnt onClickOutside not work ?
Why doesnt the
closeModal();
get called when I click outside the ref="modal"
?2 Replies
Hi @o.m. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the provided code and knowledge sources, there are a few potential reasons why the
onClickOutside
function might not be working as expected:
1. Timing issue: The onClickOutside
function is being called immediately in the setup function, but modalTarget.value
might not be available yet. The ref might not be populated until after the component is mounted.
2. Incorrect ref usage: You're using both useTemplateRef
and a regular ref
. This might be causing confusion.
To address these issues, you could try the following modifications:
1. Move the onClickOutside
call into an onMounted
hook to ensure the ref is available:
2. Use a single ref approach:
3. Make sure the isOpen
prop is being properly controlled by the parent component.
If these modifications don't resolve the issue, it's possible that there might be other factors interfering with the click detection, such as event propagation or z-index issues.
For more information on using onClickOutside
and other Vue 3 composables, you can refer to the VueUse documentation (https://vueuse.org/core/onClickOutside/).
If the problem persists after trying these suggestions, you might want to create a minimal reproduction of the issue to help identify any other potential causes.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@674984670156423171> Kapa.ai is still learning and improving, please let me know how I did by reacting below.