@simpreact/simpreact 0.0.5 → 0.0.7

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 (83) hide show
  1. package/LICENSE.txt +1 -1
  2. package/compat/context.d.ts +4 -4
  3. package/compat/context.js +3 -2
  4. package/compat/core.d.ts +4 -0
  5. package/compat/core.js +9 -5
  6. package/compat/dom.d.ts +4 -5
  7. package/compat/dom.js +3 -2
  8. package/compat/hooks.d.ts +16 -13
  9. package/compat/hooks.js +19 -15
  10. package/compat/index.d.ts +12 -9
  11. package/compat/index.js +3 -0
  12. package/compat/renderRuntime.d.ts +6 -0
  13. package/compat/renderRuntime.js +17 -0
  14. package/component/index.d.ts +16 -0
  15. package/component/index.js +164 -0
  16. package/context/index.d.ts +12 -5
  17. package/context/index.js +62 -57
  18. package/core/createElement.d.ts +29 -20
  19. package/core/createElement.js +159 -133
  20. package/core/hostAdapter.d.ts +8 -12
  21. package/core/hostAdapter.js +1 -4
  22. package/core/hostOperations.d.ts +5 -0
  23. package/core/hostOperations.js +15 -0
  24. package/core/index.d.ts +20 -6
  25. package/core/internal.d.ts +5 -0
  26. package/core/internal.js +5 -0
  27. package/core/lifecycleEventBus.d.ts +15 -6
  28. package/core/lifecycleEventBus.js +16 -2
  29. package/core/memo.d.ts +0 -2
  30. package/core/memo.js +1 -3
  31. package/core/mounting.d.ts +6 -9
  32. package/core/mounting.js +177 -86
  33. package/core/mountingChildren.d.ts +4 -0
  34. package/core/mountingChildren.js +47 -0
  35. package/core/patching.d.ts +7 -8
  36. package/core/patching.js +245 -252
  37. package/core/patchingChildren.d.ts +6 -0
  38. package/core/patchingChildren.js +343 -0
  39. package/core/portal.d.ts +1 -1
  40. package/core/portal.js +17 -7
  41. package/core/processStack.d.ts +106 -0
  42. package/core/processStack.js +75 -0
  43. package/core/ref.d.ts +4 -3
  44. package/core/rerender.d.ts +4 -14
  45. package/core/rerender.js +67 -112
  46. package/core/runtime.d.ts +17 -0
  47. package/core/runtime.js +2 -0
  48. package/core/unmounting.d.ts +6 -5
  49. package/core/unmounting.js +58 -57
  50. package/core/unmountingChildren.d.ts +4 -0
  51. package/core/unmountingChildren.js +23 -0
  52. package/core/utils.d.ts +11 -0
  53. package/core/utils.js +168 -0
  54. package/dom/attach-element-to-dom.d.ts +4 -3
  55. package/dom/attach-element-to-dom.js +12 -7
  56. package/dom/domAdapter.js +22 -25
  57. package/dom/events.d.ts +5 -5
  58. package/dom/events.js +33 -16
  59. package/dom/index.d.ts +16 -5
  60. package/dom/index.js +4 -3
  61. package/dom/props/controlled/index.d.ts +3 -3
  62. package/dom/props/controlled/index.js +8 -8
  63. package/dom/props/controlled/input.d.ts +3 -3
  64. package/dom/props/controlled/input.js +57 -34
  65. package/dom/props/controlled/select.d.ts +3 -3
  66. package/dom/props/controlled/select.js +39 -26
  67. package/dom/props/controlled/textarea.d.ts +3 -3
  68. package/dom/props/controlled/textarea.js +57 -34
  69. package/dom/props/dangerInnerHTML.d.ts +5 -5
  70. package/dom/props/dangerInnerHTML.js +10 -15
  71. package/dom/props/props.d.ts +4 -4
  72. package/dom/props/props.js +24 -21
  73. package/dom/render.d.ts +4 -5
  74. package/dom/render.js +38 -34
  75. package/hooks/index.d.ts +15 -13
  76. package/hooks/index.js +154 -157
  77. package/jsx-runtime/index.d.ts +2 -1
  78. package/package.json +9 -1
  79. package/shared/index.d.ts +10 -0
  80. package/shared/index.js +4 -7
  81. package/shared/utils.js +4 -4
  82. package/shared/EventBus.d.ts +0 -18
  83. package/shared/EventBus.js +0 -28
