应用场景
以前遇到过一个需求。一个按钮触发显示/隐藏一个悬浮框,点击悬浮框其他地方可以触发clickOutSide 事件隐藏悬浮框,所以按钮应该例外。
ClickOutSide应该比较常用,下面简单贴一下clickOutSide工具类代码
import type { ComponentPublicInstance, DirectiveBinding, ObjectDirective } from 'vue';
type DocumentHandler = <T extends MouseEvent>(mouseup: T, mousedown: T) => void;
type FlushList = Map<
HTMLElement,
{
documentHandler: DocumentHandler;
bindingFn: (...args: unknown[]) => unknown;
}
>;
const on = function (
element: HTMLElement | Document | Window,
event: string,
handler: EventListenerOrEventListenerObject,
useCapture = false,
): void {
if (element && event && handler) {
element.addEventListener(event, handler, useCapture);
}
};
const nodeList: FlushList = new Map();
let startClick: MouseEvent;
on(document, 'mousedown', (e) => (startClick = e as MouseEvent));
on(document, 'mouseup', (e) => {
for (const { documentHandler } of nodeList.values()) {
documentHandler(e as MouseEvent, startClick);
}
});
function createDocumentHandler(el: HTMLElement, binding: DirectiveBinding): DocumentHandler {
let excludes: HTMLElement[] = [];
if (Array.isArray(binding.arg)) {
excludes = binding.arg;
} else {
// due to current implementation on binding type is wrong the type casting is necessary here
excludes.push(binding.arg as unknown as HTMLElement);
}
return function (mouseup, mousedown) {
const popperRef = (
binding.instance as ComponentPublicInstance<{
popperRef: HTMLElement;
}>
).popperRef;
const mouseUpTarget = mouseup.target as Node;
const mouseDownTarget = mousedown.target as Node;
const isBound = !binding || !binding.instance;
const isTargetExists = !mouseUpTarget || !mouseDownTarget;
const isContainedByEl = el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
const isSelf = el === mouseUpTarget;
const isTargetExcluded =
(excludes.length && excludes.some((item) => item?.contains(mouseUpTarget))) ||
(excludes.length && excludes.includes(mouseDownTarget as HTMLElement));
const isContainedByPopper =
popperRef && (popperRef.contains(mouseUpTarget) || popperRef.contains(mouseDownTarget));
if (
isBound ||
isTargetExists ||
isContainedByEl ||
isSelf ||
isTargetExcluded ||
isContainedByPopper
) {
return;
}
binding.value();
};
}
export const ClickOutside: ObjectDirective = {
beforeMount(el, binding) {
nodeList.set(el, {
documentHandler: createDocumentHandler(el, binding),
bindingFn: binding.value,
});
},
updated(el, binding) {
nodeList.set(el, {
documentHandler: createDocumentHandler(el, binding),
bindingFn: binding.value,
});
},
unmounted(el) {
nodeList.delete(el);
},
};
设置响应例外的元素(触发的按钮)
直接贴代码 自取
该处的selectCamere 指的就是触发的按钮DIV容器。
hideFilter 是clickoutside 触发的函数.
v-click-outside:[selectCamera]=“hideFilter”
该写法可以过滤selectCamera 容器,点击该按钮不触发hideFilter 方法