1.0.5 • Published 2 years ago

vue-unclick v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Vue-unclick

:rocket: Vue.js directive to handle clicking outside of an element \ :muscle: Great for use cases such as dropdowns and popovers \ :computer: Simple integration with Vue template \ :racing_car: Advanced configuration to increase performance

Installation

Install via npm:

npm install vue-unclick

Register the directive globally with your Vue.js app:

import { createApp } from "vue"
import unclick from "vue-unclick"

const app = createApp({})
app.directive("unclick", unclick)
app.mount("#app")

Usage

You can apply the directive to any element and the callback method will be fired whenever there is a click on the screen outside of an element (this is what we mean by "un-clicking"):

<template>
  <div v-unclick="hideDropdown">
    <a href="#" @click="toggleDropdown">
      Toggle dropdown
    </a>
    <div v-show="isVisible">
      <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
      <p>Dolore ea quibusdam doloremque pariatur rerum cumque quam.</p>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isVisible: false
      }
    },
    methods: {
      hideDropdown(event) {
        console.log(event)
        this.isVisible = false
      },
      toggleDropdown() {
        this.isVisible = !this.isVisible
      }
    }
  }
</script>

In order to optimize the performance, you can conditionally control when the directive listens for clicks by adding an boolean argument to the directive:

<template>
  <div v-unclick:[isVisible]="hideDropdown">
    ...
  </div>
</template>

In the above example, event listeners will only be added when isVisible is truthy in order to avoid unnecessary overheads. This approach is strongly recommended.

Testing

docker run -it --rm -v "$PWD":/usr/src/app -w /usr/src/app node:latest npm run test -- --coverage
1.0.5

2 years ago

1.0.4

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.6

2 years ago