delegate-it
Advanced tools
Comparing version 3.0.0 to 3.0.1
import type { ParseSelector } from 'typed-query-selector/parser'; | ||
export declare type DelegateOptions = boolean | Omit<AddEventListenerOptions, 'once'>; | ||
export declare type EventType = keyof GlobalEventHandlersEventMap; | ||
@@ -15,5 +16,6 @@ declare type GlobalEvent = Event; | ||
* Delegates event to a selector. | ||
* @param options A boolean value setting options.capture or an options object of type AddEventListenerOptions without the `once` option | ||
*/ | ||
declare function delegate<Selector extends string, TElement extends Element = ParseSelector<Selector, HTMLElement>, TEventType extends EventType = EventType>(base: EventTarget | Document | ArrayLike<Element> | string, selector: Selector, type: TEventType, callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>, options?: boolean | AddEventListenerOptions): delegate.Subscription; | ||
declare function delegate<TElement extends Element = HTMLElement, TEventType extends EventType = EventType>(base: EventTarget | Document | ArrayLike<Element> | string, selector: string, type: TEventType, callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>, options?: boolean | AddEventListenerOptions): delegate.Subscription; | ||
declare function delegate<Selector extends string, TElement extends Element = ParseSelector<Selector, HTMLElement>, TEventType extends EventType = EventType>(base: EventTarget | Document | ArrayLike<Element> | string, selector: Selector, type: TEventType, callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>, options?: DelegateOptions): delegate.Subscription; | ||
declare function delegate<TElement extends Element = HTMLElement, TEventType extends EventType = EventType>(base: EventTarget | Document | ArrayLike<Element> | string, selector: string, type: TEventType, callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>, options?: DelegateOptions): delegate.Subscription; | ||
export default delegate; |
12
index.js
@@ -48,5 +48,3 @@ /** Keeps track of raw listeners added to the base elements to avoid duplication */ | ||
if (!isEventTarget(base)) { | ||
const subscriptions = Array.prototype.map.call(base, (element) => { | ||
return delegate(element, selector, type, callback, options); | ||
}); | ||
const subscriptions = Array.prototype.map.call(base, (element) => delegate(element, selector, type, callback, options)); | ||
return { | ||
@@ -57,3 +55,3 @@ destroy() { | ||
} | ||
} | ||
}, | ||
}; | ||
@@ -72,2 +70,6 @@ } | ||
}; | ||
// Drop unsupported `once` option https://github.com/fregante/delegate-it/pull/28#discussion_r863467939 | ||
if (typeof options === 'object') { | ||
delete options.once; | ||
} | ||
const setup = JSON.stringify({ selector, type, capture }); | ||
@@ -79,3 +81,3 @@ const isAlreadyListening = editLedger(true, baseElement, callback, setup); | ||
editLedger(false, baseElement, callback, setup); | ||
} | ||
}, | ||
}; | ||
@@ -82,0 +84,0 @@ if (!isAlreadyListening) { |
{ | ||
"name": "delegate-it", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Lightweight and modern event delegation in the browser", | ||
@@ -41,20 +41,19 @@ "keywords": [ | ||
"rules": { | ||
"import/extensions": "off", | ||
"import/no-useless-path-segments": "off", | ||
"max-params": "off", | ||
"unicorn/import-index": "off" | ||
"@typescript-eslint/no-namespace": "off", | ||
"@typescript-eslint/naming-convention": "off" | ||
} | ||
}, | ||
"dependencies": { | ||
"typed-query-selector": "^2.4.1" | ||
"typed-query-selector": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^1.0.1", | ||
"ava": "^3.15.0", | ||
"jsdom": "^16.5.2", | ||
"@sindresorhus/tsconfig": "^2.0.0", | ||
"ava": "^4.2.0", | ||
"jsdom": "^19.0.0", | ||
"npm-run-all": "^4.1.5", | ||
"sinon": "^10.0.0", | ||
"typescript": "^4.2.4", | ||
"xo": "^0.38.2" | ||
"sinon": "^14.0.0", | ||
"typescript": "^4.6.4", | ||
"xo": "^0.48.0" | ||
} | ||
} |
@@ -55,2 +55,19 @@ # delegate-it [![][badge-gzip]][link-bundlephobia] | ||
#### With listener options | ||
```js | ||
delegate(document.body, '.btn', 'click', event => { | ||
console.log(event.delegateTarget); | ||
}, true); | ||
// Or equivalent: | ||
delegate(document.body, '.btn', 'click', event => { | ||
console.log(event.delegateTarget); | ||
}, { | ||
capture: true | ||
}); | ||
``` | ||
**Note:** the `once` option is currently not supported. | ||
### Remove event delegation | ||
@@ -57,0 +74,0 @@ |
10917
107
114
Updatedtyped-query-selector@^2.6.1