2.2.2 • Published 7 years ago

vue-clickaway v2.2.2

Weekly downloads
85,495
License
MIT
Repository
github
Last release
7 years ago

vue-clickaway

Reusable clickaway directive for reusable Vue.js components

npm version CDNJS

Overview

Sometimes you need to detect clicks outside of the element (to close a modal window or hide a dropdown select). There is no native event for that, and Vue.js does not cover you either. This is why vue-clickaway exists. Please check out the demo before reading further.

Requirements

  • vue: ^2.0.0

If you need a version for Vue 1, try vue-clickaway@1.0.

Install

From npm:

$ npm install vue-clickaway --save

From CDN, chose the one you prefer:

<script src="https://cdn.jsdelivr.net/npm/vue-clickaway@2.2.2/dist/vue-clickaway.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-clickaway/2.2.2/vue-clickaway.min.js"></script>
<script src="https://cdn.rawgit.com/simplesmiler/vue-clickaway/2.2.2/dist/vue-clickaway.min.js"></script>

Usage

  1. Make the directive available to your component
  2. Define a method to be called
  3. Use the directive in the template

The recommended way is to use the mixin:

import { mixin as clickaway } from 'vue-clickaway';

export default {
  mixins: [ clickaway ],
  template: '<p v-on-clickaway="away">Click away</p>',
  methods: {
    away: function() {
      console.log('clicked away');
    },
  },
};

If mixin does not suit your needs, you can use the directive directly:

import { directive as onClickaway } from 'vue-clickaway';

export default {
  directives: {
    onClickaway: onClickaway,
  },
  template: '<p v-on-clickaway="away">Click away</p>',
  methods: {
    away: function() {
      console.log('clicked away');
    },
  },
};

Caveats

  1. Pay attention to the letter case. onClickaway turns into v-on-clickaway, while onClickAway turns into v-on-click-away.
  2. Prior to vue@^2.0, directive were able to accept statements. This is no longer the case. If you need to pass arguments, just do v-on-clickaway="() => away(arg1)".
  3. There is a common issue with dropdowns (and modals) inside an element with v-on-clickaway. Some UI libraries chose to implement these UI elements by attaching the DOM element directly to the body. This makes clicks on a dropped element trigger away handler. To combat that, you have to add an extra check in the handler, for where the event originated from. See #9 for an example.

License

MIT

vue-nxp-plugin@j-cat/calchez-componentsvia-websites@rock-content/rock-components-vue@luchmewep/mdbvuevue-reboot-uiknime-ui-tablejfs-ui-testrobinapp-vue-sdkvue-hub20-web@branchito/wcrobin.io-vuerobin.vue-sdknboard-ui@infinitebrahmanuniverse/nolb-vue-clicwbu-design-system@everything-registry/sub-chunk-3076fxhellofx-antv-appwolf-oil-uivue-my-tablevue-my-viewvue-my-datepickervue-lite-kitvue-jql-autocompletevue-mabuyavue-bmodalvue-cli-mulit-chentestvue-input-dropdownvue-instantxiabaiwei-test-demoxiabaiwei-test3-demoxdvstorium-shop-libraryvue-context-v2vue-contextux-baseveformvue-accessible-datepickervue-fabvue-enyo-componentsvue-emoji-mart-pickervitorm11-cj-daterange-pickervue-daterange-pickervue-daterange-picker-lightvue-autocomplete2vue-bootstap-material-tools@cnvrg/vue-selectorbwi-designjobbsontableizteach-design-systemizteach-design-system-prodizteach-design-system-testiztech-design-system@fusebase/controlpanelstiona.core.modulestoolbloxtopological-constructortui-skeleton@angustrau/vue-bootstrap-select@anhdt.developer/izteach-design-system@anhdt.developer/izteach-design-system-test@anhdt.developer/theme-duong-dai@autoantwort/vue-bootstrap-select@adomni/vue-components@alfsnd/vue-bootstrap-select@awestlib/feathers-design-system@bahyali/vue2-daterangepicker@azibai/chat@aeternity/aepp-components@aeternity/aepp-components-3@blocksley/blocksley@blogsley/blocksley@carrene/vue-chatbox@chumager/itdfw-site-base@3yourmind/vue-comments@abler/ats-front-resources-api@desionlab/vue-vcl@danzf/bwi-designxx-onemap-biz-uizp-front-upvue_basecomponentvue_basecomponent_2vue_basecomponent_newvue-select-picker-bootstrapvue-semantic-modalvue-semantic-modal2vue-unstyledvue-material-datepicker-personal-updatew2p-componentswc-tempvue-white-dashboardvue2-relative-daterange-pickervue2-daterange-picker-ashokvue2-daterange-picker-thewildbunch-forkvue2-daterange-picker-with-custom-button@fishtank/fishtank-vue@ian-regcheq/vue-bootstrap-select@heyunxia/vue-twemoji-picker@icreate/ics-ui
2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.0

9 years ago

1.2.0

9 years ago

2.0.0

9 years ago

2.0.0-rc.1

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.1

9 years ago

1.0.1

9 years ago

1.1.0

9 years ago

1.0.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago