Overview
Detects clicks outside of specified element(s) with automatic cleanup. Uses two-phase detection (pointerdown → pointerup) to prevent drag-out false positives.Features
- Two-phase detection prevents false positives from drag gestures
- Touch scroll threshold ignores swipes/scrolls on mobile
- Capture phase listeners work with stopPropagation
- Pause/resume/stop functionality
- Optional iframe focus detection
- SSR-safe (no-op when not in browser)
- Support for multiple target elements
- Ignore specific elements via refs or CSS selectors
- Bounds detection mode for native dialog elements
Function Signature
Parameters
Element ref(s) to detect clicks outside of. Accepts a single ref/getter or array of refs/getters.
Callback invoked when a click outside is detected.
Use capture phase for event listeners. Ensures detection works even when inner elements call stopPropagation.
Touch movement threshold in pixels. If finger moves more than this distance, it’s treated as a scroll, not a tap.
Detect focus moving to iframes as an outside click.
Elements to ignore when detecting outside clicks. Accepts element refs, getters, or CSS selector strings.
Use bounding rect instead of DOM containment. When true, checks if click coordinates are outside the element’s bounding box. Useful for native
<dialog> elements.Return Value
Whether the listener is currently active.
Whether the listener is currently paused.
Pause listening (stops detection but keeps state).
Resume listening.
Stop listening and clean up.
Examples
Native element ref
Component ref
Multiple targets
Ignoring elements
Native dialog with bounds detection
Pause and resume
Notes
- For accessible components (dialogs, popovers, menus), pair with
useHotkeyfor Escape key dismissal per WCAG/APG requirements - CSS selectors cannot match across Shadow DOM boundaries due to browser limitations. Use element refs instead when ignoring shadow hosts
- The composable uses capture phase by default to work with elements that call stopPropagation