Forked from disgusting-dev/vue-directive-clickOutside.js
Last active
December 5, 2022 16:58
-
-
Save clockwiser/9077709911e7dbc08a10655df9f1b266 to your computer and use it in GitHub Desktop.
Change directive definition for vue 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = ['click']; | |
function onClickOutside({ event, el, handler, middleware }) { | |
const isClickOutside = | |
event.target !== el | |
&& !el.contains(event.target); | |
if (!isClickOutside || !middleware(event, el)) { | |
return null; | |
} | |
return handler(event, el); | |
} | |
const instances = new Map(); | |
//Requires loop to toggle events for several listeners of an element | |
function toggleEventListeners(eventHandlers) { | |
return (action) => { | |
eventHandlers.forEach(({ event, handler }) => { | |
document[`${action}EventListener`](event, handler, true); | |
}) | |
} | |
} | |
//Validator function | |
function processArgs(value) { | |
const isFunction = typeof value === 'function'; | |
if (!isFunction && typeof value !== 'object') { | |
throw new Error(`v-click-outside: Binding value should be a function or an object, ${typeof bindingValue} given`) | |
} | |
return { | |
handler: isFunction ? value : value.handler, | |
middleware: value.middleware || (() => true), | |
} | |
} | |
//Now need adapter to handle several events for one Map element | |
function eventAdapter(events, { el, handler, middleware }) { | |
return events.map((eventName) => ({ | |
event: eventName, | |
handler: (event) => onClickOutside({ event, el, handler, middleware }) | |
})); | |
} | |
function bind(el, { value }) { | |
const { handler, middleware } = processArgs(value); | |
const eventHandlers = eventAdapter(events, { el, handler, middleware }); | |
instances.set( | |
el, | |
eventHandlers, | |
); | |
toggleEventListeners(eventHandlers)('add'); | |
} | |
function unbind(el) { | |
const eventHandlers = instances.get(el); | |
toggleEventListeners(eventHandlers)('remove'); | |
instances.delete(el); | |
} | |
const directive = { | |
beforeMount:bind, | |
unmounted:unbind, | |
}; | |
export default directive; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#Setting global custom directive in Vue 3.
import clickOutside from './directive/vue-directive-clickOutside'
createApp(App).directive('click-outside', clickOutside)