@wc-bindable/mithril 0.6.1 → 0.7.1
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 +126 -122
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,122 +1,126 @@
|
|
|
1
|
-
# @wc-bindable/mithril
|
|
2
|
-
|
|
3
|
-
Mithril.js adapter for the **wc-bindable** protocol.
|
|
4
|
-
|
|
5
|
-
Bridges the protocol's `bind()` into Mithril's `oncreate` / `onremove` lifecycle
|
|
6
|
-
and Mithril's `redraw()` cycle, so a Web Component's declared properties show up
|
|
7
|
-
as plain state you can read inside `view()`.
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install @wc-bindable/mithril mithril
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## API
|
|
16
|
-
|
|
17
|
-
### `wcBindable(el, onUpdate): unbind`
|
|
18
|
-
|
|
19
|
-
Low-level helper. Call it from any lifecycle hook that gives you the DOM node;
|
|
20
|
-
the adapter does no redraw — you decide what to do with each update.
|
|
21
|
-
|
|
22
|
-
| Parameter | Type | Description |
|
|
23
|
-
|---|---|---|
|
|
24
|
-
| `el` | `HTMLElement` | The Web Component DOM node |
|
|
25
|
-
| `onUpdate` | `(name: string, value: unknown) => void` | Called once per declared property on initial sync, and again on every change event |
|
|
26
|
-
|
|
27
|
-
Returns an `unbind` function. If `el` does not implement the wc-bindable
|
|
28
|
-
protocol, the helper is a no-op and the returned function is safe to call.
|
|
29
|
-
|
|
30
|
-
### `createWcBindable<V>(initialValues?, options?): WcBindableState<V>`
|
|
31
|
-
|
|
32
|
-
Stateful helper. Holds a values object, mutates it as events arrive, and calls
|
|
33
|
-
`m.redraw()` so your `view()` rerenders automatically.
|
|
34
|
-
|
|
35
|
-
Returns `{ values, oncreate, onremove }` — wire those directly into your
|
|
36
|
-
component.
|
|
37
|
-
|
|
38
|
-
| Option | Type | Default |
|
|
39
|
-
|---|---|---|
|
|
40
|
-
| `redraw` | `() => void` | `() => m.redraw()` — override to use `m.redraw.sync()` or batch redraws |
|
|
41
|
-
|
|
42
|
-
## Usage
|
|
43
|
-
|
|
44
|
-
`oncreate` and `onremove` must run on the bindable element itself — Mithril
|
|
45
|
-
hands the hook the vnode whose DOM node it is attached to, and the adapter only
|
|
46
|
-
binds if that node implements the protocol. So always attach the hooks to the
|
|
47
|
-
`<my-input>` vnode, not to a wrapping component or `<div>`:
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
import m from "mithril";
|
|
51
|
-
import { createWcBindable } from "@wc-bindable/mithril";
|
|
52
|
-
|
|
53
|
-
const Form = () => {
|
|
54
|
-
const binder = createWcBindable<{ value: string; checked: boolean }>({
|
|
55
|
-
value: "",
|
|
56
|
-
checked: false,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
view: () =>
|
|
61
|
-
m("div", [
|
|
62
|
-
m("my-input", {
|
|
63
|
-
oncreate: binder.oncreate,
|
|
64
|
-
onremove: binder.onremove,
|
|
65
|
-
}),
|
|
66
|
-
m("output", `value: ${binder.values.value}`),
|
|
67
|
-
m("output", `checked: ${binder.values.checked}`),
|
|
68
|
-
]),
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
m.mount(document.body, Form);
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
If your component's view root **is** the bindable element (no wrapper), you can
|
|
76
|
-
attach the hooks at the component level instead — `vnode.dom` will then be the
|
|
77
|
-
bindable element:
|
|
78
|
-
|
|
79
|
-
```ts
|
|
80
|
-
const Input = () => {
|
|
81
|
-
const binder = createWcBindable<{ value: string }>({ value: "" });
|
|
82
|
-
return {
|
|
83
|
-
oncreate: binder.oncreate,
|
|
84
|
-
onremove: binder.onremove,
|
|
85
|
-
view: () => m("my-input"),
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Low-level usage
|
|
91
|
-
|
|
92
|
-
```ts
|
|
93
|
-
import m from "mithril";
|
|
94
|
-
import { wcBindable } from "@wc-bindable/mithril";
|
|
95
|
-
|
|
96
|
-
const Form = () => {
|
|
97
|
-
const state: Record<string, unknown> = { value: "" };
|
|
98
|
-
let unbind: (() => void) | null = null;
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
view: () =>
|
|
102
|
-
m("div", [
|
|
103
|
-
m("my-input", {
|
|
104
|
-
oncreate(vnode) {
|
|
105
|
-
unbind = wcBindable(vnode.dom as HTMLElement, (name, value) => {
|
|
106
|
-
state[name] = value;
|
|
107
|
-
m.redraw();
|
|
108
|
-
});
|
|
109
|
-
},
|
|
110
|
-
onremove() {
|
|
111
|
-
unbind?.();
|
|
112
|
-
},
|
|
113
|
-
}),
|
|
114
|
-
m("output", state.value as string),
|
|
115
|
-
]),
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
##
|
|
121
|
-
|
|
122
|
-
|
|
1
|
+
# @wc-bindable/mithril
|
|
2
|
+
|
|
3
|
+
Mithril.js adapter for the **wc-bindable** protocol.
|
|
4
|
+
|
|
5
|
+
Bridges the protocol's `bind()` into Mithril's `oncreate` / `onremove` lifecycle
|
|
6
|
+
and Mithril's `redraw()` cycle, so a Web Component's declared properties show up
|
|
7
|
+
as plain state you can read inside `view()`.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @wc-bindable/mithril mithril
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
### `wcBindable(el, onUpdate): unbind`
|
|
18
|
+
|
|
19
|
+
Low-level helper. Call it from any lifecycle hook that gives you the DOM node;
|
|
20
|
+
the adapter does no redraw — you decide what to do with each update.
|
|
21
|
+
|
|
22
|
+
| Parameter | Type | Description |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| `el` | `HTMLElement` | The Web Component DOM node |
|
|
25
|
+
| `onUpdate` | `(name: string, value: unknown) => void` | Called once per declared property on initial sync, and again on every change event |
|
|
26
|
+
|
|
27
|
+
Returns an `unbind` function. If `el` does not implement the wc-bindable
|
|
28
|
+
protocol, the helper is a no-op and the returned function is safe to call.
|
|
29
|
+
|
|
30
|
+
### `createWcBindable<V>(initialValues?, options?): WcBindableState<V>`
|
|
31
|
+
|
|
32
|
+
Stateful helper. Holds a values object, mutates it as events arrive, and calls
|
|
33
|
+
`m.redraw()` so your `view()` rerenders automatically.
|
|
34
|
+
|
|
35
|
+
Returns `{ values, oncreate, onremove }` — wire those directly into your
|
|
36
|
+
component.
|
|
37
|
+
|
|
38
|
+
| Option | Type | Default |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `redraw` | `() => void` | `() => m.redraw()` — override to use `m.redraw.sync()` or batch redraws |
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
`oncreate` and `onremove` must run on the bindable element itself — Mithril
|
|
45
|
+
hands the hook the vnode whose DOM node it is attached to, and the adapter only
|
|
46
|
+
binds if that node implements the protocol. So always attach the hooks to the
|
|
47
|
+
`<my-input>` vnode, not to a wrapping component or `<div>`:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import m from "mithril";
|
|
51
|
+
import { createWcBindable } from "@wc-bindable/mithril";
|
|
52
|
+
|
|
53
|
+
const Form = () => {
|
|
54
|
+
const binder = createWcBindable<{ value: string; checked: boolean }>({
|
|
55
|
+
value: "",
|
|
56
|
+
checked: false,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
view: () =>
|
|
61
|
+
m("div", [
|
|
62
|
+
m("my-input", {
|
|
63
|
+
oncreate: binder.oncreate,
|
|
64
|
+
onremove: binder.onremove,
|
|
65
|
+
}),
|
|
66
|
+
m("output", `value: ${binder.values.value}`),
|
|
67
|
+
m("output", `checked: ${binder.values.checked}`),
|
|
68
|
+
]),
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
m.mount(document.body, Form);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If your component's view root **is** the bindable element (no wrapper), you can
|
|
76
|
+
attach the hooks at the component level instead — `vnode.dom` will then be the
|
|
77
|
+
bindable element:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const Input = () => {
|
|
81
|
+
const binder = createWcBindable<{ value: string }>({ value: "" });
|
|
82
|
+
return {
|
|
83
|
+
oncreate: binder.oncreate,
|
|
84
|
+
onremove: binder.onremove,
|
|
85
|
+
view: () => m("my-input"),
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Low-level usage
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import m from "mithril";
|
|
94
|
+
import { wcBindable } from "@wc-bindable/mithril";
|
|
95
|
+
|
|
96
|
+
const Form = () => {
|
|
97
|
+
const state: Record<string, unknown> = { value: "" };
|
|
98
|
+
let unbind: (() => void) | null = null;
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
view: () =>
|
|
102
|
+
m("div", [
|
|
103
|
+
m("my-input", {
|
|
104
|
+
oncreate(vnode) {
|
|
105
|
+
unbind = wcBindable(vnode.dom as HTMLElement, (name, value) => {
|
|
106
|
+
state[name] = value;
|
|
107
|
+
m.redraw();
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
onremove() {
|
|
111
|
+
unbind?.();
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
m("output", state.value as string),
|
|
115
|
+
]),
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Specification
|
|
121
|
+
|
|
122
|
+
The protocol contract this adapter implements lives in [SPEC.md](../../SPEC.md); the optional input/command invocation surface and the remote wire format live in [SPEC-extensions.md](../../SPEC-extensions.md). Runnable conformance vectors are in [CONFORMANCE.md](../../CONFORMANCE.md).
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wc-bindable/mithril",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Mithril.js adapter for wc-bindable protocol",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test:watch": "vitest tests"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@wc-bindable/core": "^0.
|
|
26
|
+
"@wc-bindable/core": "^0.7.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"mithril": ">=2"
|