Skip to content
On this page

onClickOutside

Category
Last Changed
25 days ago

Listen for clicks outside of an element. Useful for modal or dropdown.

Demo

Usage


















 





























<template>
  <div ref="target">
    Hello world
  </div>
  <div>
    Outside element
  </div>
</template>

<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'

export default {
  setup() {
    const target = ref(null)

    onClickOutside(target, (event) => console.log(event))

    return { target }
  }
}
</script>
<template>
  <div ref="target">
    Hello world
  </div>
  <div>
    Outside element
  </div>
</template>

<script>
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'

export default {
  setup() {
    const target = ref(null)

    onClickOutside(target, (event) => console.log(event))

    return { target }
  }
}
</script>

This function uses Event.composedPath() which is NOT supported by IE 11, Edge 18 and below. If you are targeting these browsers, we recommend you to include this code snippet on your project.

Component

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.
<OnClickOutside @trigger="count++">
  <div>
    Click Outside of Me
  </div>
</OnClickOutside>
<OnClickOutside @trigger="count++">
  <div>
    Click Outside of Me
  </div>
</OnClickOutside>

Type Declarations

export interface OnClickOutsideOptions extends ConfigurableWindow {
  /**
   * List of elements that should not trigger the event.
   */
  ignore?: MaybeElementRef[]
}
/**
 * Listen for clicks outside of an element.
 *
 * @see https://vueuse.org/onClickOutside
 * @param target
 * @param handler
 * @param options
 */
export declare function onClickOutside(
  target: MaybeElementRef,
  handler: (evt: PointerEvent) => void,
  options?: OnClickOutsideOptions
): (() => void) | undefined
export interface OnClickOutsideOptions extends ConfigurableWindow {
  /**
   * List of elements that should not trigger the event.
   */
  ignore?: MaybeElementRef[]
}
/**
 * Listen for clicks outside of an element.
 *
 * @see https://vueuse.org/onClickOutside
 * @param target
 * @param handler
 * @param options
 */
export declare function onClickOutside(
  target: MaybeElementRef,
  handler: (evt: PointerEvent) => void,
  options?: OnClickOutsideOptions
): (() => void) | undefined

Source

SourceDemoDocs

Contributors

Anthony Fu
Nurettin Kaya
wheat
Jelf
JserWang
Alex Kozack

Changelog

v7.6.0 on 2/8/2022
c275a - feat: add ignore option (#1205)
v7.0.0 on 11/20/2021
2ff6c - refactor!: listen click event, remove event option (#817)
v6.4.0 on 9/17/2021
e557f - feat: added 'as' prop to renderable components (#742)
v6.1.0 on 9/2/2021
9548a - feat: whitelist click event (#693)
v5.0.0-beta.2 on 5/25/2021
5bede - feat: introduce components & directives (#486)
v4.11.1 on 5/24/2021
c0c4b - fix: duplicate code (#519)
v4.11.0 on 5/14/2021
46f6d - feat: default to just pointerDown (#508)
v4.11.1 on 5/24/2021
06814 - fix: duplicate code (#519)
v4.11.0 on 5/14/2021
582af - feat: default to just pointerDown (#508)
onClickOutside has loaded