@simpreact/simpreact 0.0.0-alpha.1f6ee65 → 0.0.1

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.
Files changed (65) hide show
  1. package/core/createElement.d.ts +22 -15
  2. package/core/createElement.js +67 -23
  3. package/core/hostAdapter.d.ts +17 -7
  4. package/core/hostAdapter.js +4 -1
  5. package/core/index.d.ts +68 -3
  6. package/core/index.js +6 -3
  7. package/core/internal.d.ts +6 -3
  8. package/core/internal.js +6 -3
  9. package/core/lifecycleEventBus.d.ts +26 -0
  10. package/core/lifecycleEventBus.js +2 -0
  11. package/core/mounting.d.ts +9 -8
  12. package/core/mounting.js +87 -50
  13. package/core/patching.d.ts +6 -4
  14. package/core/patching.js +150 -162
  15. package/core/portal.d.ts +2 -0
  16. package/core/portal.js +9 -0
  17. package/core/ref.d.ts +17 -0
  18. package/core/ref.js +27 -0
  19. package/core/rerender.d.ts +11 -0
  20. package/core/rerender.js +61 -3
  21. package/core/unmounting.d.ts +2 -4
  22. package/core/unmounting.js +26 -27
  23. package/dom/attach-element-to-dom.d.ts +4 -0
  24. package/dom/attach-element-to-dom.js +9 -0
  25. package/dom/domAdapter.d.ts +3 -2
  26. package/dom/domAdapter.js +37 -113
  27. package/dom/events.d.ts +19 -0
  28. package/dom/events.js +129 -0
  29. package/dom/index.d.ts +1733 -1
  30. package/dom/index.js +2 -0
  31. package/dom/namespace.d.ts +2 -0
  32. package/dom/namespace.js +1 -0
  33. package/dom/props/controlled/index.d.ts +7 -0
  34. package/dom/props/controlled/index.js +51 -0
  35. package/dom/props/controlled/input.d.ts +7 -0
  36. package/dom/props/controlled/input.js +80 -0
  37. package/dom/props/controlled/select.d.ts +6 -0
  38. package/dom/props/controlled/select.js +76 -0
  39. package/dom/props/controlled/textarea.d.ts +6 -0
  40. package/dom/props/controlled/textarea.js +61 -0
  41. package/dom/props/dangerInnerHTML.d.ts +7 -0
  42. package/dom/props/dangerInnerHTML.js +24 -0
  43. package/dom/props/index.d.ts +1 -0
  44. package/dom/props/index.js +1 -0
  45. package/dom/props/props.d.ts +5 -0
  46. package/dom/props/props.js +197 -0
  47. package/dom/props/style.d.ts +1 -0
  48. package/dom/props/style.js +32 -0
  49. package/dom/render.d.ts +2 -2
  50. package/dom/render.js +31 -19
  51. package/hooks/index.d.ts +23 -12
  52. package/hooks/index.js +123 -29
  53. package/jsx-runtime/index.d.ts +247 -4
  54. package/jsx-runtime/index.js +2 -5
  55. package/package.json +22 -15
  56. package/shared/index.d.ts +19 -4
  57. package/shared/index.js +5 -4
  58. package/shared/lang.d.ts +3 -3
  59. package/shared/lang.js +3 -3
  60. package/shared/utils.d.ts +3 -2
  61. package/shared/utils.js +3 -6
  62. package/core/global.d.ts +0 -21
  63. package/core/global.js +0 -5
  64. package/shared/types.d.ts +0 -8
  65. package/shared/types.js +0 -1
@@ -1,26 +1,33 @@
1
- import type { Many, Maybe, Primitive } from '../shared';
1
+ import type { Many, Maybe, Nullable, SimpText } from '../shared';
2
2
  import type { HostReference } from './hostAdapter';
3
3
  import type { SimpContextMap } from './context';
4
- export type SimpNode = SimpElement | string | number | bigint | Array<SimpNode> | boolean | null | undefined;
5
- export type Props = any;
4
+ export type SimpNode = SimpElement | SimpText | Array<SimpNode> | boolean | null | undefined;
6
5
  export type Key = string | number | bigint;
