vue3-click-away is a Vue 3.x compatible directive designed to detect click events that occur outside of a specified element. It provides a simple mechanism for components to react when a user clicks anywhere on the document except on the element itself or its children. Currently at version 1.2.4, the package sees irregular but active maintenance, primarily addressing critical fixes such as recent server-side rendering (SSR) compatibility issues. Key differentiators include its explicit support for Vue 3's plugin system, direct directive registration, and a mixin option, offering flexibility in integration depending on the project's setup and preference. It ships with TypeScript types, facilitating its use in TypeScript-first Vue projects. Its primary goal is to abstract away the complexity of managing global click listeners for 'click away' scenarios.
npm install vue3-click-awayVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the global registration of vue3-click-away as a Vue plugin and its basic usage within a component to detect clicks outside a specific element, incrementing a counter upon detection.
For global usage, import `VueClickAway` (default export) and use `app.use(VueClickAway)`. For local component registration, import `directive` (named export) and use `directives: { ClickAway: directive }`.Ensure you are using version 1.2.4 or newer. If issues persist in SSR, review the SSR configuration of your Vue application and ensure `v-click-away` is appropriately handled or rendered only on the client-side where necessary.
Ensure your handler function is defined to accept a single argument, which will be the native `MouseEvent`. E.g., `onClickAway(event: MouseEvent) { /* ... */ }`.Globally register the directive using `app.use(VueClickAway)` where `VueClickAway` is the default import. Alternatively, locally register it within your component's `directives` option: `import { directive } from 'vue3-click-away'; directives: { ClickAway: directive }`.Ensure `VueClickAway` is imported as the default export (`import VueClickAway from 'vue3-click-away'`) and that `app.use()` is called on a valid Vue 3 application instance created with `createApp()`.
Update `vue3-click-away` to the latest version (1.2.4+). If the problem persists, consider wrapping the component using `v-click-away` in a `<ClientOnly>` component (if your SSR framework supports it) to ensure the directive is only active on the client-side.