@@ -1,7 +1,7 @@
1
- import type { Maybe } from '../../shared/index.js';
2
1
  import type { SimpElement } from '../../core/internal.js';
3
- export declare function patchDangerInnerHTML(prevValue: Maybe<{
4
- __html: string;
5
- }>, nextValue: Maybe<{
2
+ import type { Maybe } from '../../shared/index.js';
3
+ type DangerInnerHTMLValue = {
6
4
  __html: string;
7
- }>, prevElement: Maybe<SimpElement>, nextElement: SimpElement, dom: Element): void;
5
+ };
6
+ export declare function patchDangerInnerHTML(prevValue: Maybe<DangerInnerHTMLValue>, nextValue: Maybe<DangerInnerHTMLValue>, nextElement: SimpElement, dom: Element): void;
7
+ export {};
@@ -1,20 +1,15 @@
1
- import { unmount } from '../../core/internal.js';
2
- export function patchDangerInnerHTML(prevValue, nextValue, prevElement, nextElement, dom) {
3
- const prevHTML = prevValue?.__html || '';
4
- const nextHTML = nextValue?.__html || '';
1
+ export function patchDangerInnerHTML(prevValue, nextValue, nextElement, dom) {
2
+ const prevHTML = prevValue?.__html;
3
+ const nextHTML = nextValue?.__html;
5
4
  if (nextElement.children) {
6
- console.warn('Avoid setting both children and props.dangerouslySetInnerHTML at the same time — this can cause unpredictable behavior.');
5
+ console.warn('Avoid setting both children and props.dangerouslySetInnerHTML at the same time — this causes unpredictable behavior.');
7
6
  }
8
- if (prevHTML !== nextHTML) {
9
- if (nextHTML != null && !isSameInnerHTML(dom, nextHTML)) {
10
- if (prevElement != null) {
11
- if (prevElement.children) {
12
- unmount(prevElement.children);
13
- prevElement.children = undefined;
14
- }
15
- }
16
- dom.innerHTML = nextHTML;
17
- }
7
+ if (prevHTML === nextHTML) {
8
+ return;
9
+ }
10
+ const nextInnerHTML = nextHTML ?? '';
11
+ if (!isSameInnerHTML(dom, nextInnerHTML)) {
12
+ dom.innerHTML = nextInnerHTML;
18
13
  }
19
14
  }
20
15
  function isSameInnerHTML(dom, innerHTML) {
@@ -1,5 +1,5 @@
1
- import type { SimpElement } from '../../core/internal.js';
1
+ import type { SimpElement, SimpRenderRuntime } from '../../core/internal.js';
2
2
  import type { Namespace } from '../namespace.js';
3
- export declare function mountProps(dom: HTMLElement | SVGElement, element: SimpElement, namespace: Namespace): void;
4
- export declare function unmountProps(dom: HTMLElement | SVGElement, element: SimpElement): void;
5
- export declare function patchProps(dom: HTMLElement | SVGElement, prevElement: SimpElement, nextElement: SimpElement, namespace: Namespace): void;
3
+ export declare function mountProps(dom: HTMLElement | SVGElement, element: SimpElement, namespace: Namespace, renderRuntime: SimpRenderRuntime): void;
4
+ export declare function unmountProps(dom: HTMLElement | SVGElement, element: SimpElement, renderRuntime: SimpRenderRuntime): void;
5
+ export declare function patchProps(dom: HTMLElement | SVGElement, prevElement: SimpElement, nextElement: SimpElement, namespace: Namespace, renderRuntime: SimpRenderRuntime): void;
@@ -1,36 +1,39 @@
1
1
  import { emptyObject } from '../../shared/index.js';
2
+ import { isPropNameEventName, patchEvent } from '../events.js';
2
3
  import { defaultNamespace } from '../namespace.js';
3
4
  import { addControlledFormElementEventHandlers, isEventNameIgnored, isFormElementControlled, removeControlledFormElementEventHandlers, syncControlledFormElementPropsWithAttrs, } from './controlled/index.js';
4
- import { isPropNameEventName, patchEvent } from '../events.js';
5
- import { patchStyle } from './style.js';
6
5
  import { patchDangerInnerHTML } from './dangerInnerHTML.js';
7
- export function mountProps(dom, element, namespace) {
6
+ import { patchStyle } from './style.js';
7
+ export function mountProps(dom, element, namespace, renderRuntime) {
8
8
  if (!isFormElement(element)) {
9
9
  for (const propName in element.props) {
10
- patchDefaultElementPropAndAttrs(propName, dom, null, element, null, element.props[propName], namespace);
10
+ patchDefaultElementPropAndAttrs(propName, dom, element, null, element.props[propName], namespace, renderRuntime);
11
11
  }
12
12
  return;
13
13
  }
14
14
  const isControlled = isFormElementControlled(element.props);
15
15
  for (const propName in element.props) {
16
- patchFormElementsPropAndAttrs(propName, dom, element, isControlled, null, element.props[propName]);
16
+ patchFormElementsPropAndAttrs(propName, dom, element, isControlled, null, element.props[propName], renderRuntime);
17
17
  }
18
18
  if (isControlled) {
19
- addControlledFormElementEventHandlers(element);
19
+ addControlledFormElementEventHandlers(element, renderRuntime);
20
20
  syncControlledFormElementPropsWithAttrs(element, element.props, true);
21
21
  }
22
22
  }
23
- export function unmountProps(dom, element) {
23
+ export function unmountProps(dom, element, renderRuntime) {
24
24
  if (!element.props) {
25
25
  return;
26
26
  }
27
+ if (isFormElement(element) && isFormElementControlled(element.props)) {
28
+ removeControlledFormElementEventHandlers(element, renderRuntime);
29
+ }
27
30
  for (const propName in element.props) {
28
31
  if (isPropNameEventName(propName)) {
29
- patchEvent(propName, element.props[propName], null, dom);
32
+ patchEvent(propName, element.props[propName], null, dom, renderRuntime);
30
33
  }
31
34
  }
32
35
  }
33
- export function patchProps(dom, prevElement, nextElement, namespace) {
36
+ export function patchProps(dom, prevElement, nextElement, namespace, renderRuntime) {
34
37
  const prevProps = prevElement.props || emptyObject;
35
38
  const nextProps = nextElement.props || emptyObject;
36
39
  if (!isFormElement(nextElement)) {
@@ -38,12 +41,12 @@ export function patchProps(dom, prevElement, nextElement, namespace) {
38
41
  const prevValue = prevProps[propName];
39
42
  const nextValue = nextProps[propName];
40
43
  if (prevValue !== nextValue) {
41
- patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevValue, nextValue, namespace);
44
+ patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevValue, nextValue, namespace, renderRuntime);
42
45
  }
43
46
  }
44
47
  for (const propName in prevProps) {
45
48
  if (nextProps[propName] == null && prevProps[propName] != null) {
46
- patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevProps[propName], null, namespace);
49
+ patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevProps[propName], null, namespace, renderRuntime);
47
50
  }
48
51
  }
49
52
  return;
@@ -54,25 +57,25 @@ export function patchProps(dom, prevElement, nextElement, namespace) {
54
57
  const prevValue = prevProps[propName];
55
58
  const nextValue = nextProps[propName];
56
59
  if (prevValue !== nextValue) {
57
- patchFormElementsPropAndAttrs(propName, dom, nextElement, isNextElementControlled, prevValue, nextValue);
60
+ patchFormElementsPropAndAttrs(propName, dom, nextElement, isNextElementControlled, prevValue, nextValue, renderRuntime);
58
61
  }
59
62
  }
60
63
  for (const propName in prevProps) {
61
64
  if (nextProps[propName] == null && prevProps[propName] != null) {
62
- patchFormElementsPropAndAttrs(propName, dom, prevElement, isPrevElementControlled, prevProps[propName], null);
65
+ patchFormElementsPropAndAttrs(propName, dom, prevElement, isPrevElementControlled, prevProps[propName], null, renderRuntime);
63
66
  }
64
67
  }
65
68
  if (!isPrevElementControlled && isNextElementControlled) {
66
- addControlledFormElementEventHandlers(nextElement);
69
+ addControlledFormElementEventHandlers(nextElement, renderRuntime);
67
70
  }
68
71
  else if (isPrevElementControlled && !isNextElementControlled) {
69
- removeControlledFormElementEventHandlers(prevElement);
72
+ removeControlledFormElementEventHandlers(prevElement, renderRuntime);
70
73
  }
71
74
  if (isNextElementControlled) {
72
75
  syncControlledFormElementPropsWithAttrs(nextElement, nextProps);
73
76
  }
74
77
  }
75
- function patchFormElementsPropAndAttrs(propName, dom, element, isControlled, prevValue, nextValue) {
78
+ function patchFormElementsPropAndAttrs(propName, dom, element, isControlled, prevValue, nextValue, renderRuntime) {
76
79
  switch (propName) {
77
80
  case 'children':
78
81
  case 'className':
@@ -109,17 +112,17 @@ function patchFormElementsPropAndAttrs(propName, dom, element, isControlled, pre
109
112
  default:
110
113
  if (isPropNameEventName(propName)) {
111
114
  if (isControlled && isEventNameIgnored(element, propName)) {
112
- patchEvent(propName, prevValue, null, dom);
115
+ patchEvent(propName, prevValue, null, dom, renderRuntime);
113
116
  break;
114
117
  }
115
- patchEvent(propName, prevValue, nextValue, dom);
118
+ patchEvent(propName, prevValue, nextValue, dom, renderRuntime);
116
119
  }
117
120
  else {
118
121
  patchDomAttr(dom, nextValue, propName);
119
122
  }
120
123
  }
121
124
  }
122
- function patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevValue, nextValue, namespace) {
125
+ function patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevValue, nextValue, namespace, renderRuntime) {
123
126
  switch (propName) {
124
127
  case 'children':
125
128
  case 'className':
@@ -150,11 +153,11 @@ function patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement
150
153
  patchStyle(prevValue, nextValue, dom);
151
154
  break;
152
155
  case 'dangerouslySetInnerHTML':
153
- patchDangerInnerHTML(prevValue, nextValue, prevElement, nextElement, dom);
156
+ patchDangerInnerHTML(prevValue, nextValue, nextElement, dom);
154
157
  break;
155
158
  default:
156
159
  if (isPropNameEventName(propName)) {
157
- patchEvent(propName, prevValue, nextValue, dom);
160
+ patchEvent(propName, prevValue, nextValue, dom, renderRuntime);
158
161
  }
159
162
  else {
160
163
  patchDomAttr(dom, nextValue, propName, namespace);
package/dom/render.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { type SimpElement } from '../core/internal.js';
1
+ import { type SimpElement, type SimpRenderRuntime } from '../core/internal.js';
2
2
  import type { Nullable } from '../shared/index.js';
3
- export declare function render(element: Nullable<SimpElement>, container: Nullable<Element | DocumentFragment>): void;
4
- interface SimpRoot {
3
+ export declare function createRenderer(renderRuntime: SimpRenderRuntime): (element: Nullable<SimpElement>, container: Nullable<Element | DocumentFragment>) => void;
4
+ export interface SimpRoot {
5
5
  render(element: SimpElement): void;
6
6
  unmount(): void;
7
7
  }
8
- export declare function createRoot(container: Element | DocumentFragment): SimpRoot;
9
- export {};
8
+ export declare function createCreateRoot(renderRuntime: SimpRenderRuntime): (container: Element | DocumentFragment) => SimpRoot;
package/dom/render.js CHANGED
@@ -1,41 +1,45 @@
1
- import { renderingRerenderLocker, hostAdapter, mount, patch, provideHostAdapter, remove, } from '../core/internal.js';
2
- import { domAdapter } from './domAdapter.js';
3
- import { attachElementToDom, getElementFromDom } from './attach-element-to-dom.js';
4
- provideHostAdapter(domAdapter);
5
- export function render(element, container) {
6
- if (!container) {
7
- return;
8
- }
9
- const currentRootElement = getElementFromDom(container);
10
- renderingRerenderLocker.lock();
11
- if (!currentRootElement) {
12
- if (element) {
13
- hostAdapter.clearNode(container);
14
- attachElementToDom((element.parent = { flag: 'HOST', reference: container, children: element, parent: null }), container);
15
- mount(element, container, null, null, hostAdapter.getHostNamespaces(element, undefined)?.self);
1
+ import { createElement, mount, patch, unmount } from '../core/internal.js';
2
+ export function createRenderer(renderRuntime) {
3
+ return (element, container) => {
4
+ if (!container) {
5
+ return;
16
6
  }
17
- }
18
- else {
19
- if (!element) {
20
- remove(currentRootElement.children, container);
21
- currentRootElement.children = null;
7
+ const currentRootElement = renderRuntime.hostAdapter.getElementFromReference(container, renderRuntime);
8
+ if (!currentRootElement) {
9
+ if (element) {
10
+ renderRuntime.hostAdapter.clearNode(container);
11
+ element.parent = createElement('', null, element);
12
+ element.parent.type = null;
13
+ element.parent.reference = container;
14
+ renderRuntime.hostAdapter.attachElementToReference(element.parent, container, renderRuntime);
15
+ mount(element, container, null, null, renderRuntime.hostAdapter.getHostNamespaces(element, undefined)?.self, renderRuntime);
16
+ }
22
17
  }
23
18
  else {
24
- const prevChildren = currentRootElement.children;
25
- currentRootElement.children = element;
26
- element.parent = currentRootElement;
27
- patch(prevChildren, element, container, null, null, hostAdapter.getHostNamespaces(element, undefined)?.self);
19
+ if (!element) {
20
+ unmount(currentRootElement.children, renderRuntime);
21
+ renderRuntime.hostAdapter.detachElementFromReference(container, renderRuntime);
22
+ renderRuntime.hostAdapter.clearNode(container);
23
+ }
24
+ else {
25
+ const prevChildren = currentRootElement.children;
26
+ currentRootElement.children = element;
27
+ element.parent = currentRootElement;
28
+ patch(prevChildren, element, container, null, null, renderRuntime.hostAdapter.getHostNamespaces(element, undefined)?.self, renderRuntime);
29
+ }
28
30
  }
29
- }
30
- renderingRerenderLocker.flush();
31
+ };
31
32
  }
32
- export function createRoot(container) {
33
- return {
34
- render(element) {
35
- render(element, container);
36
- },
37
- unmount() {
38
- render(null, container);
39
- },
33
+ export function createCreateRoot(renderRuntime) {
34
+ const render = createRenderer(renderRuntime);
35
+ return (container) => {
36
+ return {
37
+ render(element) {
38
+ render(element, container);
39
+ },
40
+ unmount() {
41
+ render(null, container);
42
+ },
43
+ };
40
44
  };
41
45
  }
package/hooks/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RefObject } from '../core/index.js';
1
+ import type { RefObject, SimpRenderRuntime } from '../core/index.js';
2
2
 
3
3
  export type Cleanup = () => void;
4
4
  export type Effect = () => void | Cleanup;
@@ -7,21 +7,23 @@ export type DependencyList = readonly unknown[];
7
7
  export type Dispatch<A> = (value: A) => void;
8
8
  export type SetStateAction<S> = S | ((prevState: S) => S);
9
9
 
10
- declare function useRef<T>(initialValue: T): RefObject<T>;
11
- declare function useRef<T>(initialValue: T | null): RefObject<T | null>;
12
- declare function useRef<T>(initialValue: T | undefined): RefObject<T | undefined>;
10
+ export interface UseRef {
11
+ <T>(initialValue: T): RefObject<T>;
12
+ <T>(initialValue: T | null): RefObject<T | null>;
13
+ <T = undefined>(initialValue?: T): RefObject<T | undefined>;
14
+ }
15
+ declare function createUseRef(renderRuntime: SimpRenderRuntime): UseRef;
13
16
 
14
- declare function useRerender(): () => void;
17
+ declare function createUseRerender(renderRuntime: SimpRenderRuntime): () => void;
15
18
 
16
- export function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
17
- export function useState<S = undefined>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>];
19
+ export interface UseState {
20
+ <S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
21
+ <S>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>];
22
+ }
23
+ declare function createUseState(renderRuntime: SimpRenderRuntime): useState;
18
24
 
19
- declare function useEffect(effect: Effect, deps?: DependencyList): void;
25
+ declare function createUseEffect(renderRuntime: SimpRenderRuntime): (effect: Effect, deps?: DependencyList) => void;
20
26
 
21
- declare function useMounted(effect: Effect): void;
22
-
23
- declare function useUnmounted(cleanup: Cleanup): void;
24
-
25
- declare function useCatch(cb: (error: any) => void): void;
27
+ declare function createUseCatch(renderRuntime: SimpRenderRuntime): (cb: (error: any) => void) => void;
26
28
 
27
29
  declare function areDepsEqual(nextDeps: DependencyList | undefined, prevDeps: DependencyList | undefined): boolean;