All Projects → kmohrf → vue-on-click-outside

kmohrf / vue-on-click-outside

Licence: MIT license
vue mixin/directive that does something when you click outside a container

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vue-on-click-outside

Vue Awesome Mui
🏆Mui component for Vue.js(1.x ~ 2.x)
Stars: ✭ 307 (+859.38%)
Mutual labels:  vue2, vue-directive
vue-drag-zone
Drag Zone component for @vuejs
Stars: ✭ 127 (+296.88%)
Mutual labels:  vue2, vue-directive
Vue Awesome Swiper
🏆 Swiper component for @vuejs
Stars: ✭ 12,072 (+37625%)
Mutual labels:  vue2, vue-directive
Draggable Vue Directive
Vue2 directive that handles drag & drop
Stars: ✭ 286 (+793.75%)
Mutual labels:  vue2, vue-directive
Vuebar
(🗃️ Archived) Vue 2 directive for custom scrollbar that uses native scroll behavior. Lightweight, performant, customizable and without dependencies. Used successfully in production on https://ggather.com
Stars: ✭ 650 (+1931.25%)
Mutual labels:  vue2, vue-directive
Vue Quill Editor
🍡@quilljs editor component for @vuejs
Stars: ✭ 6,874 (+21381.25%)
Mutual labels:  vue2, vue-directive
Vue Codemirror
⌨️ @codemirror component for @vuejs
Stars: ✭ 2,115 (+6509.38%)
Mutual labels:  vue2, vue-directive
Vue Cnode
🔥Vue.js打造一个开源的CNode社区。CNode by Vue.js
Stars: ✭ 249 (+678.13%)
Mutual labels:  vue2
vue-tinymce-editor
This a component provides use of tinymce for vue developers
Stars: ✭ 221 (+590.63%)
Mutual labels:  vue2
Vue Firebase Auth Vuex
Vue Firebase🔥 Authentication with Vuex
Stars: ✭ 248 (+675%)
Mutual labels:  vue2
Vue Mapbox Gl
A Vue.js component for Mapbox GL JS
Stars: ✭ 242 (+656.25%)
Mutual labels:  vue2
Vue Form Json Schema
Create forms using JSON schema. Bring your components!
Stars: ✭ 253 (+690.63%)
Mutual labels:  vue2
generator-vue-plugin
Yeoman generator generating vue plugin 🚀
Stars: ✭ 29 (-9.37%)
Mutual labels:  vue2
Vue2 Daterange Picker
Vue2 date range picker
Stars: ✭ 248 (+675%)
Mutual labels:  vue2
plugin-ceres
This is the official Ceres plugin developed by plentymarkets. Ceres is the default template plugin for plentymarkets 7 online stores.
Stars: ✭ 43 (+34.38%)
Mutual labels:  vue2
Login
Vue + Vue-router + Vuex 实现前端页面及逻辑,Express 实现注册登录登出的RestFul API 。
Stars: ✭ 246 (+668.75%)
Mutual labels:  vue2
vue-add-to-calendar
A Vue.js component that provides "Add to Calendar" functionality, works with Vue 2.X
Stars: ✭ 133 (+315.63%)
Mutual labels:  vue2
vpin
基于 vue 的钉子组件,把你的内容固定到屏幕中📌。
Stars: ✭ 12 (-62.5%)
Mutual labels:  vue2
vue-input-autowidth
A Vue.js directive that automatically resizes an input's width to fit its contents.
Stars: ✭ 94 (+193.75%)
Mutual labels:  vue2
vue-text-marquee
A Vue component to Marquee. Just used CSS3 animation.
Stars: ✭ 24 (-25%)
Mutual labels:  vue2

vue-on-click-outside

vue-on-click-outside is a Vue directive which calls a method whenever a click event is registered outside of the element the directive was bound to. The most obvious use case is to close an element whenever someone clicks outside of it.

Installation

npm install vue-on-click-outside --save

# or if you prefer yarn
yarn add vue-on-click-outside

Usage

vue-on-click-outside provides a mixin and directive export. You may use either of them to register the directive globally or locally.

// global directive
import Vue from 'vue'
import { directive as onClickOutside } from 'vue-on-click-outside' 
Vue.directive('on-click-outside', onClickOutside)

// local mixin
import { mixin as onClickOutside } from 'vue-on-click-outside'
const MyComponent = {
  mixins: [onClickOutside]
}
export default MyComponent

If you’ve registered the directive inside of your component you’re good to go and can use it in your templates.

<template>
  <button type="button" class="i-am-a-popover-trigger" @click="open"></button>  
  <div class="i-am-a-popover close-me-by-clicking-outside-of-me" 
    v-if="showPopover" v-on-click-outside="close">
    <span>Interesting Text!</span>
  </div>
</template>

<script>
  import { mixin as onClickOutside } from 'vue-on-click-outside'
  export default {
    mixins: [onClickOutside],
    data() {
      return { showPopover: false }
    },
    methods: {
      open() { this.showPopover = true },
      close() { this.showPopover = false }
    }
  }
</script>

How it works

vue-on-click-outside uses mouseenter and mouseleave events to determine if the user is still inside the element. This should account for content that is dynamically removed on click events and is the primary distinction to the existing vue-clickaway library. Once a user clicks on something the click event bubbles up the DOM tree until it reaches the documentElement. A click listener on the documentElement then looks for elements that were not targetted by the click event and calls the callback you provided via the directive.

If you use vue-on-click-outside multiple times on a single page it will only ever create one single documentElement click listener and two event listeners for your element. If you know that your element won’t contain any dynamically created/removed content you can also use the static modifier of the directive (turning v-on-click-outside="close" into v-on-click-outside.static="close"). It will switch to a different strategy that works without the two event listeners per element.

Caveats

Some JavaScript code will make it appear as if their content is inside your element but in fact it is absolutely positioned from the body element. A lot of datepicker, tooltip, popover and many more libraries rely on this kind of behaviour. Look out for static modes or options that allow you to attach their root element to your element, that uses the directive (or some element inside the tree of your element).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].