@joist/element 4.2.3-next.13 → 4.2.3-next.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.
Files changed (82) hide show
  1. package/package.json +5 -2
  2. package/src/lib/attr.test.ts +17 -36
  3. package/src/lib/element.test.ts +25 -33
  4. package/src/lib/element.ts +18 -11
  5. package/src/lib/listen.test.ts +0 -75
  6. package/src/lib/templating/README.md +406 -0
  7. package/src/lib/templating/bind.ts +40 -0
  8. package/src/lib/templating/define.ts +5 -0
  9. package/src/lib/templating/elements/async.element.test.ts +90 -0
  10. package/src/lib/templating/elements/async.element.ts +122 -0
  11. package/src/lib/templating/elements/for.element.test.ts +221 -0
  12. package/src/lib/templating/elements/for.element.ts +189 -0
  13. package/src/lib/templating/elements/if.element.test.ts +90 -0
  14. package/src/lib/templating/elements/if.element.ts +93 -0
  15. package/src/lib/templating/elements/props.element.test.ts +62 -0
  16. package/src/lib/templating/elements/props.element.ts +80 -0
  17. package/src/lib/templating/elements/scope.ts +45 -0
  18. package/src/lib/templating/elements/value.element.test.ts +20 -0
  19. package/src/lib/templating/elements/value.element.ts +41 -0
  20. package/src/lib/templating/events.ts +21 -0
  21. package/src/lib/templating/token.test.ts +74 -0
  22. package/src/lib/templating/token.ts +34 -0
  23. package/src/lib/templating.ts +2 -0
  24. package/target/lib/attr.test.js +19 -42
  25. package/target/lib/attr.test.js.map +1 -1
  26. package/target/lib/element.js +8 -8
  27. package/target/lib/element.js.map +1 -1
  28. package/target/lib/element.test.js +0 -68
  29. package/target/lib/element.test.js.map +1 -1
  30. package/target/lib/listen.test.js +0 -97
  31. package/target/lib/listen.test.js.map +1 -1
  32. package/target/lib/templating/bind.d.ts +1 -0
  33. package/target/lib/templating/bind.js +30 -0
  34. package/target/lib/templating/bind.js.map +1 -0
  35. package/target/lib/templating/define.d.ts +5 -0
  36. package/target/lib/templating/define.js +6 -0
  37. package/target/lib/templating/define.js.map +1 -0
  38. package/target/lib/templating/elements/async.element.d.ts +17 -0
  39. package/target/lib/templating/elements/async.element.js +115 -0
  40. package/target/lib/templating/elements/async.element.js.map +1 -0
  41. package/target/lib/templating/elements/async.element.test.d.ts +1 -0
  42. package/target/lib/templating/elements/async.element.test.js +75 -0
  43. package/target/lib/templating/elements/async.element.test.js.map +1 -0
  44. package/target/lib/templating/elements/for.element.d.ts +24 -0
  45. package/target/lib/templating/elements/for.element.js +186 -0
  46. package/target/lib/templating/elements/for.element.js.map +1 -0
  47. package/target/lib/templating/elements/for.element.test.d.ts +2 -0
  48. package/target/lib/templating/elements/for.element.test.js +153 -0
  49. package/target/lib/templating/elements/for.element.test.js.map +1 -0
  50. package/target/lib/templating/elements/if.element.d.ts +12 -0
  51. package/target/lib/templating/elements/if.element.js +85 -0
  52. package/target/lib/templating/elements/if.element.js.map +1 -0
  53. package/target/lib/templating/elements/if.element.test.d.ts +1 -0
  54. package/target/lib/templating/elements/if.element.test.js +78 -0
  55. package/target/lib/templating/elements/if.element.test.js.map +1 -0
  56. package/target/lib/templating/elements/props.element.d.ts +11 -0
  57. package/target/lib/templating/elements/props.element.js +92 -0
  58. package/target/lib/templating/elements/props.element.js.map +1 -0
  59. package/target/lib/templating/elements/props.element.test.d.ts +1 -0
  60. package/target/lib/templating/elements/props.element.test.js +53 -0
  61. package/target/lib/templating/elements/props.element.test.js.map +1 -0
  62. package/target/lib/templating/elements/scope.d.ts +13 -0
  63. package/target/lib/templating/elements/scope.js +59 -0
  64. package/target/lib/templating/elements/scope.js.map +1 -0
  65. package/target/lib/templating/elements/value.element.d.ts +9 -0
  66. package/target/lib/templating/elements/value.element.js +56 -0
  67. package/target/lib/templating/elements/value.element.js.map +1 -0
  68. package/target/lib/templating/elements/value.element.test.d.ts +1 -0
  69. package/target/lib/templating/elements/value.element.test.js +16 -0
  70. package/target/lib/templating/elements/value.element.test.js.map +1 -0
  71. package/target/lib/templating/events.d.ts +12 -0
  72. package/target/lib/templating/events.js +10 -0
  73. package/target/lib/templating/events.js.map +1 -0
  74. package/target/lib/templating/token.d.ts +8 -0
  75. package/target/lib/templating/token.js +27 -0
  76. package/target/lib/templating/token.js.map +1 -0
  77. package/target/lib/templating/token.test.d.ts +1 -0
  78. package/target/lib/templating/token.test.js +56 -0
  79. package/target/lib/templating/token.test.js.map +1 -0
  80. package/target/lib/templating.d.ts +2 -0
  81. package/target/lib/templating.js +3 -0
  82. package/target/lib/templating.js.map +1 -0
