@w-lfpup/wctk 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -12
- package/dist/events.d.ts +4 -4
- package/dist/microtask.d.ts +1 -1
- package/dist/microtask.js +7 -6
- package/docs/events.md +7 -11
- package/docs/microtask.md +2 -2
- package/docs/query_selector.md +1 -1
- package/docs/wc.md +2 -2
- package/examples/counter/index.html +1 -1
- package/examples/counter/mod.js +1 -1
- package/examples/counter/mod.ts +1 -1
- package/examples/form_associated/text_input.js +1 -1
- package/examples/form_associated/text_input.ts +2 -1
- package/examples/stopwatch/index.html +1 -1
- package/examples/stopwatch/stopwatch.ts +1 -1
- package/package.json +4 -3
- package/src/events.ts +10 -8
- package/src/microtask.ts +8 -6
package/README.md
CHANGED
|
@@ -17,23 +17,23 @@ Four (4) controllers help developers:
|
|
|
17
17
|
- listen for [events](./docs/events.md)
|
|
18
18
|
- cache selector [queries](./docs/query_selector.md)
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
Controllers are flexible and not restricted to webcomponents. The can be used on any `HTMLElement`.
|
|
21
21
|
|
|
22
22
|
## Install
|
|
23
23
|
|
|
24
|
-
Install
|
|
24
|
+
Install directly from github.
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
npm install --save-dev
|
|
27
|
+
npm install --save-dev https://github.com/w-lfpup/wctk-js/
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Install with npm.
|
|
31
31
|
|
|
32
32
|
```bash
|
|
33
|
-
npm install --save-dev
|
|
33
|
+
npm install --save-dev @w-lfpup/wctk
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
## Create a
|
|
36
|
+
## Create a webcomponent
|
|
37
37
|
|
|
38
38
|
Add a `Wc` controller to a custom element with only one line
|
|
39
39
|
|
|
@@ -59,15 +59,17 @@ The following examples demonstrate several common SSR use cases:
|
|
|
59
59
|
|
|
60
60
|
## Design Goals
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
The `wctk` is a collection of bare-metal facades over vanilla browser apis. They provide the basics for
|
|
63
|
+
events, reactivity, and forms.
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
or tree-walks for your super cool custom button? Do you really need a framework for that checkbox?
|
|
65
|
+
If you know vanilla javascript and the DOM you are good to go.
|
|
66
66
|
|
|
67
|
-
The `wctk` is
|
|
67
|
+
The `wctk` is designed with SSR and declarative shadow dom in mind. Developers
|
|
68
|
+
can pick up what the HTML threw down with interactive SSR friendly webcomponents.
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
Templating is not provided. You can use lit-html or react or vue. But first consider the majority
|
|
71
|
+
of components only have a few moving pieces. Do you really need templating for that custom button?
|
|
72
|
+
Do you really need a flux-pattern for that checkbox?
|
|
71
73
|
|
|
72
74
|
## License
|
|
73
75
|
|
package/dist/events.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
interface GenericEventListener<E> {
|
|
1
|
+
interface GenericEventListener<E extends Event> {
|
|
2
2
|
(evt: E): void;
|
|
3
3
|
}
|
|
4
|
-
interface GenericEventListenerObject<E> {
|
|
4
|
+
interface GenericEventListenerObject<E extends Event> {
|
|
5
5
|
handleEvent(object: E): void;
|
|
6
6
|
}
|
|
7
|
-
type GenericCallbacks<E> = GenericEventListener<E> | GenericEventListenerObject<E>;
|
|
7
|
+
type GenericCallbacks<E extends Event> = GenericEventListener<E> | GenericEventListenerObject<E>;
|
|
8
8
|
type EventMap = Partial<{
|
|
9
9
|
[Property in keyof GlobalEventHandlersEventMap]: GenericCallbacks<GlobalEventHandlersEventMap[Property]>;
|
|
10
|
-
}>;
|
|
10
|
+
}> | Record<string, EventListenerOrEventListenerObject>;
|
|
11
11
|
interface EventElementInterface {
|
|
12
12
|
addEventListener: Element["addEventListener"];
|
|
13
13
|
removeEventListener: Element["removeEventListener"];
|
package/dist/microtask.d.ts
CHANGED
package/dist/microtask.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
export class Microtask {
|
|
2
2
|
#queued = false;
|
|
3
3
|
#callback;
|
|
4
|
-
queue = this.#queue.bind(this);
|
|
5
4
|
constructor(callback) {
|
|
6
5
|
this.#callback = callback;
|
|
7
6
|
}
|
|
7
|
+
queue = this.#queue.bind(this);
|
|
8
8
|
#queue() {
|
|
9
9
|
if (this.#queued)
|
|
10
10
|
return;
|
|
11
11
|
this.#queued = true;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
window.queueMicrotask(this.#microtask);
|
|
13
|
+
}
|
|
14
|
+
#microtask = this.#unboundMicrotask.bind(this);
|
|
15
|
+
#unboundMicrotask() {
|
|
16
|
+
this.#queued = false;
|
|
17
|
+
this.#callback();
|
|
17
18
|
}
|
|
18
19
|
}
|
package/docs/events.md
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
# Events Controller
|
|
2
2
|
|
|
3
|
-
Add event listeners to
|
|
3
|
+
Add event listeners to webcomponents.
|
|
4
4
|
|
|
5
5
|
## How to use
|
|
6
6
|
|
|
7
|
-
Add an `Events` controller to a
|
|
7
|
+
Add an `Events` controller to a webcomponent. Use a params object on instantiation.
|
|
8
8
|
|
|
9
9
|
### Params
|
|
10
10
|
|
|
11
|
-
An Events `params` object has
|
|
11
|
+
An Events `params` object has three properties:
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
interface EventParams {
|
|
15
|
-
listeners: Record<string, EventListenerOrEventListenerObject>;
|
|
16
15
|
connected?: boolean;
|
|
16
|
+
listeners: Record<string, EventListenerOrEventListenerObject>;
|
|
17
17
|
target: EventTarget;
|
|
18
18
|
}
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
The `Events` controller
|
|
22
|
-
|
|
23
|
-
Afterwards, the `Events` controller adds the listeners as event listeners on a `target` node.
|
|
21
|
+
The `Events` controller adds event listeners on a `target` node.
|
|
24
22
|
|
|
25
23
|
The `target` node can be a shadowRoot, a document, or the custom element itself.
|
|
26
24
|
|
|
27
|
-
If the `target` property is undefined, the `host` property is used as a fallback.
|
|
28
|
-
|
|
29
25
|
### Controller
|
|
30
26
|
|
|
31
27
|
Here is an example of using the `Events` controller.
|
|
@@ -64,7 +60,7 @@ class MyElement extends HTMLElement {
|
|
|
64
60
|
}
|
|
65
61
|
```
|
|
66
62
|
|
|
67
|
-
### Shortcut life
|
|
63
|
+
### Shortcut life-cycle methods
|
|
68
64
|
|
|
69
65
|
In the example below, the `connected` property is set to true and listeners are immediately added to the `target`.
|
|
70
66
|
|
|
@@ -74,8 +70,8 @@ import { Events, Wc } from "wctk";
|
|
|
74
70
|
class MyElement extends HTMLElement {
|
|
75
71
|
#wc = new Wc({ this: host });
|
|
76
72
|
#ec = new Events({
|
|
77
|
-
target: this.#wc.shadowRoot,
|
|
78
73
|
connected: true,
|
|
74
|
+
target: this.#wc.shadowRoot,
|
|
79
75
|
listeners: {
|
|
80
76
|
click: this.#onClick.bind(this),
|
|
81
77
|
keydown: this.#onKeyDown.bind(this),
|
package/docs/microtask.md
CHANGED
|
@@ -4,7 +4,7 @@ Add callbacks to the `microtask queue`.
|
|
|
4
4
|
|
|
5
5
|
## How to use
|
|
6
6
|
|
|
7
|
-
Add a `Microtask` controller to a
|
|
7
|
+
Add a `Microtask` controller to a webcomponent. Provide a callback.
|
|
8
8
|
|
|
9
9
|
Call `Microtask.queue()` to push the callback to the microtask queue.
|
|
10
10
|
|
|
@@ -29,4 +29,4 @@ class MyElement extends HTMLElement {
|
|
|
29
29
|
}
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
The `Microtask.queue()` method can be called multiple times per event loop but the callback will only be called _once_
|
|
32
|
+
The `Microtask.queue()` method can be called multiple times per event loop but the callback will only be called _once_ during the microtaskqueue phase of the event-loop.
|
package/docs/query_selector.md
CHANGED
package/docs/wc.md
CHANGED
package/examples/counter/mod.js
CHANGED
|
@@ -2,8 +2,8 @@ import { Wc, Events } from "wctk";
|
|
|
2
2
|
class Counter extends HTMLElement {
|
|
3
3
|
#wc = new Wc({ host: this });
|
|
4
4
|
#ev = new Events({
|
|
5
|
-
target: this.#wc.shadowRoot,
|
|
6
5
|
connected: true,
|
|
6
|
+
target: this.#wc.shadowRoot,
|
|
7
7
|
listeners: {
|
|
8
8
|
click: this.#clickHandler.bind(this),
|
|
9
9
|
},
|
package/examples/counter/mod.ts
CHANGED
|
@@ -3,8 +3,8 @@ export class TextInput extends HTMLElement {
|
|
|
3
3
|
static formAssociated = true;
|
|
4
4
|
#wc = new Wc({ host: this });
|
|
5
5
|
#ev = new Events({
|
|
6
|
-
target: this.#wc.shadowRoot,
|
|
7
6
|
connected: true,
|
|
7
|
+
target: this.#wc.shadowRoot,
|
|
8
8
|
listeners: {
|
|
9
9
|
change: this.#changeHandler.bind(this),
|
|
10
10
|
},
|
|
@@ -8,9 +8,10 @@ export class TextInput extends HTMLElement {
|
|
|
8
8
|
static formAssociated = true;
|
|
9
9
|
|
|
10
10
|
#wc = new Wc({ host: this });
|
|
11
|
+
|
|
11
12
|
#ev = new Events({
|
|
12
|
-
target: this.#wc.shadowRoot,
|
|
13
13
|
connected: true,
|
|
14
|
+
target: this.#wc.shadowRoot,
|
|
14
15
|
listeners: {
|
|
15
16
|
change: this.#changeHandler.bind(this),
|
|
16
17
|
},
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
// This example uses window.requestAnimationFrame.
|
|
7
7
|
// Multiple stopwatches means multiple animation frame requests.
|
|
8
|
-
// This is not terribly performant but it's a quick way to
|
|
8
|
+
// This is not terribly performant but it's a quick way to retrieve
|
|
9
9
|
// accurate timestamp data for a stopwatch.
|
|
10
10
|
|
|
11
11
|
import { Wc, Microtask } from "wctk";
|
package/package.json
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
"name": "@w-lfpup/wctk",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"main": "dist/mod.js",
|
|
5
|
-
"description": "A bare-metal toolkit
|
|
5
|
+
"description": "A bare-metal webcomponent toolkit",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
7
|
-
"version": "0.2.
|
|
7
|
+
"version": "0.2.2",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"prepare": "npm run build",
|
|
10
10
|
"build": "npm run build:src && npm run build:tests && npm run build:examples",
|
|
11
11
|
"build:src": "npx tsc --project ./src",
|
|
12
12
|
"build:tests": "npx tsc --project ./tests",
|
|
13
13
|
"build:examples": "npx tsc --project ./examples",
|
|
14
|
-
"test": "
|
|
14
|
+
"test": "npm run test:browsers",
|
|
15
|
+
"test:browsers": "npx jackrabbit_webdriver jr.json tests/dist/mod.js",
|
|
15
16
|
"format": "prettier --write ./"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
package/src/events.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
interface GenericEventListener<E> {
|
|
1
|
+
interface GenericEventListener<E extends Event> {
|
|
2
2
|
(evt: E): void;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
interface GenericEventListenerObject<E> {
|
|
5
|
+
interface GenericEventListenerObject<E extends Event> {
|
|
6
6
|
handleEvent(object: E): void;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
type GenericCallbacks<E> =
|
|
9
|
+
type GenericCallbacks<E extends Event> =
|
|
10
10
|
| GenericEventListener<E>
|
|
11
11
|
| GenericEventListenerObject<E>;
|
|
12
12
|
|
|
13
|
-
type EventMap =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
type EventMap =
|
|
14
|
+
| Partial<{
|
|
15
|
+
[Property in keyof GlobalEventHandlersEventMap]: GenericCallbacks<
|
|
16
|
+
GlobalEventHandlersEventMap[Property]
|
|
17
|
+
>;
|
|
18
|
+
}>
|
|
19
|
+
| Record<string, EventListenerOrEventListenerObject>;
|
|
18
20
|
|
|
19
21
|
type Callbacks = Array<[string, EventListenerOrEventListenerObject]>;
|
|
20
22
|
|
package/src/microtask.ts
CHANGED
|
@@ -7,20 +7,22 @@ type Callback = () => void;
|
|
|
7
7
|
export class Microtask implements MicrotaskInterface {
|
|
8
8
|
#queued = false;
|
|
9
9
|
#callback: Callback;
|
|
10
|
-
queue = this.#queue.bind(this);
|
|
11
10
|
|
|
12
11
|
constructor(callback: Callback) {
|
|
13
12
|
this.#callback = callback;
|
|
14
13
|
}
|
|
15
14
|
|
|
15
|
+
queue = this.#queue.bind(this);
|
|
16
16
|
#queue() {
|
|
17
17
|
if (this.#queued) return;
|
|
18
18
|
this.#queued = true;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
window.queueMicrotask(this.#microtask);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#microtask = this.#unboundMicrotask.bind(this);
|
|
24
|
+
#unboundMicrotask() {
|
|
25
|
+
this.#queued = false;
|
|
26
|
+
this.#callback();
|
|
25
27
|
}
|
|
26
28
|
}
|