delegate-it
Advanced tools
Comparing version 1.1.0-7 to 1.1.0
@@ -9,3 +9,3 @@ declare namespace delegate { | ||
type: EventType; | ||
useCapture?: boolean | AddEventListenerOptions; | ||
capture: boolean; | ||
}; | ||
@@ -17,5 +17,5 @@ type DelegateEventHandler<TEvent extends Event = Event, TElement extends Element = Element> = (event: DelegateEvent<TEvent, TElement>) => void; | ||
} | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, useCapture?: boolean | AddEventListenerOptions): delegate.DelegateSubscription; | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(elements: EventTarget | Document, selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, useCapture?: boolean | AddEventListenerOptions): delegate.DelegateSubscription; | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(elements: ArrayLike<Element> | string, selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, useCapture?: boolean | AddEventListenerOptions): delegate.DelegateSubscription[]; | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, options?: boolean | AddEventListenerOptions): delegate.DelegateSubscription; | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(elements: EventTarget | Document, selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, options?: boolean | AddEventListenerOptions): delegate.DelegateSubscription; | ||
declare function delegate<TElement extends Element = Element, TEvent extends Event = Event>(elements: ArrayLike<Element> | string, selector: string, type: delegate.EventType, callback: delegate.DelegateEventHandler<TEvent, TElement>, options?: boolean | AddEventListenerOptions): delegate.DelegateSubscription[]; | ||
export = delegate; |
22
index.js
"use strict"; | ||
const elements = new WeakMap(); | ||
function _delegate(element, selector, type, callback, useCapture) { | ||
function _delegate(element, selector, type, callback, options) { | ||
const capture = Boolean(typeof options === 'object' ? options.capture : options); | ||
const listenerFn = (event) => { | ||
@@ -18,3 +19,3 @@ const delegateTarget = event.target.closest(selector); | ||
destroy() { | ||
element.removeEventListener(type, listenerFn, useCapture); | ||
element.removeEventListener(type, listenerFn, options); | ||
if (!elements.has(element)) { | ||
@@ -34,3 +35,3 @@ return; | ||
setup.type !== type || | ||
setup.useCapture === useCapture) { | ||
setup.capture === capture) { | ||
continue; | ||
@@ -51,3 +52,3 @@ } | ||
setup.type === type && | ||
setup.useCapture === useCapture) { | ||
setup.capture === capture) { | ||
return delegateSubscription; | ||
@@ -57,5 +58,5 @@ } | ||
// Remember event in tree | ||
elements.set(element, elementMap.set(callback, setups.add({ selector, type, useCapture }))); | ||
elements.set(element, elementMap.set(callback, setups.add({ selector, type, capture }))); | ||
// Add event on delegate | ||
element.addEventListener(type, listenerFn, useCapture); | ||
element.addEventListener(type, listenerFn, options); | ||
return delegateSubscription; | ||
@@ -66,7 +67,6 @@ } | ||
*/ | ||
// eslint-disable-next-line no-redeclare | ||
function delegate(elements, selector, type, callback, useCapture) { | ||
function delegate(elements, selector, type, callback, options) { | ||
// Handle the regular Element usage | ||
if (elements instanceof EventTarget) { | ||
return _delegate(elements, selector, type, callback, useCapture); | ||
if (typeof elements.addEventListener === 'function') { | ||
return _delegate(elements, selector, type, callback, options); | ||
} | ||
@@ -83,3 +83,3 @@ // Handle Element-less usage, it defaults to global delegation | ||
return Array.prototype.map.call(elements, (element) => { | ||
return _delegate(element, selector, type, callback, useCapture); | ||
return _delegate(element, selector, type, callback, options); | ||
}); | ||
@@ -86,0 +86,0 @@ } |
{ | ||
"name": "delegate-it", | ||
"description": "Lightweight event delegation", | ||
"version": "1.1.0-7", | ||
"version": "1.1.0", | ||
"repository": "bfred-it/delegate-it", | ||
@@ -17,10 +17,11 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^0.2.1", | ||
"@typescript-eslint/eslint-plugin": "^1.4.2", | ||
"ava": "^1.3.1", | ||
"eslint-config-xo-typescript": "^0.8.0", | ||
"jsdom": "^13.2.0", | ||
"@sindresorhus/tsconfig": "^0.4.0", | ||
"@typescript-eslint/eslint-plugin": "^1.6.0", | ||
"@typescript-eslint/parser": "^1.11.0", | ||
"ava": "^2.1.0", | ||
"eslint-config-xo-typescript": "^0.14.0", | ||
"jsdom": "^15.1.1", | ||
"npm-run-all": "^4.1.5", | ||
"sinon": "^1.17.6", | ||
"typescript": "^3.3.3333", | ||
"sinon": "^7.3.2", | ||
"typescript": "^3.4.3", | ||
"xo": "*" | ||
@@ -27,0 +28,0 @@ }, |
@@ -1,3 +0,7 @@ | ||
# delegate-it [![Build Status](https://api.travis-ci.com/bfred-it/delegate-it.svg?branch=master)](https://travis-ci.com/bfred-it/delegate-it) | ||
# delegate-it [![(size)][badge-gzip]](#no-link) [![(status)][badge-travis]][link-travis] | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/delegate-it.svg?label=gzipped | ||
[badge-travis]: https://api.travis-ci.com/bfred-it/delegate-it.svg | ||
[link-travis]: https://travis-ci.org/bfred-it/delegate-it | ||
> Lightweight event delegation | ||
@@ -7,5 +11,5 @@ | ||
- debugged ([2d54c11](https://github.com/bfred-it/delegate-it/commit/2d54c1182aefd3ec9d8250fda76290971f5d7166), [c6bb88c](https://github.com/bfred-it/delegate-it/commit/c6bb88c2aa8097b25f22993a237cf09c96bcbfb8)) | ||
- modern: ES6, TypeScript, Edge 15+ (it uses `WeakMap` and `Element.closest()`) | ||
- idempotent: identical listeners aren't added multiple times, just like the native `addEventListener` | ||
- debugged ([2d54c11](https://github.com/bfred-it/delegate-it/commit/2d54c1182aefd3ec9d8250fda76290971f5d7166), [c6bb88c](https://github.com/bfred-it/delegate-it/commit/c6bb88c2aa8097b25f22993a237cf09c96bcbfb8)) | ||
@@ -103,3 +107,3 @@ If you need IE support, you can keep using [`delegate`](https://github.com/zenorocha/delegate) | ||
```js | ||
import { EventType } from "delegate-it"; | ||
import {EventType} from "delegate-it"; | ||
@@ -116,1 +120,8 @@ const someEventType1: EventType = 'details:toggled'; // all good | ||
| Latest ✔ | Latest ✔ | Latest ✔ | No ✕ | Latest ✔ | Latest ✔ | | ||
## Related | ||
- [select-dom](https://github.com/bfred-it/select-dom) - Lightweight `querySelector`/`All` wrapper that outputs an Array. | ||
- [doma](https://github.com/bfred-it/doma) - Parse an HTML string into `DocumentFragment` or one `Element`, in a few bytes. | ||
- [Refined GitHub](https://github.com/sindresorhus/refined-github) - Uses this module. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10737
1
125
10