@intent-framework/dom 0.1.0-alpha.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mahyar
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.
@@ -0,0 +1,19 @@
1
+ import type { ScreenDefinition, DefaultScreenServices } from "@intent-framework/core";
2
+ import type { Router, RoutePathArgs } from "@intent-framework/router";
3
+ export type RouterDomHandle<Routes extends Record<string, {
4
+ path: string;
5
+ }>> = {
6
+ navigate<Name extends Extract<keyof Routes, string>>(name: Name, ...args: RoutePathArgs<Routes[Name]["path"]>): void;
7
+ renderPath(pathname: string): void;
8
+ dispose(): void;
9
+ };
10
+ export type RenderRouterOptions<TServices extends object = DefaultScreenServices> = {
11
+ target: HTMLElement;
12
+ window?: Window;
13
+ notFound?: ScreenDefinition<TServices> | ((pathname: string) => ScreenDefinition<TServices>);
14
+ services?: Omit<TServices, "navigate" | "route">;
15
+ showScreenName?: boolean;
16
+ };
17
+ export declare function renderRouter<Routes extends Record<string, {
18
+ path: string;
19
+ }>, TServices extends object = DefaultScreenServices>(router: Router<Routes, TServices>, options: RenderRouterOptions<TServices>): RouterDomHandle<Routes>;
@@ -0,0 +1,9 @@
1
+ import type { ScreenDefinition, DefaultScreenServices } from "@intent-framework/core";
2
+ export type DomRendererOptions<TServices extends object = DefaultScreenServices> = {
3
+ target: HTMLElement;
4
+ services?: TServices;
5
+ showScreenName?: boolean;
6
+ };
7
+ export { renderRouter } from "./dom-router.js";
8
+ export type { RouterDomHandle, RenderRouterOptions } from "./dom-router.js";
9
+ export declare function renderDom<TServices extends object = DefaultScreenServices>(screenDef: ScreenDefinition<TServices>, options: DomRendererOptions<TServices>): () => void;
package/dist/index.js ADDED
@@ -0,0 +1,266 @@
1
+ import { createScreenRuntime } from "@intent-framework/core";
2
+
3
+ //#region src/dom-router.ts
4
+ function renderRouter(router, options) {
5
+ const { showScreenName } = options;
6
+ const win = options.window ?? window;
7
+ let currentCleanup;
8
+ const navigate = (name, ...args) => {
9
+ const routerPath = router.path;
10
+ const path = routerPath(name, args[0]);
11
+ win.history.pushState(null, "", path);
12
+ renderPath(path);
13
+ };
14
+ function renderPath(pathname) {
15
+ currentCleanup?.();
16
+ currentCleanup = void 0;
17
+ options.target.innerHTML = "";
18
+ const match = router.match(pathname);
19
+ const route = match.found ? {
20
+ name: match.name,
21
+ path: match.path,
22
+ params: match.params
23
+ } : void 0;
24
+ const mergedServices = {
25
+ ...options.services,
26
+ navigate,
27
+ route
28
+ };
29
+ if (match.found) {
30
+ currentCleanup = renderDom(match.screen, {
31
+ target: options.target,
32
+ services: mergedServices,
33
+ showScreenName
34
+ });
35
+ return;
36
+ }
37
+ if (options.notFound) {
38
+ const screen = typeof options.notFound === "function" ? options.notFound(pathname) : options.notFound;
39
+ currentCleanup = renderDom(screen, {
40
+ target: options.target,
41
+ services: mergedServices,
42
+ showScreenName
43
+ });
44
+ } else options.target.textContent = "Not found";
45
+ }
46
+ renderPath(win.location.pathname);
47
+ function onPopState() {
48
+ renderPath(win.location.pathname);
49
+ }
50
+ win.addEventListener("popstate", onPopState);
51
+ return {
52
+ navigate,
53
+ renderPath,
54
+ dispose() {
55
+ currentCleanup?.();
56
+ currentCleanup = void 0;
57
+ win.removeEventListener("popstate", onPopState);
58
+ }
59
+ };
60
+ }
61
+
62
+ //#endregion
63
+ //#region src/index.ts
64
+ function getReasonId(actId) {
65
+ return `${actId}-reason`;
66
+ }
67
+ function getEnterHintId(askId) {
68
+ return `${askId}-enter-hint`;
69
+ }
70
+ function sanitizeLabel(label) {
71
+ return label.replace(/\.+$/, "");
72
+ }
73
+ function findDefaultAction(acts) {
74
+ const primaryActs = acts.filter((a) => a.primary);
75
+ if (primaryActs.length === 1) return primaryActs[0];
76
+ if (acts.length === 1) return acts[0];
77
+ return void 0;
78
+ }
79
+ function renderDom(screenDef, options) {
80
+ const { target, services, showScreenName } = options;
81
+ target.innerHTML = "";
82
+ const root = buildDom(screenDef, showScreenName);
83
+ target.appendChild(root);
84
+ const runtime = createScreenRuntime(screenDef, { services });
85
+ runtime.start();
86
+ const form = target.querySelector("form");
87
+ const output = target.querySelector("output#feedback-output");
88
+ const unsubscribers = [];
89
+ for (const act of screenDef.acts) {
90
+ const button = form.querySelector(`#${act.id}`);
91
+ if (button) {
92
+ const unsub = act.enabled.subscribe(() => {
93
+ button.disabled = !act.enabled.current;
94
+ const reasonId = getReasonId(act.id);
95
+ let reasonEl = form.querySelector(`#${reasonId}`);
96
+ if (!act.enabled.current && act.blockedReasons.length > 0) {
97
+ button.setAttribute("aria-describedby", reasonId);
98
+ if (!reasonEl) {
99
+ reasonEl = document.createElement("p");
100
+ reasonEl.id = reasonId;
101
+ form.appendChild(reasonEl);
102
+ }
103
+ reasonEl.textContent = act.blockedReasons[0];
104
+ } else {
105
+ button.removeAttribute("aria-describedby");
106
+ if (reasonEl) reasonEl.remove();
107
+ }
108
+ });
109
+ unsubscribers.push(unsub);
110
+ }
111
+ }
112
+ for (const act of screenDef.acts) {
113
+ const unsub = act.onStatusChange(() => {
114
+ updateFeedback(act, output);
115
+ });
116
+ unsubscribers.push(unsub);
117
+ }
118
+ for (const act of screenDef.acts) {
119
+ const button = form.querySelector(`#${act.id}`);
120
+ if (button) button.addEventListener("click", () => {
121
+ if (act.enabled.current) runtime.executeAct(act);
122
+ });
123
+ }
124
+ const defaultActionForHint = findDefaultAction(screenDef.acts);
125
+ if (defaultActionForHint) {
126
+ const unsub = defaultActionForHint.enabled.subscribe(() => {
127
+ const isEnabled = defaultActionForHint.enabled.current;
128
+ for (const ask of screenDef.asks) {
129
+ const input = form.querySelector(`#${ask.id}`);
130
+ const hint = form.querySelector(`#${getEnterHintId(ask.id)}`);
131
+ if (input && hint) {
132
+ const hintId = getEnterHintId(ask.id);
133
+ if (isEnabled) {
134
+ hint.style.display = "";
135
+ const existing = input.getAttribute("aria-describedby") || "";
136
+ const ids = existing.split(/\s+/).filter(Boolean);
137
+ if (!ids.includes(hintId)) ids.push(hintId);
138
+ input.setAttribute("aria-describedby", ids.join(" "));
139
+ } else {
140
+ hint.style.display = "none";
141
+ const existing = input.getAttribute("aria-describedby") || "";
142
+ const ids = existing.split(/\s+/).filter(Boolean).filter((id) => id !== hintId);
143
+ if (ids.length > 0) input.setAttribute("aria-describedby", ids.join(" "));
144
+ else input.removeAttribute("aria-describedby");
145
+ }
146
+ }
147
+ }
148
+ });
149
+ unsubscribers.push(unsub);
150
+ }
151
+ for (const ask of screenDef.asks) {
152
+ const input = form.querySelector(`#${ask.id}`);
153
+ if (input) {
154
+ const onKeyDown = (event) => {
155
+ if (event.key !== "Enter") return;
156
+ if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) return;
157
+ if (input.tagName === "TEXTAREA") return;
158
+ const defaultAction = findDefaultAction(screenDef.acts);
159
+ if (!defaultAction || !defaultAction.enabled.current) return;
160
+ event.preventDefault();
161
+ runtime.executeAct(defaultAction);
162
+ };
163
+ input.addEventListener("keydown", onKeyDown);
164
+ unsubscribers.push(() => input.removeEventListener("keydown", onKeyDown));
165
+ }
166
+ }
167
+ return () => {
168
+ for (const unsub of unsubscribers) unsub();
169
+ runtime.dispose();
170
+ };
171
+ }
172
+ function buildDom(screenDef, showScreenName) {
173
+ const surface = screenDef.surfaces[0];
174
+ const main = document.createElement("main");
175
+ if (surface) main.id = surface.id;
176
+ if (showScreenName) {
177
+ const heading = document.createElement("h1");
178
+ heading.textContent = screenDef.name;
179
+ main.appendChild(heading);
180
+ }
181
+ const form = document.createElement("form");
182
+ form.setAttribute("method", "POST");
183
+ form.setAttribute("novalidate", "");
184
+ for (const ask of screenDef.asks) {
185
+ const container = document.createElement("div");
186
+ container.className = "ask-group";
187
+ const label = document.createElement("label");
188
+ label.textContent = ask.label;
189
+ label.htmlFor = ask.id;
190
+ container.appendChild(label);
191
+ const input = createInputForAsk(ask);
192
+ input.id = ask.id;
193
+ input.name = ask.id;
194
+ if (ask.required) input.required = true;
195
+ if (ask.kind === "contact" && ask.contactKind) input.setAttribute("autocomplete", ask.contactKind);
196
+ input.addEventListener("input", () => {
197
+ const stateObj = ask.state;
198
+ if (typeof stateObj.set === "function") stateObj.set(input.value);
199
+ });
200
+ container.appendChild(input);
201
+ if (ask.hintText) {
202
+ const hint = document.createElement("p");
203
+ hint.id = `${ask.id}-hint`;
204
+ hint.textContent = ask.hintText;
205
+ container.appendChild(hint);
206
+ }
207
+ const defaultAction = findDefaultAction(screenDef.acts);
208
+ if (defaultAction) {
209
+ const hintId = getEnterHintId(ask.id);
210
+ const hint = document.createElement("p");
211
+ hint.id = hintId;
212
+ hint.textContent = `Press Enter to ${sanitizeLabel(defaultAction.label)}.`;
213
+ if (!defaultAction.enabled.current) hint.style.display = "none";
214
+ container.appendChild(hint);
215
+ if (defaultAction.enabled.current) {
216
+ const existing = input.getAttribute("aria-describedby");
217
+ if (existing) input.setAttribute("aria-describedby", `${existing} ${hintId}`);
218
+ else input.setAttribute("aria-describedby", hintId);
219
+ }
220
+ }
221
+ form.appendChild(container);
222
+ }
223
+ for (const act of screenDef.acts) {
224
+ const button = document.createElement("button");
225
+ button.id = act.id;
226
+ button.type = "button";
227
+ button.textContent = act.label;
228
+ if (act.primary) button.className = "primary";
229
+ if (!act.enabled.current) {
230
+ button.disabled = true;
231
+ if (act.blockedReasons.length > 0) {
232
+ const reasonId = getReasonId(act.id);
233
+ button.setAttribute("aria-describedby", reasonId);
234
+ const reasonEl = document.createElement("p");
235
+ reasonEl.id = reasonId;
236
+ reasonEl.textContent = act.blockedReasons[0];
237
+ form.appendChild(reasonEl);
238
+ }
239
+ }
240
+ form.appendChild(button);
241
+ }
242
+ const output = document.createElement("output");
243
+ output.id = "feedback-output";
244
+ output.setAttribute("aria-live", "polite");
245
+ form.appendChild(output);
246
+ main.appendChild(form);
247
+ return main;
248
+ }
249
+ function updateFeedback(act, output) {
250
+ const msg = act.feedback && act.statusMessage ? act.statusMessage : "";
251
+ if (msg) output.textContent = msg;
252
+ else output.textContent = "";
253
+ }
254
+ function createInputForAsk(ask) {
255
+ const input = document.createElement("input");
256
+ if (ask.kind === "contact" && ask.contactKind === "email") input.type = "email";
257
+ else if (ask.kind === "secret") input.type = "password";
258
+ else if (ask.kind === "choice") {
259
+ input.type = "text";
260
+ input.setAttribute("role", "combobox");
261
+ } else input.type = "text";
262
+ return input;
263
+ }
264
+
265
+ //#endregion
266
+ export { renderDom, renderRouter };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@intent-framework/dom",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.1.0-alpha.0",
7
+ "description": "DOM materializer for Intent screens and router",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/intent-framework/intent.git",
12
+ "directory": "packages/dom"
13
+ },
14
+ "type": "module",
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "dependencies": {
29
+ "@intent-framework/core": "0.1.0-alpha.0",
30
+ "@intent-framework/router": "0.1.0-alpha.0"
31
+ },
32
+ "devDependencies": {
33
+ "jsdom": "^29.1.1",
34
+ "tsdown": "^0.3.0",
35
+ "typescript": "^5.7.0",
36
+ "vitest": "^3.0.0"
37
+ },
38
+ "scripts": {
39
+ "build": "tsdown",
40
+ "test": "vitest run",
41
+ "typecheck": "tsc -b",
42
+ "lint": "tsc -b"
43
+ }
44
+ }