# vue-unclick

> Vue.js directive to handle clicking outside of an element

Latest version **1.0.5** (published 2022-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-unclick
pnpm add vue-unclick
yarn add vue-unclick
bun add vue-unclick
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-06-27 |
| First published | 2022-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | marvioso |
| Keywords | vue |

## Links

- npm: https://www.npmjs.com/package/vue-unclick
- Homepage: https://github.com/marvi0so/vue-unclick
- npm.io page: https://npm.io/package/vue-unclick

## Recent versions

- 1.0.5 (latest) — 2022-06-27
- 1.0.4 — 2022-03-03
- 1.0.1 — 2022-03-03
- 1.0.0 — 2022-03-03
- 0.0.6 — 2022-03-03

## README

# 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
```

---
_Source: https://npm.io/package/vue-unclick · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
