effect-ui-react 0.0.0-stage → 0.1.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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +303 -2
  3. package/dist/Intrinsics.d.ts +16 -0
  4. package/dist/Intrinsics.js +1 -0
  5. package/dist/React.d.ts +58 -0
  6. package/dist/React.js +55 -0
  7. package/dist/ReactHost.d.ts +115 -0
  8. package/dist/ReactHost.js +750 -0
  9. package/dist/ReactRefs.d.ts +16 -0
  10. package/dist/ReactRefs.js +23 -0
  11. package/dist/dom/ReactDomCommand.d.ts +83 -0
  12. package/dist/dom/ReactDomCommand.js +86 -0
  13. package/dist/dom/ReactDomFiles.d.ts +1 -0
  14. package/dist/dom/ReactDomFiles.js +1 -0
  15. package/dist/dom/ReactDomHead.d.ts +1 -0
  16. package/dist/dom/ReactDomHead.js +1 -0
  17. package/dist/dom/ReactDomHost.d.ts +60 -0
  18. package/dist/dom/ReactDomHost.js +186 -0
  19. package/dist/dom/ReactDomImages.d.ts +1 -0
  20. package/dist/dom/ReactDomImages.js +1 -0
  21. package/dist/dom/ReactDomKeyboard.d.ts +1 -0
  22. package/dist/dom/ReactDomKeyboard.js +1 -0
  23. package/dist/dom/ReactDomRouter.d.ts +1 -0
  24. package/dist/dom/ReactDomRouter.js +1 -0
  25. package/dist/dom/ReactDomScheduler.d.ts +1 -0
  26. package/dist/dom/ReactDomScheduler.js +1 -0
  27. package/dist/dom/ReactDomSubscription.d.ts +1 -0
  28. package/dist/dom/ReactDomSubscription.js +1 -0
  29. package/dist/dom/index.d.ts +10 -0
  30. package/dist/dom/index.js +10 -0
  31. package/dist/dom/intrinsics.d.ts +21 -0
  32. package/dist/dom/intrinsics.js +1 -0
  33. package/dist/dom/server/ReactDomServer.d.ts +11 -0
  34. package/dist/dom/server/ReactDomServer.js +16 -0
  35. package/dist/dom/server/index.d.ts +1 -0
  36. package/dist/dom/server/index.js +1 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.js +3 -0
  39. package/dist/internal/child-placement.d.ts +49 -0
  40. package/dist/internal/child-placement.js +136 -0
  41. package/dist/internal/react-component-bridge.d.ts +133 -0
  42. package/dist/internal/react-component-bridge.js +220 -0
  43. package/dist/internal/react-element.d.ts +138 -0
  44. package/dist/internal/react-element.js +261 -0
  45. package/dist/internal/render-view.d.ts +24 -0
  46. package/dist/internal/render-view.js +110 -0
  47. package/dist/internal/to-react-element.d.ts +20 -0
  48. package/dist/internal/to-react-element.js +135 -0
  49. package/dist/internal/wrap-helper.d.ts +9 -0
  50. package/dist/internal/wrap-helper.js +31 -0
  51. package/package.json +78 -3
