@joist/element 4.2.3-next.12 → 4.2.3-next.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/package.json +5 -4
- package/src/lib/attr.test.ts +17 -36
- package/src/lib/element.test.ts +25 -33
- package/src/lib/element.ts +18 -11
- package/src/lib/listen.test.ts +0 -75
- package/src/lib/templating/README.md +406 -0
- package/src/lib/templating/bind.ts +40 -0
- package/src/lib/templating/define.ts +5 -0
- package/src/lib/templating/elements/async.element.test.ts +90 -0
- package/src/lib/templating/elements/async.element.ts +122 -0
- package/src/lib/templating/elements/for.element.test.ts +221 -0
- package/src/lib/templating/elements/for.element.ts +189 -0
- package/src/lib/templating/elements/if.element.test.ts +90 -0
- package/src/lib/templating/elements/if.element.ts +93 -0
- package/src/lib/templating/elements/props.element.test.ts +62 -0
- package/src/lib/templating/elements/props.element.ts +80 -0
- package/src/lib/templating/elements/scope.ts +45 -0
- package/src/lib/templating/elements/value.element.test.ts +20 -0
- package/src/lib/templating/elements/value.element.ts +41 -0
- package/src/lib/templating/events.ts +21 -0
- package/src/lib/templating/token.test.ts +74 -0
- package/src/lib/templating/token.ts +34 -0
- package/src/lib/templating.ts +2 -0
- package/target/lib/attr.test.js +19 -42
- package/target/lib/attr.test.js.map +1 -1
- package/target/lib/element.js +8 -8
- package/target/lib/element.js.map +1 -1
- package/target/lib/element.test.js +0 -68
- package/target/lib/element.test.js.map +1 -1
- package/target/lib/listen.test.js +0 -97
- package/target/lib/listen.test.js.map +1 -1
- package/target/lib/templating/bind.d.ts +1 -0
- package/target/lib/templating/bind.js +30 -0
- package/target/lib/templating/bind.js.map +1 -0
- package/target/lib/templating/define.d.ts +5 -0
- package/target/lib/templating/define.js +6 -0
- package/target/lib/templating/define.js.map +1 -0
- package/target/lib/templating/elements/async.element.d.ts +17 -0
- package/target/lib/templating/elements/async.element.js +115 -0
- package/target/lib/templating/elements/async.element.js.map +1 -0
- package/target/lib/templating/elements/async.element.test.d.ts +1 -0
- package/target/lib/templating/elements/async.element.test.js +75 -0
- package/target/lib/templating/elements/async.element.test.js.map +1 -0
- package/target/lib/templating/elements/for.element.d.ts +24 -0
- package/target/lib/templating/elements/for.element.js +186 -0
- package/target/lib/templating/elements/for.element.js.map +1 -0
- package/target/lib/templating/elements/for.element.test.d.ts +2 -0
- package/target/lib/templating/elements/for.element.test.js +153 -0
- package/target/lib/templating/elements/for.element.test.js.map +1 -0
- package/target/lib/templating/elements/if.element.d.ts +12 -0
- package/target/lib/templating/elements/if.element.js +85 -0
- package/target/lib/templating/elements/if.element.js.map +1 -0
- package/target/lib/templating/elements/if.element.test.d.ts +1 -0
- package/target/lib/templating/elements/if.element.test.js +78 -0
- package/target/lib/templating/elements/if.element.test.js.map +1 -0
- package/target/lib/templating/elements/props.element.d.ts +11 -0
- package/target/lib/templating/elements/props.element.js +92 -0
- package/target/lib/templating/elements/props.element.js.map +1 -0
- package/target/lib/templating/elements/props.element.test.d.ts +1 -0
- package/target/lib/templating/elements/props.element.test.js +53 -0
- package/target/lib/templating/elements/props.element.test.js.map +1 -0
- package/target/lib/templating/elements/scope.d.ts +13 -0
- package/target/lib/templating/elements/scope.js +59 -0
- package/target/lib/templating/elements/scope.js.map +1 -0
- package/target/lib/templating/elements/value.element.d.ts +9 -0
- package/target/lib/templating/elements/value.element.js +56 -0
- package/target/lib/templating/elements/value.element.js.map +1 -0
- package/target/lib/templating/elements/value.element.test.d.ts +1 -0
- package/target/lib/templating/elements/value.element.test.js +16 -0
- package/target/lib/templating/elements/value.element.test.js.map +1 -0
- package/target/lib/templating/events.d.ts +12 -0
- package/target/lib/templating/events.js +10 -0
- package/target/lib/templating/events.js.map +1 -0
- package/target/lib/templating/token.d.ts +8 -0
- package/target/lib/templating/token.js +27 -0
- package/target/lib/templating/token.js.map +1 -0
- package/target/lib/templating/token.test.d.ts +1 -0
- package/target/lib/templating/token.test.js +56 -0
- package/target/lib/templating/token.test.js.map +1 -0
- package/target/lib/templating.d.ts +2 -0
- package/target/lib/templating.js +3 -0
- package/target/lib/templating.js.map +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joist/element",
|
|
3
|
-
"version": "4.2.3-next.
|
|
3
|
+
"version": "4.2.3-next.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./target/lib.js",
|
|
6
6
|
"module": "./target/lib.js",
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
"src",
|
|
14
14
|
"target"
|
|
15
15
|
],
|
|
16
|
-
"sideEffects":
|
|
17
|
-
"**/define.js"
|
|
18
|
-
],
|
|
16
|
+
"sideEffects": false,
|
|
19
17
|
"description": "Intelligently apply styles to WebComponents",
|
|
20
18
|
"repository": {
|
|
21
19
|
"type": "git",
|
|
@@ -51,6 +49,9 @@
|
|
|
51
49
|
"output": [
|
|
52
50
|
"target/**",
|
|
53
51
|
"tsconfig.tsbuildinfo"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": [
|
|
54
|
+
"../observable:build"
|
|
54
55
|
]
|
|
55
56
|
},
|
|
56
57
|
"test": {
|
package/src/lib/attr.test.ts
CHANGED
|
@@ -138,6 +138,23 @@ it("should throw an error for symbols with no description", async () => {
|
|
|
138
138
|
}).to.throw("Cannot handle Symbol property without description");
|
|
139
139
|
});
|
|
140
140
|
|
|
141
|
+
it("should not reflect property to attribute", async () => {
|
|
142
|
+
@element({
|
|
143
|
+
tagName: "attr-test-5",
|
|
144
|
+
})
|
|
145
|
+
class MyElement extends HTMLElement {
|
|
146
|
+
@attr({ reflect: false })
|
|
147
|
+
accessor value = "foo";
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const el = new MyElement();
|
|
151
|
+
el.value = "bar";
|
|
152
|
+
|
|
153
|
+
expect(el.value).to.equal("bar");
|
|
154
|
+
|
|
155
|
+
expect(el.hasAttribute("value")).to.be.false;
|
|
156
|
+
});
|
|
157
|
+
|
|
141
158
|
it("non reflective attributes should still read new attribute values", async () => {
|
|
142
159
|
@element({
|
|
143
160
|
tagName: "attr-test-6",
|
|
@@ -173,39 +190,3 @@ it("should allow a manually defined attribute name", async () => {
|
|
|
173
190
|
|
|
174
191
|
el.remove();
|
|
175
192
|
});
|
|
176
|
-
|
|
177
|
-
it("should update property when attribute changes", async () => {
|
|
178
|
-
@element({
|
|
179
|
-
tagName: "attr-test-8",
|
|
180
|
-
})
|
|
181
|
-
class MyElement extends HTMLElement {
|
|
182
|
-
@attr()
|
|
183
|
-
accessor value = "foo";
|
|
184
|
-
|
|
185
|
-
@attr()
|
|
186
|
-
accessor count = 0;
|
|
187
|
-
|
|
188
|
-
@attr()
|
|
189
|
-
accessor enabled = false;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const el = new MyElement();
|
|
193
|
-
document.body.append(el);
|
|
194
|
-
|
|
195
|
-
// Test string property
|
|
196
|
-
el.setAttribute("value", "bar");
|
|
197
|
-
expect(el.value).to.equal("bar");
|
|
198
|
-
|
|
199
|
-
// Test number property
|
|
200
|
-
el.setAttribute("count", "42");
|
|
201
|
-
expect(el.count).to.equal(42);
|
|
202
|
-
|
|
203
|
-
// Test boolean property
|
|
204
|
-
el.setAttribute("enabled", "");
|
|
205
|
-
expect(el.enabled).to.equal(true);
|
|
206
|
-
|
|
207
|
-
el.removeAttribute("enabled");
|
|
208
|
-
expect(el.enabled).to.equal(false);
|
|
209
|
-
|
|
210
|
-
el.remove();
|
|
211
|
-
});
|
package/src/lib/element.test.ts
CHANGED
|
@@ -34,39 +34,31 @@ it("should write default value to attribute", async () => {
|
|
|
34
34
|
el.remove();
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
el.setAttribute("value1", "foo");
|
|
64
|
-
el.setAttribute("value2", "1");
|
|
65
|
-
el.setAttribute("value3", "false");
|
|
66
|
-
el.setAttribute("value4", "bar");
|
|
67
|
-
|
|
68
|
-
expect(observedAttrs).to.deep.equal(["value1", "value2", "value3"]);
|
|
69
|
-
});
|
|
37
|
+
// TODO: Figure out test
|
|
38
|
+
// it('should register attributes', async () => {
|
|
39
|
+
// @element({
|
|
40
|
+
// tagName: 'element-2'
|
|
41
|
+
// })
|
|
42
|
+
// class MyElement extends HTMLElement {
|
|
43
|
+
// @attr()
|
|
44
|
+
// accessor value1 = 'hello'; // no attribute
|
|
45
|
+
|
|
46
|
+
// @attr()
|
|
47
|
+
// accessor value2 = 0; // number
|
|
48
|
+
|
|
49
|
+
// @attr()
|
|
50
|
+
// accessor value3 = true; // boolean
|
|
51
|
+
|
|
52
|
+
// @attr({ observed: false }) // should be filtered out
|
|
53
|
+
// accessor value4 = 'hello world';
|
|
54
|
+
// }
|
|
55
|
+
|
|
56
|
+
// expect(Reflect.get(MyElement, 'observedAttributes')).to.deep.equal([
|
|
57
|
+
// 'value1',
|
|
58
|
+
// 'value2',
|
|
59
|
+
// 'value3'
|
|
60
|
+
// ]);
|
|
61
|
+
// });
|
|
70
62
|
|
|
71
63
|
it("should attach shadow root when the shadow property exists", async () => {
|
|
72
64
|
@element({
|
package/src/lib/element.ts
CHANGED
|
@@ -47,7 +47,11 @@ export function element<T extends ElementConstructor>(opts?: ElementOpts) {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
attributeChangedCallback(
|
|
50
|
+
attributeChangedCallback(
|
|
51
|
+
name: string,
|
|
52
|
+
oldValue: string,
|
|
53
|
+
newValue: string,
|
|
54
|
+
) {
|
|
51
55
|
const attr = meta.attrs.get(name);
|
|
52
56
|
const cbs = meta.attrChanges.get(name);
|
|
53
57
|
|
|
@@ -55,9 +59,9 @@ export function element<T extends ElementConstructor>(opts?: ElementOpts) {
|
|
|
55
59
|
if (oldValue !== newValue) {
|
|
56
60
|
const ogValue = attr.getPropValue.call(this);
|
|
57
61
|
|
|
58
|
-
if (
|
|
62
|
+
if (newValue === "") {
|
|
59
63
|
// treat as boolean
|
|
60
|
-
attr.setPropValue.call(this,
|
|
64
|
+
attr.setPropValue.call(this, true);
|
|
61
65
|
} else if (typeof ogValue === "number") {
|
|
62
66
|
// treat as number
|
|
63
67
|
attr.setPropValue.call(this, Number(newValue));
|
|
@@ -82,13 +86,13 @@ export function element<T extends ElementConstructor>(opts?: ElementOpts) {
|
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
connectedCallback() {
|
|
85
|
-
if (
|
|
86
|
-
this.#abortController = new AbortController();
|
|
87
|
-
|
|
89
|
+
if (this.isConnected) {
|
|
88
90
|
for (const { event, cb, selector } of meta.listeners) {
|
|
89
91
|
const root = selector(this);
|
|
90
92
|
|
|
91
93
|
if (root) {
|
|
94
|
+
this.#abortController = new AbortController();
|
|
95
|
+
|
|
92
96
|
root.addEventListener(event, cb.bind(this), {
|
|
93
97
|
signal: this.#abortController.signal,
|
|
94
98
|
});
|
|
@@ -96,12 +100,12 @@ export function element<T extends ElementConstructor>(opts?: ElementOpts) {
|
|
|
96
100
|
throw new Error(`could not add listener to ${root}`);
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
|
-
}
|
|
100
103
|
|
|
101
|
-
|
|
104
|
+
reflectAttributeValues(this, meta.attrs);
|
|
102
105
|
|
|
103
|
-
|
|
104
|
-
|
|
106
|
+
if (super.connectedCallback) {
|
|
107
|
+
super.connectedCallback();
|
|
108
|
+
}
|
|
105
109
|
}
|
|
106
110
|
}
|
|
107
111
|
|
|
@@ -122,7 +126,10 @@ export function element<T extends ElementConstructor>(opts?: ElementOpts) {
|
|
|
122
126
|
};
|
|
123
127
|
}
|
|
124
128
|
|
|
125
|
-
function reflectAttributeValues<T extends HTMLElement>(
|
|
129
|
+
function reflectAttributeValues<T extends HTMLElement>(
|
|
130
|
+
el: T,
|
|
131
|
+
attrs: AttrMetadata,
|
|
132
|
+
) {
|
|
126
133
|
for (const [attrName, { getPropValue, reflect }] of attrs) {
|
|
127
134
|
if (reflect) {
|
|
128
135
|
const value = getPropValue.call(el);
|
package/src/lib/listen.test.ts
CHANGED
|
@@ -102,78 +102,3 @@ describe("@listen()", () => {
|
|
|
102
102
|
el.remove();
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
|
-
|
|
106
|
-
it("should remove event listeners during cleanup", () => {
|
|
107
|
-
let clickCount = 0;
|
|
108
|
-
|
|
109
|
-
@element({
|
|
110
|
-
tagName: "listener-cleanup",
|
|
111
|
-
shadowDom: [],
|
|
112
|
-
})
|
|
113
|
-
class MyElement extends HTMLElement {
|
|
114
|
-
@listen("click")
|
|
115
|
-
onClick1() {
|
|
116
|
-
clickCount++;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
@listen("click")
|
|
120
|
-
onClick2() {
|
|
121
|
-
clickCount++;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const el = new MyElement();
|
|
126
|
-
document.body.append(el);
|
|
127
|
-
|
|
128
|
-
// First click should increment counter
|
|
129
|
-
el.shadowRoot?.dispatchEvent(new Event("click"));
|
|
130
|
-
assert.equal(clickCount, 2);
|
|
131
|
-
|
|
132
|
-
// Remove element which should cleanup listeners
|
|
133
|
-
el.remove();
|
|
134
|
-
|
|
135
|
-
// Second click after removal should not increment counter
|
|
136
|
-
el.shadowRoot?.dispatchEvent(new Event("click"));
|
|
137
|
-
assert.equal(clickCount, 2);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("should not add event listeners multiple times when element is moved", () => {
|
|
141
|
-
let clickCount = 0;
|
|
142
|
-
|
|
143
|
-
@element({
|
|
144
|
-
tagName: "listener-move",
|
|
145
|
-
shadowDom: [],
|
|
146
|
-
})
|
|
147
|
-
class MyElement extends HTMLElement {
|
|
148
|
-
@listen("click")
|
|
149
|
-
onClick() {
|
|
150
|
-
clickCount++;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const el = new MyElement();
|
|
155
|
-
const container1 = document.createElement("div");
|
|
156
|
-
const container2 = document.createElement("div");
|
|
157
|
-
|
|
158
|
-
document.body.append(container1);
|
|
159
|
-
document.body.append(container2);
|
|
160
|
-
|
|
161
|
-
// Add to first container
|
|
162
|
-
container1.append(el);
|
|
163
|
-
|
|
164
|
-
// Click should increment once
|
|
165
|
-
el.shadowRoot?.dispatchEvent(new Event("click"));
|
|
166
|
-
assert.equal(clickCount, 1);
|
|
167
|
-
|
|
168
|
-
// Move to second container
|
|
169
|
-
container2.append(el);
|
|
170
|
-
|
|
171
|
-
// Click should still only increment once
|
|
172
|
-
el.shadowRoot?.dispatchEvent(new Event("click"));
|
|
173
|
-
assert.equal(clickCount, 2);
|
|
174
|
-
|
|
175
|
-
// Cleanup
|
|
176
|
-
el.remove();
|
|
177
|
-
container1.remove();
|
|
178
|
-
container2.remove();
|
|
179
|
-
});
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# Joist Templating System
|
|
2
|
+
|
|
3
|
+
The Joist templating system provides a powerful and flexible way to handle data binding and templating in web components. This documentation covers the core components and their usage.
|
|
4
|
+
|
|
5
|
+
## Core Components
|
|
6
|
+
|
|
7
|
+
### Bind Decorator (`bind.ts`)
|
|
8
|
+
|
|
9
|
+
The `bind` decorator enables reactive data binding for web component properties. It integrates with Joist's observable system to automatically track and propagate changes.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { bind } from '@joist/element/templating.js';
|
|
13
|
+
|
|
14
|
+
class MyElement extends HTMLElement {
|
|
15
|
+
@bind()
|
|
16
|
+
myProperty: string;
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The decorator:
|
|
21
|
+
- Creates a two-way binding between component properties and templates
|
|
22
|
+
- Automatically handles value propagation through the `joist::value` event
|
|
23
|
+
- Integrates with Joist's observable system for efficient change detection
|
|
24
|
+
|
|
25
|
+
### Token System (`token.ts`)
|
|
26
|
+
|
|
27
|
+
The `JToken` class handles parsing and evaluation of binding expressions. It supports:
|
|
28
|
+
|
|
29
|
+
- Simple property bindings: `propertyName`
|
|
30
|
+
- Nested property access: `user.profile.name`
|
|
31
|
+
- Negation operator: `!isVisible`
|
|
32
|
+
|
|
33
|
+
Example usage:
|
|
34
|
+
```typescript
|
|
35
|
+
const token = new JToken('user.name');
|
|
36
|
+
const value = token.readTokenValueFrom(context);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Events (`events.ts`)
|
|
40
|
+
|
|
41
|
+
The system uses custom events for value propagation:
|
|
42
|
+
|
|
43
|
+
- `JoistValueEvent`: A custom event that handles value updates in the templating system
|
|
44
|
+
- Bubbles through the DOM tree
|
|
45
|
+
- Carries both the token and update mechanism
|
|
46
|
+
|
|
47
|
+
## Built-in Template Elements
|
|
48
|
+
|
|
49
|
+
Joist provides several built-in template elements for common templating needs:
|
|
50
|
+
|
|
51
|
+
### Conditional Rendering (`j-if`)
|
|
52
|
+
|
|
53
|
+
Conditionally renders content based on a boolean expression:
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<j-if bind="isVisible">
|
|
57
|
+
<template>
|
|
58
|
+
<div>This content is only shown when isVisible is true</div>
|
|
59
|
+
</template>
|
|
60
|
+
</j-if>
|
|
61
|
+
|
|
62
|
+
<!-- With negation -->
|
|
63
|
+
<j-if bind="!isHidden">
|
|
64
|
+
<template>
|
|
65
|
+
<div>This content is shown when isHidden is false</div>
|
|
66
|
+
</template>
|
|
67
|
+
</j-if>
|
|
68
|
+
|
|
69
|
+
<!-- With else template -->
|
|
70
|
+
<j-if bind="isLoggedIn">
|
|
71
|
+
<template>
|
|
72
|
+
<div>Welcome back!</div>
|
|
73
|
+
</template>
|
|
74
|
+
<template else>
|
|
75
|
+
<div>Please log in</div>
|
|
76
|
+
</template>
|
|
77
|
+
</j-if>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The `j-if` element supports:
|
|
81
|
+
- Boolean expressions for conditional rendering
|
|
82
|
+
- Negation operator (`!`) for inverse conditions
|
|
83
|
+
- Optional `else` template for fallback content
|
|
84
|
+
- Automatic cleanup of removed content
|
|
85
|
+
|
|
86
|
+
Common use cases:
|
|
87
|
+
- Toggling visibility of UI elements
|
|
88
|
+
- Conditional form fields
|
|
89
|
+
- Feature flags
|
|
90
|
+
- Authentication states
|
|
91
|
+
- Loading states
|
|
92
|
+
|
|
93
|
+
### Property Binding (`j-props`)
|
|
94
|
+
|
|
95
|
+
Binds values to element properties (rather than attributes). This is particularly useful for boolean properties, form inputs, and other cases where attribute binding isn't sufficient:
|
|
96
|
+
|
|
97
|
+
```html
|
|
98
|
+
<j-props>
|
|
99
|
+
<!-- Bind to boolean properties -->
|
|
100
|
+
<input type="checkbox" $.checked="isComplete">
|
|
101
|
+
|
|
102
|
+
<!-- Bind to form input values -->
|
|
103
|
+
<input type="text" $.value="userName">
|
|
104
|
+
|
|
105
|
+
<!-- Bind to custom element properties -->
|
|
106
|
+
<my-element $.data="complexObject">
|
|
107
|
+
</j-props>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Note the `$.` prefix for property bindings. This distinguishes property bindings from attribute bindings.
|
|
111
|
+
|
|
112
|
+
Common use cases:
|
|
113
|
+
- Form input states (`checked`, `value`, `disabled`)
|
|
114
|
+
- Boolean properties that don't work well as attributes
|
|
115
|
+
- Complex objects that need to be passed as properties
|
|
116
|
+
- Custom element properties
|
|
117
|
+
|
|
118
|
+
### List Rendering (`j-for`)
|
|
119
|
+
|
|
120
|
+
Renders lists of items with support for keyed updates:
|
|
121
|
+
|
|
122
|
+
```html
|
|
123
|
+
<j-for bind="todos" key="id">
|
|
124
|
+
<template>
|
|
125
|
+
<j-props>
|
|
126
|
+
<div
|
|
127
|
+
class="todo-item"
|
|
128
|
+
$.dataset.id="each.value.id"
|
|
129
|
+
$.dataset.completed="each.value.completed"
|
|
130
|
+
>
|
|
131
|
+
<j-props>
|
|
132
|
+
<input type="checkbox" $.checked="each.value.completed">
|
|
133
|
+
</j-props>
|
|
134
|
+
|
|
135
|
+
<j-value bind="each.value.text"></j-value>
|
|
136
|
+
|
|
137
|
+
<j-props>
|
|
138
|
+
<button $.disabled="!each.value.text">×</button>
|
|
139
|
+
</j-props>
|
|
140
|
+
</div>
|
|
141
|
+
</j-props>
|
|
142
|
+
</template>
|
|
143
|
+
</j-for>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The `j-for` element provides context variables:
|
|
147
|
+
- `each.value`: The current item
|
|
148
|
+
- `each.index`: Zero-based index
|
|
149
|
+
- `each.position`: One-based position
|
|
150
|
+
|
|
151
|
+
### Value Display (`j-value`)
|
|
152
|
+
|
|
153
|
+
Displays a bound value as text content:
|
|
154
|
+
|
|
155
|
+
```html
|
|
156
|
+
<j-value bind="user.name"></j-value>
|
|
157
|
+
<j-value bind="formattedPrice"></j-value>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Async State Handling (`j-async`)
|
|
161
|
+
|
|
162
|
+
Handles asynchronous operations and state management with loading, success, and error states:
|
|
163
|
+
|
|
164
|
+
```html
|
|
165
|
+
<j-async bind="userPromise">
|
|
166
|
+
<template loading>Loading user data...</template>
|
|
167
|
+
<template success>
|
|
168
|
+
<div>Welcome, <j-value bind="state.data.name"></j-value>!</div>
|
|
169
|
+
</template>
|
|
170
|
+
<template error>
|
|
171
|
+
<div>Error loading user data: <j-value bind="state.error"></j-value></div>
|
|
172
|
+
</template>
|
|
173
|
+
</j-async>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The `j-async` element supports:
|
|
177
|
+
- Promise handling with automatic state transitions
|
|
178
|
+
- Loading, success, and error templates
|
|
179
|
+
- Automatic cleanup on disconnection
|
|
180
|
+
|
|
181
|
+
Example usage:
|
|
182
|
+
```typescript
|
|
183
|
+
// In your component
|
|
184
|
+
@bind()
|
|
185
|
+
accessor userPromise = fetch('/api/user').then(r => r.json());
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```html
|
|
189
|
+
<j-async bind="userPromise">
|
|
190
|
+
<template loading>Loading...</template>
|
|
191
|
+
|
|
192
|
+
<template success>
|
|
193
|
+
<div>Welcome, <j-value bind="state.data.name"></j-value>!</div>
|
|
194
|
+
</template>
|
|
195
|
+
|
|
196
|
+
<template error>
|
|
197
|
+
<div>Error: <j-value bind="state.error"></j-value></div>
|
|
198
|
+
</template>
|
|
199
|
+
</j-async>
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Complete Example
|
|
203
|
+
|
|
204
|
+
Here's a complete todo application in a single component:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import { bind } from '@joist/element/templating.js';
|
|
208
|
+
import { element, html, css, listen, query } from '@joist/element';
|
|
209
|
+
|
|
210
|
+
interface Todo {
|
|
211
|
+
id: number;
|
|
212
|
+
text: string;
|
|
213
|
+
completed: boolean;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@element({
|
|
217
|
+
tagName: 'todo-list',
|
|
218
|
+
shadowDom: [
|
|
219
|
+
css`
|
|
220
|
+
:host {
|
|
221
|
+
display: block;
|
|
222
|
+
max-width: 600px;
|
|
223
|
+
margin: 2rem auto;
|
|
224
|
+
}
|
|
225
|
+
.form {
|
|
226
|
+
display: flex;
|
|
227
|
+
gap: 1rem;
|
|
228
|
+
}
|
|
229
|
+
.todo-item {
|
|
230
|
+
display: flex;
|
|
231
|
+
gap: 0.5rem;
|
|
232
|
+
margin: 0.5rem 0;
|
|
233
|
+
}
|
|
234
|
+
.todo-text {
|
|
235
|
+
flex: 1;
|
|
236
|
+
}
|
|
237
|
+
[data-completed="true"] .todo-text {
|
|
238
|
+
text-decoration: line-through;
|
|
239
|
+
opacity: 0.6;
|
|
240
|
+
}
|
|
241
|
+
`,
|
|
242
|
+
html`
|
|
243
|
+
<form class="form">
|
|
244
|
+
<input type="text" placeholder="What needs to be done?">
|
|
245
|
+
<button type="submit">Add</button>
|
|
246
|
+
</form>
|
|
247
|
+
|
|
248
|
+
<j-if bind="!todos.length">
|
|
249
|
+
<template>
|
|
250
|
+
<p>No todos yet!</p>
|
|
251
|
+
</template>
|
|
252
|
+
</j-if>
|
|
253
|
+
|
|
254
|
+
<j-for id="todos" bind="todos" key="id">
|
|
255
|
+
<template>
|
|
256
|
+
<j-props class="todo-item" $.dataset.completed="each.value.completed">
|
|
257
|
+
<j-props>
|
|
258
|
+
<input type="checkbox" $.id="each.value.id" $.checked="each.value.completed">
|
|
259
|
+
<j-value class="todo-text" bind="each.value.text"></j-value>
|
|
260
|
+
<button $.id="each.value.id" $.disabled="!each.value.text">×</button>
|
|
261
|
+
</j-props>
|
|
262
|
+
</j-props>
|
|
263
|
+
</template>
|
|
264
|
+
</j-for>
|
|
265
|
+
|
|
266
|
+
<j-value bind="stats.remaining"></j-value> remaining
|
|
267
|
+
`
|
|
268
|
+
]
|
|
269
|
+
})
|
|
270
|
+
export class TodoList extends HTMLElement {
|
|
271
|
+
@bind()
|
|
272
|
+
accessor todos: Todo[] = [];
|
|
273
|
+
|
|
274
|
+
@bind()
|
|
275
|
+
accessor stats = {
|
|
276
|
+
remaining: 0,
|
|
277
|
+
completed: 0
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
#nextId = 1;
|
|
281
|
+
#input = query('input');
|
|
282
|
+
|
|
283
|
+
#updateStats() {
|
|
284
|
+
this.stats = {
|
|
285
|
+
completed: this.todos.filter(todo => todo.completed).length,
|
|
286
|
+
remaining: this.todos.length - this.stats.completed
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
@listen('submit', 'form')
|
|
291
|
+
onSubmit(e: SubmitEvent) {
|
|
292
|
+
e.preventDefault();
|
|
293
|
+
const input = this.#input();
|
|
294
|
+
const text = input.value.trim();
|
|
295
|
+
|
|
296
|
+
if (text) {
|
|
297
|
+
this.todos = [
|
|
298
|
+
...this.todos,
|
|
299
|
+
{ id: this.#nextId++, text, completed: false }
|
|
300
|
+
];
|
|
301
|
+
|
|
302
|
+
input.value = '';
|
|
303
|
+
|
|
304
|
+
this.#updateStats();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
@listen('change', '#todos')
|
|
309
|
+
onToggle(e: Event) {
|
|
310
|
+
if(e.target instanceof HTMLInputElement) {
|
|
311
|
+
const id = Number(e.target.id);
|
|
312
|
+
|
|
313
|
+
this.todos = this.todos.map(todo => {
|
|
314
|
+
if (todo.id === id) {
|
|
315
|
+
return { ...todo, completed: checkbox.checked };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return todo;
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
this.#updateStats();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
@listen('click', '#todos')
|
|
326
|
+
onDelete(e: Event) {
|
|
327
|
+
if(e.target instanceof HTMLElement) {
|
|
328
|
+
const id = Number(e.target.id);
|
|
329
|
+
|
|
330
|
+
this.todos = this.todos.filter(todo => todo.id !== id);
|
|
331
|
+
|
|
332
|
+
this.#updateStats();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Usage
|
|
339
|
+
|
|
340
|
+
1. Use the `@bind()` decorator on properties you want to make bindable
|
|
341
|
+
2. Properties will automatically integrate with the templating system
|
|
342
|
+
3. Changes are propagated through the component tree using the custom event system
|
|
343
|
+
|
|
344
|
+
## Integration with Observable
|
|
345
|
+
|
|
346
|
+
The templating system is built on top of Joist's observable system (`@joist/observable`), providing:
|
|
347
|
+
- Automatic change detection
|
|
348
|
+
- Efficient updates
|
|
349
|
+
- Integration with the component lifecycle
|
|
350
|
+
|
|
351
|
+
## Best Practices
|
|
352
|
+
|
|
353
|
+
1. Use the `@bind()` decorator only on properties that need reactivity
|
|
354
|
+
2. Keep binding expressions simple and avoid deep nesting
|
|
355
|
+
3. Consider performance implications when binding to frequently changing values
|
|
356
|
+
4. Always use a `key` attribute with `j-for` when items can be reordered
|
|
357
|
+
5. Place template content directly inside `j-if` and `j-for` elements
|
|
358
|
+
|
|
359
|
+
## Manual Value Handling
|
|
360
|
+
|
|
361
|
+
You can manually handle value requests and updates by listening for the `joist::value` event. This is useful when you need more control over the binding process or want to implement custom binding logic:
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
class MyElement extends HTMLElement {
|
|
365
|
+
connectedCallback() {
|
|
366
|
+
// Listen for value requests
|
|
367
|
+
this.addEventListener('joist::value', (e: JoistValueEvent) => {
|
|
368
|
+
const token = e.token;
|
|
369
|
+
|
|
370
|
+
// Handle the value request
|
|
371
|
+
if (token.bindTo === 'myValue') {
|
|
372
|
+
// Update the value
|
|
373
|
+
e.update({
|
|
374
|
+
oldValue: this.myValue,
|
|
375
|
+
newValue: this.myValue
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Example with async value handling:
|
|
384
|
+
```typescript
|
|
385
|
+
class MyElement extends HTMLElement {
|
|
386
|
+
connectedCallback() {
|
|
387
|
+
this.addEventListener('joist::value', (e: JoistValueEvent) => {
|
|
388
|
+
const token = e.token;
|
|
389
|
+
|
|
390
|
+
if (token.bindTo === 'userData') {
|
|
391
|
+
e.update({
|
|
392
|
+
oldValue: this.userData,
|
|
393
|
+
newValue: data
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
Common use cases for manual value handling:
|
|
402
|
+
- Custom data transformation before binding
|
|
403
|
+
- Async data loading and caching
|
|
404
|
+
- Complex state management
|
|
405
|
+
- Integration with external data sources
|
|
406
|
+
- Custom validation or error handling
|