UNPKG

tiny-event-intercept

Version:

Lightweight (~1.0KB) TypeScript library for conditional event interception with browser-standard API. Zero dependencies, SSR compatible, robust edge case handling.

39 lines 1.43 kB
//#region src/types.d.ts /** * 支持的事件目标类型 */ type EventTarget = Element | Document | Window; /** * 目标元素类型:支持直接传入元素或返回元素的函数 */ type TargetElement = EventTarget | (() => EventTarget | null) | null; /** * 清理函数类型 */ type CleanupFunction = () => void; /** * 事件类型:支持单个事件或事件数组 */ type EventTypes = keyof GlobalEventHandlersEventMap | readonly (keyof GlobalEventHandlersEventMap)[]; /** * 拦截选项,扩展浏览器标准的 AddEventListenerOptions */ interface InterceptOptions extends AddEventListenerOptions { /** 要拦截的事件类型,支持单个事件或事件数组 */ readonly events: EventTypes; /** 拦截条件函数,返回 true 时拦截事件 */ readonly when: () => boolean; /** 事件监听器函数 */ readonly listener: EventListener; } //#endregion //#region src/index.d.ts /** * 事件拦截器主函数 * @param target 目标元素,支持元素、函数或 null(默认 document) * @param options 拦截选项,包含事件类型、条件函数、监听器等 * @returns 清理函数,用于移除所有事件监听器 */ declare function interceptEvents(target: TargetElement, options: InterceptOptions): CleanupFunction; //#endregion export { type CleanupFunction, type EventTarget, type EventTypes, type InterceptOptions, type TargetElement, interceptEvents };