stagnate 1.0.7 → 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 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
- declare class Component<REFS = {}, PROPS = undefined, ROOT extends SVGElement | HTMLElement = SVGElement | HTMLElement> {
178
- // props from JSX (set via constructor)
179
- readonly props: PROPS extends undefined ? {} : PROPS
180
- // HTML root element of this component (can be not set if not attached)
181
- protected root: ROOT
182
- // parent of this component (can be not set if not attached)
183
- protected parent: Component<any, any>
184
- // the refs object containing bound elements created during render
185
- protected refs: REFS
186
- // overcomplicated constructor signature made so props is optional only if component has no props defined
187
- constructor(...props: PROPS extends undefined ? [] : [never])
188
- // the render function, called to get the component root element, null can be returned if component
189
- // has nothing to render but will result with exception if component is being created via create or replace
190
- protected render(): Element | null
191
- // the ref callback generation function with a overcomplicated signature, used to bind JSX elements to refs
192
- protected ref<T extends keyof REFS | undefined = undefined>(key?: T): (x: REFS[NonNullable<T>] extends never ? any : REFS[NonNullable<T>]) => void
193
- // create component and replace target element
194
- replace(parent: Component<any, any>, target?: Element | Component<any, any> | null): void
195
- // create component and insert to target
196
- create(parent: Component<any, any>, target?: Element | Component<any, any> | null, before?: Element | Component<any, any> | number): void
197
- // crate component as root (attached set on creation)
198
- createOrphanized(target?: Element | null): void
199
- // bind component to a already existing element (called internally on JSX ref binding)
200
- bind(parent: Component<any, any>, target?: Element): void
201
- // destroy component and it child components
202
- destroy(): void
203
- // check if component is attached
204
- get attached(): boolean
205
- // get the root element of this component
206
- get htmlRoot(): ROOT;
207
- // get and array of child components
208
- protected get components(): Readonly<Component<any, any, any>[]>
209
- // called before render is called
210
- protected onBeforeRender(): void
211
- // called after render is called
212
- protected onRender(): void
213
- // called when parent becomes attached (or on parent assignment if parent is attached)
214
- protected onAttach(): void
215
- // called when element is destroyed
216
- protected onDetach(): void;
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
- // get props of an JSX element or component function / class
301
+ /** get props of an JSX element or component function / class */
225
302
  type ComponentProps<IntrinsicElement | ClassElement | FunctionElement>
226
- // anything that can be legally used in JSX
303
+
304
+ /** any value that can be used in JSX */
227
305
  type StagnateNode
228
- // the JSX element css class attribute
306
+
307
+ /** the JSX element css class attribute */
229
308
  type ClassAttribute
230
309
  ```
231
310
 
@@ -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
- _jsx(): Element;
13
- replace(parent: Component<any, any>, target?: Element | Component<any, any> | null): void;
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
- createOrphanized(target?: Element | null): void;
16
- bind(parent: Component<any, any>, target?: Element): void;
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
- protected attach(): void;
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
- protected get components(): Readonly<Component<any, any, any>[]>;
23
- protected onBeforeRender(): void;
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.bind(this);
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
- _jsx() {
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
- replace(parent, target) {
32
- if (this.root) {
33
- throw new Error("can not create a already created item");
34
- }
35
- if (!target) {
36
- target = parent.root;
37
- }
38
- this.onBeforeRender();
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 (target instanceof Component) {
47
- target.root.replaceWith(html);
48
- target.destroy();
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.onBeforeRender();
75
- const html = this.render();
76
- if (!html) {
77
- throw new Error("can not create component: render function returned null");
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
- this.root = html;
80
- this.onRender();
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
- if (this.root) {
90
- throw new Error("can not create a already created item");
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.remove();
122
- this.parent._components.splice(this.parent._components.indexOf(this), 1);
123
- this.parent = null;
124
- this.root = null;
125
- }
126
- attach() {
127
- for (let i = 0; i < this._components.length; i += 1) {
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._attached = true;
134
- this.onAttach();
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
@@ -1 +1 @@
1
- {"version":3,"file":"Component.js","sourceRoot":"","sources":["../src/Component.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,SAAS;IAWrB,YAAmB,KAAW;QAC7B,IAAI,CAAC,WAAW,GAAG,EAAE,CAAA;QACrB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,EAAU,CAAA;QACtB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACnB,CAAC;IAES,MAAM;QACf,OAAO,IAAI,CAAA;IACZ,CAAC;IAES,GAAG,CAA+C,GAAO;QAClE,OAAO,CAAC,CAAkE,EAAE,EAAE;YAC7E,IAAI,CAAC,YAAY,SAAS,EAAE;gBAC3B,CAAC,CAAC,IAAI,CAAC,IAAgC,CAAC,CAAA;aACxC;YACD,IAAI,GAAG,EAAE;gBACR,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;aAClB;QACF,CAAC,CAAA;IACF,CAAC;IAEM,IAAI;QACV,IAAI,CAAC,cAAc,EAAE,CAAA;QACrB,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;QAC1B,IAAI,CAAC,IAAI,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAA;SAC1E;QACD,IAAI,CAAC,IAAI,GAAG,IAAY,CAAA;QACxB,IAAI,CAAC,QAAQ,EAAE,CAAA;QACf,OAAO,IAAI,CAAA;IACZ,CAAC;IAEM,OAAO,CAAC,MAA2B,EAAE,MAA6C;QACxF,IAAI,IAAI,CAAC,IAAI,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;SACxD;QACD,IAAI,CAAC,MAAM,EAAE;YACZ,MAAM,GAAG,MAAM,CAAC,IAAI,CAAA;SACpB;QACD,IAAI,CAAC,cAAc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AA;IAClB,CAAC;IAED;;;;;;OAMG;IACI,gBAAgB,CAAC,MAAY;QACnC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;QAChC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAChB,CAAC;IAED,8DAA8D;IACvD,OAAO;QACb,IAAI,CAAC,MAAM,EAAE,CAAA;QACb,IAAI,CAAC,IAAI,GAAG,EAAU,CAAA;QACtB,IAAI,IAAI,CAAC,IAAI,EAAE;YACd,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAA;SAClB;QACD,IAAI,IAAI,CAAC,MAAM,EAAE;YAChB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;YACnD,IAAI,KAAK,IAAI,CAAC,EAAE;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
- var extract: <T = Record<string, any>>(input: StagnateNode, slots?: Record<string, any>) => T;
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
- Slot.extract = function (input, slots = {}) {
12
- if (input instanceof SlotArray && input.length) {
13
- slots[input.name] = input[0];
14
- input.pop();
15
- }
16
- else if (Array.isArray(input)) {
17
- for (let i = 0; i < input.length; i += 1) {
18
- const item = input[i];
19
- if (item instanceof SlotArray) {
20
- if (input.length) {
21
- slots[item.name] = item[0];
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
- return slots;
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,CAAC,OAAO,GAAG,UAAkC,KAAmB,EAAE,QAA6B,EAAE;IACpG,IAAI,KAAK,YAAY,SAAS,IAAI,KAAK,CAAC,MAAM,EAAE;QAC/C,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;QAC5B,KAAK,CAAC,GAAG,EAAE,CAAA;KACX;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QAChC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AAA;SACX;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YAChC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;gBACzC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;gBACrB,IAAI,IAAI,YAAY,SAAS,EAAE;oBAC9B,IAAI,KAAK,CAAC,MAAM,EAAE;wBACjB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;qBAC1B;oBACD,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAA;iBACf;qBAAM,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;oBAC/B,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;iBACpB;aACD;SACD;QACD,OAAO,KAAU,CAAA;IAClB,CAAC;IAlBe,YAAO,UAkBtB,CAAA;AACF,CAAC,EA1BgB,IAAI,KAAJ,IAAI,QA0BpB"}
@@ -1,6 +1,7 @@
1
1
  import type { JSX, StagnateNode } from "./types";
2
2
  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;
@@ -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];
@@ -51,8 +52,13 @@ export function jsx(type, props) {
51
52
  props.ref(element);
52
53
  }
53
54
  else if (key == "class") {
54
- const value = props.class;
55
- element.setAttribute("class", typeof value == "string" ? value : collect(value).join(" "));
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
+ }
56
62
  }
57
63
  else if (key == "style") {
58
64
  Object.assign(element.style, props.style);
@@ -77,9 +83,9 @@ export function jsx(type, props) {
77
83
  children.forEach(x => element.appendChild(x instanceof Node ? x : document.createTextNode(x.toString())));
78
84
  return element;
79
85
  }
80
- else if ("prototype" in type && type.prototype._jsx) {
86
+ else if (type.prototype instanceof Component) {
81
87
  const component = new type(props);
82
- const element = component._jsx();
88
+ const element = component.build();
83
89
  if (props.ref) {
84
90
  props.ref(component);
85
91
  }
@@ -97,6 +103,7 @@ export function createElement(type, props, ...children) {
97
103
  props.children = children;
98
104
  return jsx(type, props);
99
105
  }
106
+ /** JSX fragment component, <> can be used as an alias */
100
107
  export function Fragment(props) {
101
108
  return props.children;
102
109
  }
@@ -1 +1 @@
1
- {"version":3,"file":"jsx-runtime.js","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"AAEA,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,CAAC,EAAE;QACxB,MAAM,WAAW,GAAG,EAAS,CAAA;QAC5B,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;QAC5B,OAAO,WAAW,CAAA;KAClB;IACD,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;AAC1B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AAC,SAAS,CAAC,CAAA;SACpB;QACD,OAAO,OAAO,CAAA;KACd;SAAM;QACN,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,IAAI,KAAK,CAAC,GAAG,EAAE;YACd,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;SAClB;QACD,OAAO,OAAO,CAAA;KACd;AACF,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,IAAS,EAAE,KAAU,EAAE,GAAG,QAAe;IACtE,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAA;IACzB,OAAO,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;AACxB,CAAC;AAED,yDAAyD;AACzD,MAAM,UAAU,QAAQ,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<(string | false | null | undefined)>;
56
+ class?: CollectableValue<string>;
50
57
  style?: Partial<CSSStyleDeclaration>;
51
58
  title?: string;
52
59
  tabIndex?: number;
@@ -178,10 +185,13 @@ export declare namespace JSX {
178
185
  props: {};
179
186
  }
180
187
  }
188
+ /** get props of an JSX element or component function / class */
181
189
  export declare type ComponentProps<T extends keyof JSX.IntrinsicElements | {
182
190
  props: any;
183
191
  } | ((props: any) => any)> = T extends keyof JSX.IntrinsicElements ? JSX.IntrinsicElements[T] : (T extends {
184
192
  props: any;
185
193
  } ? T["props"] : (T extends (props: any) => any ? Parameters<T>[0] : never));
194
+ /** any value that can be used in JSX */
186
195
  export declare type StagnateNode = CollectableValue<JSX.Element | string>;
187
- export declare type ClassAttribute = CollectableValue<(string | false | null | undefined)>;
196
+ /** the JSX element css class attribute */
197
+ export declare type ClassAttribute = CollectableValue<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stagnate",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "non-reactive react",
5
5
  "main": "lib/index.js",
6
6
  "files": [