@opentf/afw 0.0.0-stage → 0.32.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.
Files changed (72) hide show
  1. package/README.md +66 -2
  2. package/components/Link.d.ts +12 -0
  3. package/components/Link.jsx +54 -0
  4. package/core/context.d.ts +7 -0
  5. package/core/context.js +60 -0
  6. package/core/dev.js +27 -0
  7. package/core/errors.d.ts +14 -0
  8. package/core/errors.js +48 -0
  9. package/core/html.js +10 -0
  10. package/core/metadata.js +184 -0
  11. package/core/reactive.d.ts +5 -0
  12. package/core/reactive.js +190 -0
  13. package/core/render-context.js +11 -0
  14. package/core/signals.d.ts +8 -0
  15. package/core/signals.js +264 -0
  16. package/index.d.ts +5 -0
  17. package/index.js +13 -0
  18. package/jsx-runtime.d.ts +37 -0
  19. package/package.json +60 -4
  20. package/runtime/clipboard.d.ts +7 -0
  21. package/runtime/clipboard.js +47 -0
  22. package/runtime/code-block.d.ts +14 -0
  23. package/runtime/code-block.js +76 -0
  24. package/runtime/context.browser.js +163 -0
  25. package/runtime/context.d.ts +13 -0
  26. package/runtime/context.js +44 -0
  27. package/runtime/dom.browser.js +423 -0
  28. package/runtime/dom.d.ts +142 -0
  29. package/runtime/dom.js +670 -0
  30. package/runtime/error-boundary.browser.js +140 -0
  31. package/runtime/error-boundary.d.ts +19 -0
  32. package/runtime/error-boundary.js +123 -0
  33. package/runtime/events.browser.js +29 -0
  34. package/runtime/events.d.ts +1 -0
  35. package/runtime/events.js +17 -0
  36. package/runtime/head.js +29 -0
  37. package/runtime/hmr.js +170 -0
  38. package/runtime/hydrate.browser.js +144 -0
  39. package/runtime/hydrate.d.ts +173 -0
  40. package/runtime/hydrate.js +536 -0
  41. package/runtime/index.d.ts +14 -0
  42. package/runtime/index.js +24 -0
  43. package/runtime/lifecycle.d.ts +6 -0
  44. package/runtime/lifecycle.js +14 -0
  45. package/runtime/mount.d.ts +20 -0
  46. package/runtime/mount.js +85 -0
  47. package/runtime/portal.browser.js +85 -0
  48. package/runtime/portal.d.ts +12 -0
  49. package/runtime/portal.js +74 -0
  50. package/runtime/raw-html.d.ts +11 -0
  51. package/runtime/raw-html.js +55 -0
  52. package/runtime/resource.d.ts +9 -0
  53. package/runtime/resource.js +139 -0
  54. package/runtime/route-data.d.ts +15 -0
  55. package/runtime/route-data.js +64 -0
  56. package/runtime/router.d.ts +146 -0
  57. package/runtime/router.js +765 -0
  58. package/server/adapters/node.d.ts +28 -0
  59. package/server/adapters/node.js +74 -0
  60. package/server/api.d.ts +175 -0
  61. package/server/api.js +233 -0
  62. package/server/builtins.js +42 -0
  63. package/server/cookies.d.ts +44 -0
  64. package/server/cookies.js +122 -0
  65. package/server/head.js +9 -0
  66. package/server/index.d.ts +151 -0
  67. package/server/index.js +31 -0
  68. package/server/loader.js +201 -0
  69. package/server/middleware.js +119 -0
  70. package/server/render-context.js +30 -0
  71. package/server/render.js +172 -0
  72. package/server/ssg-runtime.js +239 -0
@@ -0,0 +1,76 @@
1
+ //! Code block — a built-in that renders a (build-time syntax-highlighted) code block
2
+ //! and **owns its copy behavior**.
3
+ //
4
+ // <CodeFence html={"<div class='afw-code'>…<button class='afw-copy'>…</button><pre>…</pre></div>"} />
5
+ //
6
+ // The MDX front-end emits this for fenced code (compiles to `afw-internal-code-block`,
7
+ // like `<RawHtml>` → `afw-internal-raw-html`). The highlighted markup is a trusted
8
+ // string from the compiler, so it's assigned to `innerHTML`; the difference from
9
+ // `RawHtml` is that on connect this element wires its own `.afw-copy` button to copy
10
+ // the `<pre>` text. That keeps copy working wherever a code block is rendered (docs,
11
+ // blog, anywhere) — no delegated listener in a layout. SSG renders the string inline
12
+ // (server/builtins.js); the behavior wires when the element upgrades in the browser.
13
+
14
+ import { copyWithFeedback } from "./clipboard.js";
15
+ import { isHydrating } from "./hydrate.js";
16
+
17
+ export class CodeBlockElement extends HTMLElement {
18
+ set html(v) {
19
+ this._html = v;
20
+ if (!this.isConnected) return;
21
+ // First value handed to a host that adopted its server DOM: that markup *is* this
22
+ // string (same compiler, same build), so re-rendering would destroy and re-parse the
23
+ // block we just adopted — the single biggest source of first-paint churn on a docs
24
+ // site. The adopt walk always re-applies the prop, because the MDX front-end writes
25
+ // `html={"…"}` as an expression, so this arm is the common case. Just record it; a
26
+ // genuinely *different* value later still renders.
27
+ if (this._adopted) {
28
+ this._adopted = false;
29
+ this._applied = v;
30
+ return;
31
+ }
32
+ if (v !== this._applied) this.render();
33
+ }
34
+ get html() {
35
+ return this._html;
36
+ }
37
+
38
+ connectedCallback() {
39
+ // Server-rendered inline by `server/builtins.js` and adopted on first paint: keep the
40
+ // markup, wire only the behavior (docs/HYDRATION.md §3.4 — the same
41
+ // `isHydrating() && this.firstChild` discriminator a compiled component uses).
42
+ if (isHydrating() && this.firstChild) {
43
+ this._adopted = true;
44
+ this._rendered = true;
45
+ this._applied = this._html;
46
+ this.wireCopy();
47
+ return;
48
+ }
49
+ // The `html` property is set before append during a CSR build; render once.
50
+ if (!this._rendered) this.render();
51
+ }
52
+
53
+ render() {
54
+ this.innerHTML = this._html == null ? "" : String(this._html);
55
+ this._applied = this._html;
56
+ this.wireCopy();
57
+ this._rendered = true;
58
+ }
59
+
60
+ /** Wire the copy button to the block's `<pre>` text (idempotent). */
61
+ wireCopy() {
62
+ const button = this.querySelector(".afw-copy");
63
+ const pre = this.querySelector("pre");
64
+ if (button && pre && !button._afwWired) {
65
+ button._afwWired = true;
66
+ button.addEventListener("click", () => copyWithFeedback(button, pre.innerText));
67
+ }
68
+ }
69
+ }
70
+
71
+ // Exported so the bare import in runtime/index.js retains the registration side
72
+ // effect; JSX addresses it by the `afw-internal-code-block` tag (no import needed).
73
+ export const CodeFence = CodeBlockElement;
74
+ if (typeof customElements !== "undefined" && !customElements.get("afw-internal-code-block")) {
75
+ customElements.define("afw-internal-code-block", CodeBlockElement);
76
+ }
@@ -0,0 +1,163 @@
1
+ import { describe, expect, test } from "../../afw-test/browser-runner.js";
2
+
3
+ import { effect } from "../core/signals.js";
4
+ import { createContext, enterHost, exitHost, readContext } from "./context.js";
5
+
6
+ // Build a <afw-internal-context-provider> with a value, returning the element.
7
+ function provider(context, value) {
8
+ const el = document.createElement("afw-internal-context-provider");
9
+ el.context = context; // sets the data-afw-ctx marker
10
+ el.value = value;
11
+ return el;
12
+ }
13
+
14
+ describe("context", () => {
15
+ test("returns the default (as a signal) when there is no provider", () => {
16
+ const Theme = createContext("dark");
17
+ const child = document.createElement("div");
18
+ document.body.appendChild(child);
19
+ enterHost(child);
20
+ const t = readContext(Theme);
21
+ exitHost();
22
+ expect(t.value).toBe("dark");
23
+ child.remove();
24
+ });
25
+
26
+ test("resolves the nearest provider via DOM ancestry and stays reactive", () => {
27
+ const Theme = createContext("dark");
28
+ const p = provider(Theme, "light");
29
+ const child = document.createElement("button");
30
+ p.appendChild(child);
31
+ document.body.appendChild(p);
32
+
33
+ enterHost(child);
34
+ const t = readContext(Theme);
35
+ exitHost();
36
+
37
+ const seen = [];
38
+ effect(() => seen.push(t.value));
39
+ expect(seen).toEqual(["light"]);
40
+
41
+ p.value = "solar"; // updating the provider value updates the consumer
42
+ expect(seen).toEqual(["light", "solar"]);
43
+ p.remove();
44
+ });
45
+
46
+ test("a nested provider overrides an outer one for its subtree", () => {
47
+ const Theme = createContext("dark");
48
+ const outer = provider(Theme, "outer");
49
+ const inner = provider(Theme, "inner");
50
+ const child = document.createElement("span");
51
+ inner.appendChild(child);
52
+ outer.appendChild(inner);
53
+ document.body.appendChild(outer);
54
+
55
+ enterHost(child);
56
+ const inProvided = readContext(Theme);
57
+ exitHost();
58
+ expect(inProvided.value).toBe("inner");
59
+
60
+ // A consumer directly under the outer provider sees the outer value.
61
+ const sibling = document.createElement("span");
62
+ outer.appendChild(sibling);
63
+ enterHost(sibling);
64
+ const outProvided = readContext(Theme);
65
+ exitHost();
66
+ expect(outProvided.value).toBe("outer");
67
+ outer.remove();
68
+ });
69
+
70
+ test("resolves at connect time the way the compiler emits it", () => {
71
+ const Theme = createContext("dark");
72
+
73
+ // Mirror a compiled context consumer: enterHost(this) → readContext → exitHost().
74
+ let captured;
75
+ customElements.define(
76
+ "afw-ctx-consumer",
77
+ class extends HTMLElement {
78
+ connectedCallback() {
79
+ enterHost(this);
80
+ try {
81
+ captured = readContext(Theme);
82
+ this.textContent = captured.value;
83
+ } finally {
84
+ exitHost();
85
+ }
86
+ }
87
+ },
88
+ );
89
+
90
+ const p = provider(Theme, "light");
91
+ const consumer = document.createElement("afw-ctx-consumer");
92
+ p.appendChild(consumer); // detached
93
+ document.body.appendChild(p); // connect fires here, top-down
94
+
95
+ expect(consumer.textContent).toBe("light");
96
+ p.value = "solar";
97
+ expect(captured.value).toBe("solar"); // still wired to the provider signal
98
+ p.remove();
99
+ });
100
+
101
+ // Regression (hydration): on a server-rendered page the consumer's definition is
102
+ // registered — and every matching element in the document upgrades — *before* the
103
+ // enclosing component's hydrate code runs and assigns the provider's `context`
104
+ // prop. `readContext` resolves providers with `closest()`, so unless the server
105
+ // already wrote `data-afw-ctx` into the markup, the consumer finds nothing and
106
+ // binds to the context default forever. The SSG renderer emits that attribute
107
+ // (server/builtins.js `hostAttrs`); this proves it is what rescues the ordering.
108
+ test("a consumer upgrading before the provider's props are set still binds to it", () => {
109
+ const Theme = createContext("default-theme");
110
+ const captured = {};
111
+
112
+ // Server markup: the provider carries its token, but no script has run, so its
113
+ // `context`/`value` properties are still unset.
114
+ const host = document.createElement("div");
115
+ host.innerHTML =
116
+ `<afw-internal-context-provider data-afw-ctx="${Theme.id}">` +
117
+ `<afw-ctx-late></afw-ctx-late>` +
118
+ `</afw-internal-context-provider>`;
119
+ document.body.appendChild(host);
120
+ const p = host.firstElementChild;
121
+
122
+ // The consumer upgrades here — before the provider is given its value below.
123
+ customElements.define(
124
+ "afw-ctx-late",
125
+ class extends HTMLElement {
126
+ connectedCallback() {
127
+ enterHost(this);
128
+ try {
129
+ captured.theme = readContext(Theme);
130
+ effect(() => (this.textContent = String(captured.theme.value)));
131
+ } finally {
132
+ exitHost();
133
+ }
134
+ }
135
+ },
136
+ );
137
+ customElements.upgrade(host);
138
+
139
+ // It must have resolved the *provider's* signal, not the context default — so the
140
+ // value the hydrate pass assigns a moment later reaches it.
141
+ p.context = Theme;
142
+ p.value = "light";
143
+ expect(captured.theme.value).toBe("light");
144
+ expect(host.querySelector("afw-ctx-late").textContent).toBe("light");
145
+
146
+ p.value = "solar"; // and it stays reactive
147
+ expect(host.querySelector("afw-ctx-late").textContent).toBe("solar");
148
+ host.remove();
149
+ });
150
+
151
+ test("the host stack restores the parent host after a nested pop", () => {
152
+ const A = document.createElement("div");
153
+ const B = document.createElement("div");
154
+ enterHost(A);
155
+ enterHost(B); // nested connect
156
+ exitHost(); // B done
157
+ const Theme = createContext("x");
158
+ // Now A is current again; with no provider it falls back, proving A is on top.
159
+ const t = readContext(Theme);
160
+ exitHost();
161
+ expect(t.value).toBe("x");
162
+ });
163
+ });
@@ -0,0 +1,13 @@
1
+ export { createContext, readContext, enterHost, exitHost, getCurrentInstance } from "../core/context.js";
2
+
3
+ export class ContextProviderElement extends HTMLElement {
4
+ _signal: {
5
+ value: any;
6
+ peek(): any;
7
+ };
8
+ set context(ctx: any);
9
+ set value(v: any);
10
+ get value(): any;
11
+ }
12
+ /** JSX props consumed by the compiler-generated custom-element host. */
13
+ export const ContextProvider: { new(props: { context: { id: string; fallback: { value: unknown } }; value: unknown; children?: unknown }): ContextProviderElement; prototype: ContextProviderElement };
@@ -0,0 +1,44 @@
1
+ //! Context API — scoped dependency injection over the component tree.
2
+ //
3
+ // Unlike a module-level signal (which is global) or a prop (which must be drilled),
4
+ // context provides a value to a *subtree* and lets a nested provider override it.
5
+ // Because components compile to Custom Elements, the component tree *is* the DOM
6
+ // tree, so we resolve a consumer's provider with `closest()` — no special graph.
7
+ //
8
+ // const ThemeContext = createContext("dark");
9
+ // <ContextProvider context={ThemeContext} value={theme}> … </ContextProvider>
10
+ // const theme = $context(ThemeContext); // compiler macro; use the bare name
11
+ //
12
+ // `value` flows through the provider's element as a signal, so a reactive `value`
13
+ // (e.g. backed by `$state`) updates every consumer fine-grained.
14
+
15
+ import { signal } from "../core/signals.js";
16
+
17
+ export { createContext, readContext, enterHost, exitHost, getCurrentInstance } from "../core/context.js";
18
+
19
+ // `<ContextProvider context={Ctx} value={v}>`: a host element that publishes `v`
20
+ // (as a signal) for `Ctx` to its subtree. `context`/`value` arrive as element
21
+ // properties (the compiler sets component props reactively), so a changing `value`
22
+ // propagates to consumers automatically.
23
+ export class ContextProviderElement extends HTMLElement {
24
+ constructor() {
25
+ super();
26
+ this._signal = signal(undefined);
27
+ }
28
+ set context(ctx) {
29
+ if (ctx && ctx.id) this.setAttribute("data-afw-ctx", ctx.id);
30
+ }
31
+ set value(v) {
32
+ this._signal.value = v;
33
+ }
34
+ get value() {
35
+ return this._signal.value;
36
+ }
37
+ }
38
+
39
+ // Exported so `import { ContextProvider } from "@opentf/afw"` resolves; the import
40
+ // side effect registers the element (JSX uses the `afw-internal-context-provider` tag).
41
+ export const ContextProvider = ContextProviderElement;
42
+ if (typeof customElements !== "undefined" && !customElements.get("afw-internal-context-provider")) {
43
+ customElements.define("afw-internal-context-provider", ContextProviderElement);
44
+ }