element-vir 6.1.6 → 6.2.0
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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# element-vir
|
|
2
2
|
|
|
3
|
+
A wrapper for [lit-element](http://lit.dev) that adds type-safe custom element usage and I/O with declarative custom element definition.
|
|
4
|
+
|
|
3
5
|
Heroic. Reactive. Declarative. Type safe. Web components without compromise.
|
|
4
6
|
|
|
5
7
|
No need for an extra build step,<br>
|
|
@@ -11,8 +13,6 @@ _**It's just TypeScript.**_
|
|
|
11
13
|
|
|
12
14
|
Uses the power of _native_ JavaScript custom web elements, _native_ JavaScript template literals, _native_ JavaScript functions<sup>\*</sup>, _native_ HTML, and [lit-element](http://lit.dev).
|
|
13
15
|
|
|
14
|
-
In reality this is basically a [lit-element](http://lit.dev) wrapper that adds type-safe element tag usage and I/O with declarative style component definition.
|
|
15
|
-
|
|
16
16
|
[Works in every major web browser except Internet Explorer.](https://caniuse.com/mdn-api_window_customelements)
|
|
17
17
|
|
|
18
18
|
<sub>\*okay I hope it's obvious that functions are native</sub>
|
|
@@ -29,13 +29,13 @@ Make sure to install this as a normal dependency (not just a dev dependency) bec
|
|
|
29
29
|
|
|
30
30
|
# Usage
|
|
31
31
|
|
|
32
|
-
Most usage of this package is done through the
|
|
32
|
+
Most usage of this package is done through the `defineElement` or `defineElementNoInputs` functions. See the `DeclarativeElementInit` type for that function's inputs. These inputs are also described below with examples.
|
|
33
33
|
|
|
34
34
|
All of [`lit`](https://lit.dev)'s syntax and functionality is also available for use if you wish.
|
|
35
35
|
|
|
36
36
|
## Simple element definition
|
|
37
37
|
|
|
38
|
-
Use `defineElementNoInputs` to define your element if
|
|
38
|
+
Use `defineElementNoInputs` to define your element if it's not going to accept any inputs (or just for now as you're getting started). It must be given an object with at least `tagName` and `renderCallback` properties (the types enforce this). Here is a bare-minimum example custom element:
|
|
39
39
|
|
|
40
40
|
<!-- example-link: src/readme-examples/my-simple.element.ts -->
|
|
41
41
|
|
|
@@ -201,6 +201,40 @@ export const MyAppWithAssignmentElement = defineElementNoInputs({
|
|
|
201
201
|
});
|
|
202
202
|
```
|
|
203
203
|
|
|
204
|
+
## Other callbacks
|
|
205
|
+
|
|
206
|
+
There are two other callbacks you can define that are sort of similar to lifecycle callbacks. They are much simpler than lifecycle callbacks however.
|
|
207
|
+
|
|
208
|
+
- `initCallback`: called right before the first render, has all state and inputs setup.
|
|
209
|
+
- `cleanupCallback`: called when an element is removed from the DOM. (This is the same as the `disconnectedCallback` in standard HTMLElement classes.)
|
|
210
|
+
|
|
211
|
+
<!-- example-link: src/readme-examples/my-app-with-cleanup-callback.element.ts -->
|
|
212
|
+
|
|
213
|
+
```TypeScript
|
|
214
|
+
import {defineElementNoInputs, html} from 'element-vir';
|
|
215
|
+
|
|
216
|
+
export const MyAppWithAssignmentCleanupCallbackElement = defineElementNoInputs({
|
|
217
|
+
tagName: 'my-app-with-cleanup-callback',
|
|
218
|
+
stateInit: {
|
|
219
|
+
intervalId: undefined as undefined | number,
|
|
220
|
+
},
|
|
221
|
+
initCallback: ({updateState}) => {
|
|
222
|
+
updateState({
|
|
223
|
+
intervalId: window.setInterval(() => console.log('hi'), 1000),
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
renderCallback: () => html`
|
|
227
|
+
<h1>My App</h1>
|
|
228
|
+
`,
|
|
229
|
+
cleanupCallback: ({state, updateState}) => {
|
|
230
|
+
window.clearInterval(state.intervalId);
|
|
231
|
+
updateState({
|
|
232
|
+
intervalId: undefined,
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
204
238
|
## Element events (outputs)
|
|
205
239
|
|
|
206
240
|
Define events with `events` when defining a declarative element. Each event must be initialized with `defineElementEvent` and a type parameter. `defineElementEvent` accepts no inputs as it doesn't make sense for events to have default values.
|
|
@@ -29,5 +29,6 @@ export declare type DeclarativeElementInit<InputsGeneric extends PropertyInitMap
|
|
|
29
29
|
/** Called as part of the first renderCallback call, before the first renderCallback call. */
|
|
30
30
|
initCallback?: InitCallback<InputsGeneric, StateInit, EventsInitGeneric, HostClassKeys, CssVarKeys>;
|
|
31
31
|
renderCallback: RenderCallback<InputsGeneric, StateInit, EventsInitGeneric, HostClassKeys, CssVarKeys>;
|
|
32
|
+
cleanupCallback?: InitCallback<InputsGeneric, StateInit, EventsInitGeneric, HostClassKeys, CssVarKeys>;
|
|
32
33
|
options?: Partial<DeclarativeElementDefinitionOptions> | undefined;
|
|
33
34
|
};
|
|
@@ -83,6 +83,14 @@ export function defineElementNoInputs(initInput) {
|
|
|
83
83
|
});
|
|
84
84
|
return renderResult;
|
|
85
85
|
}
|
|
86
|
+
disconnectedCallback() {
|
|
87
|
+
super.disconnectedCallback();
|
|
88
|
+
if (initInput.cleanupCallback) {
|
|
89
|
+
const renderParams = this.createRenderParams();
|
|
90
|
+
initInput.cleanupCallback(renderParams);
|
|
91
|
+
}
|
|
92
|
+
this.initCalled = false;
|
|
93
|
+
}
|
|
86
94
|
assignInputs(inputs) {
|
|
87
95
|
getObjectTypedKeys(inputs).forEach((key) => {
|
|
88
96
|
property()(this, key);
|