8

How to Detect Outside Clicks in Vue

 1 year ago
source link: https://tahazsh.com/blog/how-to-detect-outside-clicks-in-vue/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Detect Outside Clicks in Vue

Sep 8 2022 | By Taha Shashtari

Detecting outside clicks is a common thing we need in almost all the apps that we build.

Having a reusable component to detect that is a great way to save time and help us focus on what we build.

Let me first show you the code of the component and explain how it works below. We're going to call this component DetectOutsideClick.vue.

<template>
<div ref="root">
<slot />
</div>
</template>

<script setup>
import { ref } from 'vue'

const emit = defineEmits(['detect'])
const root = ref(null)

document.body.addEventListener('click', (e) => {
if (e.target === root.value || root.value.contains(e.target)) {
return
}
emit('detect')
})
</script>

Here's an example on how to use it:

<DetectOutsideClick @detect="onOutsideClick">
<div class="popup">A popup component</div>
</DetectOutsideClick>

How it works?

The idea here is to listen to all clicks on the page. If the clicked element is not the element inside <DetectOutsideClick> or any children of it, then it's an outside click.

We listen to all clicks on the page using this:

document.body.addEventListener('click', (e) => {})

And here's how we check if it's the same element or a child of it:

if (e.target === root.value || root.value.contains(e.target))

In this case we return because it's not an outside click. But if it is an outside click then we emit an event called detect.

That's it!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK