-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.ts
629 lines (562 loc) · 15.9 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import {
Cleanup,
MaybeSignal,
Signal,
useEffect,
useSubscope,
useSignal,
SignalLike,
} from "./scope.js";
import type { DomEventProps, DomProps } from "./dom.js";
import { runWithRenderer } from "./renderer.js";
import {
camelCaseToKebabCase,
JsxPropNameToEventName,
jsxPropNameToEventName,
} from "./utils.js";
import { useScope } from "./scope.js";
import { Context, isContext, provideContext } from "./context.js";
import { Template, TemplateNodes } from "./template.js";
interface Tagged<in out T> {
_tag: T;
}
type OmitNever<T> = Omit<
T,
{ [K in keyof T]: T[K] extends never ? K : never }[keyof T]
>;
type PartialRequire<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
/** @ignore */
export interface PropMeta<T> extends PropOptions<T>, Tagged<"p"> {
_type?: [T];
_defaultOrContext: T;
}
export interface AttributeOptions<T> {
/**
* The name of the attribute to observe.
*
* Defaults to the kebab-case version of the prop.
*/
name?: string;
/**
* A function to transform the attribute value to the prop value.
*/
transform: (value: string) => T;
/**
* Set to `true` to not observe the attribute for changes.
*
* @default false
*/
static?: boolean;
}
type PartialPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export interface PropOptions<T> {
attribute?:
| ((value: string) => T)
| (string extends T
? PartialPartial<AttributeOptions<T>, "transform">
: AttributeOptions<T>);
}
type Props<M> = OmitNever<{
readonly [K in keyof M]: M[K] extends PropMeta<infer T> ? Signal<T> : never;
}>;
export type EventConstructor<T = any, E = Event> = new (
name: string,
arg: T,
) => E;
/** @ignore */
export interface EventMeta<out E extends EventConstructor> extends Tagged<"e"> {
_event: E;
}
type Events<M> = OmitNever<
Omit<
{
readonly [K in keyof M]: K extends `on${string}`
? M[K] extends EventMeta<infer E>
? E
: never
: never;
},
`on${Lowercase<keyof HTMLElementEventMap>}`
>
>;
type GeneralJsxProps<T> = Partial<
OmitNever<{
[K in keyof T]: K extends
| typeof jsxPropsSym
| keyof DomProps<any>
| `on${string}`
// Readonly HTMLElement properties
| `${Uppercase<infer _>}${string}`
| "accessKeyLabel"
| "offsetHeight"
| "offsetLeft"
| "offsetParent"
| "offsetTop"
| "offsetWidth"
| "attributes"
| "classList"
| "clientHeight"
| "clientLeft"
| "clientTop"
| "clientWidth"
| "localName"
| "namespaceURI"
| "ownerDocument"
| "part"
| "prefix"
| "scrollHeight"
| "scrollWidth"
| "shadowRoot"
| "tagName"
| "baseURI"
| "childNodes"
| "firstChild"
| "isConnected"
| "lastChild"
| "nextSibling"
| "nodeName"
| "nodeType"
| "parentElement"
| "parentNode"
| "previousSibling"
| "nextElementSibling"
| "previousElementSibling"
| "childElementCount"
| "firstElementChild"
| "lastElementChild"
| "assignedSlot"
| "attributeStyleMap"
| "isContentEditable"
| "dataset"
? never
: T[K] extends Function
? never
: MaybeSignal<T[K]>;
}>
> &
DomProps<any> &
DomEventProps<never> &
// Allow other HTMLElement attributes
Record<string, any>;
export type JsxProps<T extends HTMLElement> = typeof jsxPropsSym extends keyof T
? NonNullable<T[typeof jsxPropsSym]>
: any;
type ComponentJsxProps<M> = Partial<
OmitNever<{
[K in keyof Props<M>]: Props<M>[K] extends Signal<infer T>
? MaybeSignal<T>
: never;
}> & {
[K in keyof Events<M>]: (evt: InstanceType<Events<M>[K]>) => void;
}
> &
GeneralJsxProps<HTMLElement>;
type EventEmitters<M> = OmitNever<
Omit<
{
[K in keyof Events<M>]: Events<M>[K] extends EventConstructor<infer E>
? undefined extends E
? (arg?: E) => boolean
: (arg: E) => boolean
: never;
},
`on${Lowercase<keyof HTMLElementEventMap>}`
>
>;
/**
* Defines a property in your component metadata that can be set from outside
* of the component.
*
* Make sure to avoid conflicts with native `HTMLElement` properties.
*
* You can get properties by accessing the {@link Signal} in `this.props`.
* It's also possible to set the properties directly on the component instance.
*
* It's also possible to define an attribute for the property by setting the
* `attribute` option. By default, the attribute name is the kebab-case version
* of the property name. The attribute will be observed and the signal updated
* on changes. You can also provide a custom name and a transform function to
* convert the attribute value to the property value.
*
* @example
* ```tsx
* class App extends Component("x-app", {
* greetingMessage: prop<string>("Hello, world!", {
* attribute: {
* name: "greeting",
* }
* }),
* }) {
* render() {
* return <h1>{this.props.greetingMessage}</h1>;
* }
* }
*
* defineComponents(App);
*
* const app = new App();
* app.greetingMessage = "Hello, universe!";
* ```
*/
export const prop: (<T>(
context: Context<T>,
opts?: PropOptions<T>,
) => PropMeta<T | undefined>) &
(<T>(defaultValue: T, opts?: PropOptions<T>) => PropMeta<T>) &
(<T>(
defaultValue?: T,
opts?: PropOptions<T | undefined>,
) => PropMeta<T | undefined>) = <T>(
defaultOrContext?: Context<T> | T,
opts?: PropOptions<T>,
): PropMeta<any> => ({
_tag: "p",
_defaultOrContext: defaultOrContext,
...opts,
});
// CustomEvent<T> has a flaw in its constructor signature since it allows
// `detail` to be optional. This is a workaround to make it required unless
// `undefined` can be assigned to `T`.
type _CustomEventContructor<T> = undefined extends T
? typeof CustomEvent<T>
: EventConstructor<
PartialRequire<CustomEventInit<T>, "detail">,
CustomEvent<T>
>;
/**
* Defines an event in your component metadata that can be dispatched by
* the component.
*
* Make sure your event name starts with `on` and to avoid conflicts with
* native `HTMLElement` events. The event name will be converted to kebab-case.
*
* You can dispatch events either using `HTMLElement.dispatchEvent` or by
* calling the event emitter function in `this.events` inside the `render`
* function of a component.
*
* @example
* ```tsx
* class App extends Component("x-app", {
* onSomethingHappen: event<string>(),
* // Event name will be `something-happen`
* }) {
* render() {
* // …
* this.events.onSomethingHappen({ detail: "Something happened! "});
* }
* }
*
* const app = new App();
* app.addEventListener("something-happen", (evt) => {
* // `evt` is `CustomEvent<string>`
* console.log(evt.detail);
* });
* ```
*
* You can also provide a custom event constructor:
*
* @example
* ```tsx
* class App extends Component("x-app", {
* onSomethingClick: event(() => MouseEvent),
* }) {
* render() {
* return (
* <button onclick={evt => this.events.onSomethingClick(evt)}>
* Click me!
* </button>
* );
* }
* }
* ```
*/
export const event: (() => EventMeta<_CustomEventContructor<undefined>>) &
(<T>() => EventMeta<_CustomEventContructor<T>>) &
(<E extends EventConstructor>(eventConstructor: E) => EventMeta<E>) = ((
eventConstructor: EventConstructor = CustomEvent,
): EventMeta<EventConstructor> => ({
_tag: "e",
_event: eventConstructor,
})) as any;
export type Metadata = {
// Forbid all library properties
[K in keyof ComponentInner<any> | "props" | "events"]?: never;
} & {
// Forbid all dom props
[K in keyof DomProps<any>]?: never;
} & {
// Forbid all HTMLElement props
[K in keyof HTMLElement]?: never;
} & {
[name: string]: PropMeta<any> | EventMeta<any>;
};
export const componentSym = Symbol("Component");
export declare const jsxPropsSym: unique symbol;
declare abstract class ComponentInner<M extends Metadata> {
protected props: Props<M>;
protected events: EventEmitters<M>;
readonly [jsxPropsSym]?: ComponentJsxProps<M>;
readonly [componentSym]: {
_scope?: ReturnType<typeof useScope>;
_destroy?: (() => void) | void;
};
connectedCallback(): void;
disconnectedCallback(): void;
attributeChangedCallback(
name: string,
oldValue: string | null,
value: string | null,
): void;
addEventListener<K extends keyof Events<M> & string>(
type: JsxPropNameToEventName<K>,
listener: (event: InstanceType<Events<M>[K]>) => void,
options?: boolean | AddEventListenerOptions,
): void;
removeEventListener<K extends keyof Events<M> & string>(
type: JsxPropNameToEventName<K>,
listener: (event: InstanceType<Events<M>[K]>) => void,
options?: boolean | EventListenerOptions,
): void;
abstract render(): Template;
}
export type Component<M extends Metadata = {}> = {
-readonly [K in keyof Props<M>]: Props<M>[K] extends Signal<infer T>
? T
: never;
} & ComponentInner<M> &
HTMLElement;
export interface ComponentConstructor<M extends Metadata = {}> {
/** @ignore */
readonly [componentSym]: {
readonly _tagName: string;
};
readonly observedAttributes: readonly string[];
new (): Component<M>;
}
export interface ComponentOptions {
/**
* Shadow DOM options. Set to `false` to disable shadow DOM.
*
* @default { mode: "open" }
*/
shadow?: ShadowRootInit | false;
}
let mountEffects:
| [fn: () => Cleanup, deps?: SignalLike<unknown>[]][]
| undefined;
/**
* Creates an effect which will rerun when any accessed signal changes.
*
* If used inside of a component and the component is not yet mounted, the
* effect will run only after the component is mounted. Otherwise, the effect
* will run immediately.
*
* @param fn The function to run; it may return a cleanup function.
*/
export const useMountEffect = (
fn: () => Cleanup,
deps?: SignalLike<unknown>[],
): void => {
if (mountEffects) {
mountEffects.push([fn, deps]);
} else {
useEffect(fn, deps);
}
};
/**
* Creates a new web component class.
*
* Specify props and events using the `metadata` parameter.
*
* @example
* ```tsx
* class MyComponent extends Component("my-component", {
* myProp: prop<string>("Hello, world!"),
* onMyEvent: event(),
* }) {
* render() {
* return (
* <>
* <h1>{this.props.myProp}</h1>
* <button onclick={() => this.events.onMyEvent()}>Click me</button>
* </>
* );
* },
* }
*
* customElements.define("my-component", MyComponent);
* ```
*/
export const Component: ((tagName: string) => ComponentConstructor<{}>) &
(<const M extends Metadata>(
tagName: string,
metadata: M,
opts?: ComponentOptions,
) => ComponentConstructor<M>) = ((
tagName: string,
metadata: Metadata = {},
opts: ComponentOptions = {},
): ComponentConstructor => {
// Extract attribute information
const observedAttributes: string[] = [];
const attributePropMap = new Map<
string,
{
name: string;
meta: PropMeta<any> & {
attribute: Required<
NonNullable<Exclude<PropMeta<any>["attribute"], boolean | Function>>
>;
};
}
>();
for (const name in metadata) {
const meta = metadata[name] as PropMeta<any> | EventMeta<any>;
if (meta._tag == "p" && meta.attribute) {
if (typeof meta.attribute == "function") {
meta.attribute = { transform: meta.attribute };
}
const attribute: AttributeOptions<any> = (meta.attribute = {
name: camelCaseToKebabCase(name),
static: false,
transform: (x) => x,
...meta.attribute,
});
attributePropMap.set(attribute.name!, {
name,
meta: meta as any,
});
if (!attribute.static) {
observedAttributes.push(attribute.name!);
}
}
}
// Create base class
opts.shadow ??= { mode: "open" };
const getRenderParent = (component: _Component) =>
opts.shadow
? component.shadowRoot ?? component.attachShadow(opts.shadow)
: component;
abstract class _Component extends HTMLElement {
static readonly [componentSym]: ComponentConstructor[typeof componentSym] =
{
_tagName: tagName,
};
static readonly observedAttributes: readonly string[] = observedAttributes;
protected props: Record<string, Signal<any>> = {};
protected events: Record<string, (arg: unknown) => any> = {};
readonly [componentSym]: ComponentInner<any>[typeof componentSym] = {};
constructor() {
super();
for (const name in metadata) {
const meta = metadata[name];
if (meta._tag == "p") {
const context = isContext(meta._defaultOrContext)
? meta._defaultOrContext
: null;
const [getter, setter] = useSignal<unknown>(
context ? undefined : meta._defaultOrContext,
);
this.props[name] = getter;
if (context) {
provideContext(context, this, getter);
}
Object.defineProperty(this, name, {
get: getter.peek,
set: (value) => setter(() => value, { force: true }),
});
} else if (meta._tag == "e" && name.startsWith("on")) {
const eventName = jsxPropNameToEventName(name as `on${string}`);
this.events[name] = (arg: unknown) =>
this.dispatchEvent(new meta._event(eventName, arg));
}
}
}
connectedCallback(): void {
const renderParent = getRenderParent(this);
this[componentSym]._destroy = useSubscope(() =>
runWithRenderer(
{
_svg: false,
_component: this as any,
_nodes: renderParent.childNodes.values(),
},
() => {
this[componentSym]._scope = useScope();
// Render
const prevMountEffects = mountEffects;
mountEffects = [];
try {
TemplateNodes.forEach(this.render().build(), (node) => {
renderParent.append(node);
});
// Run mount effects
mountEffects.forEach(([fn, opts]) => useEffect(fn, opts));
} finally {
mountEffects = prevMountEffects;
}
},
),
)[1];
}
disconnectedCallback(): void {
this[componentSym]._destroy?.();
}
attributeChangedCallback(
name: string,
_: string | null,
value: string | null,
): void {
const prop = attributePropMap.get(name);
if (prop) {
this[prop.name as keyof this] =
value != null
? prop.meta.attribute.transform.call(this, value)
: isContext(prop.meta._defaultOrContext)
? undefined
: prop.meta._defaultOrContext;
}
}
abstract render(): Template;
}
return _Component as any;
}) as any;
/**
* Determines whether the given value is a component created by
* extending {@link ComponentConstructor}.
*/
export const isComponent = (
value: any,
): value is ComponentConstructor | Component => !!value?.[componentSym];
/**
* Represents a functional component.
*
* @example
* ```tsx
* const MyComponent: FunctionalComponent<{
* name: MaybeSignal<string>;
* }> = ({ name }) => {
* return <h1>Hello, {name}!</h1>;
* };
* ```
*/
export interface FunctionalComponent<in P extends object = {}> {
(props: P): Template;
}
/**
* Defines a set of components with the given prefix.
*/
export const defineComponents: ((
...components: ComponentConstructor[]
) => void) &
((prefix: string, ...components: ComponentConstructor[]) => void) = (
...args: [string | ComponentConstructor, ...ComponentConstructor[]]
) => {
const [prefix, components] =
typeof args[0] == "string"
? [args[0], args.slice(1) as ComponentConstructor[]]
: ["", args as ComponentConstructor[]];
for (const component of components) {
customElements.define(prefix + component[componentSym]._tagName, component);
}
};