@lui-ui/lui-vue
Version:
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
14 lines (13 loc) • 452 B
JavaScript
import { onMounted, onUnmounted } from "vue";
import { isClient } from "../utils/isClient.mjs";
export function useOutsideClick(ref, callback) {
function handleClick(event) {
if (event.target instanceof Node && !ref.value?.contains(event?.target))
callback();
}
onMounted(() => {
if (isClient())
document.addEventListener("click", handleClick);
});
onUnmounted(() => document.removeEventListener("click", handleClick));
}