UNPKG

@hypothesis/frontend-shared

Version:

Shared components, styles and utilities for Hypothesis projects

34 lines (33 loc) 1.35 kB
export type Listener = { eventTarget: EventTarget; eventType: string; listener: EventListener; options?: AddEventListenerOptions; }; /** * Return the event type that a listener will receive. * * For example `EventType<HTMLElement, 'keydown'>` evaluates to `KeyboardEvent`. * * The event type is extracted from the target's `on${Type}` property (eg. * `HTMLElement.onkeydown` here) If there is no such property, the type defaults * to `Event`. */ export type EventType<Target extends EventTarget, TypeName extends string> = `on${TypeName}` extends keyof Target ? Target[`on${TypeName}`] extends ((...args: any[]) => void) | null ? Parameters<NonNullable<Target[`on${TypeName}`]>>[0] : Event : Event; /** * Utility that provides a way to conveniently remove a set of DOM event * listeners when they are no longer needed. */ export declare class ListenerCollection { private _listeners; constructor(); /** * Add a listener and return an ID that can be used to remove it later */ add<ListenerTarget extends EventTarget, Type extends string>(eventTarget: ListenerTarget, eventType: Type, listener: (event: EventType<ListenerTarget, Type>) => void, options?: AddEventListenerOptions): symbol; /** * Remove a specific listener. */ remove(listenerId: symbol): void; removeAll(): void; }