vue3-click-outside-directive 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,237 @@
1
+ # v-click-outside
2
+
3
+ > Vue 3 directive that fires when a click is registered outside of an element.
4
+ > TypeScript-first · Zero dependencies · Shadow DOM ready · SSR safe
5
+
6
+ [![npm version](https://img.shields.io/npm/v/v-click-outside)](https://npmjs.com/package/v-click-outside)
7
+ [![license](https://img.shields.io/npm/l/v-click-outside)](./LICENSE)
8
+
9
+ ---
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install v-click-outside
15
+ # or
16
+ pnpm add v-click-outside
17
+ # or
18
+ yarn add v-click-outside
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Vue 3
24
+
25
+ ### Global registration
26
+
27
+ Register the plugin once in `main.ts` — the directive becomes available in every component.
28
+
29
+ ```ts
30
+ // main.ts
31
+ import { createApp } from "vue";
32
+ import { ClickOutsidePlugin } from "v-click-outside";
33
+ import App from "./App.vue";
34
+
35
+ const app = createApp(App);
36
+ app.use(ClickOutsidePlugin);
37
+ app.mount("#app");
38
+ ```
39
+
40
+ ```vue
41
+ <template>
42
+ <div v-click-outside="onClickOutside">...</div>
43
+ </template>
44
+
45
+ <script setup lang="ts">
46
+ function onClickOutside(event: PointerEvent) {
47
+ console.log("clicked outside", event);
48
+ }
49
+ </script>
50
+ ```
51
+
52
+ ### Local registration
53
+
54
+ Import the directive directly in the component — no global setup needed.
55
+
56
+ ```vue
57
+ <script setup lang="ts">
58
+ import { vClickOutside } from "v-click-outside";
59
+
60
+ function onClickOutside(event: PointerEvent) {
61
+ console.log("clicked outside", event);
62
+ }
63
+ </script>
64
+
65
+ <template>
66
+ <div v-click-outside="onClickOutside">...</div>
67
+ </template>
68
+ ```
69
+
70
+ ### TypeScript — global directive types
71
+
72
+ When using global registration, add a type declaration so the template gets proper type checking:
73
+
74
+ ```ts
75
+ // src/types/directives.d.ts
76
+ import type { Directive } from "vue";
77
+ import type { ClickOutsideHandler, ClickOutsideOptions } from "v-click-outside";
78
+
79
+ declare module "vue" {
80
+ interface GlobalDirectives {
81
+ vClickOutside: Directive<
82
+ HTMLElement,
83
+ ClickOutsideHandler | ClickOutsideOptions
84
+ >;
85
+ }
86
+ }
87
+ ```
88
+
89
+ ---
90
+
91
+ ## Nuxt 3 / Nuxt 4
92
+
93
+ ### Plugin
94
+
95
+ Create a plugin file — Nuxt picks it up automatically, no manual registration needed.
96
+
97
+ ```ts
98
+ // plugins/v-click-outside.ts
99
+ import { ClickOutsidePlugin } from "v-click-outside";
100
+
101
+ export default defineNuxtPlugin((nuxtApp) => {
102
+ nuxtApp.vueApp.use(ClickOutsidePlugin);
103
+ });
104
+ ```
105
+
106
+ ### TypeScript — global directive types
107
+
108
+ ```ts
109
+ // types/directives.d.ts
110
+ import type { Directive } from "vue";
111
+ import type { ClickOutsideHandler, ClickOutsideOptions } from "v-click-outside";
112
+
113
+ declare module "vue" {
114
+ interface GlobalDirectives {
115
+ vClickOutside: Directive<
116
+ HTMLElement,
117
+ ClickOutsideHandler | ClickOutsideOptions
118
+ >;
119
+ }
120
+ }
121
+ ```
122
+
123
+ ### SSR
124
+
125
+ The directive is SSR-safe — it registers the `pointerdown` listener only in `mounted`, which runs exclusively on the client.
126
+
127
+ ---
128
+
129
+ ## API
130
+
131
+ ### Simple — pass a handler function
132
+
133
+ ```vue
134
+ <div v-click-outside="handler" />
135
+ ```
136
+
137
+ ```ts
138
+ function handler(event: PointerEvent) {
139
+ console.log("clicked outside", event);
140
+ }
141
+ ```
142
+
143
+ ### Advanced — pass an options object
144
+
145
+ ```vue
146
+ <div v-click-outside="{ handler, ignore: [triggerEl], disabled: isDisabled }" />
147
+ ```
148
+
149
+ | Option | Type | Default | Description |
150
+ | ---------- | ----------------------------------------- | ------- | ------------------------------------------------------ |
151
+ | `handler` | `(e: PointerEvent) => void` | — | **Required.** Called when a click outside is detected |
152
+ | `ignore` | `Array<HTMLElement \| null \| undefined>` | `[]` | Elements whose clicks are treated as "inside" |
153
+ | `disabled` | `boolean` | `false` | Disable the directive without removing it from the DOM |
154
+
155
+ ---
156
+
157
+ ## Examples
158
+
159
+ ### Dropdown menu
160
+
161
+ The most common use case. The `ignore` option prevents the trigger button from immediately closing the menu it just opened.
162
+
163
+ ```vue
164
+ <script setup lang="ts">
165
+ import { ref } from "vue";
166
+
167
+ const open = ref(false);
168
+ const triggerRef = ref<HTMLElement>();
169
+
170
+ function close() {
171
+ open.value = false;
172
+ }
173
+ </script>
174
+
175
+ <template>
176
+ <button ref="triggerRef" @click="open = !open">Menu</button>
177
+
178
+ <ul v-if="open" v-click-outside="{ handler: close, ignore: [triggerRef] }">
179
+ <li>Item 1</li>
180
+ <li>Item 2</li>
181
+ </ul>
182
+ </template>
183
+ ```
184
+
185
+ > **Why `ignore: [triggerRef]`?**
186
+ > Without it, clicking the button fires `@click` (opens the menu) and `v-click-outside` simultaneously (closes it), because the button is outside the `<ul>`. Adding it to `ignore` breaks the cycle.
187
+
188
+ ### Modal / dialog
189
+
190
+ ```vue
191
+ <script setup lang="ts">
192
+ const props = defineProps<{ modelValue: boolean }>();
193
+ const emit = defineEmits<{ "update:modelValue": [value: boolean] }>();
194
+ </script>
195
+
196
+ <template>
197
+ <div v-if="modelValue" class="overlay">
198
+ <div
199
+ v-click-outside="() => emit('update:modelValue', false)"
200
+ class="dialog"
201
+ >
202
+ <slot />
203
+ </div>
204
+ </div>
205
+ </template>
206
+ ```
207
+
208
+ ### Conditionally disable
209
+
210
+ ```vue
211
+ <template>
212
+ <div
213
+ v-click-outside="{
214
+ handler: onClickOutside,
215
+ disabled: !isEditing,
216
+ }"
217
+ >
218
+ ...
219
+ </div>
220
+ </template>
221
+ ```
222
+
223
+ ---
224
+
225
+ ## How it works
226
+
227
+ - Listens to `pointerdown` on `document` — fires before `click` and works for touch events
228
+ - Uses `event.composedPath()` for correct Shadow DOM support, falls back to `event.target`
229
+ - The listener is created once in `mounted` and removed in `unmounted` — no leaks
230
+ - On `updated`, options are swapped in-place without recreating the listener
231
+ - `null` / `undefined` entries in `ignore` are safely skipped
232
+
233
+ ---
234
+
235
+ ## License
236
+
237
+ MIT
@@ -0,0 +1,10 @@
1
+ import { ObjectDirective } from 'vue';
2
+ import { ClickOutsideOptions } from './types';
3
+ export type { ClickOutsideHandler, ClickOutsideOptions } from './types';
4
+ interface ClickOutsideEl extends HTMLElement {
5
+ _clickOutside?: {
6
+ options: Required<ClickOutsideOptions>;
7
+ listener: (e: PointerEvent) => void;
8
+ };
9
+ }
10
+ export declare const vClickOutside: ObjectDirective<ClickOutsideEl>;
@@ -0,0 +1,3 @@
1
+ export { vClickOutside } from './directive';
2
+ export { ClickOutsidePlugin } from './plugin';
3
+ export type { ClickOutsideHandler, ClickOutsideOptions } from './types';
@@ -0,0 +1,4 @@
1
+ import { App } from 'vue';
2
+ export declare const ClickOutsidePlugin: {
3
+ install(app: App): void;
4
+ };
@@ -0,0 +1,15 @@
1
+ export type ClickOutsideHandler = (event: PointerEvent) => void;
2
+ export interface ClickOutsideOptions {
3
+ /** Handler called when a click outside the element is detected */
4
+ handler: ClickOutsideHandler;
5
+ /**
6
+ * List of extra elements to treat as "inside".
7
+ * Clicks on these elements will NOT trigger the handler.
8
+ */
9
+ ignore?: Array<HTMLElement | null | undefined>;
10
+ /**
11
+ * If true, the directive is disabled and won't react to clicks.
12
+ * @default false
13
+ */
14
+ disabled?: boolean;
15
+ }
@@ -0,0 +1,44 @@
1
+ //#region src/directive.ts
2
+ function e(e) {
3
+ if (typeof e.value == "function") return {
4
+ handler: e.value,
5
+ ignore: [],
6
+ disabled: !1
7
+ };
8
+ if (e.value !== null && typeof e.value == "object" && typeof e.value.handler == "function") return {
9
+ handler: e.value.handler,
10
+ ignore: e.value.ignore ?? [],
11
+ disabled: e.value.disabled ?? !1
12
+ };
13
+ throw TypeError("[v-click-outside] value must be a Function or { handler: Function, ignore?: HTMLElement[], disabled?: boolean }");
14
+ }
15
+ function t(e) {
16
+ return e.composedPath()[0] ?? null;
17
+ }
18
+ function n(e) {
19
+ return (n) => {
20
+ let r = e._clickOutside;
21
+ if (!r || r.options.disabled) return;
22
+ let i = t(n);
23
+ i && (e === i || e.contains(i) || r.options.ignore.some((e) => e != null && (e === i || e.contains(i))) || r.options.handler(n));
24
+ };
25
+ }
26
+ var r = {
27
+ mounted(t, r) {
28
+ let i = e(r), a = n(t);
29
+ t._clickOutside = {
30
+ options: i,
31
+ listener: a
32
+ }, document.addEventListener("pointerdown", a, { passive: !0 });
33
+ },
34
+ updated(t, n) {
35
+ t._clickOutside && (t._clickOutside.options = e(n));
36
+ },
37
+ unmounted(e) {
38
+ e._clickOutside && (document.removeEventListener("pointerdown", e._clickOutside.listener), delete e._clickOutside);
39
+ }
40
+ }, i = { install(e) {
41
+ e.directive("click-outside", r);
42
+ } };
43
+ //#endregion
44
+ export { i as ClickOutsidePlugin, r as vClickOutside };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.VClickOutside={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){if(typeof e.value==`function`)return{handler:e.value,ignore:[],disabled:!1};if(e.value!==null&&typeof e.value==`object`&&typeof e.value.handler==`function`)return{handler:e.value.handler,ignore:e.value.ignore??[],disabled:e.value.disabled??!1};throw TypeError(`[v-click-outside] value must be a Function or { handler: Function, ignore?: HTMLElement[], disabled?: boolean }`)}function n(e){return e.composedPath()[0]??null}function r(e){return t=>{let r=e._clickOutside;if(!r||r.options.disabled)return;let i=n(t);i&&(e===i||e.contains(i)||r.options.ignore.some(e=>e!=null&&(e===i||e.contains(i)))||r.options.handler(t))}}var i={mounted(e,n){let i=t(n),a=r(e);e._clickOutside={options:i,listener:a},document.addEventListener(`pointerdown`,a,{passive:!0})},updated(e,n){e._clickOutside&&(e._clickOutside.options=t(n))},unmounted(e){e._clickOutside&&(document.removeEventListener(`pointerdown`,e._clickOutside.listener),delete e._clickOutside)}};e.ClickOutsidePlugin={install(e){e.directive(`click-outside`,i)}},e.vClickOutside=i});
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "vue3-click-outside-directive",
3
+ "version": "1.0.0",
4
+ "description": "Vue 3 directive that fires when a click is registered outside the element",
5
+ "type": "module",
6
+ "main": "./dist/v-click-outside.umd.js",
7
+ "module": "./dist/v-click-outside.es.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/v-click-outside.es.js",
12
+ "require": "./dist/v-click-outside.umd.js",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "vite build",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
23
+ "test:coverage": "vitest run --coverage",
24
+ "typecheck": "tsc --noEmit"
25
+ },
26
+ "keywords": [
27
+ "vue",
28
+ "vue3",
29
+ "directive",
30
+ "click-outside",
31
+ "nuxt"
32
+ ],
33
+ "license": "MIT",
34
+ "peerDependencies": {
35
+ "vue": "^3.0.0"
36
+ },
37
+ "devDependencies": {
38
+ "@vitest/coverage-v8": "^4.1.5",
39
+ "@vue/test-utils": "^2.4.10",
40
+ "happy-dom": "^20.9.0",
41
+ "typescript": "^6.0.3",
42
+ "vite": "^8.0.11",
43
+ "vite-plugin-dts": "^5.0.0",
44
+ "vitest": "^4.1.5",
45
+ "vue": "^3.5.34"
46
+ }
47
+ }