UNPKG

ranui

Version:

A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.

56 lines (55 loc) 2.17 kB
/** * EventManager — lifecycle-scoped event registry backed by AbortController. * * Web Component usage: * * private _events = new EventManager(); * * connectedCallback() { * this._events * .on(this._input, 'input', this.handleInput) * .on(this, 'click', this.handleClick, { capture: true }); * } * * disconnectedCallback() { * this._events.abort(); // removes all listeners, resets for next connect * } * * Page development usage: * * function initSection(container: HTMLElement) { * const scope = new EventManager(); * * scope * .on(input, 'input', handleSearch) * .delegate(container, '[data-action]', 'click', (ev, target) => { * handleAction(target.getAttribute('data-action')); * }); * * return () => scope.abort(); // call on section teardown * } */ export declare class EventManager { private ac; constructor(); /** The underlying AbortSignal — pass to addEventListener options directly if needed. */ get signal(): AbortSignal; on<K extends keyof HTMLElementEventMap>(target: HTMLElement, type: K, handler: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: Omit<AddEventListenerOptions, 'signal'>): this; on(target: EventTarget, type: string, handler: EventListener, options?: Omit<AddEventListenerOptions, 'signal'>): this; /** * Event delegation — attach one listener to `parent`, fire `handler` only when * the event originates from a descendant matching `selector`. * * The handler receives the original event and the matched element as arguments. * * scope.delegate(list, '.item', 'click', (ev, item) => { * console.log(item.dataset.id); * }); */ delegate<K extends keyof HTMLElementEventMap>(parent: HTMLElement, selector: string, type: K, handler: (ev: HTMLElementEventMap[K], target: Element) => void, options?: Omit<AddEventListenerOptions, 'signal'>): this; /** * Remove all registered listeners and reset internal AbortController. * Safe to call multiple times; next on() / delegate() calls start fresh. */ abort(): void; }