@@ -0,0 +1,90 @@
1
+ import "./if.element.js";
2
+
3
+ import { fixtureSync, html } from "@open-wc/testing";
4
+ import { assert } from "chai";
5
+
6
+ import type { JoistValueEvent } from "../events.js";
7
+
8
+ it("should render content when the bind value is truthy", () => {
9
+ const element = fixtureSync(html`
10
+ <div
11
+ @joist::value=${(e: JoistValueEvent) => {
12
+ e.update({ oldValue: null, newValue: true });
13
+ }}
14
+ >
15
+ <j-if bind="test">
16
+ <template>Visible Content</template>
17
+ </j-if>
18
+ </div>
19
+ `);
20
+
21
+ assert.equal(element.textContent?.trim(), "Visible Content");
22
+ });
23
+
24
+ it("should not render content when the bind value is falsy", () => {
25
+ const element = fixtureSync(html`
26
+ <div
27
+ @joist::value=${(e: JoistValueEvent) => {
28
+ e.update({ oldValue: null, newValue: true });
29
+ e.update({ oldValue: null, newValue: false });
30
+ }}
31
+ >
32
+ <j-if bind="test">
33
+ <template>Visible Content</template>
34
+ </j-if>
35
+ </div>
36
+ `);
37
+
38
+ assert.equal(element.textContent?.trim(), "");
39
+ });
40
+
41
+ it("should handle negated tokens correctly", () => {
42
+ const element = fixtureSync(html`
43
+ <div
44
+ @joist::value=${(e: JoistValueEvent) => {
45
+ e.update({ oldValue: null, newValue: false });
46
+ }}
47
+ >
48
+ <j-if bind="!test">
49
+ <template>Visible Content</template>
50
+ </j-if>
51
+ </div>
52
+ `);
53
+
54
+ assert.equal(element.textContent?.trim(), "Visible Content");
55
+ });
56
+
57
+ it("should render else template when condition is falsy", () => {
58
+ const element = fixtureSync(html`
59
+ <div
60
+ @joist::value=${(e: JoistValueEvent) => {
61
+ e.update({ oldValue: null, newValue: false });
62
+ }}
63
+ >
64
+ <j-if bind="test">
65
+ <template>If Content</template>
66
+ <template else>Else Content</template>
67
+ </j-if>
68
+ </div>
69
+ `);
70
+
71
+ assert.equal(element.textContent?.trim(), "Else Content");
72
+ });
73
+
74
+ it("should switch between if and else templates", () => {
75
+ const element = fixtureSync(html`
76
+ <div
77
+ @joist::value=${(e: JoistValueEvent) => {
78
+ e.update({ oldValue: null, newValue: false });
79
+ e.update({ oldValue: false, newValue: true });
80
+ }}
81
+ >
82
+ <j-if bind="test">
83
+ <template>If Content</template>
84
+ <template else>Else Content</template>
85
+ </j-if>
86
+ </div>
87
+ `);
88
+
89
+ assert.equal(element.textContent?.trim(), "If Content");
90
+ });
@@ -0,0 +1,93 @@
1
+ import { attr } from "../../attr.js";
2
+ import { element } from "../../element.js";
3
+ import { queryAll } from "../../query-all.js";
4
+ import { css, html } from "../../tags.js";
5
+
6
+ import { JoistValueEvent } from "../events.js";
7
+ import { JToken } from "../token.js";
8
+
9
+ declare global {
10
+ interface HTMLElementTagNameMap {
11
+ "j-if": JoistIfElement;
12
+ }
13
+ }
14
+
15
+ @element({
16
+ tagName: "j-if",
17
+ // prettier-ignore
18
+ shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
19
+ })
20
+ export class JoistIfElement extends HTMLElement {
21
+ @attr()
22
+ accessor bind = "";
23
+
24
+ #templates = queryAll<HTMLTemplateElement>("template", this);
25
+
26
+ connectedCallback(): void {
27
+ const templates = Array.from(this.#templates());
28
+
29
+ if (templates.length === 0) {
30
+ throw new Error("j-if requires at least one template element");
31
+ }
32
+
33
+ if (templates.length > 2) {
34
+ throw new Error("j-if can only have two template elements (if and else)");
35
+ }
36
+
37
+ if (
38
+ templates.length === 2 &&
39
+ !templates.some((t) => t.hasAttribute("else"))
40
+ ) {
41
+ throw new Error(
42
+ "When using two templates, one must have the else attribute",
43
+ );
44
+ }
45
+
46
+ if (templates.length === 2 && templates[0].hasAttribute("else")) {
47
+ // Swap templates to ensure if template is first
48
+ [templates[0], templates[1]] = [templates[1], templates[0]];
49
+ }
50
+
51
+ // make sure there are no other nodes after the template
52
+ this.#clean();
53
+
54
+ const token = new JToken(this.bind);
55
+
56
+ this.dispatchEvent(
57
+ new JoistValueEvent(token, ({ newValue, oldValue }) => {
58
+ if (newValue !== oldValue) {
59
+ if (typeof newValue === "object" && newValue !== null) {
60
+ this.apply(token.readTokenValueFrom(newValue), token.isNegated);
61
+ } else {
62
+ this.apply(newValue, token.isNegated);
63
+ }
64
+ }
65
+ }),
66
+ );
67
+ }
68
+
69
+ apply(value: unknown, isNegative: boolean): void {
70
+ this.#clean();
71
+
72
+ const templates = this.#templates();
73
+
74
+ const shouldShowIf = isNegative ? !value : value;
75
+ const templateToUse = shouldShowIf ? templates[0] : templates[1];
76
+
77
+ if (templateToUse) {
78
+ const content = document.importNode(templateToUse.content, true);
79
+
80
+ this.appendChild(content);
81
+ }
82
+ }
83
+
84
+ #clean(): void {
85
+ while (!(this.lastChild instanceof HTMLTemplateElement)) {
86
+ this.lastChild?.remove();
87
+ }
88
+ }
89
+
90
+ disconnectedCallback(): void {
91
+ this.#clean();
92
+ }
93
+ }
@@ -0,0 +1,62 @@
1
+ import "./props.element.js";
2
+
3
+ import { fixtureSync, html } from "@open-wc/testing";
4
+ import { assert } from "chai";
5
+
6
+ import type { JoistValueEvent } from "../events.js";
7
+
8
+ it("should pass props to child", () => {
9
+ const element = fixtureSync(html`
10
+ <div
11
+ @joist::value=${(e: JoistValueEvent) => {
12
+ if (e.token.bindTo === "href") {
13
+ e.update({
14
+ oldValue: null,
15
+ newValue: "$foo",
16
+ });
17
+ }
18
+
19
+ if (e.token.bindTo === "target") {
20
+ e.update({
21
+ oldValue: null,
22
+ newValue: {
23
+ value: "_blank",
24
+ },
25
+ });
26
+ }
27
+ }}
28
+ >
29
+ <j-props>
30
+ <a $href="href" $target="target.value">Hello World</a>
31
+ </j-props>
32
+ </div>
33
+ `);
34
+
35
+ const anchor = element.querySelector("a");
36
+
37
+ assert.equal(anchor?.getAttribute("href"), "$foo");
38
+ assert.equal(anchor?.getAttribute("target"), "_blank");
39
+ });
40
+
41
+ it("should pass props to specified child", () => {
42
+ const element = fixtureSync(html`
43
+ <div
44
+ @joist::value=${(e: JoistValueEvent) => {
45
+ e.update({
46
+ oldValue: null,
47
+ newValue: "#foo",
48
+ });
49
+ }}
50
+ >
51
+ <j-props>
52
+ <a>Default</a>
53
+ <a id="test" $href="href">Target</a>
54
+ </j-props>
55
+ </div>
56
+ `);
57
+
58
+ const anchor = element.querySelectorAll("a");
59
+
60
+ assert.equal(anchor[0].getAttribute("href"), null);
61
+ assert.equal(anchor[1].getAttribute("href"), "#foo");
62
+ });
@@ -0,0 +1,80 @@
1
+ import { attr } from "../../attr.js";
2
+ import { element } from "../../element.js";
3
+ import { css, html } from "../../tags.js";
4
+
5
+ import { JoistValueEvent } from "../events.js";
6
+ import { JToken } from "../token.js";
7
+
8
+ export class JAttrToken extends JToken {
9
+ mapTo: string;
10
+ mapsToProp: boolean;
11
+
12
+ constructor(attr: Attr) {
13
+ if (!attr.name.startsWith("$")) {
14
+ throw new Error(
15
+ `Invalid attribute token: ${attr.name}, should start with $`,
16
+ );
17
+ }
18
+
19
+ super(attr.value);
20
+
21
+ this.mapsToProp = attr.name.startsWith("$.");
22
+
23
+ this.mapTo = attr.name.slice(this.mapsToProp ? 2 : 1);
24
+ }
25
+ }
26
+
27
+ @element({
28
+ tagName: "j-props",
29
+ // prettier-ignore
30
+ shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
31
+ })
32
+ export class JoistIfElement extends HTMLElement {
33
+ @attr()
34
+ accessor target = "";
35
+
36
+ connectedCallback(): void {
37
+ this.#bindProps([this]); // bind own props
38
+ this.#bindProps(this.children); // bind child props
39
+ }
40
+
41
+ #bindProps(children: Iterable<Element>) {
42
+ for (const child of children) {
43
+ for (const attr of child.attributes) {
44
+ if (attr.name.startsWith("$")) {
45
+ const token = new JAttrToken(attr);
46
+
47
+ this.dispatchEvent(
48
+ new JoistValueEvent(token, ({ newValue, oldValue }) => {
49
+ if (newValue === oldValue) {
50
+ return;
51
+ }
52
+
53
+ let valueToWrite = newValue;
54
+
55
+ if (typeof newValue === "object" && newValue !== null) {
56
+ valueToWrite = token.readTokenValueFrom(newValue);
57
+ }
58
+
59
+ if (token.isNegated) {
60
+ valueToWrite = !valueToWrite;
61
+ }
62
+
63
+ if (token.mapsToProp) {
64
+ Reflect.set(child, token.mapTo, valueToWrite);
65
+ } else {
66
+ if (valueToWrite === true) {
67
+ child.setAttribute(token.mapTo, "");
68
+ } else if (valueToWrite === false) {
69
+ child.removeAttribute(token.mapTo);
70
+ } else {
71
+ child.setAttribute(token.mapTo, String(valueToWrite));
72
+ }
73
+ }
74
+ }),
75
+ );
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
@@ -0,0 +1,45 @@
1
+ import { attr } from "../../attr.js";
2
+ import { element } from "../../element.js";
3
+ import { listen } from "../../listen.js";
4
+ import { css, html } from "../../tags.js";
5
+ import type { JoistValueEvent } from "../events.js";
6
+
7
+ declare global {
8
+ interface HTMLElementTagNameMap {
9
+ "j-scope": JoistScopeElement;
10
+ }
11
+ }
12
+
13
+ @element({
14
+ tagName: "j-value",
15
+ // prettier-ignore
16
+ shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
17
+ })
18
+ export class JoistScopeElement extends HTMLElement {
19
+ @attr()
20
+ accessor name = "";
21
+
22
+ @attr()
23
+ accessor value = "";
24
+
25
+ #binding: JoistValueEvent | null = null;
26
+
27
+ @listen("joist::value")
28
+ onJoistValueFound(e: JoistValueEvent): void {
29
+ if (e.token.bindTo === this.name) {
30
+ e.stopPropagation();
31
+
32
+ this.#binding = e;
33
+
34
+ this.#binding.update({ oldValue: null, newValue: this.value });
35
+ }
36
+ }
37
+
38
+ attributeChangedCallback(
39
+ _: string,
40
+ oldValue: string,
41
+ newValue: string,
42
+ ): void {
43
+ this.#binding?.update({ oldValue, newValue });
44
+ }
45
+ }
@@ -0,0 +1,20 @@
1
+ import "./value.element.js";
2
+
3
+ import { fixtureSync, html } from "@open-wc/testing";
4
+ import { assert } from "chai";
5
+
6
+ import type { JoistValueEvent } from "../events.js";
7
+
8
+ it("should render content when the bind value is truthy", () => {
9
+ const element = fixtureSync(html`
10
+ <div
11
+ @joist::value=${(e: JoistValueEvent) => {
12
+ e.update({ oldValue: null, newValue: "Hello World" });
13
+ }}
14
+ >
15
+ <j-value bind="test"></j-value>
16
+ </div>
17
+ `);
18
+
19
+ assert.equal(element.textContent?.trim(), "Hello World");
20
+ });
@@ -0,0 +1,41 @@
1
+ import { attr } from "../../attr.js";
2
+ import { element } from "../../element.js";
3
+ import { css, html } from "../../tags.js";
4
+ import { JoistValueEvent } from "../events.js";
5
+ import { JToken } from "../token.js";
6
+
7
+ declare global {
8
+ interface HTMLElementTagNameMap {
9
+ "j-value": JoistValueElement;
10
+ }
11
+ }
12
+
13
+ @element({
14
+ tagName: "j-value",
15
+ // prettier-ignore
16
+ shadowDom: [css`:host{display: contents;}`, html`<slot></slot>`],
17
+ })
18
+ export class JoistValueElement extends HTMLElement {
19
+ @attr()
20
+ accessor bind = "";
21
+
22
+ connectedCallback(): void {
23
+ const token = new JToken(this.bind);
24
+
25
+ this.dispatchEvent(
26
+ new JoistValueEvent(token, (value) => {
27
+ let valueToWrite: string;
28
+
29
+ if (typeof value.newValue === "object" && value.newValue !== null) {
30
+ valueToWrite = String(token.readTokenValueFrom(value.newValue));
31
+ } else {
32
+ valueToWrite = String(value.newValue);
33
+ }
34
+
35
+ if (this.textContent !== valueToWrite) {
36
+ this.textContent = valueToWrite;
37
+ }
38
+ }),
39
+ );
40
+ }
41
+ }
@@ -0,0 +1,21 @@
1
+ import type { Change } from "@joist/observable";
2
+
3
+ import type { JToken } from "./token.js";
4
+
5
+ declare global {
6
+ interface HTMLElementEventMap {
7
+ "joist::value": JoistValueEvent;
8
+ }
9
+ }
10
+
11
+ export class JoistValueEvent extends Event {
12
+ readonly token: JToken;
13
+ readonly update: (value: Change<unknown>) => void;
14
+
15
+ constructor(bindTo: JToken, update: (value: Change<unknown>) => void) {
16
+ super("joist::value", { bubbles: true, composed: true });
17
+
18
+ this.token = bindTo;
19
+ this.update = update;
20
+ }
21
+ }
@@ -0,0 +1,74 @@
1
+ import { assert } from "chai";
2
+
3
+ import { JToken } from "./token.js";
4
+
5
+ describe("JToken", () => {
6
+ describe("constructor", () => {
7
+ it("should initialize with a raw token", () => {
8
+ const token = new JToken("example.token");
9
+ assert.equal(token.rawToken, "example.token");
10
+ });
11
+
12
+ it("should set isNegated to true if the token starts with '!'", () => {
13
+ const token = new JToken("!example.token");
14
+ assert.isTrue(token.isNegated);
15
+ });
16
+
17
+ it("should set isNegated to false if the token does not start with '!'", () => {
18
+ const token = new JToken("example.token");
19
+ assert.isFalse(token.isNegated);
20
+ });
21
+
22
+ it("should correctly parse the bindTo property", () => {
23
+ const token = new JToken("example.token");
24
+ assert.equal(token.bindTo, "example");
25
+ });
26
+
27
+ it("should correctly parse the path property", () => {
28
+ const token = new JToken("example.token.part");
29
+ assert.deepEqual(token.path, ["token", "part"]);
30
+ });
31
+
32
+ it("should remove '!' from bindTo if present", () => {
33
+ const token = new JToken("!example.token");
34
+ assert.equal(token.bindTo, "example");
35
+ });
36
+ });
37
+
38
+ describe("readTokenValueFrom", () => {
39
+ it("should read the value from a nested object", () => {
40
+ const token = new JToken("example.token.part");
41
+ const obj = { token: { part: 42 } };
42
+ const value = token.readTokenValueFrom<number>(obj);
43
+ assert.equal(value, 42);
44
+ });
45
+
46
+ it("should return undefined if the path does not exist", () => {
47
+ const token = new JToken("example.nonexistent.path");
48
+ const obj = { token: { part: 42 } };
49
+ const value = token.readTokenValueFrom(obj);
50
+ assert.isUndefined(value);
51
+ });
52
+
53
+ it("should handle empty paths gracefully", () => {
54
+ const token = new JToken("example");
55
+ const obj = { foo: 42 };
56
+ const value = token.readTokenValueFrom(obj);
57
+
58
+ assert.deepEqual(value, { foo: 42 });
59
+ });
60
+
61
+ it("should throw an error if the object is null or undefined", () => {
62
+ const token = new JToken("example.token");
63
+ assert.throws(
64
+ () => token.readTokenValueFrom<any>(null as any),
65
+ TypeError,
66
+ );
67
+
68
+ assert.throws(
69
+ () => token.readTokenValueFrom<any>(undefined as any),
70
+ TypeError,
71
+ );
72
+ });
73
+ });
74
+ });
@@ -0,0 +1,34 @@
1
+ export class JToken {
2
+ rawToken: string;
3
+ isNegated = false;
4
+ bindTo: string;
5
+ path: string[] = [];
6
+
7
+ constructor(rawToken: string) {
8
+ this.rawToken = rawToken;
9
+
10
+ this.isNegated = this.rawToken.startsWith("!");
11
+
12
+ this.path = this.rawToken.split(".");
13
+ this.bindTo = this.path.shift() ?? "";
14
+ this.bindTo = this.bindTo.replaceAll("!", "");
15
+ }
16
+
17
+ readTokenValueFrom<T = unknown>(obj: object): T {
18
+ let pointer: any = obj;
19
+
20
+ if (!this.path.length) {
21
+ return pointer;
22
+ }
23
+
24
+ for (const part of this.path) {
25
+ pointer = pointer[part];
26
+
27
+ if (pointer === undefined) {
28
+ break;
29
+ }
30
+ }
31
+
32
+ return pointer;
33
+ }
34
+ }
@@ -0,0 +1,2 @@
1
+ export { bind } from "./templating/bind.js";
2
+ export { JoistValueEvent } from "./templating/events.js";