vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
58 lines (57 loc) • 2.32 kB
TypeScript
import type { VoidFunction } from './shared/types';
/**
* Creates a function that triggers the given handler if the current event target
* matches the given selector
*
* @param selector - CSS Selector that matches the element to delegate the event to
* @param handler - Handler to trigger if selector selector match
* @return The delegate event handler
*
* @example
*
* ```ts
* const handler = delegateHandler('.my-element', (e: Event) => {});
* document.addEventHandler('click', handler);
* ```
*/
export declare function delegateHandler(selector: string, handler: EventListenerOrEventListenerObject): EventListener;
/**
* Bind a delegated event handler for one or more event names to document.
*
* @param eventNames - Event names to bind the handler to
* @param selector - CSS Selector that matches the element to delegate the event to
* @param handler - Handler to bind to the event
* @return A function that removes the event selector handler again
*
* @example
*
* ```ts
* // Bind to a single event
* const removeDelegate = delegate('click', '.my-element', (e: Event) => {});
*
* // Bind to multiple events
* const removeDelegates = delegate(['click', 'mouseenter'], '.my-element', (e: Event) => {});
* ```
*/
declare function delegate(eventNames: string | string[], selector: string, handler: EventListenerOrEventListenerObject, options?: EventListenerOptions): VoidFunction;
/**
* Bind a delegated event handler for one or more event names to a DOM element.
*
* @param elm - DOM element to unbind the event from
* @param eventNames - Event names to bind the handler to
* @param selector - CSS Selector that matches the element to delegate the event to
* @param handler - Handler to bind to the event
* @return A function that removes the event selector handler again
*
* @example
*
* ```ts
* // Bind to a single event
* const removeDelegate = delegate(element, 'click', '.my-element', (e: Event) => {});
*
* // Bind to multiple events
* const removeDelegates = delegate(element, ['click', 'mouseenter'], '.my-element', (e: Event) => {});
* ```
*/
declare function delegate(target: EventTarget, eventNames: string | string[], selector: string, handler: EventListenerOrEventListenerObject, options?: EventListenerOptions): VoidFunction;
export default delegate;