DEV Community

Taha Shashtari
Taha Shashtari

Posted on • Originally published at tahazsh.com

4 2

How to Detect Outside Clicks in Vue

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 { onMounted, onUnmounted, ref } from 'vue'

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

const onClick = (e) => {
  if (e.target === root.value || root.value.contains(e.target)) {
    return
  }
  emit('detect')
}

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

onUnmounted(() => {
  document.body.removeEventListener('click', onClick)
})
</script>
Enter fullscreen mode Exit fullscreen mode

Here's an example on how to use it:

<DetectOutsideClick @detect="onOutsideClick">
  <div class="popup">A popup component</div>
</DetectOutsideClick>
Enter fullscreen mode Exit fullscreen mode

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) => {})
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more