-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.ts
More file actions
130 lines (110 loc) · 3.31 KB
/
Copy pathcomponent.ts
File metadata and controls
130 lines (110 loc) · 3.31 KB
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
import { destroyVNode } from '@/destroy-dom';
import { mountVNode } from '@/mount-dom';
import { patchDOM } from '@/patch-dom';
import { extractChildNodes } from '@/utils';
import { type VNode, isClassComponentVNode, isElementVNode, isFragmentVNode } from '@/vdom';
import equals from 'fast-deep-equal';
export abstract class Component<TProps, TState> {
#isMounted = false;
#vdom: VNode = null;
#hostEl: Element = null;
state: Readonly<TState>;
props: Readonly<TProps>;
children: VNode[];
#refs: Record<string, Element> = {};
constructor(props?: TProps, children?: VNode[]) {
this.props = props ?? ({} as TProps);
this.children = children ?? [];
}
get $refs() {
return this.#refs;
}
get elements(): Element[] {
if (!this.#vdom) return [];
return isFragmentVNode(this.#vdom)
? this.#extractFragmentElements()
: [this.#vdom.el as Element];
}
get firstElement(): Element | null {
return this.elements[0] ?? null;
}
get offset(): number {
return isFragmentVNode(this.#vdom)
? Array.from(this.#hostEl.childNodes).indexOf(this.firstElement)
: 0;
}
abstract render(): VNode;
abstract onMounted(): void;
abstract onUnmounted(): void;
abstract onUpdated(): void;
updateProps(props: Partial<TProps>) {
const newProps = { ...this.props, ...props };
if (equals(this.props, newProps)) return;
this.props = newProps;
this.#patch();
}
updateState(state: Partial<TState> | ((prevState: TState) => TState)) {
const newState = typeof state === 'function' ? state(this.state) : { ...this.state, ...state };
if (equals(this.state, newState)) return; // Skip if state hasn't changed
this.state = newState;
this.#patch();
}
mount(hostEl: Element, index: number = null) {
if (this.#isMounted) {
throw new Error('Component is already mounted');
}
this.#vdom = this.render();
mountVNode(this.#vdom, hostEl, index, this);
this.#setRefs();
this.#hostEl = hostEl;
this.#isMounted = true;
this.onMounted();
}
unmount() {
if (!this.#isMounted) {
throw new Error('Component is not mounted');
}
this.#refs = {};
destroyVNode(this.#vdom);
this.#vdom = null;
this.#hostEl = null;
this.#isMounted = false;
this.onUnmounted();
}
forceUpdate() {
this.#patch();
}
#patch() {
if (!this.#isMounted) {
throw new Error('Component is not mounted');
}
const vdom = this.render();
this.#vdom = patchDOM(this.#vdom, vdom, this.#hostEl, this);
this.onUpdated();
}
#setRefs() {
this.#setRefForVNode(this.#vdom);
this.children.forEach((child) => {
if (child.ref) {
this.#setRefForVNode(child);
}
});
}
#setRefForVNode(vnode: VNode) {
if (isElementVNode(vnode) && vnode.ref) {
this.#setRef(vnode.ref, vnode.el as Element);
}
}
#setRef(ref: string, el: Element) {
if (this.#refs[ref]) {
console.warn(`Ref "${ref}" already exists`);
return;
}
this.#refs[ref] = el;
}
#extractFragmentElements(): Element[] {
return extractChildNodes(this.#vdom).flatMap((child) =>
isClassComponentVNode(child) ? child.component.elements : child.el
);
}
}