Skip to content

vClickOutside

vClickOutside is a Vue directive to trigger a function when the user clicks outside an element

Basic usage

Click outside me

Advanced usage with options

The directive can accept an options object to customize its behavior:

Click outside me (but not on the button below)

Using the once option

The directive can be configured to trigger only once:

Click outside me (works only once)

All options combined

Here's an example using all available options:

vue
<script lang="ts" setup>
import { vClickOutside, type VClickOutsideOptions } from 'maz-ui/directives'

const options: VClickOutsideOptions = {
  callback: handleClickOutside,
  ignore: ['.modal', '.tooltip', '.dropdown-menu'],
  capture: true,
  once: false
}

function handleClickOutside(event: Event) {
  console.log('Clicked outside!', event)
}
</script>

<template>
  <div v-click-outside="options">
    <!-- Your content -->
  </div>
</template>

API Reference

Usage Patterns

vue
<!-- Simple function -->
<div v-click-outside="myCallback">...</div>

<!-- With options object -->
<div v-click-outside="{ callback: myCallback, ignore: ['.ignore'] }">...</div>

Options

OptionTypeDefaultDescription
callbackFunctionundefinedRequired. Function to call when clicking outside
ignorestring[][]Array of CSS selectors to ignore when detecting clicks
capturebooleanfalseWhether to use capture phase for event listening
oncebooleanfalseWhether to trigger the callback only once

Type Definitions

typescript
interface VClickOutsideOptions {
  callback: (...args: any[]) => any
  ignore?: string[]
  capture?: boolean
  once?: boolean
}

type vClickOutsideBindingValue =
  | ((...args: any[]) => any)
  | VClickOutsideOptions

Global install

Vue

typescript
import { vClickOutsideInstall } from 'maz-ui/directives'
import { createApp } from 'vue'

const app = createApp(App)

app.use(vClickOutsideInstall)

app.mount('#app')

Nuxt

Please refer to the Nuxt module documentation for more information.