-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx.ts
298 lines (271 loc) · 7.32 KB
/
jsx.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
/* eslint-disable @typescript-eslint/no-namespace */ // Reasoning: JSX types are weird and "need" namespaces to operate correctly.
import { Observable } from 'rxjs'
import {
ButterfloatAttributes,
ButterfloatIntrinsicAttributes,
JsxChildren,
ChildrenBindable,
Component,
ComponentContext,
NodeDescription,
DefaultBind,
DefaultStyleBind,
} from './component'
import { ButterfloatEvents, DefaultEvents, ObservableEvent } from './events'
/**
* Overloads to Typescript's JSX typing
*/
namespace JSXInternal {
/**
* JSX Element type
*/
export type Element = NodeDescription
/*
RANT: There aren't any easily reusable types for HTML elements and their attributes to reuse here.
@types/react uses a hand-maintained many thousands of lines file that mixes and matches React-specific
concerns like `dangerouslySetInnerHtml`.
Typescript's own "lib.dom" types are really cool and auto-generated from MDN metadata among other
sources. But the focus is on JS runtime types and don't yet have directly reusable types that can be
reflected in and used as base types for JSX.IntrinsicElements. But we can meta-type our way from
HTMLElementTagNameMap to something resembling what we want.
IfEquals/WritableKeys: https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript/52473108#52473108
*/
/**
* If types are equal. Meta-type for complex conditional types.
*/
export type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X
? 1
: 2) extends <T>() => T extends Y ? 1 : 2
? A
: B
/**
* Collect the writable keys of a type.
*/
export type WritableKeys<T> = {
[P in keyof T]-?: IfEquals<
{ [Q in P]: T[P] },
{ -readonly [Q in P]: T[P] },
P
>
}[keyof T]
/**
* Attributes of an HTML Element
*/
export type HtmlElementAttributes<T> = {
[Property in WritableKeys<T> as T[Property] extends
| string
| number
| null
| undefined
? Property
: never]?: T[Property]
}
/**
* Observable bindable attributes of an HTML Element
*/
export type HtmlElementAttributesBind<T> = {
[Property in WritableKeys<T> as T[Property] extends
| string
| number
| null
| undefined
? Property
: never]?: Observable<T[Property]>
}
/**
* Bindable DOM events
*/
export type HtmlEvents<EventMap = HTMLElementEventMap> = {
[Property in keyof EventMap]?: ObservableEvent<EventMap[Property]>
}
/**
* All Butterfloat bindable attributes of an element
*/
export type ButterfloatElementBind<T> = HtmlElementAttributesBind<T> &
DefaultBind
/**
* All Butterfloat bindable events of an element
*/
export type ButterfloatElementEvents = HtmlEvents &
ButterfloatEvents &
DefaultEvents
/**
* All bindable CSS styles of an HTML element
*/
export type HtmlElementStyleBind = {
[Property in keyof CSSStyleDeclaration]?: Observable<
CSSStyleDeclaration[Property]
>
}
/**
* All Butterfloat bindable CSS styles
*/
export type ButterfloatElementStyleBind = HtmlElementStyleBind &
DefaultStyleBind
/**
* Attributes available in Butterfloat from an HTML element
*/
export type ButterfloatElementAttributes<T> = HtmlElementAttributes<T> &
ButterfloatIntrinsicAttributes<
ButterfloatElementBind<T>,
ButterfloatElementEvents,
ButterfloatElementStyleBind
>
/**
* Available HTML Elements
*/
export type HtmlElements = {
[Property in keyof HTMLElementTagNameMap]: ButterfloatElementAttributes<
HTMLElementTagNameMap[Property]
>
}
/**
* JSX "intrinsic" elements (HTML elements for DOM binding)
*/
export interface IntrinsicElements extends HtmlElements {
[ele: string]: ButterfloatIntrinsicAttributes
}
/**
* JSX "intrinsic" attributes (additional attributes on JSX "intrinsics")
*/
export type IntrinsicAttributes = ChildrenBindable
}
/**
* Properties supported by the `<Children>` pseudo-component
*/
export interface ChildrenProperties {
/**
* Context for the component to bind the children from, for deep binding.
*
* This allows for binding children deeper into the tree, such as passing
* your component's children into a "render function" of a deeper component
* in the tree.
*/
context?: ComponentContext<unknown>
}
/**
* Bind the children of a component.
*
* @param props Children properties
* @returns Children node
*/
export function Children({ context }: ChildrenProperties): NodeDescription {
return {
type: 'children',
context,
}
}
/**
* Create a fragment of other nodes
*
* @param attributes Attributes
* @param children Children
* @returns Fragment node
*/
export function Fragment(
attributes: ButterfloatAttributes,
...children: JsxChildren
): NodeDescription {
const { childrenBind, childrenBindMode, ...otherAttributes } =
attributes ?? {}
return {
type: 'fragment',
attributes: otherAttributes,
children,
childrenBind,
childrenBindMode,
}
}
/**
* Properties supported by the `<Static>` pseudo-component
*/
export interface StaticProperties {
/**
* A static element to attach to the DOM tree.
*/
element: Element
}
/**
* Attach a static DOM element
*
* @param props Static properties
* @returns Static node
*/
export function Static({ element }: StaticProperties): NodeDescription {
return {
type: 'static',
element,
}
}
/**
* Describe a node. Builder for JSX and TSX transformation.
* @param element An element to build
* @param attributes Attributes
* @param children Children
* @returns Node description
*/
export function jsx(
element: string | Component,
attributes: ButterfloatAttributes | null,
...children: JsxChildren
): NodeDescription {
if (typeof element === 'string') {
const {
bind,
immediateBind,
childrenBind,
childrenBindMode,
events,
styleBind,
immediateStyleBind,
classBind,
immediateClassBind,
...otherAttributes
} = (attributes as ButterfloatIntrinsicAttributes) ?? {}
return {
type: 'element',
element,
attributes: otherAttributes,
bind: bind ?? {},
immediateBind: immediateBind ?? {},
children,
childrenBind,
childrenBindMode,
events: events ?? {},
styleBind: styleBind ?? {},
immediateStyleBind: immediateStyleBind ?? {},
classBind: classBind ?? {},
immediateClassBind: immediateClassBind ?? {},
}
}
if (typeof element === 'function') {
// immediately flatten fragments or children or statics
if (element === Fragment || element === Children || element === Static) {
const func = element as (
attributes: unknown,
...children: JsxChildren
) => NodeDescription
return func(attributes ?? {}, ...children)
}
const { childrenBind, childrenBindMode, ...otherAttributes } =
attributes ?? {}
return {
type: 'component',
component: element,
properties: otherAttributes,
children,
childrenBind,
childrenBindMode,
}
}
throw new Error(`Unsupported jsx in ${element}`)
}
/**
* Describe a node. Builder for JSX and TSX transformation.
*/
export namespace jsx {
/**
* JSX typing internals
*/
export import JSX = JSXInternal
}