Sensors · @vueuse/core v15.0.0 · Reviewed

How to detect outside clicks in Vue with onClickOutside

Run some code when someone clicks outside an element, like closing a dropdown.

App.vueVue SFCOpen in Vue Playground ↗
Live preview

Edit the code and the preview updates as you type.

What it does

You open a menu, then click somewhere else on the page. You expect the menu to close. onClickOutside does exactly that: give it an element and a function, and it calls your function whenever a click lands anywhere outside that element.

It works with a plain template ref or a component, cleans up its listener when the component unmounts, and handles a few browser quirks for you, like iOS Safari not firing clicks on plain page backgrounds.

The code

Vue · Composition API
import { ref } from 'vue'
import { onClickOutside } from '@vueuse/core'

const menu = ref(null) // <ul ref="menu"> in your template
const open = ref(false)

onClickOutside(menu, () => {
  open.value = false
})

Where you'd use it

  • Dropdown and account menus that close when you click away
  • A slide-out cart drawer or filter panel on a shop
  • Search suggestions under a search box
  • Popovers like date pickers, colour pickers and emoji pickers
  • Inline editing: click away from a field to save it

The problem: a dropdown that will not close correctly

An account menu should close when a user taps elsewhere. A hand-written document listener also needs to account for its trigger button, nested controls, touch and pointer input, and cleanup when the component disappears. VueUse handles the outside-pointer listener and lifecycle cleanup; your component still owns keyboard behavior and focus.

Three ways to close a menu

ApproachUse it whenKeep in mind
Manual document listenerYou need custom event ordering or a tiny one-off behavior.Remove the listener on unmount; account for ignored targets and pointer events.
onClickOutsideA custom dropdown, filter panel or popover needs outside-pointer dismissal.Pass the trigger and other in-scope elements through ignore.
Native Popover APIA simple popover can use browser light-dismiss and Escape behavior.Check browser support and whether its interaction model fits your UI.
<dialog>You are building a modal dialog.Use showModal() for modal semantics and focus behavior.

Accessibility: outside pointer input is only one part

onClickOutside listens to pointer events, including mouse, pen and touch. It does not provide keyboard dismissal or focus management by itself. Make the trigger a real button, keep aria-expanded in sync, connect it to the panel with aria-controls, close on Escape, and decide where focus should go afterward. Complex menus should use an accessible menu implementation.

import { onClickOutside, onKeyStroke } from '@vueuse/core'

const open = ref(false)
const menu = ref(null)
const trigger = ref(null)

onClickOutside(menu, () => { open.value = false }, {
  ignore: [trigger, '#nested-picker'],
})
onKeyStroke('Escape', () => { open.value = false })
<button ref="trigger" :aria-expanded="open" aria-controls="account-menu">
  Account menu
</button>
<ul v-if="open" id="account-menu" ref="menu" role="menu">
  <!-- menu items -->
</ul>

The interactive demo includes an ignored trigger. Try adding a second ignored target, opening two separate menus, or removing the trigger from ignore to see why it matters.

Nuxt details and listener controls

A template ref starts as null; pass the ref itself so VueUse can follow it as the element appears. Avoid reading window or document at module scope during SSR. The composable is safe to call from setup and attaches browser listeners on the client. For a conditional menu, keep the ref attached to the actual panel. Use an element ref in ignore when you control the element; use a CSS selector for stable external markup. If you need manual lifecycle control, enable controls: true and call the returned stop(); detectIframe handles focus moving into an iframe.

In Nuxt (and other gotchas)

It is safe to call in setup in Nuxt. On the server there is no window, so it quietly does nothing. You don't need onMounted or <ClientOnly>. With the @vueuse/nuxt module (npx nuxi module add vueuse) it is auto-imported, so you can drop the import line.

A common gotcha: the button that opens the menu is also "outside" the menu. Click it to close the menu, and the outside-click handler closes the menu, then the button's own click opens it again. Pass the button in ignore, as the playground does. Delete that option in the playground to see the bug.

Prefer it in the template? @vueuse/components has a v-on-click-outside directive and an <OnClickOutside> wrapper component.

When not to use it

It handles pointer input, not the whole interaction. Mouse, pen and touch pointer events are detected. Keyboard users need Esc to close things too, so pair it with onKeyStroke('Escape', close).

For simple popovers, the browser can do it for you. The native popover attribute closes on outside click and Esc with zero JavaScript. For real modal dialogs, use <dialog> with showModal(), which also traps focus, something onClickOutside won't do.

Links

Need a hand with your Vue or Nuxt app?

More functions