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
| Option | Type | Default | Description |
|---|---|---|---|
bounds | boolean | false | Use 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/Method | Reactive | Notes |
|---|---|---|
isActive | Computed from !isPaused | |
isPaused | ShallowRef, readonly | |
pause() | - | Stop detection, preserve state |
resume() | - | Resume detection |
stop() | - | Stop and clean up listeners |
Examples
The following API details are for the useClickOutside composable.
Was this page helpful?