v-click-outside Vue Directive

3.2.0 · active · verified Wed Apr 22

v-click-outside is a Vue.js directive that enables developers to detect and react to click events originating outside of a specific HTML element. This functionality is crucial for implementing UI patterns such as closing dropdown menus, modals, and context menus when a user clicks anywhere else on the page. The current stable version is 3.2.0. The package maintains an irregular release cadence, typically driven by bug fixes, dependency updates, and minor enhancements. A key differentiator of v-click-outside is its design to detect outside clicks without intrinsically halting event propagation, offering greater flexibility for developers managing complex event flows. It also supports configuration for various event types (e.g., 'click', 'dblclick'), detection of clicks within iframes, and the option to use the capture phase for event listeners.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to globally register and use the `v-click-outside` directive with advanced configuration options, including a handler function, a middleware for conditional execution, and custom event types like `click` and `touchstart`.

import Vue from 'vue';
import vClickOutside from 'v-click-outside';

// Globally register the directive
Vue.use(vClickOutside);

// Define a simple root Vue component
new Vue({
  el: '#app',
  data () {
    return {
      showMenu: false,
      vcoConfig: {
        handler: this.handleOutsideClickWithConfig,
        middleware: this.middleware,
        events: ['click', 'touchstart'], // Respond to both mouse and touch events
        isActive: true,
        detectIFrame: true,
        capture: false
      }
    }
  },
  methods: {
    toggleMenu() {
      this.showMenu = !this.showMenu;
      console.log('Menu toggled:', this.showMenu);
    },
    handleOutsideClickWithConfig (event) {
      if (this.showMenu) { // Only close if menu is open
        this.showMenu = false;
        console.log('Clicked outside (config handler). Event type:', event.type);
      }
    },
    // Middleware returns true to fire handler, false to prevent
    middleware (event) {
      // Example: Do not close if the click was on an element with class 'ignore-outside'
      const ignoreElement = event.target.closest('.ignore-outside');
      if (ignoreElement) {
        console.log('Middleware ignored click on element with class:', ignoreElement.className);
        return false;
      }
      return true; // Allow handler to fire
    }
  },
  template: `
    <div id="app-container" style="padding: 20px; border: 1px solid #ccc; font-family: sans-serif;">
      <h1>v-click-outside Demo</h1>
      <button @click="toggleMenu" style="padding: 8px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Toggle Menu</button>

      <div v-if="showMenu"
           v-click-outside="vcoConfig"
           style="background: #e0e0e0; border: 1px solid #999; padding: 10px; margin-top: 10px; width: 200px; box-shadow: 2px 2px 8px rgba(0,0,0,0.2);">
        <h2 style="margin-top: 0; font-size: 1.2em;">My Menu</h2>
        <ul style="list-style: none; padding: 0;">
          <li style="margin-bottom: 5px;">Item 1</li>
          <li style="margin-bottom: 5px;">Item 2</li>
        </ul>
        <button class="ignore-outside" style="padding: 5px 10px; background-color: #6c757d; color: white; border: none; border-radius: 3px; cursor: pointer;">Don't close me</button>
      </div>

      <div style="margin-top: 30px; padding: 10px; border: 1px dashed grey; background-color: #f8f9fa;">
        Click here or anywhere outside the menu to see the directive in action.
        <p class="ignore-outside" style="margin-top: 10px; font-style: italic; color: #555;">This text is inside an element with class 'ignore-outside' and clicks here will be ignored by the middleware.</p>
      </div>
    </div>
  `
});

view raw JSON →