Skip to content

useClickOutside

点击指定元素以外的区域

❣️view the code❣️

源码

ts
import { Ref } from 'vue';
import { useEventListener } from './useEventListener';

export function useClickOutside(ref: Ref, handler: (event: MouseEvent) => void) {
  const listener = (event: MouseEvent) => {
    if (!ref.value || ref.value.contains(event.target as HTMLElement)) return;

    handler(event);
  };
  useEventListener(document, 'click', listener);
  return;
}

参数

参数类型说明默认值
refRefDOM元素-
handler(event: MouseEvent) => void回调函数-