-
Notifications
You must be signed in to change notification settings - Fork 2
/
solid.ts
307 lines (267 loc) · 6.56 KB
/
solid.ts
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Another reactive library.
/// <reference types="./jsx.d.ts" />
let currentEffect: (() => void) | undefined
const toString = String
export function createEffect(fn: (this: void) => void) {
function effect() {
try {
var parentEffect = currentEffect
currentEffect = fn
fn()
} finally {
currentEffect = parentEffect
}
}
effect()
}
export function createSignal<T>(
value: T,
): [get: () => T, set: (value: T) => void] {
const listeners = new Set<() => void>()
return [
() => {
if (currentEffect) {
listeners.add(currentEffect)
}
return value
},
(newValue) => {
value = newValue
listeners.forEach((fn) => fn())
},
]
}
export function createMemo<T>(fn: (this: void) => T): () => T {
const [get, set] = (createSignal as any)() as ReturnType<
typeof createSignal<T>
>
createEffect(() => set(fn()))
return get
}
export function createUntrack<T>(fn: (this: void) => T): T {
try {
var parentEffect = currentEffect
currentEffect = undefined
return fn()
} finally {
currentEffect = parentEffect
}
}
export function render(
value: JSX.Element,
parent: { appendChild(node: ChildNode): void },
) {
if (value instanceof Node) {
parent.appendChild(value)
} else if (typeof value == "function") {
const previousElements = new Set<ChildNode>()
const textNode = new Text()
let wasPreviousText = false
createEffect(() => {
let val = value()
const isPlainText =
typeof val != "object" && typeof val != "function" && val != null
if (wasPreviousText && isPlainText) {
textNode.data = toString(val)
return
}
previousElements.forEach((node) => node.remove())
previousElements.clear()
if (val == null) {
return
}
wasPreviousText = isPlainText
if (isPlainText) {
textNode.data = toString(val)
parent.appendChild(textNode)
previousElements.add(textNode)
return
}
render(val, {
appendChild(node) {
previousElements.add(node)
parent.appendChild(node)
},
})
})
} else if (Array.isArray(value)) {
value.forEach((item) => render(item, parent))
} else if (value != null) {
parent.appendChild(new Text(toString(value)))
}
}
export function attr(element: Element, key: string, value: unknown) {
if (key in element) {
if (typeof value == "function") {
createEffect(() => ((element as any)[key] = value()))
} else {
;(element as any)[key] = value
}
} else {
if (typeof value == "function") {
createEffect(() => element.setAttribute(key, toString(value())))
} else {
element.setAttribute(key, toString(value))
}
}
}
const svgElements = new Set([
"animate",
"animateMotion",
"animateTransform",
"circle",
"clipPath",
"defs",
"desc",
"ellipse",
"feBlend",
"feColorMatrix",
"feComponentTransfer",
"feComposite",
"feConvolveMatrix",
"feDiffuseLighting",
"feDisplacementMap",
"feDistantLight",
"feDropShadow",
"feFlood",
"feFuncA",
"feFuncB",
"feFuncG",
"feFuncR",
"feGaussianBlur",
"feImage",
"feMerge",
"feMergeNode",
"feMorphology",
"feOffset",
"fePointLight",
"feSpecularLighting",
"feSpotLight",
"feTile",
"feTurbulence",
"filter",
"foreignObject",
"g",
"image",
"line",
"linearGradient",
"marker",
"mask",
"metadata",
"mpath",
"path",
"pattern",
"polygon",
"polyline",
"radialGradient",
"rect",
"set",
"stop",
"svg",
"switch",
"symbol",
"text",
"textPath",
"tspan",
"use",
"view",
])
export function h<K extends keyof HTMLElementTagNameMap>(
tag: K,
props?: Partial<JSX.Helpers.HTMLProps<HTMLElementTagNameMap[K]>> | null,
...children: readonly JSX.Element[]
): HTMLElementTagNameMap[K]
export function h<P extends Record<string, any>>(
tag: (props: P) => JSX.Element,
originalProps: Omit<P, "children"> | JSX.Helpers.NullableIfPropsCanBeEmpty<P>,
...children: P["children"] extends infer U
? U extends undefined
? []
: U extends readonly any[]
? U
: [U]
: never
): JSX.Element
export function h<P extends Record<string, any>>(
tag: (props: P) => JSX.Element,
originalProps: P | JSX.Helpers.NullableIfPropsCanBeEmpty<P>,
): JSX.Element
export function h(
tag: ((props: object) => JSX.Element) | string,
originalProps?: object | null | undefined,
...children: readonly unknown[]
): JSX.Element {
const props: Record<string, unknown> =
(originalProps as Record<string, unknown>) || {}
if (typeof tag == "function") {
if (!("children" in props)) {
if (children.length == 0) {
props.children = undefined
} else if (children.length == 1) {
props.children = children[0]
} else {
props.children = children
}
}
return tag(props)
}
if ("children" in props) {
if (props.children == null) {
children = []
} else if (Array.isArray(props.children)) {
children = props.children
} else {
children = [props.children]
}
}
const element: HTMLElement | SVGElement = svgElements.has(tag)
? document.createElementNS("http://www.w3.org/2000/svg", tag)
: document.createElement(tag)
for (const key in props) {
const value = props[key]
if (key.startsWith("style:")) {
const prop = key.slice(6)
if (typeof value == "function") {
createEffect(() => element.style.setProperty(prop, toString(value())))
} else {
element.style.setProperty(prop, toString(value))
}
} else if (key.startsWith("class:")) {
const prop = key.slice(6)
if (typeof value == "function") {
createEffect(() => element.classList.toggle(prop, !!value()))
} else {
element.classList.toggle(prop, !!value)
}
} else if (key.startsWith("on:")) {
element.addEventListener(
key.slice(3),
value as EventListenerOrEventListenerObject,
)
} else if (key != "use" && key != "children") {
attr(element, key, value)
}
}
render(children as readonly JSX.Element[], element)
if ("use" in props && typeof props.use == "function") {
props.use(element)
}
return element
}
export function unwrap<T>(value: T | (() => T)): T {
if (typeof value == "function") {
return (value as () => T)()
}
return value
}
export function Maybe<T, U = undefined>({
children,
fallback,
when,
}: {
children: T
fallback?: U
when: boolean | (() => boolean)
}): () => T | U {
return () => (unwrap(when) ? children : (fallback as U))
}