7
- export interface FunctionComponent<P = Props> {
8
- (props: P): SimpNode;
6
+ export interface FunctionComponent {
7
+ (props: any): SimpNode;
9
8
  }
10
- export type FC<P = Props> = FunctionComponent<P>;
11
- export type SimpElementFlag = 'FC' | 'HOST' | 'TEXT' | 'FRAGMENT' | 'PROVIDER' | 'CONSUMER';
12
- export interface SimpElement<T = Props> {
9
+ export type FC = FunctionComponent;
10
+ export type SimpElementFlag = 'FC' | 'HOST' | 'TEXT' | 'FRAGMENT' | 'PROVIDER' | 'CONSUMER' | 'PORTAL';
11
+ export interface SimpElementStore {
12
+ latestElement?: Maybe<SimpElement>;
13
+ hostNamespace?: Maybe<string>;
14
+ [key: string]: unknown;
15
+ }
16
+ export interface SimpElement {
13
17
  flag: SimpElementFlag;
18
+ parent: Nullable<SimpElement>;
14
19
  key?: Maybe<Key>;
15
- type?: Maybe<string | FunctionComponent<T>>;
16
- props?: Maybe<T>;
20
+ type?: Maybe<string | FunctionComponent>;
21
+ props?: any;
17
22
  children?: Maybe<SimpNode>;
18
23
  className?: Maybe<string>;
19
24
  reference?: Maybe<HostReference>;
20
- store?: unknown;
25
+ store?: SimpElementStore;
21
26
  contextMap?: Maybe<SimpContextMap>;
27
+ ref?: any;
28
+ unmounted?: boolean;
22
29
  }
23
- export declare function createElement<P = Props>(type: string | FunctionComponent<Readonly<P>>, props?: Maybe<P>, ...children: SimpNode[]): SimpElement<P>;
24
- export declare function createTextElement(text: Primitive): SimpElement;
25
- export declare function normalizeChildren(children: SimpNode): Maybe<Many<SimpElement>>;
26
- export declare function normalizeRoot(node: SimpNode): SimpElement | undefined;
30
+ export declare function createElement(type: string | FunctionComponent, props?: any, ...children: SimpNode[]): SimpElement;
31
+ export declare function createTextElement(text: SimpText): SimpElement;
32
+ export declare function normalizeChildren(children: SimpNode, skipIgnoredCheck: boolean): Maybe<Many<SimpElement>>;
33
+ export declare function normalizeRoot(node: SimpNode, skipIgnoredCheck: boolean): Maybe<SimpElement>;
@@ -1,4 +1,4 @@
1
- import { isPrimitive } from '../shared';
1
+ import { isSimpText } from '../shared';
2
2
  import { Fragment } from './fragment';
3
3
  import { isConsumer, isProvider } from './context';
4
4
  export function createElement(type, props, ...children) {
@@ -17,6 +17,7 @@ export function createElement(type, props, ...children) {
17
17
  }
18
18
  }
19
19
  if (typeof type === 'string') {
20
+ let ref;
20
21
  if (props != null) {
21
22
  for (const propName in props) {
22
23
  if (propName === 'className') {
@@ -30,6 +31,11 @@ export function createElement(type, props, ...children) {
30
31
  definedChildren = props[propName];
31
32
  }
32
33
  }
34
+ else if (propName === 'ref') {
35
+ ref = {
36
+ value: props[propName],
37
+ };
38
+ }
33
39
  else {
34
40
  (newProps ||= {})[propName] = props[propName];
35
41
  }
@@ -38,6 +44,7 @@ export function createElement(type, props, ...children) {
38
44
  const element = {
39
45
  flag: 'HOST',
40
46
  type,
47
+ parent: null,
41
48
  };
42
49
  if (className) {
43
50
  element.className = className;
@@ -45,27 +52,35 @@ export function createElement(type, props, ...children) {
45
52
  if (key) {
46
53
  element.key = key;
47
54
  }
48
- if ((definedChildren = normalizeChildren(definedChildren))) {
55
+ if ((definedChildren = normalizeChildren(definedChildren, false))) {
49
56
  element.children = definedChildren;
50
57
  }
51
58
  if (newProps) {
52
59
  element.props = newProps;
53
60
  }
61
+ if (ref) {
62
+ element.ref = ref;
63
+ }
54
64
  return element;
55
65
  }
56
66
  else if (type === Fragment) {
57
67
  const element = {
58
68
  flag: 'FRAGMENT',
69
+ parent: null,
59
70
  };
60
- element.children = normalizeChildren(definedChildren || (props != null ? props.children : null));
71
+ if ((definedChildren = normalizeChildren(definedChildren || (props != null ? props.children : null), false))) {
72
+ element.children = definedChildren;
73
+ }
61
74
  if (props != null && props.key) {
62
75
  element.key = props?.key;
63
76
  }
64
77
  return element;
65
78
  }
66
79
  else if (isProvider(type)) {
67
- const element = { flag: 'PROVIDER', type, props: { value: props.value } };
68
- element.children = normalizeChildren(definedChildren || props.children);
80
+ const element = { flag: 'PROVIDER', type, props: { value: props.value }, parent: null };
81
+ if ((definedChildren = normalizeChildren(definedChildren || props.children, false))) {
82
+ element.children = definedChildren;
83
+ }
69
84
  if (props != null && props.key) {
70
85
  element.key = props?.key;
71
86
  }
@@ -76,6 +91,7 @@ export function createElement(type, props, ...children) {
76
91
  flag: 'CONSUMER',
77
92
  type,
78
93
  props: { children: definedChildren || (props != null ? props.children : null) },
94
+ parent: null,
79
95
  };
80
96
  if (props != null && props.key) {
81
97
  element.key = props?.key;
@@ -104,6 +120,7 @@ export function createElement(type, props, ...children) {
104
120
  const element = {
105
121
  flag: 'FC',
106
122
  type,
123
+ parent: null,
107
124
  };
108
125
  if (key) {
109
126
  element.key = key;
@@ -117,50 +134,77 @@ export function createElement(type, props, ...children) {
117
134
  export function createTextElement(text) {
118
135
  return {
119
136
  flag: 'TEXT',
120
- children: text == null || text === true || text === false ? '' : text,
137
+ children: text,
138
+ parent: null,
121
139
  };
122
140
  }
123
- export function normalizeChildren(children) {
124
- if (children == null || typeof children === 'boolean') {
141
+ export function normalizeChildren(children, skipIgnoredCheck) {
142
+ if (!skipIgnoredCheck && isIgnoredNode(children)) {
125
143
  return;
126
144
  }
127
145
  const result = [];
128
- normalizeNode(children, result);
146
+ normalizeNode(children, result, undefined, true);
129
147
  if (result.length === 0) {
130
148
  return;
131
149
  }
132
150
  return result.length === 1 ? result[0] : result;
133
151
  }
134
- function normalizeNode(child, result) {
135
- if (child == null || typeof child === 'boolean') {
152
+ function normalizeNode(child, result, currentKey = '', skipIgnoredCheck) {
153
+ if (!skipIgnoredCheck && isIgnoredNode(child)) {
136
154
  return;
137
155
  }
138
- if (isPrimitive(child)) {
139
- result.push(createTextElement(child));
156
+ if (isSimpText(child)) {
157
+ child = createTextElement(child);
158
+ child.key =
159
+ currentKey ||
160
+ // Hack to treat a single child as a one-item list for more consistent reconciliation.
161
+ '.0';
162
+ result.push(child);
140
163
  return;
141
164
  }
142
165
  if (Array.isArray(child)) {
143
- for (const nestedChild of child) {
144
- normalizeNode(nestedChild, result);
166
+ for (let i = 0; i < child.length; i++) {
167
+ normalizeNode(child[i], result, currentKey + '.' + i, false);
145
168
  }
146
169
  return;
147
170
  }
148
- if (typeof child === 'object') {
149
- if (typeof child.flag !== 'string') {
150
- throw new TypeError(`Objects are not valid as a child: ${JSON.stringify(child)}.`);
151
- }
152
- result.push(child);
171
+ if (child.key) {
172
+ currentKey = currentKey.slice(0, -2) + child.key;
153
173
  }
174
+ child.key =
175
+ currentKey ||
176
+ // Hack to treat a single child as a one-item list for more consistent reconciliation.
177
+ '.0';
178
+ result.push(child);
154
179
  }
155
- export function normalizeRoot(node) {
156
- if (node == null || typeof node === 'boolean') {
180
+ export function normalizeRoot(node, skipIgnoredCheck) {
181
+ if (!skipIgnoredCheck && isIgnoredNode(node)) {
157
182
  return;
158
183
  }
159
- if (isPrimitive(node)) {
184
+ if (isSimpText(node)) {
160
185
  return createTextElement(node);
161
186
  }
187
+ if (!Array.isArray(node)) {
188
+ return node;
189
+ }
190
+ node = normalizeChildren(node, true);
162
191
  if (Array.isArray(node)) {
163
192
  return createElement(Fragment, { children: node });
164
193
  }
165
194
  return node;
166
195
  }
196
+ function isIgnoredNode(node) {
197
+ if (node == null || typeof node === 'boolean' || node === '') {
198
+ return true;
199
+ }
200
+ if (Array.isArray(node)) {
201
+ return node.length === 0;
202
+ }
203
+ if (isSimpText(node)) {
204
+ return false;
205
+ }
206
+ if (node.flag === 'FRAGMENT' || node.flag === 'PROVIDER' || node.flag === 'PORTAL') {
207
+ return node.children == null;
208
+ }
209
+ return false;
210
+ }
@@ -1,11 +1,13 @@
1
- import type { Dict, Maybe, Nullable } from '../shared';
2
- export type HostReference = never;
3
- export interface HostAdapter<HostRef = any, HostTextRef = any> {
4
- createReference(type: string): HostRef;
1
+ import type { Maybe, Nullable } from '../shared';
2
+ import type { SimpElement } from './createElement';
3
+ export type HostReference = any;
4
+ export interface HostAdapter<HostRef = any, HostTextRef = any, NS = string> {
5
+ createReference(type: string, namespace?: Maybe<NS>): HostRef;
5
6
  createTextReference(text: string): HostTextRef;
6
- mountProps(reference: HostRef, props: Dict): void;
7
- patchProp(reference: HostRef, propName: string, prevValue: unknown, nextValue: unknown): void;
8
- setClassname(reference: HostRef, className: Maybe<string>): void;
7
+ mountProps(reference: HostRef, element: SimpElement, namespace?: Maybe<NS>): void;
8
+ patchProps(reference: HostRef, prevElement: SimpElement, nextElement: SimpElement, namespace?: Maybe<NS>): void;
9
+ unmountProps(reference: HostRef, element: SimpElement): void;
10
+ setClassname(reference: HostRef, className: Maybe<string>, namespace?: Maybe<NS>): void;
9
11
  setTextContent(reference: HostRef, text: string): void;
10
12
  appendChild(parent: HostRef, child: HostRef | HostTextRef): void;
11
13
  removeChild(parent: HostRef, child: HostRef | HostTextRef): void;
@@ -13,5 +15,13 @@ export interface HostAdapter<HostRef = any, HostTextRef = any> {
13
15
  insertBefore(parent: HostRef, child: HostRef | HostTextRef, before: Nullable<HostRef | HostTextRef>): void;
14
16
  insertOrAppend(parent: HostRef, child: HostRef | HostTextRef, before: Nullable<HostRef | HostTextRef>): void;
15
17
  findParentReference(reference: HostRef | HostTextRef): Nullable<HostRef>;
18
+ findNextSiblingReference(reference: HostRef | HostTextRef): Nullable<HostRef>;
16
19
  clearNode(reference: HostRef | HostTextRef): void;
20
+ attachElementToReference(element: SimpElement, reference: HostRef | HostTextRef): void;
21
+ getHostNamespaces(element: SimpElement, currentNamespace: Maybe<NS>): Nullable<{
22
+ self: Nullable<NS>;
23
+ children: Nullable<NS>;
24
+ }>;
17
25
  }
26
+ export declare let hostAdapter: HostAdapter;
27
+ export declare function provideHostAdapter(adapter: HostAdapter): void;
@@ -1 +1,4 @@
1
- export {};
1
+ export let hostAdapter;
2
+ export function provideHostAdapter(adapter) {
3
+ hostAdapter = adapter;
4
+ }
package/core/index.d.ts CHANGED
@@ -1,3 +1,68 @@
1
- export { type FC, type SimpElement, createElement, type SimpNode, type FunctionComponent } from './createElement';
2
- export { Fragment } from './fragment';
3
- export { type SimpContext, createContext } from './context';
1
+ import type { SimpText } from '../shared';
2
+
3
+ export type ComponentType<P = {}> = FunctionComponent<P>;
4
+
5
+ export type RefObject<T> = { current: T };
6
+ export type RefCallback<T> = {
7
+ bivarianceHack(instance: T | null): (() => void | undefined) | void;
8
+ }['bivarianceHack'];
9
+ export type Ref<T> = RefCallback<T> | RefObject<T> | null;
10
+
11
+ export type Key = string | number | bigint;
12
+
13
+ export interface Attributes {
14
+ key?: Key | null | undefined;
15
+ }
16
+
17
+ export interface RefAttributes<T> extends Attributes {
18
+ ref?: Ref<T> | undefined;
19
+ }
20
+
21
+ export interface SimpElement<P = unknown, T extends string | FunctionComponent<P> = string> {
22
+ type?: T;
23
+ props?: P;
24
+ key?: string | null;
25
+ }
26
+
27
+ export type SimpNode = SimpElement | SimpText | Array<SimpNode> | boolean | null | undefined;
28
+
29
+ export interface ProviderProps<T> {
30
+ value: T;
31
+ children?: SimpNode | undefined;
32
+ }
33
+
34
+ export interface ConsumerProps<T> {
35
+ children: (value: T) => SimpNode;
36
+ }
37
+
38
+ export type SimpContext<T> = {
39
+ Provider: Provider<T>;
40
+ Consumer: Consumer<T>;
41
+ };
42
+
43
+ export type ContextType<C extends SimpContext<any>> = C extends SimpContext<infer T> ? T : never;
44
+
45
+ export type Provider<T> = FunctionComponent<ProviderProps<T>>;
46
+ export type Consumer<T> = FunctionComponent<ConsumerProps<T>>;
47
+
48
+ declare function createElement<P extends {}, T>(
49
+ type: string,
50
+ props?: (RefAttributes<T> & P) | null,
51
+ ...children: SimpNode[]
52
+ ): SimpElement<P>;
53
+ declare function createElement<P extends {}>(
54
+ type: FunctionComponent<P>,
55
+ props?: (Attributes & P) | null,
56
+ ...children: SimpNode[]
57
+ ): SimpElement<P>;
58
+
59
+ declare function createPortal<HostRef = {}>(children: SimpNode, container: HostRef): SimpElement;
60
+
61
+ declare function createContext<T>(defaultValue: T): SimpContext<T>;
62
+
63
+ declare function Fragment(props: PropsWithChildren): SimpElement;
64
+
65
+ export type FunctionComponent<P = {}> = (props: P) => SimpNode;
66
+ export type FC<P = {}> = FunctionComponent<P>;
67
+
68
+ export type PropsWithChildren<P = {}> = P & { children?: SimpNode | undefined };
package/core/index.js CHANGED
@@ -1,3 +1,6 @@
1
- export { createElement } from './createElement';
2
- export { Fragment } from './fragment';
3
- export { createContext } from './context';
1
+ import { createElement } from './createElement';
2
+ import { Fragment } from './fragment';
3
+ import { createContext } from './context';
4
+ import { createPortal } from './portal';
5
+ export { createElement, Fragment, createContext, createPortal };
6
+ export default { createElement, Fragment, createContext, createPortal };
@@ -1,8 +1,11 @@
1
- export * from './index';
2
1
  export * from './context';
3
2
  export * from './createElement';
4
- export * from './rerender';
5
- export * from './global';
3
+ export * from './fragment';
6
4
  export * from './hostAdapter';
5
+ export * from './lifecycleEventBus';
7
6
  export * from './mounting';
8
7
  export * from './patching';
8
+ export * from './portal';
9
+ export * from './ref';
10
+ export * from './rerender';
11
+ export * from './unmounting';
package/core/internal.js CHANGED
@@ -1,8 +1,11 @@
1
- export * from './index';
2
1
  export * from './context';
3
2
  export * from './createElement';
4
- export * from './rerender';
5
- export * from './global';
3
+ export * from './fragment';
6
4
  export * from './hostAdapter';
5
+ export * from './lifecycleEventBus';
7
6
  export * from './mounting';
8
7
  export * from './patching';
8
+ export * from './portal';
9
+ export * from './ref';
10
+ export * from './rerender';
11
+ export * from './unmounting';
@@ -0,0 +1,26 @@
1
+ import { EventBus } from '../shared';
2
+ import type { SimpElement } from './createElement';
3
+ export type LifecycleEvent = {
4
+ type: 'beforeRender';
5
+ element: SimpElement;
6
+ phase: 'mounting' | 'updating';
7
+ } | {
8
+ type: 'afterRender';
9
+ phase: 'mounting' | 'updating';
10
+ } | {
11
+ type: 'mounted';
12
+ element: SimpElement;
13
+ } | {
14
+ type: 'updated';
15
+ element: SimpElement;
16
+ } | {
17
+ type: 'unmounted';
18
+ element: SimpElement;
19
+ } | {
20
+ type: 'errored';
21
+ element: SimpElement;
22
+ error: any;
23
+ phase: 'mounting' | 'updating';
24
+ };
25
+ export type LifecycleEventBus = EventBus<LifecycleEvent>;
26
+ export declare const lifecycleEventBus: EventBus<LifecycleEvent>;
@@ -0,0 +1,2 @@
1
+ import { EventBus } from '../shared';
2
+ export const lifecycleEventBus = new EventBus();
@@ -1,12 +1,13 @@
1
- import type { Nullable } from '../shared';
1
+ import type { Maybe, Nullable } from '../shared';
2
2
  import type { HostReference } from './hostAdapter';
3
3
  import type { SimpElement } from './createElement';
4
4
  import type { SimpContextMap } from './context';
5
- export declare function mount(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
5
+ export declare function mount(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
6
6
  export declare function mountTextElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>): void;
7
- export declare function mountHostElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
8
- export declare function mountFunctionalElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
9
- export declare function mountFragment(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
10
- export declare function mountArrayChildren(children: SimpElement[], reference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
11
- export declare function mountProvider(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
12
- export declare function mountConsumer(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
7
+ export declare function mountHostElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
8
+ export declare function mountFunctionalElement(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
9
+ export declare function mountFragment(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
10
+ export declare function mountArrayChildren(children: SimpElement[], reference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, parentElement: SimpElement, hostNamespace: Maybe<string>): void;
11
+ export declare function mountProvider(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
12
+ export declare function mountConsumer(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
13
+ export declare function mountPortal(element: SimpElement, parentReference: Nullable<HostReference>, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
package/core/mounting.js CHANGED
@@ -1,95 +1,132 @@
1
- import { EMPTY_MAP, EMPTY_OBJECT } from '../shared';
2
- import { GLOBAL } from './global';
3
- import { normalizeRoot } from './createElement';
4
- export function mount(element, parentReference, nextReference, contextMap) {
1
+ import { emptyMap, emptyObject } from '../shared';
2
+ import { hostAdapter } from './hostAdapter';
3
+ import { createTextElement, normalizeRoot } from './createElement';
4
+ import { applyRef } from './ref';
5
+ import { lifecycleEventBus } from './lifecycleEventBus';
6
+ export function mount(element, parentReference, nextReference, contextMap, hostNamespace) {
5
7
  if (element.flag === 'TEXT') {
6
8
  mountTextElement(element, parentReference, nextReference);
7
9
  }
8
10
  else if (element.flag === 'HOST') {
9
- mountHostElement(element, parentReference, nextReference, contextMap);
11
+ mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace);
10
12
  }
11
13
  else if (element.flag === 'FC') {
12
- mountFunctionalElement(element, parentReference, nextReference, contextMap);
14
+ mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace);
13
15
  }
14
16
  else if (element.flag === 'FRAGMENT') {
15
- mountFragment(element, parentReference, nextReference, contextMap);
17
+ mountFragment(element, parentReference, nextReference, contextMap, hostNamespace);
16
18
  }
17
19
  else if (element.flag === 'PROVIDER') {
18
- mountProvider(element, parentReference, nextReference, contextMap);
20
+ mountProvider(element, parentReference, nextReference, contextMap, hostNamespace);
21
+ }
22
+ else if (element.flag === 'PORTAL') {
23
+ mountPortal(element, parentReference, nextReference, contextMap);
19
24
  }
20
25
  else {
21
- mountConsumer(element, parentReference, nextReference, contextMap);
26
+ mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace);
22
27
  }
23
28
  }
24
29
  export function mountTextElement(element, parentReference, nextReference) {
25
- const reference = (element.reference ||= GLOBAL.hostAdapter.createTextReference(element.children));
26
- if (parentReference != null) {
27
- GLOBAL.hostAdapter.insertOrAppend(parentReference, reference, nextReference);
30
+ const reference = (element.reference = hostAdapter.createTextReference(element.children));
31
+ if (parentReference) {
32
+ hostAdapter.insertOrAppend(parentReference, reference, nextReference);
28
33
  }
29
34
  }
30
- export function mountHostElement(element, parentReference, nextReference, contextMap) {
31
- const props = element.props;
32
- const className = element.className;
33
- const hostReference = (element.reference = GLOBAL.hostAdapter.createReference(element.type));
35
+ export function mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace) {
36
+ const hostNamespaces = hostAdapter.getHostNamespaces(element, hostNamespace);
37
+ hostNamespace = hostNamespaces?.self;
38
+ const hostReference = (element.reference = hostAdapter.createReference(element.type, hostNamespace));
39
+ hostAdapter.attachElementToReference(element, hostReference);
40
+ if (parentReference) {
41
+ hostAdapter.insertOrAppend(parentReference, hostReference, nextReference);
42
+ }
34
43
  // HOST element always has Maybe<Many<SimpElement>> children due to normalization process.
35
44
  const children = element.children;
36
- if (className != null && className !== '') {
37
- GLOBAL.hostAdapter.setClassname(hostReference, className);
38
- }
39
45
  if (Array.isArray(children)) {
40
- mountArrayChildren(children, hostReference, null, contextMap);
46
+ mountArrayChildren(children, hostReference, null, contextMap, element, hostNamespaces?.children);
41
47
  }
42
- else if (children != null) {
43
- mount(children, hostReference, null, contextMap);
48
+ else if (children) {
49
+ children.parent = element;
50
+ mount(children, hostReference, null, contextMap, hostNamespaces?.children);
44
51
  }
45
- if (parentReference != null) {
46
- GLOBAL.hostAdapter.insertOrAppend(parentReference, hostReference, nextReference);
52
+ if (element.props) {
53
+ hostAdapter.mountProps(hostReference, element, hostNamespace);
47
54
  }
48
- if (props != null) {
49
- GLOBAL.hostAdapter.mountProps(hostReference, props);
55
+ if (element.className) {
56
+ hostAdapter.setClassname(hostReference, element.className, hostNamespace);
50
57
  }
58
+ applyRef(element);
51
59
  }
52
- export function mountFunctionalElement(element, parentReference, nextReference, contextMap) {
53
- const type = element.type;
60
+ export function mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace) {
61
+ if (contextMap) {
62
+ element.contextMap = contextMap;
63
+ }
64
+ if (element.unmounted) {
65
+ element.unmounted = false;
66
+ }
67
+ element.store = { latestElement: element };
68
+ if (hostNamespace) {
69
+ element.store.hostNamespace = hostNamespace;
70
+ }
71
+ // FC element always has Maybe<SimpElement> children due to normalization process.
54
72
  let children;
55
- element.contextMap = contextMap;
56
- GLOBAL.eventBus.publish({ type: 'beforeRender', element });
57
- children = normalizeRoot(type(element.props || EMPTY_OBJECT));
58
- GLOBAL.eventBus.publish({ type: 'afterRender' });
59
- if (children != null) {
60
- mount((element.children = children), parentReference, nextReference, contextMap);
61
- }
62
- GLOBAL.eventBus.publish({ type: 'mounted', element });
73
+ try {
74
+ lifecycleEventBus.publish({ type: 'beforeRender', element, phase: 'mounting' });
75
+ children = normalizeRoot(element.type(element.props || emptyObject), false);
76
+ lifecycleEventBus.publish({ type: 'afterRender', phase: 'mounting' });
77
+ }
78
+ catch (error) {
79
+ lifecycleEventBus.publish({ type: 'errored', element, error, phase: 'mounting' });
80
+ return;
81
+ }
82
+ if (children) {
83
+ children.parent = element;
84
+ mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
85
+ }
86
+ lifecycleEventBus.publish({ type: 'mounted', element });
63
87
  }
64
- export function mountFragment(element, parentReference, nextReference, contextMap) {
88
+ export function mountFragment(element, parentReference, nextReference, contextMap, hostNamespace) {
65
89
  // FRAGMENT element always has Maybe<Many<SimpElement>> children due to normalization process.
66
90
  if (Array.isArray(element.children)) {
67
- mountArrayChildren(element.children, parentReference, nextReference, contextMap);
91
+ mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
68
92
  }
69
- else if (element.children != null) {
70
- mount(element.children, parentReference, nextReference, contextMap);
93
+ else if (element.children) {
94
+ element.children.parent = element;
95
+ mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
71
96
  }
72
97
  }
73
- export function mountArrayChildren(children, reference, nextReference, contextMap) {
98
+ export function mountArrayChildren(children, reference, nextReference, contextMap, parentElement, hostNamespace) {
74
99
  for (const child of children) {
75
- mount(child, reference, nextReference, contextMap);
100
+ child.parent = parentElement;
101
+ mount(child, reference, nextReference, contextMap, hostNamespace);
76
102
  }
77
103
  }
78
- export function mountProvider(element, parentReference, nextReference, contextMap) {
104
+ export function mountProvider(element, parentReference, nextReference, contextMap, hostNamespace) {
79
105
  contextMap = new Map(contextMap);
80
106
  contextMap.set(element.type.context, element.props.value);
81
107
  // PROVIDER element always has Maybe<Many<SimpElement>> children due to normalization process.
82
108
  if (Array.isArray(element.children)) {
83
- mountArrayChildren(element.children, parentReference, nextReference, contextMap);
109
+ mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
84
110
  }
85
- else if (element.children != null) {
86
- mount(element.children, parentReference, nextReference, contextMap);
111
+ else if (element.children) {
112
+ element.children.parent = element;
113
+ mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
87
114
  }
88
115
  }
89
- export function mountConsumer(element, parentReference, nextReference, contextMap) {
90
- const children = normalizeRoot(element.type(element.props || EMPTY_OBJECT, contextMap || EMPTY_MAP));
91
- if (children == null) {
116
+ export function mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace) {
117
+ const children = normalizeRoot(element.type(element.props || emptyObject, contextMap || emptyMap), false);
118
+ if (!children) {
92
119
  return;
93
120
  }
94
- mount((element.children = children), parentReference, nextReference, contextMap);
121
+ children.parent = element;
122
+ mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
123
+ }
124
+ export function mountPortal(element, parentReference, nextReference, contextMap) {
125
+ if (element.children) {
126
+ element.children.parent = element;
127
+ mount(element.children, element.ref, null, contextMap, hostAdapter.getHostNamespaces(element.children, undefined)?.self);
128
+ }
129
+ const placeHolderElement = createTextElement('');
130
+ mountTextElement(placeHolderElement, parentReference, nextReference);
131
+ element.reference = placeHolderElement.reference;
95
132
  }
@@ -1,8 +1,10 @@
1
+ import type { Maybe, Nullable } from '../shared';
1
2
  import type { SimpElement } from './createElement';
2
- import type { Nullable } from '../shared';
3
3
  import type { HostReference } from './hostAdapter';
4
4
  import type { SimpContextMap } from './context';
5
- export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
5
+ export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
6
+ export declare function patchPortal(prevElement: SimpElement, nextElement: SimpElement, contextMap: Nullable<SimpContextMap>): void;
7
+ export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
8
+ export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
9
+ export declare function findParentReferenceFromElement(element: SimpElement): Nullable<HostReference>;
6
10
  export declare function findHostReferenceFromElement(element: SimpElement): Nullable<HostReference>;
7
- export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;
8
- export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>): void;