---
category: Sensors
---
# onClickOutside
Listen for clicks outside of an element. Useful for modals or dropdowns.
## Usage
```vue
Hello world
Outside element
```
### Return Value
By default, `onClickOutside` returns a `stop` function to remove the event listeners.
```ts
const stop = onClickOutside(target, handler)
// Later, stop listening
stop()
```
### Controls
If you need more control over triggering the handler, you can use the `controls` option. This returns an object with `stop`, `cancel`, and `trigger` functions.
```ts
const { stop, cancel, trigger } = onClickOutside(
modalRef,
(event) => {
modal.value = false
},
{ controls: true },
)
// cancel prevents the next click from triggering the handler
cancel()
// trigger manually fires the handler
trigger(event)
// stop removes all event listeners
stop()
```
### Ignore Elements
Use the `ignore` option to prevent certain elements from triggering the handler. Provide elements as an array of Refs or CSS selectors.
```ts
const ignoreElRef = useTemplateRef('ignoreEl')
onClickOutside(
target,
event => console.log(event),
{ ignore: [ignoreElRef, '.ignore-class', '#ignore-id'] },
)
```
### Capture Phase
By default, the event listener uses the capture phase (`capture: true`). Set `capture: false` to use the bubbling phase instead.
```ts
onClickOutside(target, handler, { capture: false })
```
### Detect Iframe Clicks
Clicks inside an iframe are not detected by default. Enable `detectIframe` to also trigger the handler when focus moves to an iframe.
```ts
onClickOutside(target, handler, { detectIframe: true })
```
## Component Usage
```vue
Click Outside of Me
```
## Directive Usage
```vue
Hello World
```
You can also set the handler as an array to set the configuration items of the instruction.
```vue
click outside ignore element
Hello World
```
## Type Declarations
```ts
export interface OnClickOutsideOptions<
Controls extends boolean = false,
> extends ConfigurableWindow {
/**
* List of elements that should not trigger the event,
* provided as Refs or CSS Selectors.
*/
ignore?: MaybeRefOrGetter<(MaybeElementRef | string)[]>
/**
* Use capturing phase for internal event listener.
* @default true
*/
capture?: boolean
/**
* Run handler function if focus moves to an iframe.
* @default false
*/
detectIframe?: boolean
/**
* Use controls to cancel/trigger listener.
* @default false
*/
controls?: Controls
}
export type OnClickOutsideHandler<
T extends OnClickOutsideOptions = OnClickOutsideOptions,
> = (
event:
| (T["detectIframe"] extends true ? FocusEvent : never)
| (T["controls"] extends true ? Event : never)
| PointerEvent,
) => void
export type OnClickOutsideReturn =
Controls extends false
? Fn
: {
stop: Fn
cancel: Fn
trigger: (event: Event) => void
}
/**
* Listen for clicks outside of an element.
*
* @see https://vueuse.org/onClickOutside
* @param target
* @param handler
* @param options
*/
export declare function onClickOutside(
target: MaybeComputedElementRef,
handler: OnClickOutsideHandler,
options?: T,
): Fn
export declare function onClickOutside>(
target: MaybeComputedElementRef,
handler: OnClickOutsideHandler,
options: T,
): {
stop: Fn
cancel: Fn
trigger: (event: Event) => void
}
```