-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
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
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
// This module contains bare minimum DOM helper functions with Signal support.
// This module should only contain fundamental building block.
// This module should not have convenient helpers or opinionated ones.
import { effect, isReactive, type ReactiveOrStatic } from "./signals.js";
export type ElementSetup<T extends Element> = (el: T) => void;
// (internal) For code deduplication (candidate for inlining)
function setAttribute(
el: Element,
name: string,
value: string | boolean,
): void {
if (typeof value === "string") {
el.setAttribute(name, value);
return;
}
if (value) {
el.setAttribute(name, "");
return;
}
el.removeAttribute(name);
}
/**
* Sets an attribute.
* When the value is `false`, removes the attribute.
*/
export function attr<T extends Element>(
name: string,
value: ReactiveOrStatic<string | boolean>,
): ElementSetup<T> {
return (el) => {
if (isReactive(value)) {
effect(() => {
setAttribute(el, name, value.get());
});
return;
}
setAttribute(el, name, value);
};
}
/**
* Sets an element property.
*/
export function prop<T extends Element, K extends keyof T = keyof T>(
name: K,
value: ReactiveOrStatic<T[K]>,
): ElementSetup<T> {
return (el) => {
if (isReactive(value)) {
effect(() => {
el[name] = value.get();
});
return;
}
el[name] = value;
};
}
/**
* Adds an event listener for the element.
*/
export function on<T extends Element, E extends Event>(
eventName: string,
listener: ReactiveOrStatic<(event: E) => void>,
options?: AddEventListenerOptions,
): ElementSetup<T> {
return (el) => {
if (isReactive(listener)) {
effect(() => {
const f = listener.get();
// @ts-expect-error: TypeScript incorrectly models event target things.
el.addEventListener(eventName, f, options);
return () => {
// @ts-expect-error: TypeScript incorrectly models event target things.
el.removeEventListener(eventName, f, options);
};
});
return;
}
// @ts-expect-error: TypeScript incorrectly models event target things.
el.addEventListener(eventName, listener, options);
};
}
/**
* A set of types ef accept for node children.
*
* - `Node` ... Appended as-is.
* - `string` ... Appended as `Text` (DOM text node).
* - `null` ... Do nothing (skip).
* - `undefined` ... Do nothing (skip).
*/
export type NodeChild = Node | string | null | undefined;
// (internal) Append children to a Node (parent).
function adapt(
node: Node,
children: readonly ReactiveOrStatic<NodeChild>[],
): void {
for (const child of children) {
if (isReactive(child)) {
const start = document.createTextNode("");
const end = document.createTextNode("");
node.appendChild(start);
node.appendChild(end);
effect(() => {
// Due to the DocumentFragment disappears from Node Tree once appended,
// we need to use `.parentNode` because `node` does not exist in the tree.
// What a mess.
const parent = start.parentNode || node;
let oldNode: Node | null = null;
if (start.nextSibling && start.nextSibling !== end) {
oldNode = start.nextSibling;
// Remove excess (= nodes between `oldNode` and `end`).
// Cleanup approach cannot be used because how DocumentFragment works:
// its children is appended to the parent and the node itself disappears.
while (oldNode.nextSibling && oldNode.nextSibling !== end) {
parent.removeChild(oldNode.nextSibling);
}
}
const c = child.get();
if (c === null || typeof c === "undefined") {
if (oldNode) {
parent.removeChild(oldNode);
}
return;
}
// TextNode value rewrite shortcut
if (oldNode && oldNode instanceof Text && typeof c === "string") {
oldNode.nodeValue = c;
return;
}
const newNode = typeof c === "string" ? document.createTextNode(c) : c;
if (oldNode) {
// Use `replaceChild` for easy performance boost
parent.replaceChild(newNode, oldNode);
} else {
parent.insertBefore(newNode, end);
}
});
continue;
}
if (child === null || typeof child === "undefined") {
continue;
}
node.appendChild(
typeof child === "string" ? document.createTextNode(child) : child,
);
}
}
/**
* Create a DocumentFragment node.
*/
export function fragment(
children: readonly ReactiveOrStatic<NodeChild>[],
): DocumentFragment {
const f = document.createDocumentFragment();
adapt(f, children);
return f;
}
/**
* Create an HTMLElement element.
*/
export function el<TagName extends keyof HTMLElementTagNameMap>(
tagName: TagName,
setups?: readonly ElementSetup<HTMLElementTagNameMap[TagName]>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): HTMLElementTagNameMap[TagName];
export function el<T extends Element>(
tagName: string,
setups?: readonly ElementSetup<T>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): T;
export function el<TagName extends keyof HTMLElementTagNameMap>(
tagName: TagName,
setups: readonly ElementSetup<HTMLElementTagNameMap[TagName]>[] = [],
children: readonly ReactiveOrStatic<NodeChild>[] = [],
): HTMLElementTagNameMap[TagName] {
const e = document.createElement(tagName);
for (const f of setups) {
f(e);
}
adapt(e, children);
return e;
}
/**
* Create a SVGElement element.
*
* You don't need to set `xmlns` attribute for elements created by this function.
* Namespace is configured on element creation.
*/
export function svg<TagName extends keyof SVGElementTagNameMap>(
tagName: TagName,
setups?: readonly ElementSetup<SVGElementTagNameMap[TagName]>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): SVGElementTagNameMap[TagName];
export function svg<T extends SVGElement>(
tagName: string,
setups?: readonly ElementSetup<T>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): T;
export function svg<TagName extends keyof SVGElementTagNameMap>(
tagName: TagName,
setups: readonly ElementSetup<SVGElementTagNameMap[TagName]>[] = [],
children: readonly ReactiveOrStatic<NodeChild>[] = [],
): SVGElementTagNameMap[TagName] {
const e = document.createElementNS("http://www.w3.org/2000/svg", tagName);
for (const f of setups) {
f(e);
}
adapt(e, children);
return e;
}
/**
* Create a MathMLElement element.
*/
export function mathml<TagName extends keyof MathMLElementTagNameMap>(
tagName: TagName,
setups?: readonly ElementSetup<MathMLElementTagNameMap[TagName]>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): MathMLElementTagNameMap[TagName];
export function mathml<T extends MathMLElement>(
tagName: string,
setups?: readonly ElementSetup<T>[],
children?: readonly ReactiveOrStatic<NodeChild>[],
): T;
export function mathml<TagName extends keyof MathMLElementTagNameMap>(
tagName: TagName,
setups: readonly ElementSetup<MathMLElementTagNameMap[TagName]>[] = [],
children: readonly ReactiveOrStatic<NodeChild>[] = [],
): MathMLElementTagNameMap[TagName] {
const e = document.createElementNS(
"http://www.w3.org/1998/Math/MathML",
tagName,
);
for (const f of setups) {
f(e);
}
adapt(e, children);
return e;
}