@wc-bindable/mithril 0.6.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/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wc-bindable-protocol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,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
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type UnbindFn = () => void;
|
|
2
|
+
export type OnUpdate = (name: string, value: unknown) => void;
|
|
3
|
+
export declare function wcBindable(el: HTMLElement, onUpdate: OnUpdate): UnbindFn;
|
|
4
|
+
export interface CreateWcBindableOptions {
|
|
5
|
+
redraw?: () => void;
|
|
6
|
+
}
|
|
7
|
+
export interface WcBindableState<V extends object> {
|
|
8
|
+
readonly values: V;
|
|
9
|
+
oncreate(vnode: {
|
|
10
|
+
dom: Element;
|
|
11
|
+
}): void;
|
|
12
|
+
onremove(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare function createWcBindable<V extends object = Record<string, unknown>>(initialValues?: Partial<V>, options?: CreateWcBindableOptions): WcBindableState<V>;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC;AAClC,MAAM,MAAM,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAE9D,wBAAgB,UAAU,CAAC,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,GAAG,QAAQ,CAGxE;AAED,MAAM,WAAW,uBAAuB;IACtC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,MAAM;IAC/C,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE;QAAE,GAAG,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,CAAC;IACxC,QAAQ,IAAI,IAAI,CAAC;CAClB;AAED,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzE,aAAa,GAAE,OAAO,CAAC,CAAC,CAAM,EAC9B,OAAO,GAAE,uBAA4B,GACpC,eAAe,CAAC,CAAC,CAAC,CAwBpB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import m from "mithril";
|
|
2
|
+
import { bind, isWcBindable } from "@wc-bindable/core";
|
|
3
|
+
export function wcBindable(el, onUpdate) {
|
|
4
|
+
if (!isWcBindable(el))
|
|
5
|
+
return () => { };
|
|
6
|
+
return bind(el, onUpdate);
|
|
7
|
+
}
|
|
8
|
+
export function createWcBindable(initialValues = {}, options = {}) {
|
|
9
|
+
const redraw = options.redraw ?? (() => m.redraw());
|
|
10
|
+
const state = { ...initialValues };
|
|
11
|
+
let unbind = null;
|
|
12
|
+
return {
|
|
13
|
+
get values() {
|
|
14
|
+
return state;
|
|
15
|
+
},
|
|
16
|
+
oncreate(vnode) {
|
|
17
|
+
const el = vnode.dom;
|
|
18
|
+
if (!isWcBindable(el))
|
|
19
|
+
return;
|
|
20
|
+
unbind = bind(el, (name, value) => {
|
|
21
|
+
state[name] = value;
|
|
22
|
+
redraw();
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
onremove() {
|
|
26
|
+
if (unbind) {
|
|
27
|
+
unbind();
|
|
28
|
+
unbind = null;
|
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YAChB,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wc-bindable/mithril",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Mithril.js adapter for wc-bindable protocol",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc --watch",
|
|
22
|
+
"test": "vitest run tests",
|
|
23
|
+
"test:watch": "vitest tests"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@wc-bindable/core": "^0.6.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"mithril": ">=2"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/wc-bindable-protocol/wc-bindable-protocol.git",
|
|
34
|
+
"directory": "packages/mithril"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT"
|
|
37
|
+
}
|