Compare commits

...

4 commits

Author SHA1 Message Date
vlw
7f91b8717c chore: bump npm package version 2024-09-18 12:30:08 +00:00
6b7a88fd3d Add license headers and package reference (#2)
This PR adds a license reference to the NPM package declaration, and also adds license headers to the JS module.

Reviewed-on: https://codeberg.org/vlw/elevent/pulls/2
Co-authored-by: Victor Westerlund <victor.vesterlund@gmail.com>
Co-committed-by: Victor Westerlund <victor.vesterlund@gmail.com>
2024-09-18 12:28:45 +00:00
vlw
96c3dfc868 fix(versioning): package version 2024-09-16 13:46:53 +00:00
68116c3b09 Replace HTMLCollection with NodeList (#1)
I had some weird notion that `querySelectorAll` would return an `HTMLCollection` and not a `NodeList`.

```js
[...document.querySelectorAll()].forEach(element => {});
```
> Maybe I was doing this for no reason before, since `NodeList` already inherits the Array `forEach()` method...

Reviewed-on: https://codeberg.org/vlw/elevent/pulls/1
Co-authored-by: Victor Westerlund <victor.vesterlund@gmail.com>
Co-committed-by: Victor Westerlund <victor.vesterlund@gmail.com>
2024-09-13 11:49:15 +00:00
3 changed files with 15 additions and 12 deletions

View file

@ -10,7 +10,7 @@ new Elevent("click", document.querySelector("#myButton"), () => console.log("But
# Examples
## Bind from an HTMLCollection
## Bind from a NodeList
```js
new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent
```
@ -43,7 +43,7 @@ elevent.bind(document.querySelector("#bindMe"));
```ts
Elevent.constructor(
eventType: string | null
target: HTMLCollection | HTMLElement | string | null,
target: NodeList | HTMLElement | string | null,
callback: CallableFunction
)
```

View file

@ -1,8 +1,9 @@
{
"name": "elevent",
"version": "1.0.0",
"version": "1.0.2",
"main": "src/Elevent.mjs",
"type": "module",
"license": "GPL-3.0-or-later",
"exports": {
".": {
"import": {

View file

@ -1,3 +1,4 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
export class Elevent {
_type;
_callback;
@ -10,7 +11,7 @@ export class Elevent {
/**
* Create a new Elevent instance.
* @param {string|null} eventType
* @param {HTMLCollection|HTMLElement|string|null} target Strings will be treated as CSS selectors, null will create a template instance
* @param {NodeList|HTMLElement|string|null} target Strings will be treated as CSS selectors, null will create a template instance
* @param {function} callback
*/
constructor(eventType = null, target = null, callback) {
@ -19,13 +20,13 @@ export class Elevent {
if (target) {
switch (target.constructor) {
case HTMLCollection:
this.#bindHTMLCollection(target);
case NodeList:
this.#bindNodeList(target);
break;
case String:
this._selector = target;
this.#bindHTMLCollection(document.querySelectorAll(target));
this.#bindNodeList(document.querySelectorAll(target));
// Automatically bind new DOM elements that match the provided CSS selector string
this._observer.observe(document.body, {
@ -57,11 +58,11 @@ export class Elevent {
}
/**
* Bind all HTMLElements inside an HTMLCollection
* @param {HTMLCollection} collection
* Bind all HTMLElements inside a NodeList
* @param {NodeList} nodeList
*/
#bindHTMLCollection(collection) {
[...collection].forEach(element => this.bind(element));
#bindNodeList(nodeList) {
nodeList.forEach(element => this.bind(element));
}
/**
@ -99,4 +100,5 @@ export class Elevent {
this._boundElements = new WeakSet();
}
}
}
// @license-end