@w-lfpup/wctk 0.1.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/.github/workflows/browsers.macos.json +51 -0
- package/.github/workflows/browsers.ubuntu.json +37 -0
- package/.github/workflows/builds.yml +13 -4
- package/README.md +40 -13
- package/dist/events.d.ts +6 -7
- package/dist/events.js +8 -22
- package/dist/microtask.d.ts +4 -6
- package/dist/microtask.js +9 -13
- package/dist/mod.d.ts +0 -2
- package/dist/mod.js +0 -2
- package/dist/query_selector.d.ts +2 -5
- package/dist/query_selector.js +16 -14
- package/dist/subscription.d.ts +0 -1
- package/dist/subscription.js +6 -14
- package/dist/wc.d.ts +4 -4
- package/dist/wc.js +5 -7
- package/docs/events.md +15 -22
- package/docs/microtask.md +3 -6
- package/docs/query_selector.md +2 -5
- package/docs/wc.md +3 -3
- package/examples/counter/index.html +1 -1
- package/examples/counter/mod.js +14 -18
- package/examples/counter/mod.ts +16 -18
- package/examples/form_associated/mod.js +1 -3
- package/examples/form_associated/mod.ts +0 -1
- package/examples/form_associated/text_input.js +3 -8
- package/examples/form_associated/text_input.ts +4 -4
- package/examples/stopwatch/index.html +7 -4
- package/examples/stopwatch/mod.js +8 -18
- package/examples/stopwatch/mod.ts +6 -20
- package/examples/stopwatch/stopwatch.js +38 -24
- package/examples/stopwatch/stopwatch.ts +46 -25
- package/examples/tsconfig.json +1 -0
- package/jr.json +25 -0
- package/package.json +10 -5
- package/src/events.ts +20 -38
- package/src/microtask.ts +13 -22
- package/src/mod.ts +0 -2
- package/src/query_selector.ts +18 -27
- package/src/wc.ts +11 -11
- package/tests/dist/events.tests.js +60 -0
- package/tests/dist/microtask.tests.js +38 -0
- package/tests/dist/mod.js +10 -0
- package/tests/dist/query_selector.tests.js +43 -0
- package/tests/dist/wc.tests.js +41 -0
- package/tests/src/events.tests.ts +73 -0
- package/tests/src/microtask.tests.ts +54 -0
- package/tests/src/mod.ts +11 -0
- package/tests/src/query_selector.tests.ts +46 -0
- package/tests/src/wc.tests.ts +52 -0
- package/tests/tsconfig.json +8 -0
- package/dist/bind.d.ts +0 -7
- package/dist/bind.js +0 -14
- package/docs/bind.md +0 -26
- package/docs/subscription.md +0 -85
- package/src/bind.ts +0 -22
- package/src/subscription.ts +0 -47
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jackrabbit_url": "http://127.0.0.1:4000",
|
|
3
|
+
"runAsynchronusly": false,
|
|
4
|
+
"webdrivers": [
|
|
5
|
+
{
|
|
6
|
+
"command": "safaridriver -p 4001",
|
|
7
|
+
"title": "Safari",
|
|
8
|
+
"timeout_ms": 30000,
|
|
9
|
+
"webdriver_url": "http://localhost:4001"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"command": "$GECKOWEBDRIVER/geckodriver -p 4001",
|
|
13
|
+
"title": "Firefox",
|
|
14
|
+
"timeout_ms": 30000,
|
|
15
|
+
"webdriver_url": "http://127.0.0.1:4001",
|
|
16
|
+
"capabilities": {
|
|
17
|
+
"alwaysMatch": {
|
|
18
|
+
"moz:firefoxOptions": {
|
|
19
|
+
"args": ["-headless"]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"command": "$CHROMEWEBDRIVER/chromedriver --port=4001",
|
|
26
|
+
"title": "Chrome",
|
|
27
|
+
"timeout_ms": 30000,
|
|
28
|
+
"webdriver_url": "http://localhost:4001",
|
|
29
|
+
"capabilities": {
|
|
30
|
+
"alwaysMatch": {
|
|
31
|
+
"goog:chromeOptions": {
|
|
32
|
+
"args": ["--headless"]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"command": "$EDGEWEBDRIVER/msedgedriver --port=4001",
|
|
39
|
+
"title": "Edge",
|
|
40
|
+
"timeout_ms": 30000,
|
|
41
|
+
"webdriver_url": "http://localhost:4001",
|
|
42
|
+
"capabilities": {
|
|
43
|
+
"alwaysMatch": {
|
|
44
|
+
"ms:edgeOptions": {
|
|
45
|
+
"args": ["--headless"]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jackrabbit_url": "http://127.0.0.1:4000",
|
|
3
|
+
"runAsynchronusly": false,
|
|
4
|
+
"webdrivers": [
|
|
5
|
+
{
|
|
6
|
+
"title": "Firefox",
|
|
7
|
+
"command": "$GECKOWEBDRIVER/geckodriver -p 4001",
|
|
8
|
+
"timeout_ms": 30000,
|
|
9
|
+
"webdriver_url": "http://localhost:4001",
|
|
10
|
+
"capabilities": {
|
|
11
|
+
"alwaysMatch": {
|
|
12
|
+
"moz:firefoxOptions": {
|
|
13
|
+
"args": ["-headless"]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"title": "Chrome",
|
|
20
|
+
"command": "$CHROMEWEBDRIVER/chromedriver --port=4005",
|
|
21
|
+
"timeout_ms": 30000,
|
|
22
|
+
"webdriver_url": "http://localhost:4005",
|
|
23
|
+
"capabilities": {
|
|
24
|
+
"alwaysMatch": {
|
|
25
|
+
"goog:chromeOptions": {
|
|
26
|
+
"args": [
|
|
27
|
+
"--headless",
|
|
28
|
+
"--disbale-gpu",
|
|
29
|
+
"--disable-dev-shm-usage",
|
|
30
|
+
"--start-maximized"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
name:
|
|
1
|
+
name: Tests
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
@@ -11,8 +11,17 @@ jobs:
|
|
|
11
11
|
runs-on: ubuntu-latest
|
|
12
12
|
steps:
|
|
13
13
|
- uses: actions/checkout@v6
|
|
14
|
-
- uses: actions/setup-node@
|
|
14
|
+
- uses: actions/setup-node@v6
|
|
15
15
|
- name: Install
|
|
16
16
|
run: npm ci
|
|
17
|
-
- name:
|
|
18
|
-
run:
|
|
17
|
+
- name: Test browsers
|
|
18
|
+
run: npx jackrabbit_webdriver .github/workflows/browsers.ubuntu.json tests/dist/mod.js
|
|
19
|
+
build_and_test_macos:
|
|
20
|
+
runs-on: macos-latest
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v6
|
|
23
|
+
- uses: actions/setup-node@v6
|
|
24
|
+
- name: Install
|
|
25
|
+
run: npm ci
|
|
26
|
+
- name: Test browsers
|
|
27
|
+
run: npx jackrabbit_webdriver .github/workflows/browsers.macos.json tests/dist/mod.js
|
package/README.md
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
1
1
|
# Wctk-JS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An SSR friendly (w)eb(c)omponent (t)ool (k)it without dependencies.
|
|
4
4
|
|
|
5
5
|
[](https://github.com/w-lfpup/wctk-js/actions/workflows/builds.yml)
|
|
6
6
|
|
|
7
7
|
## About
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
There are no base classes, decorators, or mixins.
|
|
10
|
+
|
|
11
|
+
All features are compositional and designed for SSR and [declarative shadow dom](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM#declaratively_with_html).
|
|
12
|
+
|
|
13
|
+
Four (4) controllers help developers:
|
|
10
14
|
|
|
11
|
-
-
|
|
15
|
+
- manage [shadow dom](./docs/wc.md) and [form values](./docs/wc.md#adopted-stylesheets-and-form-values)
|
|
12
16
|
- render on the [microtask queue](./docs/microtask.md)
|
|
13
17
|
- listen for [events](./docs/events.md)
|
|
14
|
-
- [
|
|
15
|
-
- manage [form values](./docs/wc.md#adopted-stylesheets-and-form-values)
|
|
16
|
-
- [query](./docs/query_selector.md) the shadow dom
|
|
17
|
-
- [bind](./docs/bind.md) functions to elements
|
|
18
|
+
- cache selector [queries](./docs/query_selector.md)
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
There are no base classes, decorators, or mixins.
|
|
20
|
+
Controllers are flexible and not restricted to webcomponents. The can be used on any `HTMLElement`.
|
|
22
21
|
|
|
23
22
|
## Install
|
|
24
23
|
|
|
24
|
+
Install directly from github.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install --save-dev https://github.com/w-lfpup/wctk-js/
|
|
28
|
+
```
|
|
29
|
+
|
|
25
30
|
Install with npm.
|
|
26
31
|
|
|
27
32
|
```bash
|
|
28
33
|
npm install --save-dev @w-lfpup/wctk
|
|
29
34
|
```
|
|
30
35
|
|
|
31
|
-
|
|
36
|
+
## Create a webcomponent
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
Add a `Wc` controller to a custom element with only one line
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { Wc } from "wctk";
|
|
42
|
+
|
|
43
|
+
class MyElement extends HTMLElement {
|
|
44
|
+
#wc = new Wc({ host: this });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
customElements.define("my-element", MyElement);
|
|
35
48
|
```
|
|
36
49
|
|
|
37
50
|
## Examples
|
|
@@ -44,6 +57,20 @@ The following examples demonstrate several common SSR use cases:
|
|
|
44
57
|
- a [stopwatch](https://w-lfpup.github.io/wctk-js/examples/stopwatch/) with initial state in the Shadow DOM ([code](https://github.com/w-lfpup/wctk-js/tree/main/examples/stopwatch/)).
|
|
45
58
|
- a [form associated](https://w-lfpup.github.io/wctk-js/examples/form_associated/) element ([code](https://github.com/w-lfpup/wctk-js/tree/main/examples/form_associated/)).
|
|
46
59
|
|
|
60
|
+
## Design Goals
|
|
61
|
+
|
|
62
|
+
The `wctk` is a collection of bare-metal facades over vanilla browser apis. They provide the basics for
|
|
63
|
+
events, reactivity, and forms.
|
|
64
|
+
|
|
65
|
+
If you know vanilla javascript and the DOM you are good to go.
|
|
66
|
+
|
|
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.
|
|
69
|
+
|
|
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?
|
|
73
|
+
|
|
47
74
|
## License
|
|
48
75
|
|
|
49
76
|
`Wctk-js` is released under the BSD-3 Clause License.
|
package/dist/events.d.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
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"];
|
|
14
14
|
}
|
|
15
15
|
export interface EventParamsInterface {
|
|
16
|
-
|
|
16
|
+
listeners: EventMap;
|
|
17
17
|
connected?: boolean;
|
|
18
|
-
|
|
19
|
-
target?: EventElementInterface;
|
|
18
|
+
target: EventElementInterface;
|
|
20
19
|
}
|
|
21
20
|
export interface EventsInterface {
|
|
22
21
|
connect(): void;
|
package/dist/events.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export class Events {
|
|
2
2
|
#connected = false;
|
|
3
|
-
#
|
|
3
|
+
#listeners = [];
|
|
4
4
|
#target;
|
|
5
5
|
constructor(params) {
|
|
6
|
-
const {
|
|
7
|
-
this.#target = target
|
|
8
|
-
this.#
|
|
6
|
+
const { target, listeners, connected } = params;
|
|
7
|
+
this.#target = target;
|
|
8
|
+
this.#listeners = Object.entries(listeners);
|
|
9
9
|
if (connected)
|
|
10
10
|
this.connect();
|
|
11
11
|
}
|
|
@@ -13,30 +13,16 @@ export class Events {
|
|
|
13
13
|
if (this.#connected)
|
|
14
14
|
return;
|
|
15
15
|
this.#connected = true;
|
|
16
|
-
for (let [name,
|
|
17
|
-
this.#target.addEventListener(name,
|
|
16
|
+
for (let [name, listener] of this.#listeners) {
|
|
17
|
+
this.#target.addEventListener(name, listener);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
disconnect() {
|
|
21
21
|
if (!this.#connected)
|
|
22
22
|
return;
|
|
23
23
|
this.#connected = false;
|
|
24
|
-
for (let [name,
|
|
25
|
-
this.#target.removeEventListener(name,
|
|
24
|
+
for (let [name, listener] of this.#listeners) {
|
|
25
|
+
this.#target.removeEventListener(name, listener);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
function getBoundCallbacks(host, callbacks) {
|
|
30
|
-
let boundCallbacks = [];
|
|
31
|
-
for (let [name, callback] of Object.entries(callbacks)) {
|
|
32
|
-
if (callback instanceof Function &&
|
|
33
|
-
!callback.hasOwnProperty("prototype")) {
|
|
34
|
-
callback = callback.bind(host);
|
|
35
|
-
}
|
|
36
|
-
boundCallbacks.push([
|
|
37
|
-
name,
|
|
38
|
-
callback,
|
|
39
|
-
]);
|
|
40
|
-
}
|
|
41
|
-
return boundCallbacks;
|
|
42
|
-
}
|
package/dist/microtask.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
export interface MicrotaskParamsInterface {
|
|
2
|
-
host: Object;
|
|
3
|
-
callback: Function;
|
|
4
|
-
}
|
|
5
1
|
export interface MicrotaskInterface {
|
|
6
2
|
queue(): void;
|
|
7
3
|
}
|
|
4
|
+
type Callback = () => void;
|
|
8
5
|
export declare class Microtask implements MicrotaskInterface {
|
|
9
6
|
#private;
|
|
10
|
-
constructor(
|
|
11
|
-
queue()
|
|
7
|
+
constructor(callback: Callback);
|
|
8
|
+
queue: () => void;
|
|
12
9
|
}
|
|
10
|
+
export {};
|
package/dist/microtask.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
export class Microtask {
|
|
2
2
|
#queued = false;
|
|
3
3
|
#callback;
|
|
4
|
-
constructor(
|
|
5
|
-
let { host, callback } = params;
|
|
6
|
-
this.queue = this.queue.bind(this);
|
|
4
|
+
constructor(callback) {
|
|
7
5
|
this.#callback = callback;
|
|
8
|
-
if (callback instanceof Function &&
|
|
9
|
-
!callback.hasOwnProperty("prototype")) {
|
|
10
|
-
this.#callback = callback.bind(host);
|
|
11
|
-
}
|
|
12
6
|
}
|
|
13
|
-
queue()
|
|
7
|
+
queue = this.#queue.bind(this);
|
|
8
|
+
#queue() {
|
|
14
9
|
if (this.#queued)
|
|
15
10
|
return;
|
|
16
11
|
this.#queued = true;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
window.queueMicrotask(this.#microtask);
|
|
13
|
+
}
|
|
14
|
+
#microtask = this.#unboundMicrotask.bind(this);
|
|
15
|
+
#unboundMicrotask() {
|
|
16
|
+
this.#queued = false;
|
|
17
|
+
this.#callback();
|
|
22
18
|
}
|
|
23
19
|
}
|
package/dist/mod.d.ts
CHANGED
package/dist/mod.js
CHANGED
package/dist/query_selector.d.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
export interface QuerySelectorParamsInterface {
|
|
2
|
-
parent: ParentNode;
|
|
3
|
-
}
|
|
4
1
|
export interface QuerySelectorInterface {
|
|
5
2
|
querySelector(name: string): Element | undefined;
|
|
6
|
-
querySelectorAll(name: string): Element[]
|
|
3
|
+
querySelectorAll(name: string): Element[];
|
|
7
4
|
deleteAll(): void;
|
|
8
5
|
}
|
|
9
6
|
export declare class QuerySelector implements QuerySelectorInterface {
|
|
10
7
|
#private;
|
|
11
|
-
constructor(
|
|
8
|
+
constructor(parentNode: ParentNode);
|
|
12
9
|
querySelector(selector: string): Element | undefined;
|
|
13
10
|
querySelectorAll(selector: string): Element[];
|
|
14
11
|
deleteAll(): void;
|
package/dist/query_selector.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
export class QuerySelector {
|
|
2
2
|
#queries = new Map();
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
#queryAlls = new Map();
|
|
4
|
+
#parentNode;
|
|
5
|
+
constructor(parentNode) {
|
|
6
|
+
this.#parentNode = parentNode;
|
|
6
7
|
}
|
|
7
8
|
querySelector(selector) {
|
|
8
|
-
|
|
9
|
+
if (this.#queries.has(selector))
|
|
10
|
+
return this.#queries.get(selector);
|
|
11
|
+
let query = this.#parentNode.querySelector(selector) ?? undefined;
|
|
12
|
+
this.#queries.set(selector, query);
|
|
13
|
+
return query;
|
|
9
14
|
}
|
|
10
15
|
querySelectorAll(selector) {
|
|
11
|
-
|
|
16
|
+
let results = this.#queryAlls.get(selector);
|
|
17
|
+
if (results)
|
|
18
|
+
return results;
|
|
19
|
+
let query = Array.from(this.#parentNode.querySelectorAll(selector));
|
|
20
|
+
this.#queryAlls.set(selector, query);
|
|
21
|
+
return query;
|
|
12
22
|
}
|
|
13
23
|
deleteAll() {
|
|
14
24
|
this.#queries = new Map();
|
|
25
|
+
this.#queryAlls = new Map();
|
|
15
26
|
}
|
|
16
27
|
}
|
|
17
|
-
function getQuery(params, queries, selector) {
|
|
18
|
-
const { parent } = params;
|
|
19
|
-
let results = queries.get(selector);
|
|
20
|
-
if (!results) {
|
|
21
|
-
results = Array.from(parent.querySelectorAll(selector));
|
|
22
|
-
queries.set(selector, results);
|
|
23
|
-
}
|
|
24
|
-
return results;
|
|
25
|
-
}
|
package/dist/subscription.d.ts
CHANGED
package/dist/subscription.js
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
1
|
export class Subscription {
|
|
2
|
-
#
|
|
2
|
+
#params;
|
|
3
3
|
#affect;
|
|
4
|
-
#subscribe;
|
|
5
|
-
#unsubscribe;
|
|
6
4
|
constructor(params) {
|
|
7
|
-
|
|
8
|
-
this.#
|
|
9
|
-
this.#unsubscribe = unsubscribe;
|
|
10
|
-
this.#callback = callback;
|
|
11
|
-
if (callback instanceof Function &&
|
|
12
|
-
!callback.hasOwnProperty("prototype")) {
|
|
13
|
-
this.#callback = callback.bind(host);
|
|
14
|
-
}
|
|
15
|
-
if (connected)
|
|
5
|
+
this.#params = params;
|
|
6
|
+
if (this.#params.connected)
|
|
16
7
|
this.connect();
|
|
17
8
|
}
|
|
18
9
|
connect() {
|
|
10
|
+
let { callback, subscribe } = this.#params;
|
|
19
11
|
if (!this.#affect)
|
|
20
|
-
this.#affect =
|
|
12
|
+
this.#affect = subscribe(callback);
|
|
21
13
|
}
|
|
22
14
|
disconnect() {
|
|
23
15
|
if (this.#affect)
|
|
24
|
-
this.#unsubscribe(this.#affect);
|
|
16
|
+
this.#params.unsubscribe(this.#affect);
|
|
25
17
|
}
|
|
26
18
|
}
|
package/dist/wc.d.ts
CHANGED
|
@@ -2,13 +2,13 @@ export interface WcElementInterface {
|
|
|
2
2
|
attachInternals: HTMLElement["attachInternals"];
|
|
3
3
|
attachShadow: Element["attachShadow"];
|
|
4
4
|
}
|
|
5
|
-
type
|
|
5
|
+
type FormValueArgs = Parameters<ElementInternals["setFormValue"]>;
|
|
6
6
|
export interface WcParamsInterface {
|
|
7
7
|
host: WcElementInterface;
|
|
8
8
|
adoptedStyleSheets?: CSSStyleSheet[];
|
|
9
9
|
shadowRootInit?: ShadowRootInit;
|
|
10
|
-
formValue?:
|
|
11
|
-
formState?:
|
|
10
|
+
formValue?: FormValueArgs[0];
|
|
11
|
+
formState?: FormValueArgs[1];
|
|
12
12
|
}
|
|
13
13
|
export interface WcInterface {
|
|
14
14
|
readonly declarative: boolean;
|
|
@@ -27,7 +27,7 @@ export declare class Wc implements WcInterface {
|
|
|
27
27
|
set adoptedStyleSheets(stylesheets: CSSStyleSheet[]);
|
|
28
28
|
checkValidity(): boolean;
|
|
29
29
|
reportValidity(): boolean;
|
|
30
|
-
setFormValue(value:
|
|
30
|
+
setFormValue(value: FormValueArgs[0], state?: FormValueArgs[1]): void;
|
|
31
31
|
setValidity(flags?: ValidityStateFlags, message?: string, anchor?: HTMLElement): void;
|
|
32
32
|
}
|
|
33
33
|
export {};
|
package/dist/wc.js
CHANGED
|
@@ -2,22 +2,20 @@ const shadowRootInitFallback = {
|
|
|
2
2
|
mode: "closed",
|
|
3
3
|
};
|
|
4
4
|
export class Wc {
|
|
5
|
-
#declarative =
|
|
5
|
+
#declarative = false;
|
|
6
6
|
#internals;
|
|
7
7
|
#shadowRoot;
|
|
8
8
|
constructor(params) {
|
|
9
9
|
let { adoptedStyleSheets, host, formState, formValue, shadowRootInit } = params;
|
|
10
10
|
this.#internals = host.attachInternals();
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.#shadowRoot = shadowRoot;
|
|
11
|
+
if (this.#internals.shadowRoot) {
|
|
12
|
+
this.#declarative = true;
|
|
13
|
+
this.#shadowRoot = this.#internals.shadowRoot;
|
|
14
14
|
}
|
|
15
15
|
else {
|
|
16
|
-
this.#declarative = false;
|
|
17
16
|
this.#shadowRoot = host.attachShadow(shadowRootInit ?? shadowRootInitFallback);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
|
-
this.adoptedStyleSheets = adoptedStyleSheets;
|
|
18
|
+
this.adoptedStyleSheets = adoptedStyleSheets ?? [];
|
|
21
19
|
if (formValue)
|
|
22
20
|
this.setFormValue(formValue, formState);
|
|
23
21
|
}
|
package/docs/events.md
CHANGED
|
@@ -1,32 +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
|
-
callbacks: Record<string, EventListenerOrEventListenerObject>;
|
|
16
15
|
connected?: boolean;
|
|
17
|
-
|
|
18
|
-
target
|
|
16
|
+
listeners: Record<string, EventListenerOrEventListenerObject>;
|
|
17
|
+
target: EventTarget;
|
|
19
18
|
}
|
|
20
19
|
```
|
|
21
20
|
|
|
22
|
-
The `Events` controller
|
|
23
|
-
|
|
24
|
-
Afterwards, the `Events` controller adds the callbacks as event listeners on a `target` node.
|
|
21
|
+
The `Events` controller adds event listeners on a `target` node.
|
|
25
22
|
|
|
26
23
|
The `target` node can be a shadowRoot, a document, or the custom element itself.
|
|
27
24
|
|
|
28
|
-
If the `target` property is undefined, the `host` property is used as a fallback.
|
|
29
|
-
|
|
30
25
|
### Controller
|
|
31
26
|
|
|
32
27
|
Here is an example of using the `Events` controller.
|
|
@@ -38,11 +33,10 @@ class MyElement extends HTMLElement {
|
|
|
38
33
|
#wc = new Wc({ this: host });
|
|
39
34
|
|
|
40
35
|
#ec = new Events({
|
|
41
|
-
host: this,
|
|
42
36
|
target: this.#wc.shadowRoot,
|
|
43
|
-
|
|
44
|
-
click: this.#onClick,
|
|
45
|
-
keydown: this.#onKeyDown,
|
|
37
|
+
listeners: {
|
|
38
|
+
click: this.#onClick.bind(this),
|
|
39
|
+
keydown: this.#onKeyDown.bind(this),
|
|
46
40
|
},
|
|
47
41
|
});
|
|
48
42
|
|
|
@@ -66,9 +60,9 @@ class MyElement extends HTMLElement {
|
|
|
66
60
|
}
|
|
67
61
|
```
|
|
68
62
|
|
|
69
|
-
### Shortcut life
|
|
63
|
+
### Shortcut life-cycle methods
|
|
70
64
|
|
|
71
|
-
In the example below, the `connected` property is set to true and
|
|
65
|
+
In the example below, the `connected` property is set to true and listeners are immediately added to the `target`.
|
|
72
66
|
|
|
73
67
|
```ts
|
|
74
68
|
import { Events, Wc } from "wctk";
|
|
@@ -76,12 +70,11 @@ import { Events, Wc } from "wctk";
|
|
|
76
70
|
class MyElement extends HTMLElement {
|
|
77
71
|
#wc = new Wc({ this: host });
|
|
78
72
|
#ec = new Events({
|
|
79
|
-
host: this,
|
|
80
|
-
target: this.#wc.shadowRoot,
|
|
81
73
|
connected: true,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
74
|
+
target: this.#wc.shadowRoot,
|
|
75
|
+
listeners: {
|
|
76
|
+
click: this.#onClick.bind(this),
|
|
77
|
+
keydown: this.#onKeyDown.bind(this),
|
|
85
78
|
},
|
|
86
79
|
});
|
|
87
80
|
|
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
|
|
|
@@ -16,10 +16,7 @@ import { Microtask } from "wctk";
|
|
|
16
16
|
class MyElement extends HTMLElement {
|
|
17
17
|
static observedAttributes = ["width"];
|
|
18
18
|
|
|
19
|
-
#rc = new Microtask(
|
|
20
|
-
host: this,
|
|
21
|
-
callback: this.#render,
|
|
22
|
-
});
|
|
19
|
+
#rc = new Microtask(this.#render.bind(this));
|
|
23
20
|
|
|
24
21
|
#render() {
|
|
25
22
|
// update DOM here!
|
|
@@ -32,4 +29,4 @@ class MyElement extends HTMLElement {
|
|
|
32
29
|
}
|
|
33
30
|
```
|
|
34
31
|
|
|
35
|
-
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
|
@@ -4,7 +4,7 @@ Lazily map selector queries.
|
|
|
4
4
|
|
|
5
5
|
## How to use
|
|
6
6
|
|
|
7
|
-
Add a `QuerySelector` controller to a
|
|
7
|
+
Add a `QuerySelector` controller to a webcomponent.
|
|
8
8
|
|
|
9
9
|
```html
|
|
10
10
|
<my-element>
|
|
@@ -21,10 +21,7 @@ import { QuerySelector } from "wctk";
|
|
|
21
21
|
|
|
22
22
|
class MyElement extends HTMLElement {
|
|
23
23
|
#wc = new Wc({ host: this });
|
|
24
|
-
|
|
25
|
-
#qc = new QuerySelector({
|
|
26
|
-
parent: this.#wc.shadowRoot,
|
|
27
|
-
});
|
|
24
|
+
#qc = new QuerySelector(this.#wc.shadowRoot);
|
|
28
25
|
|
|
29
26
|
showcaseApi() {
|
|
30
27
|
// first Element or undefined
|
package/docs/wc.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# Wc Controller
|
|
2
2
|
|
|
3
|
-
Build a
|
|
3
|
+
Build a webcomponent.
|
|
4
4
|
|
|
5
5
|
## How to use
|
|
6
6
|
|
|
7
|
-
Add a `Wc` controller to a custom element
|
|
7
|
+
Add a `Wc` controller to a custom element:
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
10
|
import { Wc } from "wctk";
|
|
@@ -16,7 +16,7 @@ class MyElement extends HTMLElement {
|
|
|
16
16
|
|
|
17
17
|
## Adopted stylesheets and form values
|
|
18
18
|
|
|
19
|
-
The `Wc` controller
|
|
19
|
+
The `Wc` controller interfaces with core web componet APIs like adopted stylesheets and form values.
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
class MyElement extends HTMLElement {
|