-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh.ts
296 lines (250 loc) · 6.06 KB
/
h.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
/// <reference types="../jsx.js" />
import { effect } from "./reactivity.js"
import { render } from "./render.js"
type HTMLProps = Partial<JSX.Helpers.HTMLProps<HTMLElement | SVGElement>>
function setCssPropsWithSignals(
element: HTMLElement | SVGElement,
props: string | JSX.Helpers.CSSPropsWithSignals,
) {
if (typeof props == "string") {
;(element as any).style = props
return
}
;(element as any).style = ""
for (const key in props) {
const value = props[key as keyof typeof props]
if (value == null) {
continue
}
if (typeof value == "function") {
effect(() => {
element.style[key as keyof typeof props] = value()!
})
} else {
element.style[key as keyof typeof props] = value
}
}
}
function setCssProps(
element: HTMLElement | SVGElement,
props: string | JSX.Helpers.CSSProps,
) {
if (typeof props == "string") {
;(element as any).style = props
return
}
;(element as any).style = ""
for (const key in props) {
element.style[key as keyof typeof props] = props[key as keyof typeof props]!
}
}
const svgElements = [
"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",
]
function createHTMLElement(
tag: string,
props?: HTMLProps | null | undefined,
...children: JSX.Element[]
) {
const element = svgElements.includes(tag)
? document.createElementNS("http://www.w3.org/2000/svg", tag)
: document.createElement(tag)
props = props ?? {}
for (const _key in props) {
const key = _key as keyof HTMLProps
if (key.startsWith("on:")) {
const value = props[key] as (typeof props)[typeof key]
if (value != null) {
element.addEventListener(key.slice(3), value as any)
}
continue
}
if (key.startsWith("class:")) {
const value = props[key] as (typeof props)[typeof key]
if (typeof value == "function") {
effect(() => {
element.classList.toggle(key.slice(6), !!value())
})
} else {
element.classList.toggle(key.slice(6), !!value)
}
continue
}
if (key.startsWith("style:")) {
const value = props[key] as (typeof props)[typeof key]
if (typeof value == "function") {
effect(() => {
;(element.style as any)[key.slice(6)] = String(value())
})
} else {
;(element.style as any)[key.slice(6)] = String(value)
}
continue
}
if (key == "style") {
const value = props[key] as (typeof props)[typeof key]
if (value == null) {
continue
}
if (typeof value == "function") {
effect(() => {
setCssProps(element, value())
})
} else {
setCssPropsWithSignals(element, value)
}
continue
}
if (key != "children" && key != "use") {
const value = props[key] as (typeof props)[typeof key]
if (typeof value == "function") {
effect(() => {
;(element[key] as any) = value()
})
} else {
;(element[key] as any) = value
}
} else {
key satisfies "children" | "use"
}
}
if (children.length) {
render(element, children)
} else {
render(element, props.children)
}
const use = props.use
if (use) {
use(element)
}
return element
}
function createFunctionElement(
tag: (props: object) => JSX.Element,
props?: {} | null | undefined,
...children: unknown[]
) {
let _children: unknown
if (children.length == 0) {
if (props != null) {
props satisfies {}
_children = (props as any).children
}
} else if (children.length == 1) {
_children = [children[0]]
} else {
_children = children
}
return tag({ ...props, children: _children })
}
export function h<K extends keyof HTMLElementTagNameMap>(
tag: K,
props?: JSX.Helpers.HTMLProps<HTMLElementTagNameMap[K]> | null | undefined,
...children: JSX.Element[]
): HTMLElementTagNameMap[K]
export function h<K extends keyof SVGElementTagNameMap>(
tag: K,
props?: JSX.Helpers.HTMLProps<SVGElementTagNameMap[K]> | null | undefined,
...children: JSX.Element[]
): SVGElementTagNameMap[K]
export function h(
tag: string,
props?: HTMLProps | null | undefined,
...children: JSX.Element[]
): HTMLElement | SVGElement
export function h<P, T extends JSX.Element>(
tag: (props: P) => T,
props: {} extends P ? P | null | undefined : P,
): T
export function h<P extends { children: unknown }, T extends JSX.Element>(
tag: (props: P) => T,
props: {} extends Omit<P, "children"> & Partial<Pick<P, keyof P & "children">>
?
| (Omit<P, "children"> & Partial<Pick<P, keyof P & "children">>)
| null
| undefined
: Omit<P, "children"> & Partial<Pick<P, keyof P & "children">>,
...children: P["children"] extends infer C
? C extends undefined
? []
: C extends readonly (infer U)[]
? [U, ...U[]]
: [C]
: never
): T
export function h(
tag: string | ((props: any) => any),
props: any,
...children: any[]
): JSX.Element {
if (typeof tag == "function") {
return createFunctionElement(tag, props, ...children)
}
if (typeof tag == "string") {
return createHTMLElement(tag, props, ...children)
}
return undefined
}
declare global {
interface String {
startsWith<T extends string>(text: T, position?: 0): this is `${T}${string}`
}
}