2.2.2 • Published 6 years ago

vue-clickaway v2.2.2

Weekly downloads
85,495
License
MIT
Repository
github
Last release
6 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-3076@fator/vue-material@bahyali/vue2-daterangepicker@blogsley/blocksley@blocksley/blocksley@carrene/vue-chatboxdaterange-picker-grafanadaterange-picker-ifradiga-chatproofappqmt-article-use1qmt-article-use2qmt-article-use3platon-uiraisetheapp@danzf/bwi-design@cnvrg/vue-selector@chumager/itdfw-site-basecesi-eats-jalilkane@desionlab/vue-vclros-vue-context@heyunxia/vue-twemoji-pickerrender-profile@evolve-legal/shared-componentsrobin.io-vue2@insto/footer@insto/headersense-field-vuesciris-jssentioo@ian-regcheq/vue-bootstrap-select@informatix8/access-search@informatix8/access-tree@icreate/ics-uifeathers-design-system@fishtank/fishtank-vue@fusebase/controlpaneldt-ui-testeclipssis-bootstrap-selectremix-uiremix-ui-literadiate-framework@evatechsoft/mdbvue@eoapackages/vue-componentsshards-vue@kevinfaguiar/vue-twemoji-pickerfx-antv-appfxhello@platforma/core@protocol-one/ui-kitux-baseizteach-design-systemizteach-design-system-prodizteach-design-system-testiztech-design-system@ponypharmacist/atlas-components@ponypharmacist/atlas-ui-kitvue-semantic-modalvue-semantic-modal2@rockt/bigbang-comp-autocompletevue-jql-autocompletevue-accessible-datepickervue-daterange-pickervue-daterange-picker-lightvue-unstyledvue-white-dashboard@railken/quartz-config@railken/quartz-corevitorm11-cj-daterange-pickerjobbsontablevue-mabuyavue-lite-kitvue2-relative-daterange-pickervue_basecomponentvue_basecomponent_2vue_basecomponent_newvue2-daterange-picker-ashokvue2-daterange-picker-thewildbunch-forkvue2-daterange-picker-with-custom-button@sandstreamdev/chatwootvue-cli-mulit-chentestvue-context-v2w2p-componentsvue-select-picker-bootstrap
2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

7 years ago

1.2.0

7 years ago

2.0.0

8 years ago

2.0.0-rc.1

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.1

8 years ago

1.0.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.1

8 years ago

0.1.0

9 years ago