-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.ts
288 lines (258 loc) · 7.67 KB
/
component.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
import type { Observable } from 'rxjs'
import type { ButterfloatEvents, DefaultEvents } from './events.js'
/**
* Handles an effect
*/
export type EffectHandler = <T>(
observable: Observable<T>,
effect: (item: T) => void | Promise<void>,
) => void
/**
* Context for a component. Dependency injection mechanism for
* effect binders and events proxies.
*/
export interface ComponentContext<Events = DefaultEvents> {
events: Events
bindEffect: EffectHandler
bindImmediateEffect: EffectHandler
}
/**
* A Butterfloat Component provided properties and additional context-sensitive tools
*/
// Want to be forgiving in what we accept as a "component"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ContextComponent<Props = any, Events = any> = (
props: Props,
context: ComponentContext<Events>,
) => NodeDescription
/**
* The simplest form of Butterfloat Component
*/
export type SimpleComponent = () => NodeDescription
/**
* A Butterfloat Component
*/
export type Component = ContextComponent | SimpleComponent
/**
* Possible children to a JSX node
*/
export type JsxChildren = Array<NodeDescription | string>
/**
* Attributes of a Node Description
*/
export type Attributes = Record<string, unknown>
/**
* HTML Attributes
*/
export type HtmlAttributes = Record<string, unknown>
/**
* An Observable that produces child Components
*/
export type ChildrenBind = Observable<Component>
/**
* The mode to bind new children to a container
*/
export type ChildrenBindMode = 'append' | 'prepend' | 'replace'
/**
* A JSX node that may produce child Components
*/
export interface ChildrenBindable {
/**
* Bind children as they are observed.
*/
childrenBind?: ChildrenBind
/**
* Mode in which to bind children. Defaults to 'append'.
*/
childrenBindMode?: ChildrenBindMode
}
/**
* Butterfloat Attributes
*/
export type ButterfloatAttributes = HtmlAttributes & ChildrenBindable
/**
* Default bind attribute accepted binds
*/
export type DefaultBind = Record<string, Observable<unknown>>
/**
* Support for delay binding special properties
*/
export interface DelayBind {
/**
* Delay scheduled binding for the "value" property.
*
* Value is bound immediately by default to avoid user interaction
* problems. This provides an opt-in for tested interaction patterns
* and rare elements that use "value" for things aren't user
* interaction such as <progress />.
*/
bfDelayValue?: Observable<unknown>
}
/**
* Default styleBind attribute accepted binds
*/
export type DefaultStyleBind = Record<string, Observable<unknown>>
/**
* Bind for classBind
*/
export type ClassBind = Record<string, Observable<boolean>>
/**
* JSX attributes for "intrinics" (elements) supported by Butterfloat
*/
export interface ButterfloatIntrinsicAttributes<
Bind = DefaultBind,
Events = DefaultEvents & ButterfloatEvents,
Style = DefaultStyleBind,
> extends ButterfloatAttributes {
/**
* Bind an observable to an DOM property.
*
* May use an non-immediate scheduler. Obvious exception: all "value" bindings are immediate, given their role in user inputs.
*/
bind?: Bind & DelayBind
/**
* Immediately bind an observable to a DOM property
*/
immediateBind?: Bind
/**
* Bind an event observable to a DOM event.
*/
events?: Events
/**
* Bind an observable to a style property.
*/
styleBind?: Style
/**
* Immediately bind an observable to a style property.
*/
immediateStyleBind?: Style
/**
* Bind a boolean observable to the appearance of a class in classList.
*/
classBind?: ClassBind
/**
* Immediately bind a boolean observable to the appearance of a class in classList.
*/
immediateClassBind?: ClassBind
}
/*
Discussion: the "Description" types look a lot more verbose than a "standard" VNode
interface in a Virtual DOM. In this case, this is because this isn't intended for
virtual DOM usage. These should last only long enough to build a static DOM and bind
it, then tossed. There's no intention to have a diff/patch between trees of these.
On the other hand, these objects are still useful for DOM-less unit testing of
components.
So it makes sense to use full words. Users may work with these in their tests.
*/
/**
* A Description that supports binding Children
*/
export interface ChildrenBindDescription {
children: JsxChildren
childrenBind?: ChildrenBind
childrenBindMode?: ChildrenBindMode
}
/**
* Description of a DOM element and its bindings
*/
export interface ElementDescription<Bind = DefaultBind>
extends ChildrenBindDescription {
type: 'element'
element: string
attributes: Attributes
bind: Bind
immediateBind: Bind
events: DefaultEvents
styleBind: DefaultStyleBind
immediateStyleBind: DefaultStyleBind
classBind: ClassBind
immediateClassBind: ClassBind
}
/**
* Description of a Component binding
*/
export interface ComponentDescription extends ChildrenBindDescription {
type: 'component'
component: Component
properties: Attributes
}
/**
* Description of a Fragment (the `<Fragment>` pseudo-component which powers `<></>` fragment notation)
*/
export interface FragmentDescription extends ChildrenBindDescription {
type: 'fragment'
attributes: Attributes
}
/**
* Description of the `<Children>` pseudo-component
*/
export interface ChildrenDescription {
type: 'children'
context?: ComponentContext<unknown>
}
/**
* Description of the `<Static>` pseudo-component
*/
export interface StaticDescription {
type: 'static'
element: Element
}
/**
* A description of a node in a Butterfloat DOM tree
*/
export type NodeDescription =
| ElementDescription
| ComponentDescription
| FragmentDescription
| ChildrenDescription
| StaticDescription
/**
* A Component Context for Testing purposes
*/
export interface TestComponentContext<Events = DefaultEvents> {
context: ComponentContext<Events>
// Types here are just for examing test results
// eslint-disable-next-line @typescript-eslint/no-explicit-any
effects: Array<[Observable<unknown>, (item: any) => void]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
immediateEffects: Array<[Observable<unknown>, (item: any) => void]>
}
/**
* Make a test context for testing context components.
* @param events Mocked events for testing
* @returns A test context for testing context component
*/
export function makeTestComponentContext<Events = DefaultEvents>(
events: Events,
): TestComponentContext<Events> {
// Types here are just for examing test results
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const effects: Array<[Observable<unknown>, (item: any) => void]> = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const immediateEffects: Array<[Observable<unknown>, (item: any) => void]> = []
const context: ComponentContext<Events> = {
events,
bindEffect: (observable, effect) => effects.push([observable, effect]),
bindImmediateEffect: (observable, effect) =>
immediateEffects.push([observable, effect]),
}
return { context, effects, immediateEffects }
}
/**
* Does an element description have any binds?
*
* @param description Element description
* @returns True if any dynamic binds
*/
export function hasAnyBinds(description: ElementDescription): boolean {
return (
Boolean(description.childrenBind) ||
Object.keys(description.bind).length > 0 ||
Object.keys(description.immediateBind).length > 0 ||
Object.keys(description.events).length > 0 ||
Object.keys(description.styleBind).length > 0 ||
Object.keys(description.immediateStyleBind).length > 0 ||
Object.keys(description.classBind).length > 0 ||
Object.keys(description.immediateClassBind).length > 0
)
}