@@ -0,0 +1,261 @@
1
+ import { Mount, View } from "effect/ui";
2
+ import { Predicate } from "effect";
3
+ import { isValidElement } from "react";
4
+ import { ComponentRequirementsSymbol, ComponentSymbol, EventSymbol, MAX_NESTED_PROP_DEPTH, PROP_PATH_SEPARATOR, RenderSymbol, isEffectComponent, isEventMarker, isNestedPropsContainer, isRenderMarker } from "./react-component-bridge.js";
5
+ function isEffectRef(value) {
6
+ return (Predicate.isObject(value) &&
7
+ typeof value.name === "string" &&
8
+ typeof value.key === "string" &&
9
+ typeof value.at === "function");
10
+ }
11
+ function isRenderCallback(value) {
12
+ return typeof value === "function" && !(value instanceof View.ElementBuilder);
13
+ }
14
+ function hasReactElement(value) {
15
+ if (isValidElement(value))
16
+ return true;
17
+ if (Array.isArray(value))
18
+ return value.some(hasReactElement);
19
+ return false;
20
+ }
21
+ // React elements, text and arrays of them: children React can take as they are.
22
+ function isPlainReactChildren(value) {
23
+ if (isValidElement(value))
24
+ return true;
25
+ if (Array.isArray(value))
26
+ return value.every(isPlainReactChildren);
27
+ return value === null || (typeof value !== "object" && typeof value !== "function");
28
+ }
29
+ function wrapEventHandler(handler) {
30
+ return typeof handler === "function"
31
+ ? (args) => handler(...(Array.isArray(args) ? args : [args]))
32
+ : () => handler;
33
+ }
34
+ /**
35
+ * Fluent builder for React elements and components (`h.div`, `h.input`, `h.el(View)`, `h.el("custom-tag")`).
36
+ * Subclasses `View.ElementBuilder` so `.key()`, `.ref()`, `.onMount()`, `.forward()`,
37
+ * `.pipe()`, and `View.Shorthand` work natively across Effect UI.
38
+ */
39
+ export class ReactElementBuilder extends View.ElementBuilder {
40
+ }
41
+ export function eventHandler(handler, options) {
42
+ return { [EventSymbol]: { handler, options } };
43
+ }
44
+ export function renderProp(value) {
45
+ if (isRenderCallback(value)) {
46
+ return { [RenderSymbol]: { kind: "fn", fn: value } };
47
+ }
48
+ return { [RenderSymbol]: { kind: "view", view: value } };
49
+ }
50
+ const renderDispatchIdentity = (msg) => msg;
51
+ // Markers can sit anywhere in a prop's object literals and arrays, such as
52
+ // `columnDefinitions: [{ cell: h.render((item) => ...) }]`.
53
+ function hasNestedRenderMarkers(value, depth = 0) {
54
+ if (depth >= MAX_NESTED_PROP_DEPTH || !isNestedPropsContainer(value))
55
+ return false;
56
+ for (const entry of Object.values(value)) {
57
+ if (isRenderMarker(entry) || hasNestedRenderMarkers(entry, depth + 1))
58
+ return true;
59
+ }
60
+ return false;
61
+ }
62
+ // Event handlers can sit there too, such as
63
+ // `items: [{ onDismiss: h.event(...) }]`. Each one is registered under its
64
+ // path, and the React element gets a stable callback at that path.
65
+ function onNestedEvents(builder, value, path, depth = 0) {
66
+ if (depth >= MAX_NESTED_PROP_DEPTH || !isNestedPropsContainer(value))
67
+ return builder;
68
+ let current = builder;
69
+ for (const [key, entry] of Object.entries(value)) {
70
+ const entryPath = `${path}${PROP_PATH_SEPARATOR}${key}`;
71
+ if (isEventMarker(entry)) {
72
+ const { handler, options } = entry[EventSymbol];
73
+ current = View.ElementBuilder.prototype.on.call(current, entryPath, wrapEventHandler(handler), options);
74
+ }
75
+ else {
76
+ current = onNestedEvents(current, entry, entryPath, depth + 1);
77
+ }
78
+ }
79
+ return current;
80
+ }
81
+ function applyRef(builder, ref) {
82
+ if (isEffectRef(ref)) {
83
+ return View.ElementBuilder.prototype.prop.call(View.ElementBuilder.prototype.ref.call(builder, ref), "$hasRef", true);
84
+ }
85
+ return ref === undefined ? builder : View.ElementBuilder.prototype.prop.call(builder, "ref", ref);
86
+ }
87
+ function withRenderCallback(builder, marker) {
88
+ return View.ElementBuilder.prototype.on.call(View.ElementBuilder.prototype.prop.call(builder, "children", marker), "$renderDispatch", renderDispatchIdentity);
89
+ }
90
+ // Both `.children(...)` and props bags use the same child normalization.
91
+ function applyChildren(builder, inputs) {
92
+ if (inputs.length === 1) {
93
+ const only = inputs[0];
94
+ if (only === undefined)
95
+ return builder;
96
+ if (isRenderCallback(only)) {
97
+ return withRenderCallback(builder, { [RenderSymbol]: { kind: "fn", fn: only } });
98
+ }
99
+ if (isRenderMarker(only)) {
100
+ const spec = only[RenderSymbol];
101
+ return spec.kind === "fn"
102
+ ? withRenderCallback(builder, only)
103
+ : View.withContent(builder, [spec.view]);
104
+ }
105
+ if (hasReactElement(only) && isPlainReactChildren(only)) {
106
+ return View.ElementBuilder.prototype.prop.call(builder, "children", only);
107
+ }
108
+ }
109
+ return View.withContent(builder, inputs.map(liftReactElements));
110
+ }
111
+ // A React element sitting among Effect UI views becomes a node of its own
112
+ // (same type, key and props), so the whole list converts as Effect UI content.
113
+ function liftReactElements(input) {
114
+ if (isValidElement(input)) {
115
+ // Any tag a React element carries is one `reactEl` can build, intrinsic or not.
116
+ const builder = reactEl(input.type);
117
+ return applyReactProps(input.key === null ? builder : builder.key(input.key), input.props);
118
+ }
119
+ return Array.isArray(input) ? input.map(liftReactElements) : input;
120
+ }
121
+ /**
122
+ * Applies a React props object (handling `key`, `ref`, `onMount`, `children`,
123
+ * `h.render`, and `h.event` markers) onto an Effect UI `ElementBuilder`.
124
+ */
125
+ function applyReactProps(builder, rawProps) {
126
+ let current = builder;
127
+ let propNodes;
128
+ let hasRenderCallback = false;
129
+ for (const [name, value] of Object.entries(rawProps)) {
130
+ if (name === "key") {
131
+ if (typeof value === "string")
132
+ current = current.key(value);
133
+ continue;
134
+ }
135
+ if (name === "ref") {
136
+ current = applyRef(current, value);
137
+ continue;
138
+ }
139
+ if (name === "onMount") {
140
+ if (Mount.isMount(value)) {
141
+ current = View.ElementBuilder.prototype.onMount.call(current, value);
142
+ current = View.ElementBuilder.prototype.prop.call(current, "$hasRef", true);
143
+ continue;
144
+ }
145
+ if (value === undefined) {
146
+ continue;
147
+ }
148
+ }
149
+ if (name === "children") {
150
+ current = applyChildren(current, [value]);
151
+ continue;
152
+ }
153
+ if (isRenderMarker(value)) {
154
+ const spec = value[RenderSymbol];
155
+ if (spec.kind === "view") {
156
+ propNodes ??= [];
157
+ propNodes.push(new View.ContainerBuilder("$prop")
158
+ .key(`$prop:${name}`)
159
+ .prop("$propName", name)
160
+ .children(spec.view));
161
+ continue;
162
+ }
163
+ hasRenderCallback = true;
164
+ current = View.ElementBuilder.prototype.prop.call(current, name, value);
165
+ continue;
166
+ }
167
+ if (isEventMarker(value)) {
168
+ const { handler, options } = value[EventSymbol];
169
+ current = View.ElementBuilder.prototype.on.call(current, name, wrapEventHandler(handler), options);
170
+ continue;
171
+ }
172
+ if (hasNestedRenderMarkers(value)) {
173
+ hasRenderCallback = true;
174
+ }
175
+ current = onNestedEvents(current, value, name);
176
+ current = View.ElementBuilder.prototype.prop.call(current, name, value);
177
+ }
178
+ if (hasRenderCallback) {
179
+ current = View.ElementBuilder.prototype.on.call(current, "$renderDispatch", renderDispatchIdentity);
180
+ }
181
+ if (propNodes !== undefined) {
182
+ current = View.withContent(current, propNodes);
183
+ }
184
+ return current;
185
+ }
186
+ Object.defineProperties(ReactElementBuilder.prototype, {
187
+ prop: {
188
+ value(name, value) {
189
+ return applyReactProps(this, { [name]: value });
190
+ },
191
+ writable: true,
192
+ configurable: true
193
+ },
194
+ props: {
195
+ value(properties) {
196
+ return applyReactProps(this, properties);
197
+ },
198
+ writable: true,
199
+ configurable: true
200
+ },
201
+ spread: {
202
+ value(properties) {
203
+ return applyReactProps(this, properties);
204
+ },
205
+ writable: true,
206
+ configurable: true
207
+ },
208
+ ref: {
209
+ value(value) {
210
+ return applyRef(this, value);
211
+ },
212
+ writable: true,
213
+ configurable: true
214
+ },
215
+ onMount: {
216
+ value(mount) {
217
+ if (mount === undefined)
218
+ return this;
219
+ return View.ElementBuilder.prototype.prop.call(View.ElementBuilder.prototype.onMount.call(this, mount), "$hasRef", true);
220
+ },
221
+ writable: true,
222
+ configurable: true
223
+ },
224
+ children: {
225
+ value(...inputs) {
226
+ return applyChildren(this, inputs);
227
+ },
228
+ writable: true,
229
+ configurable: true
230
+ }
231
+ });
232
+ export function componentProp(target) {
233
+ if (isEffectComponent(target)) {
234
+ return { [ComponentSymbol]: { kind: "component", component: target } };
235
+ }
236
+ if (typeof target === "function") {
237
+ return { [ComponentSymbol]: { kind: "render", fn: target } };
238
+ }
239
+ throw new Error("h.component expects a React.component or a (h, props) => view function");
240
+ }
241
+ /**
242
+ * Creates a cached intrinsic JSX root builder that supports fluent chaining
243
+ * (`h.div.props({ className: "..." }).children(...)`) and callable children
244
+ * shorthand (`h.strong("bold")`, `h.div(child1, child2)`).
245
+ */
246
+ export function createIntrinsicRoot(tag) {
247
+ const base = new ReactElementBuilder(tag);
248
+ const root = (...inputs) => base.children(...inputs);
249
+ Object.setPrototypeOf(root, Object.getPrototypeOf(base));
250
+ return Object.assign(root, base);
251
+ }
252
+ /**
253
+ * Creates a fluent `ReactElementBuilder` for a React component or tag string
254
+ * (`h.el(View)`, `h.el(Button)`, `h.el("custom-tag")`).
255
+ */
256
+ export function reactEl(type, ...children) {
257
+ const builder = typeof type === "string"
258
+ ? new ReactElementBuilder(type)
259
+ : View.ElementBuilder.prototype.prop.call(new ReactElementBuilder("$react"), "$component", type);
260
+ return children.length === 0 ? builder : builder.children(...children);
261
+ }
@@ -0,0 +1,24 @@
1
+ import { type Renderer, View } from "effect/ui";
2
+ import { type ReactElement } from "react";
3
+ /**
4
+ * Runtime callbacks supplied by an owning `ReactHost` when rendering dynamic
5
+ * `h.render((...args) => view)` or function-as-child callbacks.
6
+ * @internal
7
+ */
8
+ export interface RenderViewRuntime {
9
+ readonly getBinding: () => Renderer.Events | undefined;
10
+ readonly dispatchMessage: (msg: unknown) => void;
11
+ }
12
+ /**
13
+ * Convert Effect UI view content into a `ReactElement` for render props.
14
+ * A single non-boundary element is returned directly (compatible with
15
+ * `React.cloneElement` / Base UI's `render` prop); empty, text, or multi-node
16
+ * content is wrapped in a `Fragment`.
17
+ *
18
+ * When `runtime` is supplied (from an owning `ReactHost`), `h.event(...)`
19
+ * dispatches directly into the parent component while keeping the returned
20
+ * element raw, and Effect child components / `onMount` / `h.component` nodes
21
+ * are bridged per-node via `DynamicNodeBridge`.
22
+ * @internal
23
+ */
24
+ export declare function renderView<Msg>(content: View.Input<Msg, any>, runtime?: RenderViewRuntime): ReactElement;
@@ -0,0 +1,110 @@
1
+ import { View } from "effect/ui";
2
+ import { createElement, Fragment } from "react";
3
+ import { containsComponentMarker, DynamicNodeBridge } from "./react-component-bridge.js";
4
+ import { toReactElement } from "./to-react-element.js";
5
+ /**
6
+ * Convert Effect UI view content into a `ReactElement` for render props.
7
+ * A single non-boundary element is returned directly (compatible with
8
+ * `React.cloneElement` / Base UI's `render` prop); empty, text, or multi-node
9
+ * content is wrapped in a `Fragment`.
10
+ *
11
+ * When `runtime` is supplied (from an owning `ReactHost`), `h.event(...)`
12
+ * dispatches directly into the parent component while keeping the returned
13
+ * element raw, and Effect child components / `onMount` / `h.component` nodes
14
+ * are bridged per-node via `DynamicNodeBridge`.
15
+ * @internal
16
+ */
17
+ export function renderView(content, runtime) {
18
+ function renderElement(node, key) {
19
+ if ((node.owned?.length ?? 0) > 0) {
20
+ throw new Error("renderView cannot bind Effect UI event handlers or headless parts without an owning runtime");
21
+ }
22
+ if (node.refKey !== undefined) {
23
+ if (runtime !== undefined) {
24
+ throw new Error("Effect UI refs are not supported inside dynamic render callbacks; attach the ref to the outer element, use static h.render(view), or use onMount.");
25
+ }
26
+ throw new Error("renderView does not support Effect UI refs or onMount work");
27
+ }
28
+ if (node.mount !== undefined && runtime === undefined) {
29
+ throw new Error("renderView does not support Effect UI refs or onMount work");
30
+ }
31
+ if (runtime !== undefined && (node.mount !== undefined || Object.values(node.props).some(containsComponentMarker))) {
32
+ return createElement(DynamicNodeBridge, { key, node, dispatch: runtime.dispatchMessage });
33
+ }
34
+ const userHandlerNames = Object.keys(node.handlers).filter((name) => !name.startsWith("$"));
35
+ if (userHandlerNames.length > 0 && runtime === undefined) {
36
+ throw new Error("renderView cannot bind Effect UI event handlers or headless parts without an owning runtime");
37
+ }
38
+ let propOverrides;
39
+ const normalChildren = [];
40
+ for (const child of node.children) {
41
+ if (child._tag === "Element" && child.tag === "$prop") {
42
+ const propName = child.props.$propName;
43
+ if (typeof propName === "string") {
44
+ propOverrides ??= {};
45
+ propOverrides[propName] = renderNodes(child.children);
46
+ }
47
+ continue;
48
+ }
49
+ normalChildren.push(child);
50
+ }
51
+ if (node.tag === "$boundary") {
52
+ return createElement(Fragment, key !== undefined ? { key } : null, ...normalChildren.map(render));
53
+ }
54
+ const elementBinding = userHandlerNames.length > 0 && runtime !== undefined
55
+ ? {
56
+ handlers: node.handlers,
57
+ options: node.eventOptions,
58
+ dispatch: (event, handler, payload, onMessage) => {
59
+ const parentBinding = runtime.getBinding();
60
+ if (parentBinding === undefined)
61
+ return false;
62
+ return parentBinding.dispatch(event, handler, payload, onMessage !== undefined
63
+ ? () => onMessage(node.eventOptions[event] ?? {})
64
+ : undefined);
65
+ }
66
+ }
67
+ : undefined;
68
+ return toReactElement(node, normalChildren.map(render), {
69
+ key,
70
+ binding: elementBinding,
71
+ propOverrides,
72
+ renderDynamic: (nested) => renderView(nested, runtime)
73
+ });
74
+ }
75
+ function render(node, index) {
76
+ if (node._tag === "Text")
77
+ return node.text;
78
+ if (node._tag === "Child") {
79
+ if (runtime === undefined) {
80
+ throw new Error("renderView supports only text and elements, not Effect UI child components or slot outlets");
81
+ }
82
+ return createElement(DynamicNodeBridge, {
83
+ key: node.key !== "" ? node.key : index,
84
+ node,
85
+ dispatch: runtime.dispatchMessage
86
+ });
87
+ }
88
+ if (node._tag !== "Element") {
89
+ throw new Error("renderView supports only text and elements, not Effect UI child components or slot outlets");
90
+ }
91
+ return renderElement(node, node.elementKey ?? index);
92
+ }
93
+ function renderNodes(nodes) {
94
+ if (nodes.length === 1) {
95
+ const only = nodes[0];
96
+ if (only._tag === "Element" && only.tag !== "$boundary" && only.tag !== "$prop") {
97
+ return renderElement(only, only.elementKey);
98
+ }
99
+ if (only._tag === "Child" && runtime !== undefined) {
100
+ return createElement(DynamicNodeBridge, {
101
+ key: only.key !== "" ? only.key : undefined,
102
+ node: only,
103
+ dispatch: runtime.dispatchMessage
104
+ });
105
+ }
106
+ }
107
+ return createElement(Fragment, null, ...nodes.map(render));
108
+ }
109
+ return renderNodes(View.children([content]));
110
+ }
@@ -0,0 +1,20 @@
1
+ import type { Renderer, View } from "effect/ui";
2
+ import { type Key, type ReactElement, type ReactNode } from "react";
3
+ import { type ComponentBridgeCache, type RenderBridgeCacheEntry } from "./react-component-bridge.js";
4
+ export type EventHandlerCache = Map<string, (...args: ReadonlyArray<any>) => void>;
5
+ export interface ElementOptions {
6
+ readonly key?: Key | undefined;
7
+ readonly binding?: Renderer.Events | undefined;
8
+ readonly getBinding?: (() => Renderer.Events | undefined) | undefined;
9
+ readonly handlerCache?: EventHandlerCache | undefined;
10
+ readonly refCallback?: ((instance: unknown) => void) | undefined;
11
+ readonly componentCache?: ComponentBridgeCache | undefined;
12
+ readonly onDirtyBridge?: ((entry: RenderBridgeCacheEntry) => void) | undefined;
13
+ readonly propOverrides?: Readonly<Record<string, ReactNode>> | undefined;
14
+ readonly renderDynamic?: ((view: View.Input<any, any>) => ReactElement) | undefined;
15
+ }
16
+ export declare function toReactElement(node: {
17
+ readonly tag: string;
18
+ readonly attrs: Readonly<Record<string, string>>;
19
+ readonly props: Readonly<Record<string, unknown>>;
20
+ }, children: ReadonlyArray<ReactNode>, { key, binding, getBinding, handlerCache, refCallback, componentCache, onDirtyBridge, propOverrides, renderDynamic }: ElementOptions): ReactElement;
@@ -0,0 +1,135 @@
1
+ import { Predicate } from "effect";
2
+ import { createElement } from "react";
3
+ import { ComponentSymbol, createBridgedEffectComponent, createRenderBridgeEntry, isComponentMarker, isEventMarker, isNestedPropsContainer, MAX_NESTED_PROP_DEPTH, PROP_PATH_SEPARATOR, isRenderMarker, RenderSymbol } from "./react-component-bridge.js";
4
+ function hasPreventDefault(value) {
5
+ return Predicate.isObject(value) && typeof value.preventDefault === "function";
6
+ }
7
+ function makeEventWrapper(name, getBinding) {
8
+ return (...args) => {
9
+ const binding = getBinding();
10
+ if (binding === undefined || !(name in binding.handlers))
11
+ return;
12
+ const first = args[0];
13
+ const options = binding.options[name];
14
+ if (options?.preventDefault === "always" && hasPreventDefault(first)) {
15
+ first.preventDefault();
16
+ }
17
+ binding.dispatch(name, binding.handlers[name], args, hasPreventDefault(first)
18
+ ? (resultOptions) => {
19
+ if (resultOptions.preventDefault)
20
+ first.preventDefault();
21
+ }
22
+ : undefined);
23
+ };
24
+ }
25
+ function resolveRenderMarker(marker, renderDynamic) {
26
+ if (renderDynamic === undefined) {
27
+ throw new Error("renderView cannot resolve h.render(...) markers without a render helper");
28
+ }
29
+ const spec = marker[RenderSymbol];
30
+ return spec.kind === "fn"
31
+ ? (...args) => renderDynamic(spec.fn(...args))
32
+ : renderDynamic(spec.view);
33
+ }
34
+ function resolveMarkers(value, path, renderDynamic, resolveComponent, resolveEvent, depth = 0) {
35
+ if (isRenderMarker(value))
36
+ return resolveRenderMarker(value, renderDynamic);
37
+ if (isComponentMarker(value))
38
+ return resolveComponent(path, value);
39
+ // Only nested markers reach here: top-level ones became handlers already.
40
+ if (isEventMarker(value))
41
+ return resolveEvent(path);
42
+ if (depth >= MAX_NESTED_PROP_DEPTH || !isNestedPropsContainer(value))
43
+ return value;
44
+ // Copy only the containers on the path to a marker; untouched data keeps
45
+ // its identity.
46
+ let resolved;
47
+ for (const [key, entry] of Object.entries(value)) {
48
+ const next = resolveMarkers(entry, `${path}${PROP_PATH_SEPARATOR}${key}`, renderDynamic, resolveComponent, resolveEvent, depth + 1);
49
+ if (next === entry)
50
+ continue;
51
+ resolved ??= Array.isArray(value) ? [...value] : { ...value };
52
+ resolved[key] = next;
53
+ }
54
+ return resolved ?? value;
55
+ }
56
+ // Shared by the host tree and pure view conversion.
57
+ export function toReactElement(node, children, { key, binding, getBinding, handlerCache, refCallback, componentCache, onDirtyBridge, propOverrides, renderDynamic }) {
58
+ const isReactComponent = node.tag === "$react";
59
+ const props = { ...node.attrs };
60
+ let seenComponentProps;
61
+ // Each `h.component(...)` marker is cached by its path through the props
62
+ // (`Component`, or `config\0components\0Hero\0render` when nested), so the
63
+ // bridged React component keeps its identity across re-renders.
64
+ const resolveComponent = (path, marker) => {
65
+ if (componentCache === undefined) {
66
+ throw new Error("renderView cannot mount Effect UI h.component(...) props without an owning ReactHost");
67
+ }
68
+ seenComponentProps ??= new Set();
69
+ seenComponentProps.add(path);
70
+ const spec = marker[ComponentSymbol];
71
+ const existing = componentCache.get(path);
72
+ if (spec.kind === "component") {
73
+ if (existing?.kind === "component" && existing.target === spec.component) {
74
+ return existing.Wrapper;
75
+ }
76
+ const Wrapper = createBridgedEffectComponent(spec.component);
77
+ componentCache.set(path, { kind: "component", target: spec.component, Wrapper });
78
+ return Wrapper;
79
+ }
80
+ if (existing?.kind === "render") {
81
+ if (existing.latestFn !== spec.fn) {
82
+ existing.latestFn = spec.fn;
83
+ existing.version++;
84
+ onDirtyBridge?.(existing);
85
+ }
86
+ return existing.Wrapper;
87
+ }
88
+ const entry = createRenderBridgeEntry(spec.fn);
89
+ componentCache.set(path, entry);
90
+ return entry.Wrapper;
91
+ };
92
+ const readBinding = getBinding ?? (() => binding);
93
+ // Handlers are cached by name, or by path when nested in a prop, so React
94
+ // sees the same callback on every render while each call runs the latest
95
+ // handler.
96
+ const handlerFor = (name) => {
97
+ let wrapper = handlerCache?.get(name);
98
+ if (wrapper === undefined) {
99
+ wrapper = makeEventWrapper(name, readBinding);
100
+ handlerCache?.set(name, wrapper);
101
+ }
102
+ return wrapper;
103
+ };
104
+ for (const [name, value] of Object.entries(node.props)) {
105
+ if (name.startsWith("$"))
106
+ continue;
107
+ props[name] = resolveMarkers(value, name, renderDynamic, resolveComponent, handlerFor);
108
+ }
109
+ if (componentCache !== undefined) {
110
+ for (const name of componentCache.keys()) {
111
+ if (!seenComponentProps?.has(name)) {
112
+ componentCache.delete(name);
113
+ }
114
+ }
115
+ }
116
+ Object.assign(props, propOverrides);
117
+ if (key !== undefined) {
118
+ props.key = key;
119
+ }
120
+ if (refCallback !== undefined && node.props.$hasRef === true) {
121
+ props.ref = refCallback;
122
+ }
123
+ for (const name of Object.keys(binding?.handlers ?? {})) {
124
+ if (name.startsWith("$") || name.includes(PROP_PATH_SEPARATOR))
125
+ continue;
126
+ props[name] = handlerFor(name);
127
+ }
128
+ const type = isReactComponent
129
+ ? node.props.$component
130
+ : node.tag;
131
+ // Omit the children argument when absent, preserving function-as-child or props.children.
132
+ return children.length === 0
133
+ ? createElement(type, props)
134
+ : createElement(type, props, ...children);
135
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Wraps an Effect UI view helper `h` in a `Proxy` so any unknown string key
3
+ * resolves to a cached React intrinsic element builder (`h.div.props({ ... }).children(...)`,
4
+ * `h.strong("bold")`, `h.input.props({ ... }).ref(...)`).
5
+ *
6
+ * `HelperImpl.prototype.keepAlive` uses `Object.create(this)` and `lazy` invokes
7
+ * `fn(this, ...args)`, so both inherit this Proxy automatically.
8
+ */
9
+ export declare function wrapHelper<H extends object>(helper: H): H;
@@ -0,0 +1,31 @@
1
+ import { createIntrinsicRoot } from "./react-element.js";
2
+ const intrinsicRoots = new Map();
3
+ function getIntrinsicRoot(tag) {
4
+ let root = intrinsicRoots.get(tag);
5
+ if (root === undefined) {
6
+ root = createIntrinsicRoot(tag);
7
+ intrinsicRoots.set(tag, root);
8
+ }
9
+ return root;
10
+ }
11
+ /**
12
+ * Wraps an Effect UI view helper `h` in a `Proxy` so any unknown string key
13
+ * resolves to a cached React intrinsic element builder (`h.div.props({ ... }).children(...)`,
14
+ * `h.strong("bold")`, `h.input.props({ ... }).ref(...)`).
15
+ *
16
+ * `HelperImpl.prototype.keepAlive` uses `Object.create(this)` and `lazy` invokes
17
+ * `fn(this, ...args)`, so both inherit this Proxy automatically.
18
+ */
19
+ export function wrapHelper(helper) {
20
+ return new Proxy(helper, {
21
+ get(target, prop, receiver) {
22
+ if (typeof prop !== "string" ||
23
+ prop in target ||
24
+ prop.startsWith("~") ||
25
+ prop === "then") {
26
+ return Reflect.get(target, prop, receiver);
27
+ }
28
+ return getIntrinsicRoot(prop);
29
+ }
30
+ });
31
+ }
package/package.json CHANGED
@@ -1,6 +1,81 @@
1
1
  {
2
2
  "name": "effect-ui-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "default": "./dist/index.js"
9
+ },
10
+ "./dom": {
11
+ "types": "./dist/dom/index.d.ts",
12
+ "default": "./dist/dom/index.js"
13
+ },
14
+ "./dom/server": {
15
+ "types": "./dist/dom/server/index.d.ts",
16
+ "default": "./dist/dom/server/index.js"
17
+ },
18
+ "./intrinsics": {
19
+ "types": "./dist/Intrinsics.d.ts",
20
+ "default": "./dist/Intrinsics.js"
21
+ },
22
+ "./internal/*": {
23
+ "types": "./dist/internal/*.d.ts",
24
+ "default": "./dist/internal/*.js"
25
+ }
26
+ },
27
+ "devDependencies": {
28
+ "@base-ui/react": "^1.9.0",
29
+ "@types/node": "^26.6.11",
30
+ "@types/react": "^19.3.3",
31
+ "@types/react-dom": "^19.3.0",
32
+ "effect": "4.2.0",
33
+ "happy-dom": "^21.0.4",
34
+ "react": "^19.3.1",
35
+ "react-dom": "^19.3.1",
36
+ "typescript": "^7.0.2",
37
+ "vitest": "^5.1.2"
38
+ },
39
+ "description": "A React host for Effect UI. Write Effect UI, render React.",
40
+ "license": "MIT",
41
+ "author": "Nick Breaton",
42
+ "homepage": "https://github.com/nickbreaton/effect-ui-react#readme",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/nickbreaton/effect-ui-react.git",
46
+ "directory": "packages/effect-ui-react"
47
+ },
48
+ "bugs": {
49
+ "url": "https://github.com/nickbreaton/effect-ui-react/issues"
50
+ },
51
+ "keywords": [
52
+ "effect",
53
+ "effect-ui",
54
+ "react",
55
+ "typescript"
56
+ ],
57
+ "files": [
58
+ "dist"
59
+ ],
60
+ "main": "./dist/index.js",
61
+ "types": "./dist/index.d.ts",
62
+ "publishConfig": {
63
+ "access": "public",
64
+ "registry": "https://registry.npmjs.org/"
65
+ },
66
+ "peerDependencies": {
67
+ "effect": "~4.2.0",
68
+ "react": "^19.3.0",
69
+ "react-dom": "^19.3.0"
70
+ },
71
+ "peerDependenciesMeta": {
72
+ "react-dom": {
73
+ "optional": true
74
+ }
75
+ },
76
+ "scripts": {
77
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
78
+ "check": "tsc --noEmit",
79
+ "test": "vitest run"
80
+ }
6
81
  }