@w-lfpup/wctk 0.2.1 → 0.2.3

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 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
- All controllers (aside from the WC controller) are extremely flexible and not restricted to web components. The can be used on any `HTMLElement`.
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 with npm.
24
+ Install directly from github.
25
25
 
26
26
  ```bash
27
- npm install --save-dev @w-lfpup/wctk
27
+ npm install --save-dev https://github.com/w-lfpup/wctk-js/
28
28
  ```
29
29
 
30
- Or install directly from github.
30
+ Install with npm.
31
31
 
32
32
  ```bash
33
- npm install --save-dev https://github.com/w-lfpup/wctk-js/
33
+ npm install --save-dev @w-lfpup/wctk
34
34
  ```
35
35
 
36
- ## Create a Web Component
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
- If you know vanilla javascript and the DOM you are good to go.
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
- The majority of components only have a few moving pieces. Do you really need templating or flux-patterns
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 a collection of bare-metal facades over vanilla browser apis. They provide the basics for events, reactivity, and forms.
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
- AFAIK `wctk` is the ONLY web component library built with SSR and declarative shadow dom in mind. Developers
70
- can pick-up what the HTML threw down and immediately create interactive SSR friendly web components.
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,19 +1,20 @@
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> extends EventListenerObject {
5
5
  handleEvent(object: E): void;
6
6
  }
7
- type GenericCallbacks<E> = GenericEventListener<E> | GenericEventListenerObject<E>;
8
- type EventMap = Partial<{
9
- [Property in keyof GlobalEventHandlersEventMap]: GenericCallbacks<GlobalEventHandlersEventMap[Property]>;
10
- }>;
7
+ type GenericCallbacks<E extends Event> = GenericEventListener<E> | GenericEventListenerObject<E>;
8
+ type EventMaps = DocumentEventMap & GlobalEventHandlersEventMap & ElementEventMap;
9
+ type ListenerMap = Partial<{
10
+ [Property in keyof EventMaps]: GenericCallbacks<EventMaps[Property]>;
11
+ }> | Record<string, EventListenerOrEventListenerObject>;
11
12
  interface EventElementInterface {
12
13
  addEventListener: Element["addEventListener"];
13
14
  removeEventListener: Element["removeEventListener"];
14
15
  }
15
16
  export interface EventParamsInterface {
16
- listeners: EventMap;
17
+ listeners: ListenerMap;
17
18
  connected?: boolean;
18
19
  target: EventElementInterface;
19
20
  }
@@ -4,7 +4,7 @@ export interface MicrotaskInterface {
4
4
  type Callback = () => void;
5
5
  export declare class Microtask implements MicrotaskInterface {
6
6
  #private;
7
- queue: () => void;
8
7
  constructor(callback: Callback);
8
+ queue: () => void;
9
9
  }
10
10
  export {};
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
- // could this be a bound function? less function creation
13
- queueMicrotask(() => {
14
- this.#queued = false;
15
- this.#callback();
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 web components.
3
+ Add event listeners to webcomponents.
4
4
 
5
5
  ## How to use
6
6
 
7
- Add an `Events` controller to a web component. Use a params object on instantiation.
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 four properties:
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 binds a record of `listeners` to a `host`.
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 cycle methods
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 web component. Provide a callback.
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_ at the tail end of the event loop during the microtaskqueue phase.
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.
@@ -4,7 +4,7 @@ Lazily map selector queries.
4
4
 
5
5
  ## How to use
6
6
 
7
- Add a `QuerySelector` controller to a web component.
7
+ Add a `QuerySelector` controller to a webcomponent.
8
8
 
9
9
  ```html
10
10
  <my-element>
package/docs/wc.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Wc Controller
2
2
 
3
- Build a web component.
3
+ Build a webcomponent.
4
4
 
5
5
  ## How to use
6
6
 
7
- Add a `Wc` controller to a custom element with only one line
7
+ Add a `Wc` controller to a custom element:
8
8
 
9
9
  ```ts
10
10
  import { Wc } from "wctk";
@@ -14,7 +14,7 @@
14
14
  </head>
15
15
  <body>
16
16
  <main>
17
- <!-- web component -->
17
+ <!-- webcomponent -->
18
18
  <counter-wc>
19
19
  <template shadowrootmode="closed">
20
20
  <button decrease>-</button>
@@ -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
  },
@@ -13,8 +13,8 @@ class Counter extends HTMLElement {
13
13
  #wc = new Wc({ host: this });
14
14
 
15
15
  #ev = new Events({
16
- target: this.#wc.shadowRoot,
17
16
  connected: true,
17
+ target: this.#wc.shadowRoot,
18
18
  listeners: {
19
19
  click: this.#clickHandler.bind(this),
20
20
  },
@@ -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
  },
@@ -14,7 +14,7 @@
14
14
  </head>
15
15
  <body>
16
16
  <main>
17
- <!-- web component -->
17
+ <!-- webcomponent -->
18
18
  <stopwatch-wc>
19
19
  <template shadowrootmode="closed">
20
20
  <!-- Shadow DOM content -->
@@ -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 get
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 for web components",
5
+ "description": "A bare-metal webcomponent toolkit",
6
6
  "license": "BSD-3-Clause",
7
- "version": "0.2.1",
7
+ "version": "0.2.3",
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": "npx jackrabbit_webdriver jr.json tests/dist/mod.js",
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,28 @@
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<
6
+ E extends Event,
7
+ > extends EventListenerObject {
6
8
  handleEvent(object: E): void;
7
9
  }
8
10
 
9
- type GenericCallbacks<E> =
11
+ type GenericCallbacks<E extends Event> =
10
12
  | GenericEventListener<E>
11
13
  | GenericEventListenerObject<E>;
12
14
 
13
- type EventMap = Partial<{
14
- [Property in keyof GlobalEventHandlersEventMap]: GenericCallbacks<
15
- GlobalEventHandlersEventMap[Property]
16
- >;
17
- }>;
15
+ type EventMaps = DocumentEventMap &
16
+ GlobalEventHandlersEventMap &
17
+ ElementEventMap;
18
+
19
+ type ListenerMap =
20
+ | Partial<{
21
+ [Property in keyof EventMaps]: GenericCallbacks<
22
+ EventMaps[Property]
23
+ >;
24
+ }>
25
+ | Record<string, EventListenerOrEventListenerObject>;
18
26
 
19
27
  type Callbacks = Array<[string, EventListenerOrEventListenerObject]>;
20
28
 
@@ -24,7 +32,7 @@ interface EventElementInterface {
24
32
  }
25
33
 
26
34
  export interface EventParamsInterface {
27
- listeners: EventMap;
35
+ listeners: ListenerMap;
28
36
  connected?: boolean;
29
37
  target: EventElementInterface;
30
38
  }
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
- // could this be a bound function? less function creation
21
- queueMicrotask(() => {
22
- this.#queued = false;
23
- this.#callback();
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
  }