Skip to main content
Vuetify0 is now in alpha!
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useClickOutside

A composable for detecting clicks outside of specified element(s) with automatic cleanup.

Usage

The useClickOutside composable detects when users click outside target elements. It uses two-phase detection (pointerdown → pointerup) to prevent false positives when dragging, and includes touch scroll handling for mobile.

vue
<script setup lang="ts">
  import { useClickOutside } from '@vuetify/v0'
  import { useTemplateRef } from 'vue'

  const menu = useTemplateRef('menu')

  useClickOutside(menu, () => {
    console.log('Clicked outside the menu')
  })
</script>

<template>
  <div ref="menu">
    Menu content
  </div>
</template>

Architecture

useClickOutside builds on useEventListener for pointer and focus event detection:

Multiple Targets

Pass an array of refs to ignore clicks inside any of them:

ts
import { useClickOutside } from '@vuetify/v0'
import { useTemplateRef } from 'vue'

const trigger = useTemplateRef('trigger')
const panel = useTemplateRef('panel')

// Clicks inside EITHER trigger or panel are ignored
useClickOutside([trigger, panel], () => {
  console.log('Clicked outside both elements')
})

The target parameter accepts MaybeArray<ClickOutsideTarget> — a single ref/getter or an array of refs/getters.

Options

OptionTypeDefaultDescription
boundsbooleanfalseUse bounding-rect detection instead of DOM containment. Required for native <dialog> elements — backdrop clicks have the <dialog> as the event target, so containment checks always pass
ts
import { useClickOutside } from '@vuetify/v0'

const dialog = useTemplateRef('dialog')

// For native <dialog> — backdrop clicks are detected via coordinates
useClickOutside(dialog, () => dialog.value?.close(), { bounds: true })

Reactivity

Property/MethodReactiveNotes
isActiveComputed from !isPaused
isPausedShallowRef, readonly
pause()-Stop detection, preserve state
resume()-Resume detection
stop()-Stop and clean up listeners

Examples

API Reference

The following API details are for the useClickOutside composable.
Was this page helpful?

Ctrl+/