stagnate 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -44
- package/lib/Component.d.ts +106 -11
- package/lib/Component.js +137 -91
- package/lib/Component.js.map +1 -1
- package/lib/Slot.d.ts +26 -1
- package/lib/Slot.js +45 -17
- package/lib/Slot.js.map +1 -1
- package/lib/jsx-runtime.d.ts +1 -0
- package/lib/jsx-runtime.js +16 -7
- package/lib/jsx-runtime.js.map +1 -1
- package/lib/types.d.ts +13 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -174,58 +174,137 @@ Primary used to avoid passing nested JSX elements as props.
|
|
|
174
174
|
|
|
175
175
|
### Component class
|
|
176
176
|
```typescript
|
|
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
|
-
|
|
177
|
+
/**
|
|
178
|
+
* Stagnate Component class
|
|
179
|
+
*
|
|
180
|
+
* @typeParam REFS - interface describing jsx references stored on this component, see {@link Component.refs}
|
|
181
|
+
* @typeParam PROPS - interface describing jsx props passed to this component
|
|
182
|
+
* @typeParam ROOT - type of the html root element
|
|
183
|
+
*/
|
|
184
|
+
class Component<REFS = {}, PROPS = undefined, ROOT extends SVGElement | HTMLElement = SVGElement | HTMLElement> {
|
|
185
|
+
/** properties received from jsx (or set via constructor) */
|
|
186
|
+
readonly props: PROPS extends undefined ? {} : PROPS
|
|
187
|
+
|
|
188
|
+
/** html root element, only accessible after {@link build} was called */
|
|
189
|
+
protected root: ROOT
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* parent component, only accessible after {@link bind} was called
|
|
193
|
+
* for self-bound components `this.parent = this`
|
|
194
|
+
*/
|
|
195
|
+
protected parent: Component<any, any>
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* jsx references stored on this component,
|
|
199
|
+
* the REFS type should be set to a interface describing what references the component will use
|
|
200
|
+
* see {@link ref} for how to set references
|
|
201
|
+
*/
|
|
202
|
+
protected refs: REFS
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* if component is to be used from jsx it has to have one constructor argument being the props,
|
|
206
|
+
* any other constructor signature will fail when used from jsx
|
|
207
|
+
*/
|
|
208
|
+
constructor(props: PROPS)
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* component render function, should return the component JSX
|
|
212
|
+
* if null is returned {@link build} call will fail with an exception
|
|
213
|
+
*/
|
|
214
|
+
protected render(): Element | null
|
|
215
|
+
|
|
216
|
+
/** called in {@link build} before render is called */
|
|
217
|
+
protected onBeforeRender(): void
|
|
218
|
+
|
|
219
|
+
/** called in {@link build} after render is called and root is set */
|
|
220
|
+
protected onRender(): void
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* called when component if fully attached (children are attached and `this.attached = true`),
|
|
224
|
+
* `onAttach` will be called only if the component is bound to a parent component or the component
|
|
225
|
+
* is self-bound
|
|
226
|
+
*/
|
|
227
|
+
protected onAttach(): void
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* called when component if fully detached (children are detached and `this.attached = false`),
|
|
231
|
+
* `onDetach` will be called only if the component is bound to a parent component or it {@link destroy} was
|
|
232
|
+
* called on the component directly
|
|
233
|
+
*/
|
|
234
|
+
protected onDetach(): void
|
|
235
|
+
|
|
236
|
+
/** function meant to be used in jsx for binding references */
|
|
237
|
+
protected ref<T extends keyof REFS | undefined = undefined>(key?: T): (x: REFS[NonNullable<T>] extends never ? any : REFS[NonNullable<T>]) => void
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* calls {@link render} and sets {@link root}
|
|
241
|
+
* @returns the created DOM element ({@link root})
|
|
242
|
+
*/
|
|
243
|
+
build(): Element
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* add component to a parent component,
|
|
247
|
+
* attach component if parent component is attached
|
|
248
|
+
*
|
|
249
|
+
* a component can be self-bound by passing itself as parent (`x.bind(x)`),
|
|
250
|
+
* self-bound components get automatically attached
|
|
251
|
+
*/
|
|
252
|
+
bind(parent: Component<any, any>): void
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* render the component, add it as a child of {@link parent} and insert it into DOM
|
|
256
|
+
*
|
|
257
|
+
* calls {@link build} and {@link bind} internally
|
|
258
|
+
*
|
|
259
|
+
* @param parent - parent component
|
|
260
|
+
* @param target - DOM element to add the component to, if unset or null `parent.root` is used, if a component is passed it's root will be used
|
|
261
|
+
* @param before - add the element before the given DOM child, a number can be used as a child index, if unset adds the element as the last child
|
|
262
|
+
*/
|
|
263
|
+
create(parent: Component<any, any>, target?: Element | Component<any, any> | null, before?: Element | Component<any, any> | number): void
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* render the component, add it as a child of {@link parent} and add it to DOM by replacing a existing element or component
|
|
267
|
+
*
|
|
268
|
+
* calls {@link build} and {@link bind} internally
|
|
269
|
+
*
|
|
270
|
+
* @param parent - parent component
|
|
271
|
+
* @param target - component or DOM element to replace, if component is passed {@link destroy} will be called on it
|
|
272
|
+
*/
|
|
273
|
+
replace(parent: Component<any, any>, target: Element | Component<any, any>): void
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* render the component, add it to DOM and self-bound, meant to be used for creating the root component
|
|
277
|
+
*
|
|
278
|
+
* calls {@link build} and {@link bind} internally
|
|
279
|
+
*
|
|
280
|
+
* @param target - DOM target to append the component to
|
|
281
|
+
*/
|
|
282
|
+
createOrphanized(target: Node): void
|
|
283
|
+
|
|
284
|
+
/** remove this component from DOM and its parent component */
|
|
285
|
+
destroy(): void
|
|
286
|
+
|
|
287
|
+
/** true if component is attached */
|
|
288
|
+
get attached(): boolean
|
|
289
|
+
|
|
290
|
+
/** public accessor for {@link root} */
|
|
291
|
+
get htmlRoot(): ROOT
|
|
292
|
+
|
|
293
|
+
/** child component list accessor */
|
|
294
|
+
get components(): Readonly<Component<any, any, any>[]>
|
|
217
295
|
}
|
|
218
|
-
|
|
219
296
|
```
|
|
220
297
|
|
|
221
298
|
### Utility Types
|
|
222
299
|
|
|
223
300
|
```typescript
|
|
224
|
-
|
|
301
|
+
/** get props of an JSX element or component function / class */
|
|
225
302
|
type ComponentProps<IntrinsicElement | ClassElement | FunctionElement>
|
|
226
|
-
|
|
303
|
+
|
|
304
|
+
/** any value that can be used in JSX */
|
|
227
305
|
type StagnateNode
|
|
228
|
-
|
|
306
|
+
|
|
307
|
+
/** the JSX element css class attribute */
|
|
229
308
|
type ClassAttribute
|
|
230
309
|
```
|
|
231
310
|
|
package/lib/Component.d.ts
CHANGED
|
@@ -1,27 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stagnate Component class
|
|
3
|
+
*
|
|
4
|
+
* @typeParam REFS - interface describing jsx references stored on this component, see {@link Component.refs}
|
|
5
|
+
* @typeParam PROPS - interface describing jsx props passed to this component
|
|
6
|
+
* @typeParam ROOT - type of the html root element
|
|
7
|
+
*/
|
|
1
8
|
export declare class Component<REFS = {}, PROPS = undefined, ROOT extends SVGElement | HTMLElement = SVGElement | HTMLElement> {
|
|
9
|
+
/** properties received from jsx (or set via constructor) */
|
|
2
10
|
readonly props: PROPS extends undefined ? {} : PROPS;
|
|
3
11
|
private _components;
|
|
4
12
|
private _attached;
|
|
13
|
+
/** html root element, only accessible after {@link build} was called */
|
|
5
14
|
protected root: ROOT;
|
|
15
|
+
/**
|
|
16
|
+
* parent component, only accessible after {@link bind} was called
|
|
17
|
+
* for self-bound components `this.parent = this`
|
|
18
|
+
*/
|
|
6
19
|
protected parent: Component<any, any>;
|
|
20
|
+
/**
|
|
21
|
+
* jsx references stored on this component,
|
|
22
|
+
* the REFS type should be set to a interface describing what references the component will use
|
|
23
|
+
* see {@link ref} for how to set references
|
|
24
|
+
*/
|
|
7
25
|
protected refs: REFS;
|
|
26
|
+
/**
|
|
27
|
+
* if component is to be used from jsx it has to have one constructor argument being the props,
|
|
28
|
+
* any other constructor signature will fail when used from jsx
|
|
29
|
+
*/
|
|
8
30
|
constructor(...props: PROPS extends undefined ? [] : [never]);
|
|
9
31
|
constructor(props: PROPS);
|
|
32
|
+
private attach;
|
|
33
|
+
private detach;
|
|
34
|
+
/**
|
|
35
|
+
* component render function, should return the component JSX
|
|
36
|
+
* if null is returned {@link build} call will fail with an exception
|
|
37
|
+
*/
|
|
10
38
|
protected render(): Element | null;
|
|
39
|
+
/** called in {@link build} before render is called */
|
|
40
|
+
protected onBeforeRender(): void;
|
|
41
|
+
/** called in {@link build} after render is called and root is set */
|
|
42
|
+
protected onRender(): void;
|
|
43
|
+
/**
|
|
44
|
+
* called when component if fully attached (children are attached and `this.attached = true`),
|
|
45
|
+
* `onAttach` will be called only if the component is bound to a parent component or the component
|
|
46
|
+
* is self-bound
|
|
47
|
+
*/
|
|
48
|
+
protected onAttach(): void;
|
|
49
|
+
/**
|
|
50
|
+
* called when component if fully detached (children are detached and `this.attached = false`),
|
|
51
|
+
* `onDetach` will be called only if the component is bound to a parent component or it {@link destroy} was
|
|
52
|
+
* called on the component directly
|
|
53
|
+
*/
|
|
54
|
+
protected onDetach(): void;
|
|
55
|
+
/**
|
|
56
|
+
* function meant to be used in jsx for binding references
|
|
57
|
+
*
|
|
58
|
+
* the following will bind the div under `this.refs.foo` and requires the `REFS` type to include `{foo: HTMLDivElement}`
|
|
59
|
+
* ```
|
|
60
|
+
* <div ref={this.ref("foo")} />
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* the following will bind the child component under `this.refs.bar` and requires the `REFS` type to include `{bar: BarComponent}`
|
|
64
|
+
* ```
|
|
65
|
+
* <BarComponent ref={this.ref("bar")} />
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* the following will bind the child component without adding it to refs
|
|
69
|
+
* ```
|
|
70
|
+
* <BarComponent ref={this.ref()} />
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
11
73
|
protected ref<T extends keyof REFS | undefined = undefined>(key?: T): (x: REFS[NonNullable<T>] extends never ? any : REFS[NonNullable<T>]) => void;
|
|
12
|
-
|
|
13
|
-
|
|
74
|
+
/**
|
|
75
|
+
* calls {@link render} and sets {@link root}
|
|
76
|
+
* @returns the created DOM element ({@link root})
|
|
77
|
+
*/
|
|
78
|
+
build(): Element;
|
|
79
|
+
/**
|
|
80
|
+
* add component to a parent component,
|
|
81
|
+
* attach component if parent component is attached
|
|
82
|
+
*
|
|
83
|
+
* a component can be self-bound by passing itself as parent (`x.bind(x)`),
|
|
84
|
+
* self-bound components get automatically attached
|
|
85
|
+
*/
|
|
86
|
+
bind(parent: Component<any, any>): void;
|
|
87
|
+
/**
|
|
88
|
+
* render the component, add it as a child of {@link parent} and insert it into DOM
|
|
89
|
+
*
|
|
90
|
+
* calls {@link build} and {@link bind} internally
|
|
91
|
+
*
|
|
92
|
+
* @param parent - parent component
|
|
93
|
+
* @param target - DOM element to add the component to, if unset or null `parent.root` is used, if a component is passed it's root will be used
|
|
94
|
+
* @param before - add the element before the given DOM child, a number can be used as a child index, if unset adds the element as the last child
|
|
95
|
+
*/
|
|
14
96
|
create(parent: Component<any, any>, target?: Element | Component<any, any> | null, before?: Element | Component<any, any> | number): void;
|
|
15
|
-
|
|
16
|
-
|
|
97
|
+
/**
|
|
98
|
+
* render the component, add it as a child of {@link parent} and add it to DOM by replacing a existing element or component
|
|
99
|
+
*
|
|
100
|
+
* calls {@link build} and {@link bind} internally
|
|
101
|
+
*
|
|
102
|
+
* @param parent - parent component
|
|
103
|
+
* @param target - component or DOM element to replace, if component is passed {@link destroy} will be called on it
|
|
104
|
+
*/
|
|
105
|
+
replace(parent: Component<any, any>, target: Element | Component<any, any>): void;
|
|
106
|
+
/**
|
|
107
|
+
* render the component, add it to DOM and self-bound, meant to be used for creating the root component
|
|
108
|
+
*
|
|
109
|
+
* calls {@link build} and {@link bind} internally
|
|
110
|
+
*
|
|
111
|
+
* @param target - DOM target to append the component to
|
|
112
|
+
*/
|
|
113
|
+
createOrphanized(target: Node): void;
|
|
114
|
+
/** remove this component from DOM and its parent component */
|
|
17
115
|
destroy(): void;
|
|
18
|
-
|
|
19
|
-
protected detach(): void;
|
|
116
|
+
/** true if component is attached */
|
|
20
117
|
get attached(): boolean;
|
|
118
|
+
/** public accessor for {@link root} */
|
|
21
119
|
get htmlRoot(): ROOT;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
protected onRender(): void;
|
|
25
|
-
protected onAttach(): void;
|
|
26
|
-
protected onDetach(): void;
|
|
120
|
+
/** child component list accessor */
|
|
121
|
+
get components(): Readonly<Component<any, any, any>[]>;
|
|
27
122
|
}
|
package/lib/Component.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stagnate Component class
|
|
3
|
+
*
|
|
4
|
+
* @typeParam REFS - interface describing jsx references stored on this component, see {@link Component.refs}
|
|
5
|
+
* @typeParam PROPS - interface describing jsx props passed to this component
|
|
6
|
+
* @typeParam ROOT - type of the html root element
|
|
7
|
+
*/
|
|
1
8
|
export class Component {
|
|
2
9
|
constructor(props) {
|
|
3
10
|
this._components = [];
|
|
@@ -5,20 +12,86 @@ export class Component {
|
|
|
5
12
|
this.refs = {};
|
|
6
13
|
this.props = props;
|
|
7
14
|
}
|
|
15
|
+
attach() {
|
|
16
|
+
for (let i = 0; i < this._components.length; i += 1) {
|
|
17
|
+
const component = this._components[i];
|
|
18
|
+
if (!component._attached) {
|
|
19
|
+
component.attach();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
this._attached = true;
|
|
23
|
+
this.onAttach();
|
|
24
|
+
}
|
|
25
|
+
detach() {
|
|
26
|
+
this._components.forEach(x => x.detach());
|
|
27
|
+
this._components = [];
|
|
28
|
+
this._attached = false;
|
|
29
|
+
this.onDetach();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* component render function, should return the component JSX
|
|
33
|
+
* if null is returned {@link build} call will fail with an exception
|
|
34
|
+
*/
|
|
8
35
|
render() {
|
|
9
36
|
return null;
|
|
10
37
|
}
|
|
38
|
+
/** called in {@link build} before render is called */
|
|
39
|
+
onBeforeRender() {
|
|
40
|
+
}
|
|
41
|
+
/** called in {@link build} after render is called and root is set */
|
|
42
|
+
onRender() {
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* called when component if fully attached (children are attached and `this.attached = true`),
|
|
46
|
+
* `onAttach` will be called only if the component is bound to a parent component or the component
|
|
47
|
+
* is self-bound
|
|
48
|
+
*/
|
|
49
|
+
onAttach() {
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* called when component if fully detached (children are detached and `this.attached = false`),
|
|
53
|
+
* `onDetach` will be called only if the component is bound to a parent component or it {@link destroy} was
|
|
54
|
+
* called on the component directly
|
|
55
|
+
*/
|
|
56
|
+
onDetach() {
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* function meant to be used in jsx for binding references
|
|
60
|
+
*
|
|
61
|
+
* the following will bind the div under `this.refs.foo` and requires the `REFS` type to include `{foo: HTMLDivElement}`
|
|
62
|
+
* ```
|
|
63
|
+
* <div ref={this.ref("foo")} />
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* the following will bind the child component under `this.refs.bar` and requires the `REFS` type to include `{bar: BarComponent}`
|
|
67
|
+
* ```
|
|
68
|
+
* <BarComponent ref={this.ref("bar")} />
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* the following will bind the child component without adding it to refs
|
|
72
|
+
* ```
|
|
73
|
+
* <BarComponent ref={this.ref()} />
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
11
76
|
ref(key) {
|
|
12
77
|
return (x) => {
|
|
13
78
|
if (x instanceof Component) {
|
|
14
|
-
x.
|
|
79
|
+
x.parent = this;
|
|
80
|
+
this._components.push(x);
|
|
81
|
+
if (this._attached) {
|
|
82
|
+
x.attach();
|
|
83
|
+
}
|
|
15
84
|
}
|
|
16
85
|
if (key) {
|
|
17
86
|
this.refs[key] = x;
|
|
18
87
|
}
|
|
19
88
|
};
|
|
20
89
|
}
|
|
21
|
-
|
|
90
|
+
/**
|
|
91
|
+
* calls {@link render} and sets {@link root}
|
|
92
|
+
* @returns the created DOM element ({@link root})
|
|
93
|
+
*/
|
|
94
|
+
build() {
|
|
22
95
|
this.onBeforeRender();
|
|
23
96
|
const html = this.render();
|
|
24
97
|
if (!html) {
|
|
@@ -28,37 +101,35 @@ export class Component {
|
|
|
28
101
|
this.onRender();
|
|
29
102
|
return html;
|
|
30
103
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const html = this.render();
|
|
40
|
-
if (!html) {
|
|
41
|
-
throw new Error("can not replace component: render function returned null");
|
|
42
|
-
}
|
|
43
|
-
this.root = html;
|
|
44
|
-
this.onRender();
|
|
104
|
+
/**
|
|
105
|
+
* add component to a parent component,
|
|
106
|
+
* attach component if parent component is attached
|
|
107
|
+
*
|
|
108
|
+
* a component can be self-bound by passing itself as parent (`x.bind(x)`),
|
|
109
|
+
* self-bound components get automatically attached
|
|
110
|
+
*/
|
|
111
|
+
bind(parent) {
|
|
45
112
|
this.parent = parent;
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
113
|
+
if (parent != this) {
|
|
114
|
+
parent._components.push(this);
|
|
115
|
+
if (parent._attached) {
|
|
116
|
+
this.attach();
|
|
117
|
+
}
|
|
49
118
|
}
|
|
50
119
|
else {
|
|
51
|
-
target.replaceWith(html);
|
|
52
|
-
}
|
|
53
|
-
this.parent._components.push(this);
|
|
54
|
-
if (parent._attached) {
|
|
55
120
|
this.attach();
|
|
56
121
|
}
|
|
57
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* render the component, add it as a child of {@link parent} and insert it into DOM
|
|
125
|
+
*
|
|
126
|
+
* calls {@link build} and {@link bind} internally
|
|
127
|
+
*
|
|
128
|
+
* @param parent - parent component
|
|
129
|
+
* @param target - DOM element to add the component to, if unset or null `parent.root` is used, if a component is passed it's root will be used
|
|
130
|
+
* @param before - add the element before the given DOM child, a number can be used as a child index, if unset adds the element as the last child
|
|
131
|
+
*/
|
|
58
132
|
create(parent, target, before) {
|
|
59
|
-
if (this.root) {
|
|
60
|
-
throw new Error("can not create a already created item");
|
|
61
|
-
}
|
|
62
133
|
if (!target) {
|
|
63
134
|
target = parent.root;
|
|
64
135
|
}
|
|
@@ -71,90 +142,65 @@ export class Component {
|
|
|
71
142
|
else if (before instanceof Component) {
|
|
72
143
|
before = before.root;
|
|
73
144
|
}
|
|
74
|
-
this.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
145
|
+
target.insertBefore(this.build(), before === undefined ? null : before);
|
|
146
|
+
this.bind(parent);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* render the component, add it as a child of {@link parent} and add it to DOM by replacing a existing element or component
|
|
150
|
+
*
|
|
151
|
+
* calls {@link build} and {@link bind} internally
|
|
152
|
+
*
|
|
153
|
+
* @param parent - parent component
|
|
154
|
+
* @param target - component or DOM element to replace, if component is passed {@link destroy} will be called on it
|
|
155
|
+
*/
|
|
156
|
+
replace(parent, target) {
|
|
157
|
+
if (target instanceof Component) {
|
|
158
|
+
target.root.replaceWith(this.build());
|
|
159
|
+
target.destroy();
|
|
78
160
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
target.insertBefore(html, before === undefined ? null : before);
|
|
82
|
-
this.parent = parent;
|
|
83
|
-
this.parent._components.push(this);
|
|
84
|
-
if (parent._attached) {
|
|
85
|
-
this.attach();
|
|
161
|
+
else {
|
|
162
|
+
target.replaceWith(this.build());
|
|
86
163
|
}
|
|
164
|
+
this.bind(parent);
|
|
87
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* render the component, add it to DOM and self-bound, meant to be used for creating the root component
|
|
168
|
+
*
|
|
169
|
+
* calls {@link build} and {@link bind} internally
|
|
170
|
+
*
|
|
171
|
+
* @param target - DOM target to append the component to
|
|
172
|
+
*/
|
|
88
173
|
createOrphanized(target) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
this.onBeforeRender();
|
|
93
|
-
const html = this.render();
|
|
94
|
-
if (!html) {
|
|
95
|
-
throw new Error("can not create component: render function returned null");
|
|
96
|
-
}
|
|
97
|
-
this.root = html;
|
|
98
|
-
this.onRender();
|
|
99
|
-
target === null || target === void 0 ? void 0 : target.appendChild(html);
|
|
100
|
-
this.attach();
|
|
101
|
-
}
|
|
102
|
-
bind(parent, target) {
|
|
103
|
-
if (target) {
|
|
104
|
-
if (this.root) {
|
|
105
|
-
throw new Error("can not bind a already bound item");
|
|
106
|
-
}
|
|
107
|
-
this.root = target;
|
|
108
|
-
}
|
|
109
|
-
else if (!this.root) {
|
|
110
|
-
throw new Error("can not bind not rendered item to itself");
|
|
111
|
-
}
|
|
112
|
-
this.parent = parent;
|
|
113
|
-
parent._components.push(this);
|
|
114
|
-
if (parent._attached) {
|
|
115
|
-
this.attach();
|
|
116
|
-
}
|
|
174
|
+
target.appendChild(this.build());
|
|
175
|
+
this.bind(this);
|
|
117
176
|
}
|
|
177
|
+
/** remove this component from DOM and its parent component */
|
|
118
178
|
destroy() {
|
|
119
179
|
this.detach();
|
|
120
180
|
this.refs = {};
|
|
121
|
-
this.root
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
this.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const component = this._components[i];
|
|
129
|
-
if (!component._attached) {
|
|
130
|
-
component.attach();
|
|
181
|
+
if (this.root) {
|
|
182
|
+
this.root.remove();
|
|
183
|
+
}
|
|
184
|
+
if (this.parent) {
|
|
185
|
+
const index = this.parent._components.indexOf(this);
|
|
186
|
+
if (index >= 0) {
|
|
187
|
+
this.parent._components.splice(index, 1);
|
|
131
188
|
}
|
|
132
189
|
}
|
|
133
|
-
this.
|
|
134
|
-
this.
|
|
135
|
-
}
|
|
136
|
-
detach() {
|
|
137
|
-
this._components.forEach(x => x.detach());
|
|
138
|
-
this._components = [];
|
|
139
|
-
this._attached = false;
|
|
140
|
-
this.onDetach();
|
|
190
|
+
this.parent = null;
|
|
191
|
+
this.root = null;
|
|
141
192
|
}
|
|
193
|
+
/** true if component is attached */
|
|
142
194
|
get attached() {
|
|
143
195
|
return this._attached;
|
|
144
196
|
}
|
|
197
|
+
/** public accessor for {@link root} */
|
|
145
198
|
get htmlRoot() {
|
|
146
199
|
return this.root;
|
|
147
200
|
}
|
|
201
|
+
/** child component list accessor */
|
|
148
202
|
get components() {
|
|
149
203
|
return this._components;
|
|
150
204
|
}
|
|
151
|
-
onBeforeRender() {
|
|
152
|
-
}
|
|
153
|
-
onRender() {
|
|
154
|
-
}
|
|
155
|
-
onAttach() {
|
|
156
|
-
}
|
|
157
|
-
onDetach() {
|
|
158
|
-
}
|
|
159
205
|
}
|
|
160
206
|
//# sourceMappingURL=Component.js.map
|
package/lib/Component.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Component.js","sourceRoot":"","sources":["../src/Component.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,SAAS;
|
|
1
|
+
{"version":3,"file":"Component.js","sourceRoot":"","sources":["../src/Component.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IA6BrB,YAAmB,KAAW;Q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;gBACf,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;aACxC;SACD;QACD,IAAI,CAAC,MAAM,GAAG,IAAW,CAAA;QACzB,IAAI,CAAC,IAAI,GAAG,IAAW,CAAA;IACxB,CAAC;IAED,oCAAoC;IACpC,IAAW,QAAQ;QAClB,OAAO,IAAI,CAAC,SAAS,CAAA;IACtB,CAAC;IAED,uCAAuC;IACvC,IAAW,QAAQ;QAClB,OAAO,IAAI,CAAC,IAAI,CAAA;IACjB,CAAC;IAED,oCAAoC;IACpC,IAAW,UAAU;QACpB,OAAO,IAAI,CAAC,WAAW,CAAA;IACxB,CAAC;CACD"}
|
package/lib/Slot.d.ts
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
import { JSX, StagnateNode } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Pseudo component used to create named slots.
|
|
4
|
+
*
|
|
5
|
+
* example use:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* function Foo(props: {children: StagnateNode}) {
|
|
8
|
+
* const slots = Slot.extract<{foo: HTMLElement, bar: HTMLElement}>(props.children)
|
|
9
|
+
* return <div>
|
|
10
|
+
* <div>{slots.foo}</div>
|
|
11
|
+
* <div>{slots.bar}</div>
|
|
12
|
+
* </div>
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* const example = <Foo>
|
|
16
|
+
* <Slot name="foo">FOO</Slot>
|
|
17
|
+
* <Slot name="bar">BAR</Slot>
|
|
18
|
+
* </Foo>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
2
21
|
export declare function Slot(props: {
|
|
3
22
|
name: string;
|
|
4
23
|
children: StagnateNode;
|
|
5
24
|
}): JSX.Element;
|
|
6
25
|
export declare namespace Slot {
|
|
7
|
-
|
|
26
|
+
/**
|
|
27
|
+
* This function extracts slotted children from the child list,
|
|
28
|
+
* it has to be called if slots are in the child list or render will fail on un-extracted slot data.
|
|
29
|
+
*
|
|
30
|
+
* see {@link Slot} for a usage example
|
|
31
|
+
*/
|
|
32
|
+
function extract<T = Record<string, any>>(input: StagnateNode, slots?: Record<string, any>): T;
|
|
8
33
|
}
|
package/lib/Slot.js
CHANGED
|
@@ -5,28 +5,56 @@ class SlotArray extends Array {
|
|
|
5
5
|
this.push(elements);
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Pseudo component used to create named slots.
|
|
10
|
+
*
|
|
11
|
+
* example use:
|
|
12
|
+
* ```typescript
|
|
13
|
+
* function Foo(props: {children: StagnateNode}) {
|
|
14
|
+
* const slots = Slot.extract<{foo: HTMLElement, bar: HTMLElement}>(props.children)
|
|
15
|
+
* return <div>
|
|
16
|
+
* <div>{slots.foo}</div>
|
|
17
|
+
* <div>{slots.bar}</div>
|
|
18
|
+
* </div>
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* const example = <Foo>
|
|
22
|
+
* <Slot name="foo">FOO</Slot>
|
|
23
|
+
* <Slot name="bar">BAR</Slot>
|
|
24
|
+
* </Foo>
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
8
27
|
export function Slot(props) {
|
|
9
28
|
return new SlotArray(props.name, props.children);
|
|
10
29
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
(function (Slot) {
|
|
31
|
+
/**
|
|
32
|
+
* This function extracts slotted children from the child list,
|
|
33
|
+
* it has to be called if slots are in the child list or render will fail on un-extracted slot data.
|
|
34
|
+
*
|
|
35
|
+
* see {@link Slot} for a usage example
|
|
36
|
+
*/
|
|
37
|
+
function extract(input, slots = {}) {
|
|
38
|
+
if (input instanceof SlotArray && input.length) {
|
|
39
|
+
slots[input.name] = input[0];
|
|
40
|
+
input.pop();
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(input)) {
|
|
43
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
44
|
+
const item = input[i];
|
|
45
|
+
if (item instanceof SlotArray) {
|
|
46
|
+
if (input.length) {
|
|
47
|
+
slots[item.name] = item[0];
|
|
48
|
+
}
|
|
49
|
+
input[i] = null;
|
|
50
|
+
}
|
|
51
|
+
else if (Array.isArray(item)) {
|
|
52
|
+
extract(item, slots);
|
|
22
53
|
}
|
|
23
|
-
input[i] = null;
|
|
24
|
-
}
|
|
25
|
-
else if (Array.isArray(item)) {
|
|
26
|
-
this.extract(item, slots);
|
|
27
54
|
}
|
|
28
55
|
}
|
|
56
|
+
return slots;
|
|
29
57
|
}
|
|
30
|
-
|
|
31
|
-
};
|
|
58
|
+
Slot.extract = extract;
|
|
59
|
+
})(Slot || (Slot = {}));
|
|
32
60
|
//# sourceMappingURL=Slot.js.map
|
package/lib/Slot.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Slot.js","sourceRoot":"","sources":["../src/Slot.ts"],"names":[],"mappings":"AAEA,MAAM,SAAU,SAAQ,KAAmB;IAE1C,YAAa,IAAY,EAAE,QAAsB;QAChD,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACpB,CAAC;CACD;AAED,MAAM,UAAU,IAAI,CAAC,KAA6C;IACjE,OAAO,IAAI,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAQ,CAAA;AACxD,CAAC;AAED,IAAI,
|
|
1
|
+
{"version":3,"file":"Slot.js","sourceRoot":"","sources":["../src/Slot.ts"],"names":[],"mappings":"AAEA,MAAM,SAAU,SAAQ,KAAmB;IAE1C,YAAa,IAAY,EAAE,QAAsB;QAChD,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,CAAC,QAA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
|
export declare function jsx(type: any, props: any): any;
|
|
3
3
|
export declare function createElement(type: any, props: any, ...children: any[]): any;
|
|
4
|
+
/** JSX fragment component, <> can be used as an alias */
|
|
4
5
|
export declare function Fragment(props: {
|
|
5
6
|
children: StagnateNode;
|
|
6
7
|
}): JSX.Element;
|
package/lib/jsx-runtime.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Component } from "./Component";
|
|
1
2
|
function _collect(data, accumulator) {
|
|
2
3
|
for (let i = 0; i < data.length; i += 1) {
|
|
3
4
|
const value = data[i];
|
|
@@ -29,8 +30,10 @@ export function jsx(type, props) {
|
|
|
29
30
|
else if (typeof type == "string") {
|
|
30
31
|
const children = collect(props.children);
|
|
31
32
|
let element;
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
let isSvg = false;
|
|
34
|
+
if (type.startsWith("svg")) {
|
|
35
|
+
element = document.createElementNS("http://www.w3.org/2000/svg", type.length == 3 ? "svg" : type.slice(4));
|
|
36
|
+
isSvg = true;
|
|
34
37
|
}
|
|
35
38
|
else {
|
|
36
39
|
element = document.createElement(type);
|
|
@@ -49,15 +52,20 @@ export function jsx(type, props) {
|
|
|
49
52
|
props.ref(element);
|
|
50
53
|
}
|
|
51
54
|
else if (key == "class") {
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
let value = props.class;
|
|
56
|
+
if (Array.isArray(value)) {
|
|
57
|
+
value = collect(value).join(" ");
|
|
58
|
+
}
|
|
59
|
+
if (value) {
|
|
60
|
+
element.setAttribute("class", value);
|
|
61
|
+
}
|
|
54
62
|
}
|
|
55
63
|
else if (key == "style") {
|
|
56
64
|
Object.assign(element.style, props.style);
|
|
57
65
|
}
|
|
58
66
|
else {
|
|
59
67
|
const value = props[key];
|
|
60
|
-
const attribute =
|
|
68
|
+
const attribute = isSvg ? key : key.toLowerCase();
|
|
61
69
|
if (value === true) {
|
|
62
70
|
element.setAttribute(attribute, "");
|
|
63
71
|
}
|
|
@@ -75,9 +83,9 @@ export function jsx(type, props) {
|
|
|
75
83
|
children.forEach(x => element.appendChild(x instanceof Node ? x : document.createTextNode(x.toString())));
|
|
76
84
|
return element;
|
|
77
85
|
}
|
|
78
|
-
else if (
|
|
86
|
+
else if (type.prototype instanceof Component) {
|
|
79
87
|
const component = new type(props);
|
|
80
|
-
const element = component.
|
|
88
|
+
const element = component.build();
|
|
81
89
|
if (props.ref) {
|
|
82
90
|
props.ref(component);
|
|
83
91
|
}
|
|
@@ -95,6 +103,7 @@ export function createElement(type, props, ...children) {
|
|
|
95
103
|
props.children = children;
|
|
96
104
|
return jsx(type, props);
|
|
97
105
|
}
|
|
106
|
+
/** JSX fragment component, <> can be used as an alias */
|
|
98
107
|
export function Fragment(props) {
|
|
99
108
|
return props.children;
|
|
100
109
|
}
|
package/lib/jsx-runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsx-runtime.js","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"jsx-runtime.js","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAGvC,SAAS,QAAQ,CAAI,IAAyB,EAAE,WAAgB;IAC/D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;QACrB,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YACzB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAA;SAC5B;aAAM,IAAI,KAAK,EAAE;YACjB,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;SACvB;KACD;AACF,CAAC;AAED,SAAS,OAAO,CAAI,IAA6B;IAChD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAA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CAAC,KAA+B;IACvD,OAAO,KAAK,CAAC,QAAuB,CAAA;AACrC,CAAC;AAED,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE,CAAA"}
|
package/lib/types.d.ts
CHANGED
|
@@ -10,6 +10,13 @@ export interface EvenHandlers {
|
|
|
10
10
|
onChange: (ev: Event) => void;
|
|
11
11
|
onClick: (ev: MouseEvent) => void;
|
|
12
12
|
onClose: (ev: Event) => void;
|
|
13
|
+
onDrag: (ev: DragEvent) => void;
|
|
14
|
+
onDragEnd: (ev: DragEvent) => void;
|
|
15
|
+
onDragEnter: (ev: DragEvent) => void;
|
|
16
|
+
onDragLeave: (ev: DragEvent) => void;
|
|
17
|
+
onDragOver: (ev: DragEvent) => void;
|
|
18
|
+
onDragStart: (ev: DragEvent) => void;
|
|
19
|
+
onDrop: (ev: DragEvent) => void;
|
|
13
20
|
onContextMenu: (ev: MouseEvent) => void;
|
|
14
21
|
onDblClick: (ev: MouseEvent) => void;
|
|
15
22
|
onFocus: (ev: FocusEvent) => void;
|
|
@@ -46,7 +53,7 @@ export interface EvenHandlers {
|
|
|
46
53
|
export interface GlobalAttributes<T> extends Partial<EvenHandlers> {
|
|
47
54
|
ref?: (value: T) => void;
|
|
48
55
|
id?: string;
|
|
49
|
-
class?: CollectableValue<
|
|
56
|
+
class?: CollectableValue<string>;
|
|
50
57
|
style?: Partial<CSSStyleDeclaration>;
|
|
51
58
|
title?: string;
|
|
52
59
|
tabIndex?: number;
|
|
@@ -148,10 +155,6 @@ export interface SvgElementAttributes extends GlobalAttributes<SVGElement> {
|
|
|
148
155
|
fill?: string;
|
|
149
156
|
children?: CollectableValue<JSX.Element | string>;
|
|
150
157
|
}
|
|
151
|
-
export interface SvgPathElementAttributes extends GlobalAttributes<SVGPathElement> {
|
|
152
|
-
fill?: string;
|
|
153
|
-
d?: string;
|
|
154
|
-
}
|
|
155
158
|
export declare namespace JSX {
|
|
156
159
|
interface IntrinsicElements {
|
|
157
160
|
text: TextElementAttributes;
|
|
@@ -165,7 +168,7 @@ export declare namespace JSX {
|
|
|
165
168
|
option: OptionElementAttributes;
|
|
166
169
|
iframe: IframeElementAttributes;
|
|
167
170
|
svg: SvgElementAttributes;
|
|
168
|
-
|
|
171
|
+
[key: `svg:${string}`]: GlobalAttributes<SVGElement> & Record<string, any>;
|
|
169
172
|
}
|
|
170
173
|
interface ElementChildrenAttribute {
|
|
171
174
|
children: {};
|
|
@@ -182,10 +185,13 @@ export declare namespace JSX {
|
|
|
182
185
|
props: {};
|
|
183
186
|
}
|
|
184
187
|
}
|
|
188
|
+
/** get props of an JSX element or component function / class */
|
|
185
189
|
export declare type ComponentProps<T extends keyof JSX.IntrinsicElements | {
|
|
186
190
|
props: any;
|
|
187
191
|
} | ((props: any) => any)> = T extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[T] : (T extends {
|
|
188
192
|
props: any;
|
|
189
193
|
} ? T["props"] : (T extends (props: any) => any ? Parameters<T>[0] : never));
|
|
194
|
+
/** any value that can be used in JSX */
|
|
190
195
|
export declare type StagnateNode = CollectableValue<JSX.Element | string>;
|
|
191
|
-
|
|
196
|
+
/** the JSX element css class attribute */
|
|
197
|
+
export declare type ClassAttribute = CollectableValue<string>;
|