UNPKG

@relaxed/vue-directives

Version:

vue 兼容2 3 工具库 函数库

65 lines (60 loc) 1.36 kB
// src/compatible/isVue3.ts var isVue3 = (app) => "config" in app && "globalProperties" in app.config; // src/compatible/hookKey.ts var getHooks = (app) => { return isVue3(app) ? { created: "created", mounted: "mounted", updated: "updated", unMounted: "unmounted" } : { created: "bind", mounted: "inserted", updated: "updated", unMounted: "unbind" }; }; // src/directives/clickoutside/src/v-clickoutside.ts var clickoutside = (el, value) => { document.addEventListener( "click", (e) => { !el.contains(e.target) && value(); }, false ); }; var deleteClickOutside = () => { document.removeEventListener("click", () => { }); }; // src/directives/clickoutside/src/options.ts var DEFAULT_PLUGIN_OPTIONS = { directive: "clickoutside" }; // src/directives/clickoutside/index.ts var VClickOutSideDirective = (app) => { const hooks = getHooks(app); const globalOptions = { ...DEFAULT_PLUGIN_OPTIONS }; app.directive(globalOptions.directive, { [hooks.mounted](el, { value }) { clickoutside(el, value); }, [hooks.updated](el, { value }) { clickoutside(el, value); }, [hooks.unMounted]() { deleteClickOutside(); } }); }; var VClickoutside = { install: function(app, options) { VClickOutSideDirective(app); } }; export { VClickoutside };