@tschk/moonshine-dom 0.0.0-stage → 0.4.49
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 +15 -0
- package/README.md +467 -2
- package/package.json +62 -4
- package/src/attributes.ts +56 -0
- package/src/binding.ts +430 -0
- package/src/cleanup-order.ts +16 -0
- package/src/component-instance.ts +291 -0
- package/src/component.ts +187 -0
- package/src/control.ts +469 -0
- package/src/custom-element.ts +54 -0
- package/src/dom.ts +870 -0
- package/src/error.ts +59 -0
- package/src/framework-lazy.ts +20 -0
- package/src/framework-native.ts +141 -0
- package/src/framework-source.ts +28 -0
- package/src/head-ssr.ts +108 -0
- package/src/head.ts +34 -0
- package/src/html.ts +54 -0
- package/src/jsx-types.d.ts +19 -0
- package/src/platform.d.ts +4 -0
- package/src/platform.js +4 -0
- package/src/reactive.ts +693 -0
- package/src/render-scheduler.ts +17 -0
- package/src/resource-ssr.ts +73 -0
- package/src/resource.ts +195 -0
- package/src/rotating-text.ts +38 -0
- package/src/ssr-attempt.ts +64 -0
- package/src/ssr-owner.ts +38 -0
- package/src/ssr.ts +874 -0
- package/src/store.ts +527 -0
- package/src/style.ts +30 -0
- package/src/template-retention.ts +4 -0
package/src/binding.ts
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
/** Optional compiler-facing element bindings; no hydration or compiler ABI. */
|
|
2
|
+
import {
|
|
3
|
+
createRenderEffect,
|
|
4
|
+
createRoot,
|
|
5
|
+
getOwner,
|
|
6
|
+
onCleanup,
|
|
7
|
+
untrack,
|
|
8
|
+
} from "./reactive";
|
|
9
|
+
|
|
10
|
+
export type InputValue = string | number | null | undefined;
|
|
11
|
+
|
|
12
|
+
/** Install after user toggle listeners; nullish state closes rather than adopts. */
|
|
13
|
+
export function bindOpen(
|
|
14
|
+
element: HTMLDetailsElement,
|
|
15
|
+
read: () => boolean | null | undefined,
|
|
16
|
+
write: (value: boolean) => void,
|
|
17
|
+
): void {
|
|
18
|
+
const owner = getOwner();
|
|
19
|
+
if (!owner || owner.disposed)
|
|
20
|
+
throw new Error("Open bindings require a live native owner");
|
|
21
|
+
if (
|
|
22
|
+
!(element instanceof HTMLDetailsElement) ||
|
|
23
|
+
typeof read !== "function" ||
|
|
24
|
+
typeof write !== "function"
|
|
25
|
+
)
|
|
26
|
+
throw new TypeError(
|
|
27
|
+
"Open bindings require details and getter/setter functions",
|
|
28
|
+
);
|
|
29
|
+
const toggle = () => untrack(() => write(element.open));
|
|
30
|
+
element.addEventListener("toggle", toggle);
|
|
31
|
+
onCleanup(() => element.removeEventListener("toggle", toggle));
|
|
32
|
+
createRenderEffect(() => {
|
|
33
|
+
element.open = Boolean(read());
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Call after element attributes and user listeners are installed, under a live owner. */
|
|
38
|
+
export function bindValue(
|
|
39
|
+
element: HTMLInputElement | HTMLTextAreaElement,
|
|
40
|
+
read: () => InputValue,
|
|
41
|
+
write: (value: string | number | undefined, reset?: true) => void,
|
|
42
|
+
): void {
|
|
43
|
+
const owner = getOwner();
|
|
44
|
+
if (!owner || owner.disposed)
|
|
45
|
+
throw new Error("Value bindings require a live native owner");
|
|
46
|
+
if (
|
|
47
|
+
!(element instanceof HTMLTextAreaElement) &&
|
|
48
|
+
(!(element instanceof HTMLInputElement) ||
|
|
49
|
+
![
|
|
50
|
+
"text",
|
|
51
|
+
"search",
|
|
52
|
+
"tel",
|
|
53
|
+
"url",
|
|
54
|
+
"email",
|
|
55
|
+
"password",
|
|
56
|
+
"number",
|
|
57
|
+
"range",
|
|
58
|
+
"date",
|
|
59
|
+
"datetime-local",
|
|
60
|
+
"month",
|
|
61
|
+
"week",
|
|
62
|
+
"time",
|
|
63
|
+
"color",
|
|
64
|
+
].includes(element.type))
|
|
65
|
+
)
|
|
66
|
+
throw new TypeError(
|
|
67
|
+
"Value bindings support text, numeric, date/time and color inputs or textarea",
|
|
68
|
+
);
|
|
69
|
+
const numberlike = () =>
|
|
70
|
+
element instanceof HTMLInputElement &&
|
|
71
|
+
(element.type === "number" || element.type === "range");
|
|
72
|
+
function value(text = element.value): string | number | undefined {
|
|
73
|
+
if (!numberlike()) return text;
|
|
74
|
+
const number = text === "" ? NaN : Number(text);
|
|
75
|
+
return Number.isNaN(number) ? undefined : number;
|
|
76
|
+
}
|
|
77
|
+
function apply(): void {
|
|
78
|
+
const next = read();
|
|
79
|
+
// Preserve numeric formatting/caret when the represented number is unchanged.
|
|
80
|
+
if (numberlike() && next === value()) return;
|
|
81
|
+
const text = String(next ?? "");
|
|
82
|
+
if (element.value !== text) element.value = text;
|
|
83
|
+
}
|
|
84
|
+
const input = (reset?: true) =>
|
|
85
|
+
untrack(() => {
|
|
86
|
+
if (reset) write(value(element.defaultValue), true);
|
|
87
|
+
else write(value());
|
|
88
|
+
apply();
|
|
89
|
+
});
|
|
90
|
+
listen(element, "input", input);
|
|
91
|
+
untrack(() => {
|
|
92
|
+
if (read() == null && element.value) write(value());
|
|
93
|
+
});
|
|
94
|
+
createRenderEffect(apply);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function listen(
|
|
98
|
+
element: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement,
|
|
99
|
+
eventName: "input" | "change",
|
|
100
|
+
handler: (reset?: true) => void,
|
|
101
|
+
): void {
|
|
102
|
+
let active = true;
|
|
103
|
+
const reset = (event: Event) => {
|
|
104
|
+
if (event.target !== element.form || !element.form) return;
|
|
105
|
+
queueMicrotask(() => {
|
|
106
|
+
if (!active || event.defaultPrevented) return;
|
|
107
|
+
handler(true);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
const document = element.ownerDocument;
|
|
111
|
+
const listener = () => handler();
|
|
112
|
+
element.addEventListener(eventName, listener);
|
|
113
|
+
// shortcut: connected forms only; add form-local tracking if detached-form support is required.
|
|
114
|
+
document.addEventListener("reset", reset, true);
|
|
115
|
+
onCleanup(() => {
|
|
116
|
+
active = false;
|
|
117
|
+
element.removeEventListener(eventName, listener);
|
|
118
|
+
document.removeEventListener("reset", reset, true);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** FileList binding; nullish models leave the browser's current selection intact. */
|
|
123
|
+
export function bindFiles(
|
|
124
|
+
element: HTMLInputElement,
|
|
125
|
+
read: () => FileList | null | undefined,
|
|
126
|
+
write: (value: FileList | null) => void,
|
|
127
|
+
): void {
|
|
128
|
+
const owner = getOwner();
|
|
129
|
+
if (!owner || owner.disposed)
|
|
130
|
+
throw new Error("Files bindings require a live native owner");
|
|
131
|
+
if (
|
|
132
|
+
!(element instanceof HTMLInputElement) ||
|
|
133
|
+
element.type !== "file" ||
|
|
134
|
+
typeof read !== "function" ||
|
|
135
|
+
typeof write !== "function"
|
|
136
|
+
)
|
|
137
|
+
throw new TypeError(
|
|
138
|
+
"Files bindings require a file input and getter/setter functions",
|
|
139
|
+
);
|
|
140
|
+
listen(element, "change", () => untrack(() => write(element.files)));
|
|
141
|
+
createRenderEffect(() => {
|
|
142
|
+
const files = read();
|
|
143
|
+
if (files != null) element.files = files;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Checkbox checked binding; call after attributes and user change listeners. */
|
|
148
|
+
export function bindChecked(
|
|
149
|
+
element: HTMLInputElement,
|
|
150
|
+
read: () => boolean | null | undefined,
|
|
151
|
+
write: (value: boolean) => void,
|
|
152
|
+
): void {
|
|
153
|
+
const owner = getOwner();
|
|
154
|
+
if (!owner || owner.disposed)
|
|
155
|
+
throw new Error("Checked bindings require a live native owner");
|
|
156
|
+
if (!(element instanceof HTMLInputElement) || element.type !== "checkbox")
|
|
157
|
+
throw new TypeError("Checked bindings support checkbox inputs only");
|
|
158
|
+
const apply = () => {
|
|
159
|
+
element.checked = Boolean(read());
|
|
160
|
+
};
|
|
161
|
+
listen(element, "change", () =>
|
|
162
|
+
untrack(() => {
|
|
163
|
+
write(element.checked);
|
|
164
|
+
apply();
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
untrack(() => {
|
|
168
|
+
if (read() == null) write(element.checked);
|
|
169
|
+
});
|
|
170
|
+
createRenderEffect(apply);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const optionValues = new WeakMap<HTMLOptionElement, unknown>();
|
|
174
|
+
const selectUpdates = new WeakMap<HTMLSelectElement, () => void>();
|
|
175
|
+
|
|
176
|
+
/** Preserve source-level option identity; plain h(option,{value}) stays string-valued. */
|
|
177
|
+
export function setOptionValue(
|
|
178
|
+
option: HTMLOptionElement,
|
|
179
|
+
value: unknown,
|
|
180
|
+
): void {
|
|
181
|
+
if (!(option instanceof HTMLOptionElement))
|
|
182
|
+
throw new TypeError("Option values require an option element");
|
|
183
|
+
const text = value == null ? "" : String(value);
|
|
184
|
+
optionValues.set(option, value);
|
|
185
|
+
option.value = text;
|
|
186
|
+
const select = option.closest("select");
|
|
187
|
+
if (select) untrack(() => selectUpdates.get(select)?.());
|
|
188
|
+
}
|
|
189
|
+
export function optionValue(option: HTMLOptionElement): unknown {
|
|
190
|
+
return optionValues.has(option) ? optionValues.get(option) : option.value;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Typed single/multiple select binding, after options and user listeners are installed. */
|
|
194
|
+
export function bindSelectValue(
|
|
195
|
+
select: HTMLSelectElement,
|
|
196
|
+
read: () => unknown,
|
|
197
|
+
write: (value: unknown) => void,
|
|
198
|
+
equal: (left: unknown, right: unknown) => boolean = Object.is,
|
|
199
|
+
): void {
|
|
200
|
+
const owner = getOwner();
|
|
201
|
+
if (!owner || owner.disposed)
|
|
202
|
+
throw new Error("Select bindings require a live native owner");
|
|
203
|
+
if (!(select instanceof HTMLSelectElement))
|
|
204
|
+
throw new TypeError("Select bindings require a select element");
|
|
205
|
+
if (selectUpdates.has(select))
|
|
206
|
+
throw new Error("Select already has a value binding");
|
|
207
|
+
function selected(reset = false): unknown {
|
|
208
|
+
const options = Array.from(select.options).filter((option) =>
|
|
209
|
+
reset ? option.hasAttribute("selected") : option.selected,
|
|
210
|
+
);
|
|
211
|
+
if (select.multiple) return options.map(optionValue);
|
|
212
|
+
const option =
|
|
213
|
+
options[0] ??
|
|
214
|
+
Array.from(select.options).find((candidate) => !candidate.disabled);
|
|
215
|
+
return option ? optionValue(option) : null;
|
|
216
|
+
}
|
|
217
|
+
function apply(): void {
|
|
218
|
+
const next = read();
|
|
219
|
+
if (select.multiple) {
|
|
220
|
+
if (next == null) return;
|
|
221
|
+
if (!Array.isArray(next))
|
|
222
|
+
throw new TypeError(
|
|
223
|
+
"Multiple select bindings require an array or nullish value",
|
|
224
|
+
);
|
|
225
|
+
for (const option of select.options)
|
|
226
|
+
option.selected = next.includes(optionValue(option));
|
|
227
|
+
} else {
|
|
228
|
+
let found = false;
|
|
229
|
+
for (const option of select.options) {
|
|
230
|
+
const matches = !found && equal(optionValue(option), next);
|
|
231
|
+
option.selected = matches;
|
|
232
|
+
if (matches) found = true;
|
|
233
|
+
}
|
|
234
|
+
if (!found) select.selectedIndex = -1;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
listen(select, "change", (reset) =>
|
|
238
|
+
untrack(() => {
|
|
239
|
+
const value = selected(reset);
|
|
240
|
+
write(value);
|
|
241
|
+
// An unchanged accepted value preserves the user's duplicate option choice.
|
|
242
|
+
// Rejected/transformed writes still reconcile the DOM with the actual model.
|
|
243
|
+
if (select.multiple || !equal(read(), value)) apply();
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
let active = true;
|
|
247
|
+
const observer = new MutationObserver(() => {
|
|
248
|
+
if (active) untrack(apply);
|
|
249
|
+
});
|
|
250
|
+
selectUpdates.set(select, apply);
|
|
251
|
+
onCleanup(() => {
|
|
252
|
+
active = false;
|
|
253
|
+
observer.disconnect();
|
|
254
|
+
selectUpdates.delete(select);
|
|
255
|
+
});
|
|
256
|
+
untrack(() => {
|
|
257
|
+
// Typed undefined is a real option value; match it before default adoption.
|
|
258
|
+
if (!select.multiple && read() === undefined) {
|
|
259
|
+
const match = Array.from(select.options).find((option) =>
|
|
260
|
+
equal(optionValue(option), undefined),
|
|
261
|
+
);
|
|
262
|
+
if (match) match.selected = true;
|
|
263
|
+
}
|
|
264
|
+
if (
|
|
265
|
+
read() === undefined &&
|
|
266
|
+
(select.multiple ||
|
|
267
|
+
Array.from(select.options).some((option) => option.selected))
|
|
268
|
+
)
|
|
269
|
+
write(selected());
|
|
270
|
+
});
|
|
271
|
+
createRenderEffect(apply);
|
|
272
|
+
observer.observe(select, {
|
|
273
|
+
childList: true,
|
|
274
|
+
subtree: true,
|
|
275
|
+
characterData: true,
|
|
276
|
+
attributes: true,
|
|
277
|
+
attributeFilter: ["value", "selected"],
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const inputValues = /* @__PURE__ */ new WeakMap<HTMLInputElement, unknown>();
|
|
282
|
+
const inputUpdates = /* @__PURE__ */ new WeakMap<
|
|
283
|
+
HTMLInputElement,
|
|
284
|
+
() => void
|
|
285
|
+
>();
|
|
286
|
+
|
|
287
|
+
/** Compiler hook for typed radio/checkbox values; ordinary DOM values stay strings. */
|
|
288
|
+
export function setInputValue(
|
|
289
|
+
input: HTMLInputElement,
|
|
290
|
+
value: unknown,
|
|
291
|
+
notify = true,
|
|
292
|
+
): void {
|
|
293
|
+
if (!(input instanceof HTMLInputElement))
|
|
294
|
+
throw new TypeError("Input values require an input element");
|
|
295
|
+
const text = value == null ? "" : String(value);
|
|
296
|
+
inputValues.set(input, value);
|
|
297
|
+
input.value = text;
|
|
298
|
+
if (notify) untrack(() => inputUpdates.get(input)?.());
|
|
299
|
+
}
|
|
300
|
+
function inputValue(input: HTMLInputElement): unknown {
|
|
301
|
+
return inputValues.has(input) ? inputValues.get(input) : input.value;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** One explicit token per compiler binding target; never inferred from HTML names. */
|
|
305
|
+
export function createInputGroup(
|
|
306
|
+
kind: "radio" | "checkbox" | "mixed",
|
|
307
|
+
read: () => unknown,
|
|
308
|
+
write: (value: unknown) => void,
|
|
309
|
+
equal: (left: unknown, right: unknown) => boolean = Object.is,
|
|
310
|
+
): { bind(input: HTMLInputElement, watch?: () => unknown): void } {
|
|
311
|
+
const owner = getOwner();
|
|
312
|
+
if (!owner || owner.disposed || owner.disposing)
|
|
313
|
+
throw new Error("Input groups require a live native owner");
|
|
314
|
+
if (kind !== "radio" && kind !== "checkbox" && kind !== "mixed")
|
|
315
|
+
throw new TypeError(
|
|
316
|
+
"Input groups support radio, checkbox or mixed inputs only",
|
|
317
|
+
);
|
|
318
|
+
type Member = {
|
|
319
|
+
input: HTMLInputElement;
|
|
320
|
+
checkbox: boolean;
|
|
321
|
+
watch: (() => unknown) | undefined;
|
|
322
|
+
stop: () => void;
|
|
323
|
+
};
|
|
324
|
+
const members = new Set<Member>();
|
|
325
|
+
let active = true;
|
|
326
|
+
function release(member: Member): void {
|
|
327
|
+
if (!members.delete(member)) return;
|
|
328
|
+
inputUpdates.delete(member.input);
|
|
329
|
+
member.stop();
|
|
330
|
+
}
|
|
331
|
+
onCleanup(() => {
|
|
332
|
+
active = false;
|
|
333
|
+
let failed = false,
|
|
334
|
+
failure: unknown;
|
|
335
|
+
for (const member of members) {
|
|
336
|
+
try {
|
|
337
|
+
release(member);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
if (!failed) failure = error;
|
|
340
|
+
failed = true;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (failed) throw failure;
|
|
344
|
+
});
|
|
345
|
+
function apply(member: Member): void {
|
|
346
|
+
if (!active || !members.has(member)) return;
|
|
347
|
+
member.watch?.();
|
|
348
|
+
const value = read();
|
|
349
|
+
if (member.checkbox) {
|
|
350
|
+
if (value != null && !Array.isArray(value))
|
|
351
|
+
throw new TypeError(
|
|
352
|
+
"Checkbox groups require an array or nullish value",
|
|
353
|
+
);
|
|
354
|
+
member.input.checked =
|
|
355
|
+
(value as unknown[] | null | undefined)?.includes(
|
|
356
|
+
inputValue(member.input),
|
|
357
|
+
) ?? false;
|
|
358
|
+
} else member.input.checked = equal(inputValue(member.input), value);
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
bind(input, watch) {
|
|
362
|
+
const current = getOwner();
|
|
363
|
+
if (!active || !current || current.disposed || current.disposing)
|
|
364
|
+
throw new Error(
|
|
365
|
+
"Input group members require a live group and native owner",
|
|
366
|
+
);
|
|
367
|
+
if (
|
|
368
|
+
!(input instanceof HTMLInputElement) ||
|
|
369
|
+
(kind === "mixed"
|
|
370
|
+
? input.type !== "radio" && input.type !== "checkbox"
|
|
371
|
+
: input.type !== kind)
|
|
372
|
+
)
|
|
373
|
+
throw new TypeError(
|
|
374
|
+
kind === "mixed"
|
|
375
|
+
? "Input group requires radio or checkbox inputs"
|
|
376
|
+
: `Input group requires ${kind} inputs`,
|
|
377
|
+
);
|
|
378
|
+
if (watch !== undefined && typeof watch !== "function")
|
|
379
|
+
throw new TypeError("Input group value watch must be a function");
|
|
380
|
+
if (inputUpdates.has(input))
|
|
381
|
+
throw new Error("Input already belongs to a group");
|
|
382
|
+
const checkbox =
|
|
383
|
+
kind === "checkbox" || (kind === "mixed" && input.type === "checkbox");
|
|
384
|
+
const member: Member = { input, checkbox, watch, stop: () => {} };
|
|
385
|
+
onCleanup(() => release(member));
|
|
386
|
+
try {
|
|
387
|
+
createRoot((stop) => {
|
|
388
|
+
member.stop = stop;
|
|
389
|
+
members.add(member);
|
|
390
|
+
inputUpdates.set(input, () => apply(member));
|
|
391
|
+
listen(input, "change", (reset) =>
|
|
392
|
+
untrack(() => {
|
|
393
|
+
if (!active || !members.has(member)) return;
|
|
394
|
+
if (reset) write(checkbox ? [] : null);
|
|
395
|
+
else if (!checkbox) write(inputValue(input));
|
|
396
|
+
else {
|
|
397
|
+
const ordered = [...members].toSorted((a, b) => {
|
|
398
|
+
const position = a.input.compareDocumentPosition(b.input);
|
|
399
|
+
return position & Node.DOCUMENT_POSITION_DISCONNECTED
|
|
400
|
+
? 0
|
|
401
|
+
: position & Node.DOCUMENT_POSITION_FOLLOWING
|
|
402
|
+
? -1
|
|
403
|
+
: position & Node.DOCUMENT_POSITION_PRECEDING
|
|
404
|
+
? 1
|
|
405
|
+
: 0;
|
|
406
|
+
});
|
|
407
|
+
const values = new Set(
|
|
408
|
+
ordered
|
|
409
|
+
.filter((entry) => entry.input.checked)
|
|
410
|
+
.map((entry) => inputValue(entry.input)),
|
|
411
|
+
);
|
|
412
|
+
if (!input.checked) values.delete(inputValue(input));
|
|
413
|
+
write([...values]);
|
|
414
|
+
}
|
|
415
|
+
for (const entry of members) apply(entry);
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
createRenderEffect(() => apply(member));
|
|
419
|
+
});
|
|
420
|
+
} catch (error) {
|
|
421
|
+
try {
|
|
422
|
+
release(member);
|
|
423
|
+
} catch {
|
|
424
|
+
/* Preserve binding setup failure. */
|
|
425
|
+
}
|
|
426
|
+
throw error;
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const cleanupOrderKey = /* @__PURE__ */ Symbol(
|
|
2
|
+
"moonshine.cleanup-order",
|
|
3
|
+
);
|
|
4
|
+
export interface CleanupPolicy {
|
|
5
|
+
reverse: boolean;
|
|
6
|
+
deferRemoval(
|
|
7
|
+
owned: readonly Node[],
|
|
8
|
+
anchor: Node,
|
|
9
|
+
retain: ((node: Node) => boolean) | undefined,
|
|
10
|
+
remove: (
|
|
11
|
+
owned: readonly Node[],
|
|
12
|
+
retained?: ReadonlySet<Node>,
|
|
13
|
+
retain?: (node: Node) => boolean,
|
|
14
|
+
) => void,
|
|
15
|
+
): void;
|
|
16
|
+
}
|