effect-ui-react 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +305 -2
- package/dist/Intrinsics.d.ts +16 -0
- package/dist/Intrinsics.js +1 -0
- package/dist/React.d.ts +58 -0
- package/dist/React.js +55 -0
- package/dist/ReactHost.d.ts +115 -0
- package/dist/ReactHost.js +750 -0
- package/dist/ReactRefs.d.ts +16 -0
- package/dist/ReactRefs.js +23 -0
- package/dist/dom/ReactDomCommand.d.ts +83 -0
- package/dist/dom/ReactDomCommand.js +86 -0
- package/dist/dom/ReactDomFiles.d.ts +1 -0
- package/dist/dom/ReactDomFiles.js +1 -0
- package/dist/dom/ReactDomHead.d.ts +1 -0
- package/dist/dom/ReactDomHead.js +1 -0
- package/dist/dom/ReactDomHost.d.ts +60 -0
- package/dist/dom/ReactDomHost.js +186 -0
- package/dist/dom/ReactDomImages.d.ts +1 -0
- package/dist/dom/ReactDomImages.js +1 -0
- package/dist/dom/ReactDomKeyboard.d.ts +1 -0
- package/dist/dom/ReactDomKeyboard.js +1 -0
- package/dist/dom/ReactDomRouter.d.ts +1 -0
- package/dist/dom/ReactDomRouter.js +1 -0
- package/dist/dom/ReactDomScheduler.d.ts +1 -0
- package/dist/dom/ReactDomScheduler.js +1 -0
- package/dist/dom/ReactDomSubscription.d.ts +1 -0
- package/dist/dom/ReactDomSubscription.js +1 -0
- package/dist/dom/index.d.ts +10 -0
- package/dist/dom/index.js +10 -0
- package/dist/dom/intrinsics.d.ts +21 -0
- package/dist/dom/intrinsics.js +1 -0
- package/dist/dom/server/ReactDomServer.d.ts +11 -0
- package/dist/dom/server/ReactDomServer.js +16 -0
- package/dist/dom/server/index.d.ts +1 -0
- package/dist/dom/server/index.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/internal/child-placement.d.ts +49 -0
- package/dist/internal/child-placement.js +136 -0
- package/dist/internal/react-component-bridge.d.ts +133 -0
- package/dist/internal/react-component-bridge.js +220 -0
- package/dist/internal/react-element.d.ts +138 -0
- package/dist/internal/react-element.js +261 -0
- package/dist/internal/render-view.d.ts +24 -0
- package/dist/internal/render-view.js +110 -0
- package/dist/internal/to-react-element.d.ts +20 -0
- package/dist/internal/to-react-element.js +135 -0
- package/dist/internal/wrap-helper.d.ts +9 -0
- package/dist/internal/wrap-helper.js +31 -0
- package/package.json +78 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Renderer } from "effect/ui";
|
|
2
|
+
import { Effect, Option } from "effect";
|
|
3
|
+
import * as React from "./React.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resolves an Effect UI ref to its mounted React instance (DOM element,
|
|
6
|
+
* `useImperativeHandle` handle, Three.js object, etc.), waiting for both
|
|
7
|
+
* Effect UI's commit (`Renderer.Refs.lookup`) and React's post-commit
|
|
8
|
+
* (`React.Host.resolveRef`) to complete.
|
|
9
|
+
*/
|
|
10
|
+
export declare const lookup: <Node>(ref: Renderer.Ref<Node>) => Effect.Effect<Option.Option<Node>, never, React.Host | Renderer.Refs>;
|
|
11
|
+
/**
|
|
12
|
+
* Looks up a ref and applies `f` to the mounted React instance if present.
|
|
13
|
+
* Renderer-agnostic: works with DOM elements, imperative handles, or any custom
|
|
14
|
+
* React reconciler node.
|
|
15
|
+
*/
|
|
16
|
+
export declare const withRef: <Node, A>(ref: Renderer.Ref<Node>, f: (instance: Node) => A) => Effect.Effect<Option.Option<A>, never, Renderer.Refs | React.Host>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Renderer } from "effect/ui";
|
|
2
|
+
import { Effect, Option } from "effect";
|
|
3
|
+
import * as React from "./React.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resolves an Effect UI ref to its mounted React instance (DOM element,
|
|
6
|
+
* `useImperativeHandle` handle, Three.js object, etc.), waiting for both
|
|
7
|
+
* Effect UI's commit (`Renderer.Refs.lookup`) and React's post-commit
|
|
8
|
+
* (`React.Host.resolveRef`) to complete.
|
|
9
|
+
*/
|
|
10
|
+
export const lookup = Effect.fnUntraced(function* (ref) {
|
|
11
|
+
const refs = yield* Renderer.Refs;
|
|
12
|
+
const found = yield* refs.lookup(ref);
|
|
13
|
+
if (Option.isNone(found))
|
|
14
|
+
return Option.none();
|
|
15
|
+
const reactHost = yield* React.Host;
|
|
16
|
+
return yield* reactHost.resolveRef(found.value);
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Looks up a ref and applies `f` to the mounted React instance if present.
|
|
20
|
+
* Renderer-agnostic: works with DOM elements, imperative handles, or any custom
|
|
21
|
+
* React reconciler node.
|
|
22
|
+
*/
|
|
23
|
+
export const withRef = (ref, f) => Effect.map(lookup(ref), Option.map(f));
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Command, Renderer } from "effect/ui";
|
|
2
|
+
import { type Html } from "effect/ui/html";
|
|
3
|
+
import { Option } from "effect";
|
|
4
|
+
export type FocusOptions = Html.FocusOptions;
|
|
5
|
+
export type ScrollOptions = Html.ScrollOptions;
|
|
6
|
+
export type SelectOptions = Html.SelectOptions;
|
|
7
|
+
export type Rect = Html.Rect;
|
|
8
|
+
export interface Found {
|
|
9
|
+
readonly found: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface Submitted extends Found {
|
|
12
|
+
readonly submitted: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface Opened extends Found {
|
|
15
|
+
readonly opened: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare const Focus: Command.Factory<{
|
|
18
|
+
readonly ref: Renderer.Ref<any>;
|
|
19
|
+
readonly options?: {
|
|
20
|
+
readonly preventScroll?: boolean | undefined;
|
|
21
|
+
} | undefined;
|
|
22
|
+
}, {
|
|
23
|
+
found: boolean;
|
|
24
|
+
}, false, never>;
|
|
25
|
+
export declare const Blur: Command.Factory<{
|
|
26
|
+
readonly ref: Renderer.Ref<any>;
|
|
27
|
+
}, {
|
|
28
|
+
found: boolean;
|
|
29
|
+
}, false, never>;
|
|
30
|
+
export declare const ScrollIntoView: Command.Factory<{
|
|
31
|
+
readonly ref: Renderer.Ref<any>;
|
|
32
|
+
readonly options?: {
|
|
33
|
+
readonly behavior?: "auto" | "instant" | "smooth" | undefined;
|
|
34
|
+
readonly block?: "center" | "end" | "nearest" | "start" | undefined;
|
|
35
|
+
readonly inline?: "center" | "end" | "nearest" | "start" | undefined;
|
|
36
|
+
} | undefined;
|
|
37
|
+
}, {
|
|
38
|
+
found: boolean;
|
|
39
|
+
}, false, never>;
|
|
40
|
+
export declare const Measure: Command.Factory<{
|
|
41
|
+
readonly ref: Renderer.Ref<any>;
|
|
42
|
+
}, Option.Option<Html.Rect>, false, never>;
|
|
43
|
+
export declare const Select: Command.Factory<{
|
|
44
|
+
readonly ref: Renderer.Ref<any>;
|
|
45
|
+
readonly options?: {
|
|
46
|
+
readonly start: number;
|
|
47
|
+
readonly end?: number | undefined;
|
|
48
|
+
} | undefined;
|
|
49
|
+
}, {
|
|
50
|
+
found: boolean;
|
|
51
|
+
}, false, never>;
|
|
52
|
+
export declare const RequestSubmit: Command.Factory<{
|
|
53
|
+
readonly ref: Renderer.Ref<any>;
|
|
54
|
+
}, Submitted, false, never>;
|
|
55
|
+
export declare const ShowPicker: Command.Factory<{
|
|
56
|
+
readonly ref: Renderer.Ref<any>;
|
|
57
|
+
}, Opened, false, never>;
|
|
58
|
+
export declare const WaitForAnimations: Command.Factory<{
|
|
59
|
+
readonly ref: Renderer.Ref<any>;
|
|
60
|
+
}, Found, false, never>;
|
|
61
|
+
export declare const focus: <N extends HTMLElement | SVGElement, Msg>(ref: Renderer.Ref<N>, f: (result: Found) => Msg, options?: FocusOptions) => Command.Issued<Msg, never, {
|
|
62
|
+
found: boolean;
|
|
63
|
+
}, "Effect", never>;
|
|
64
|
+
export declare const blur: <N extends HTMLElement | SVGElement, Msg>(ref: Renderer.Ref<N>, f: (result: Found) => Msg) => Command.Issued<Msg, never, {
|
|
65
|
+
found: boolean;
|
|
66
|
+
}, "Effect", never>;
|
|
67
|
+
export declare const scrollIntoView: <N extends HTMLElement | SVGElement, Msg>(ref: Renderer.Ref<N>, f: (result: Found) => Msg, options?: ScrollOptions) => Command.Issued<Msg, never, {
|
|
68
|
+
found: boolean;
|
|
69
|
+
}, "Effect", never>;
|
|
70
|
+
export declare const measure: <N extends Element, Msg>(ref: Renderer.Ref<N>, f: (rect: Option.Option<Rect>) => Msg) => Command.Issued<Msg, never, Option.Option<Html.Rect>, "Effect", never>;
|
|
71
|
+
export declare const select: <N extends HTMLInputElement | HTMLTextAreaElement, Msg>(ref: Renderer.Ref<N>, f: (result: Found) => Msg, options?: SelectOptions) => Command.Issued<Msg, never, {
|
|
72
|
+
found: boolean;
|
|
73
|
+
}, "Effect", never>;
|
|
74
|
+
export declare const requestSubmit: <N extends HTMLFormElement, Msg>(ref: Renderer.Ref<N>, f: (result: Submitted) => Msg) => Command.Issued<Msg, never, Submitted, "Effect", never>;
|
|
75
|
+
export declare const showPicker: <N extends HTMLInputElement | HTMLSelectElement, Msg>(ref: Renderer.Ref<N>, f: (result: Opened) => Msg) => Command.Issued<Msg, never, Opened, "Effect", never>;
|
|
76
|
+
export declare const waitForAnimations: <N extends Element, Msg>(ref: Renderer.Ref<N>, f: (result: Found) => Msg) => Command.Issued<Msg, never, Found, "Effect", never>;
|
|
77
|
+
/**
|
|
78
|
+
* Waits two animation frames after the render commit. It needs no element,
|
|
79
|
+
* only the browser's animation frames, which `ReactDomHost` provides, so this
|
|
80
|
+
* is `effect/ui/html`'s own command.
|
|
81
|
+
*/
|
|
82
|
+
export declare const WaitForPaint: Command.Factory<{}, void, false, never>;
|
|
83
|
+
export declare const waitForPaint: <Msg>(f: () => Msg) => Command.Mapped<Command.Issued<void, never, void, "Effect">, Msg>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Command, Renderer } from "effect/ui";
|
|
2
|
+
import { BrowserHost, HtmlCommand } from "effect/ui/html";
|
|
3
|
+
import { Effect, Option, Schema } from "effect";
|
|
4
|
+
import * as ReactRefs from "../ReactRefs.js";
|
|
5
|
+
const FocusOptionsSchema = Schema.Struct({
|
|
6
|
+
preventScroll: Schema.optional(Schema.Boolean)
|
|
7
|
+
});
|
|
8
|
+
const ScrollOptionsSchema = Schema.Struct({
|
|
9
|
+
behavior: Schema.optional(Schema.Literals(["auto", "instant", "smooth"])),
|
|
10
|
+
block: Schema.optional(Schema.Literals(["center", "end", "nearest", "start"])),
|
|
11
|
+
inline: Schema.optional(Schema.Literals(["center", "end", "nearest", "start"]))
|
|
12
|
+
});
|
|
13
|
+
const SelectOptionsSchema = Schema.Struct({
|
|
14
|
+
start: Schema.Number,
|
|
15
|
+
end: Schema.optional(Schema.Number)
|
|
16
|
+
});
|
|
17
|
+
const withElement = Effect.fnUntraced(function* (ref, f) {
|
|
18
|
+
const element = yield* ReactRefs.lookup(ref);
|
|
19
|
+
if (Option.isNone(element) || !(element.value instanceof Element)) {
|
|
20
|
+
return Option.none();
|
|
21
|
+
}
|
|
22
|
+
return Option.some(f(element.value));
|
|
23
|
+
});
|
|
24
|
+
const found = Effect.fnUntraced(function* (ref, f) {
|
|
25
|
+
const result = yield* withElement(ref, f);
|
|
26
|
+
return { found: Option.isSome(result) };
|
|
27
|
+
});
|
|
28
|
+
export const Focus = Command.make("ReactDomCommand.focus", {
|
|
29
|
+
args: {
|
|
30
|
+
ref: Renderer.Ref,
|
|
31
|
+
options: Schema.optional(FocusOptionsSchema)
|
|
32
|
+
}
|
|
33
|
+
}, ({ options, ref }) => found(ref, (el) => BrowserHost.htmlHost.focus(el, options ?? {})));
|
|
34
|
+
export const Blur = Command.make("ReactDomCommand.blur", { args: { ref: Renderer.Ref } }, ({ ref }) => found(ref, (el) => BrowserHost.htmlHost.blur(el)));
|
|
35
|
+
export const ScrollIntoView = Command.make("ReactDomCommand.scrollIntoView", {
|
|
36
|
+
args: {
|
|
37
|
+
ref: Renderer.Ref,
|
|
38
|
+
options: Schema.optional(ScrollOptionsSchema)
|
|
39
|
+
}
|
|
40
|
+
}, ({ options, ref }) => found(ref, (el) => BrowserHost.htmlHost.scrollIntoView(el, options ?? {})));
|
|
41
|
+
export const Measure = Command.make("ReactDomCommand.measure", { args: { ref: Renderer.Ref } }, ({ ref }) => withElement(ref, (el) => BrowserHost.htmlHost.measure(el)));
|
|
42
|
+
export const Select = Command.make("ReactDomCommand.select", {
|
|
43
|
+
args: {
|
|
44
|
+
ref: Renderer.Ref,
|
|
45
|
+
options: Schema.optional(SelectOptionsSchema)
|
|
46
|
+
}
|
|
47
|
+
}, ({ options, ref }) => found(ref, (el) => BrowserHost.htmlHost.select(el, options)));
|
|
48
|
+
export const RequestSubmit = Command.make("ReactDomCommand.requestSubmit", { args: { ref: Renderer.Ref } }, ({ ref }) => Effect.map(withElement(ref, (el) => BrowserHost.htmlHost.requestSubmit(el)), (done) => ({
|
|
49
|
+
found: Option.isSome(done),
|
|
50
|
+
submitted: Option.getOrElse(done, () => false)
|
|
51
|
+
})));
|
|
52
|
+
export const ShowPicker = Command.make("ReactDomCommand.showPicker", { args: { ref: Renderer.Ref } }, ({ ref }) => Effect.map(withElement(ref, (el) => BrowserHost.htmlHost.showPicker(el)), (done) => ({
|
|
53
|
+
found: Option.isSome(done),
|
|
54
|
+
opened: Option.getOrElse(done, () => false)
|
|
55
|
+
})));
|
|
56
|
+
function isFiniteAnimation(animation) {
|
|
57
|
+
const timing = animation.effect?.getComputedTiming();
|
|
58
|
+
return timing !== undefined && Number.isFinite(timing.endTime);
|
|
59
|
+
}
|
|
60
|
+
const settled = { found: true };
|
|
61
|
+
const animationsSettled = (element) => Effect.suspend(() => {
|
|
62
|
+
const running = element.isConnected && typeof element.getAnimations === "function"
|
|
63
|
+
? element.getAnimations({ subtree: true }).filter(isFiniteAnimation)
|
|
64
|
+
: [];
|
|
65
|
+
if (running.length === 0)
|
|
66
|
+
return Effect.succeed(settled);
|
|
67
|
+
return Effect.flatMap(Effect.promise(() => Promise.all(running.map((animation) => animation.finished)).then(() => true, () => false)), (finished) => (finished || !element.isConnected ? Effect.succeed(settled) : animationsSettled(element)));
|
|
68
|
+
});
|
|
69
|
+
export const WaitForAnimations = Command.make("ReactDomCommand.waitForAnimations", { args: { ref: Renderer.Ref } }, ({ ref }) => Effect.flatMap(ReactRefs.lookup(ref), (element) => Option.isNone(element) || !(element.value instanceof Element)
|
|
70
|
+
? Effect.succeed({ found: false })
|
|
71
|
+
: animationsSettled(element.value)));
|
|
72
|
+
export const focus = (ref, f, options) => Command.map(Focus.make({ ref, ...(options !== undefined ? { options } : {}) }), f);
|
|
73
|
+
export const blur = (ref, f) => Command.map(Blur.make({ ref }), f);
|
|
74
|
+
export const scrollIntoView = (ref, f, options) => Command.map(ScrollIntoView.make({ ref, ...(options !== undefined ? { options } : {}) }), f);
|
|
75
|
+
export const measure = (ref, f) => Command.map(Measure.make({ ref }), f);
|
|
76
|
+
export const select = (ref, f, options) => Command.map(Select.make({ ref, ...(options !== undefined ? { options } : {}) }), f);
|
|
77
|
+
export const requestSubmit = (ref, f) => Command.map(RequestSubmit.make({ ref }), f);
|
|
78
|
+
export const showPicker = (ref, f) => Command.map(ShowPicker.make({ ref }), f);
|
|
79
|
+
export const waitForAnimations = (ref, f) => Command.map(WaitForAnimations.make({ ref }), f);
|
|
80
|
+
/**
|
|
81
|
+
* Waits two animation frames after the render commit. It needs no element,
|
|
82
|
+
* only the browser's animation frames, which `ReactDomHost` provides, so this
|
|
83
|
+
* is `effect/ui/html`'s own command.
|
|
84
|
+
*/
|
|
85
|
+
export const WaitForPaint = HtmlCommand.WaitForPaint;
|
|
86
|
+
export const waitForPaint = HtmlCommand.waitForPaint;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlFiles";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlFiles";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlHead";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlHead";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Component, Renderer, type View } from "effect/ui";
|
|
2
|
+
import { Effect, Layer, type Scope } from "effect";
|
|
3
|
+
import type * as React from "../React.js";
|
|
4
|
+
import * as ReactHost from "../ReactHost.js";
|
|
5
|
+
/**
|
|
6
|
+
* Passes when a root shows React elements (or is host-neutral), and otherwise
|
|
7
|
+
* names the host its vocabulary needs.
|
|
8
|
+
*/
|
|
9
|
+
export type ReactRoot<V> = ReactHost.ReactRoot<V>;
|
|
10
|
+
export interface Options extends ReactHost.Options {
|
|
11
|
+
/** Target document for caret preservation and browser services; defaults to `globalThis.document`. */
|
|
12
|
+
readonly document?: Document | undefined;
|
|
13
|
+
}
|
|
14
|
+
export type MountOptions<V extends View.Elements, RF = never, Events = unknown> = ReactHost.MountOptions<V, RF, Events>;
|
|
15
|
+
export interface Instance extends ReactHost.Instance {
|
|
16
|
+
/** Mounts the React subscription root into `element` and returns its unmount cleanup function. */
|
|
17
|
+
readonly renderInto: (element: Element | DocumentFragment) => () => void;
|
|
18
|
+
/**
|
|
19
|
+
* Hydrates server-rendered markup in `element` with the React subscription
|
|
20
|
+
* root and returns its unmount cleanup function. The tree must already be
|
|
21
|
+
* mounted with the same props the server rendered with.
|
|
22
|
+
*/
|
|
23
|
+
readonly hydrateInto: (element: Element | Document) => () => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a DOM-backed React host instance layered over `ReactHost.makeHost`,
|
|
27
|
+
* adding `react-dom/client` mounting, controlled input caret restoration,
|
|
28
|
+
* and scoped browser document services.
|
|
29
|
+
*/
|
|
30
|
+
export declare function makeHost(options?: Options): Instance;
|
|
31
|
+
/**
|
|
32
|
+
* Mounts a React Effect UI component tree into `element` for the life of the
|
|
33
|
+
* current `Scope`, returning its `Renderer.Root`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function mountEffect<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, element: Element | DocumentFragment, options?: MountOptions<V, RF, Events>): Effect.Effect<Renderer.Root<Model, Msg, Props>, never, Exclude<R | RF | RP, React.Host> | Scope.Scope>;
|
|
36
|
+
/**
|
|
37
|
+
* A Layer that mounts a React Effect UI component tree into `element` for as
|
|
38
|
+
* long as the Layer is built.
|
|
39
|
+
*/
|
|
40
|
+
export declare function mount<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, element: Element | DocumentFragment, options?: MountOptions<V, RF, Events>): Layer.Layer<never, never, Exclude<R | RF | RP, React.Host>>;
|
|
41
|
+
/**
|
|
42
|
+
* Mounts `component` with the services `layer` builds, for an existing React
|
|
43
|
+
* app to render through the returned `Component` (inside a `<Suspense>`). Call
|
|
44
|
+
* it once, at module level: every rendered `Component` shows this one tree,
|
|
45
|
+
* which lives until `dispose` is called. When the surrounding app hydrates
|
|
46
|
+
* server markup, `Component` hydrates with it, so there is no separate
|
|
47
|
+
* hydrating version.
|
|
48
|
+
*/
|
|
49
|
+
export declare function make<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, E, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, layer: Layer.Layer<Exclude<R | RF | RP, React.Host>, E>, options?: MountOptions<V, RF, Events>): ReactHost.Embedded<Model, Msg, Props>;
|
|
50
|
+
/**
|
|
51
|
+
* Like `mountEffect`, but hydrates markup that `effect-ui-react/dom/server`
|
|
52
|
+
* rendered into `element` instead of replacing it. Pass the same props the
|
|
53
|
+
* server used so the first client render matches the server HTML.
|
|
54
|
+
*/
|
|
55
|
+
export declare function hydrateEffect<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, element: Element | Document, options?: MountOptions<V, RF, Events>): Effect.Effect<Renderer.Root<Model, Msg, Props>, never, Exclude<R | RF | RP, React.Host> | Scope.Scope>;
|
|
56
|
+
/**
|
|
57
|
+
* A Layer that hydrates server-rendered markup in `element` for as long as the
|
|
58
|
+
* Layer is built. See `hydrateEffect`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function hydrate<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, element: Element | Document, options?: MountOptions<V, RF, Events>): Layer.Layer<never, never, Exclude<R | RF | RP, React.Host>>;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { Renderer } from "effect/ui";
|
|
2
|
+
import { BrowserHost, Html } from "effect/ui/html";
|
|
3
|
+
import { Context, Effect, Layer, Predicate } from "effect";
|
|
4
|
+
import { createElement } from "react";
|
|
5
|
+
import { createRoot, hydrateRoot } from "react-dom/client";
|
|
6
|
+
import * as ReactHost from "../ReactHost.js";
|
|
7
|
+
function isComposingEvent(value) {
|
|
8
|
+
if (!Predicate.isObject(value))
|
|
9
|
+
return false;
|
|
10
|
+
const rec = value;
|
|
11
|
+
if (rec.isComposing === true)
|
|
12
|
+
return true;
|
|
13
|
+
if (Predicate.isObject(rec.nativeEvent) &&
|
|
14
|
+
rec.nativeEvent.isComposing === true) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
if (Predicate.isObject(rec.event)) {
|
|
18
|
+
return isComposingEvent(rec.event);
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
function isComposingPayload(payload) {
|
|
23
|
+
if (Array.isArray(payload)) {
|
|
24
|
+
for (const item of payload) {
|
|
25
|
+
if (isComposingEvent(item))
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return isComposingEvent(payload);
|
|
31
|
+
}
|
|
32
|
+
function offsetIn(current, next, offset) {
|
|
33
|
+
const max = Math.min(current.length, next.length);
|
|
34
|
+
let prefix = 0;
|
|
35
|
+
while (prefix < max && current.charCodeAt(prefix) === next.charCodeAt(prefix))
|
|
36
|
+
prefix++;
|
|
37
|
+
if (offset <= prefix)
|
|
38
|
+
return offset;
|
|
39
|
+
let suffix = 0;
|
|
40
|
+
while (suffix < max - prefix &&
|
|
41
|
+
current.charCodeAt(current.length - 1 - suffix) === next.charCodeAt(next.length - 1 - suffix)) {
|
|
42
|
+
suffix++;
|
|
43
|
+
}
|
|
44
|
+
const fromEnd = current.length - offset;
|
|
45
|
+
return fromEnd <= suffix ? next.length - fromEnd : Math.min(offset, next.length - suffix);
|
|
46
|
+
}
|
|
47
|
+
function createCaretPreserver(doc) {
|
|
48
|
+
let pending;
|
|
49
|
+
function capture(payload) {
|
|
50
|
+
if (doc === undefined || isComposingPayload(payload))
|
|
51
|
+
return;
|
|
52
|
+
const active = doc.activeElement;
|
|
53
|
+
if (!(active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement))
|
|
54
|
+
return;
|
|
55
|
+
if (active.selectionStart === null || active.selectionEnd === null)
|
|
56
|
+
return;
|
|
57
|
+
pending = {
|
|
58
|
+
element: active,
|
|
59
|
+
value: active.value,
|
|
60
|
+
start: active.selectionStart,
|
|
61
|
+
end: active.selectionEnd,
|
|
62
|
+
direction: active.selectionDirection ?? undefined
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function restore() {
|
|
66
|
+
const saved = pending;
|
|
67
|
+
if (saved === undefined)
|
|
68
|
+
return;
|
|
69
|
+
pending = undefined;
|
|
70
|
+
const target = saved.element;
|
|
71
|
+
if (!target.isConnected || doc?.activeElement !== target)
|
|
72
|
+
return;
|
|
73
|
+
if (target.value === "")
|
|
74
|
+
return;
|
|
75
|
+
const start = offsetIn(saved.value, target.value, saved.start);
|
|
76
|
+
const end = offsetIn(saved.value, target.value, saved.end);
|
|
77
|
+
if (target.selectionStart !== start || target.selectionEnd !== end) {
|
|
78
|
+
target.setSelectionRange(start, end, saved.direction);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { capture, restore };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Builds the scoped browser document services (GlobalEvents, AnimationFrames,
|
|
85
|
+
* MediaQueries, Files, Images, Visibility, Connectivity) without leaking
|
|
86
|
+
* `BrowserHost`'s `Renderer` or `Html.Host`.
|
|
87
|
+
*/
|
|
88
|
+
function browserServicesLayer(doc) {
|
|
89
|
+
if (doc === undefined)
|
|
90
|
+
return Layer.empty;
|
|
91
|
+
return Layer.effectContext(Effect.map(Layer.build(BrowserHost.layer({ document: doc })), (context) => Context.omit(Renderer.Renderer, Html.Host)(context)));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Creates a DOM-backed React host instance layered over `ReactHost.makeHost`,
|
|
95
|
+
* adding `react-dom/client` mounting, controlled input caret restoration,
|
|
96
|
+
* and scoped browser document services.
|
|
97
|
+
*/
|
|
98
|
+
export function makeHost(options) {
|
|
99
|
+
const doc = options?.document ?? (typeof document !== "undefined" ? document : undefined);
|
|
100
|
+
const caret = createCaretPreserver(doc);
|
|
101
|
+
const base = ReactHost.makeHost({
|
|
102
|
+
...options,
|
|
103
|
+
onBeforeDispatch: (payload) => {
|
|
104
|
+
caret.capture(payload);
|
|
105
|
+
options?.onBeforeDispatch?.(payload);
|
|
106
|
+
},
|
|
107
|
+
onAfterCommit: () => {
|
|
108
|
+
caret.restore();
|
|
109
|
+
options?.onAfterCommit?.();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const rawLayer = Layer.mergeAll(browserServicesLayer(doc), base.layer);
|
|
113
|
+
const layer = Layer.effectContext(Effect.gen(function* () {
|
|
114
|
+
const built = yield* Layer.build(rawLayer);
|
|
115
|
+
base.setContext(Context.merge(yield* Effect.context(), built));
|
|
116
|
+
return built;
|
|
117
|
+
}));
|
|
118
|
+
function renderInto(element) {
|
|
119
|
+
const root = createRoot(element);
|
|
120
|
+
root.render(createElement(base.Root));
|
|
121
|
+
return () => root.unmount();
|
|
122
|
+
}
|
|
123
|
+
function hydrateInto(element) {
|
|
124
|
+
const root = hydrateRoot(element, createElement(base.Root));
|
|
125
|
+
return () => root.unmount();
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
...base,
|
|
129
|
+
layer,
|
|
130
|
+
renderInto,
|
|
131
|
+
hydrateInto
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Mounts a React Effect UI component tree into `element` for the life of the
|
|
136
|
+
* current `Scope`, returning its `Renderer.Root`.
|
|
137
|
+
*/
|
|
138
|
+
export function mountEffect(component, props, element, options) {
|
|
139
|
+
return Effect.gen(function* () {
|
|
140
|
+
const renderer = makeHost({ document: element.ownerDocument ?? undefined });
|
|
141
|
+
const root = yield* ReactHost.mountEffect(component, props, renderer, options);
|
|
142
|
+
const unmount = renderer.renderInto(element);
|
|
143
|
+
yield* Effect.addFinalizer(() => Effect.sync(unmount));
|
|
144
|
+
return root;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* A Layer that mounts a React Effect UI component tree into `element` for as
|
|
149
|
+
* long as the Layer is built.
|
|
150
|
+
*/
|
|
151
|
+
export function mount(component, props, element, options) {
|
|
152
|
+
return Layer.effectDiscard(mountEffect(component, props, element, options));
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Mounts `component` with the services `layer` builds, for an existing React
|
|
156
|
+
* app to render through the returned `Component` (inside a `<Suspense>`). Call
|
|
157
|
+
* it once, at module level: every rendered `Component` shows this one tree,
|
|
158
|
+
* which lives until `dispose` is called. When the surrounding app hydrates
|
|
159
|
+
* server markup, `Component` hydrates with it, so there is no separate
|
|
160
|
+
* hydrating version.
|
|
161
|
+
*/
|
|
162
|
+
export function make(component, props, layer, options) {
|
|
163
|
+
return ReactHost.make(component, props, layer, { ...options, renderer: makeHost() });
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Like `mountEffect`, but hydrates markup that `effect-ui-react/dom/server`
|
|
167
|
+
* rendered into `element` instead of replacing it. Pass the same props the
|
|
168
|
+
* server used so the first client render matches the server HTML.
|
|
169
|
+
*/
|
|
170
|
+
export function hydrateEffect(component, props, element, options) {
|
|
171
|
+
return Effect.gen(function* () {
|
|
172
|
+
const doc = "documentElement" in element ? element : element.ownerDocument;
|
|
173
|
+
const renderer = makeHost({ document: doc ?? undefined });
|
|
174
|
+
const root = yield* ReactHost.mountEffect(component, props, renderer, options);
|
|
175
|
+
const unmount = renderer.hydrateInto(element);
|
|
176
|
+
yield* Effect.addFinalizer(() => Effect.sync(unmount));
|
|
177
|
+
return root;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A Layer that hydrates server-rendered markup in `element` for as long as the
|
|
182
|
+
* Layer is built. See `hydrateEffect`.
|
|
183
|
+
*/
|
|
184
|
+
export function hydrate(component, props, element, options) {
|
|
185
|
+
return Layer.effectDiscard(hydrateEffect(component, props, element, options));
|
|
186
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlImages";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlImages";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlKeyboard";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlKeyboard";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/BrowserRouter";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/BrowserRouter";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlScheduler";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlScheduler";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlSubscription";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "effect/ui/html/HtmlSubscription";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "./intrinsics.js";
|
|
2
|
+
export * as ReactDomCommand from "./ReactDomCommand.js";
|
|
3
|
+
export * as ReactDomFiles from "./ReactDomFiles.js";
|
|
4
|
+
export * as ReactDomHead from "./ReactDomHead.js";
|
|
5
|
+
export * as ReactDomHost from "./ReactDomHost.js";
|
|
6
|
+
export * as ReactDomImages from "./ReactDomImages.js";
|
|
7
|
+
export * as ReactDomKeyboard from "./ReactDomKeyboard.js";
|
|
8
|
+
export * as ReactDomRouter from "./ReactDomRouter.js";
|
|
9
|
+
export * as ReactDomScheduler from "./ReactDomScheduler.js";
|
|
10
|
+
export * as ReactDomSubscription from "./ReactDomSubscription.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "./intrinsics.js";
|
|
2
|
+
export * as ReactDomCommand from "./ReactDomCommand.js";
|
|
3
|
+
export * as ReactDomFiles from "./ReactDomFiles.js";
|
|
4
|
+
export * as ReactDomHead from "./ReactDomHead.js";
|
|
5
|
+
export * as ReactDomHost from "./ReactDomHost.js";
|
|
6
|
+
export * as ReactDomImages from "./ReactDomImages.js";
|
|
7
|
+
export * as ReactDomKeyboard from "./ReactDomKeyboard.js";
|
|
8
|
+
export * as ReactDomRouter from "./ReactDomRouter.js";
|
|
9
|
+
export * as ReactDomScheduler from "./ReactDomScheduler.js";
|
|
10
|
+
export * as ReactDomSubscription from "./ReactDomSubscription.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { JSX } from "react";
|
|
2
|
+
declare module "../Intrinsics.js" {
|
|
3
|
+
interface IntrinsicElements extends JSX.IntrinsicElements {
|
|
4
|
+
}
|
|
5
|
+
interface VoidIntrinsicElements {
|
|
6
|
+
area: true;
|
|
7
|
+
base: true;
|
|
8
|
+
br: true;
|
|
9
|
+
col: true;
|
|
10
|
+
embed: true;
|
|
11
|
+
hr: true;
|
|
12
|
+
img: true;
|
|
13
|
+
input: true;
|
|
14
|
+
link: true;
|
|
15
|
+
meta: true;
|
|
16
|
+
param: true;
|
|
17
|
+
source: true;
|
|
18
|
+
track: true;
|
|
19
|
+
wbr: true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Component, type View } from "effect/ui";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import type * as React from "../../React.js";
|
|
4
|
+
import * as ReactHost from "../../ReactHost.js";
|
|
5
|
+
export type MountOptions<V extends View.Elements, RF = never, Events = unknown> = ReactHost.MountOptions<V, RF, Events>;
|
|
6
|
+
/**
|
|
7
|
+
* Mounts `component` with `props`, renders its first commit to an HTML string
|
|
8
|
+
* and unmounts it again. Hydrate the result on the client with
|
|
9
|
+
* `ReactDomHost.hydrate` and the same props.
|
|
10
|
+
*/
|
|
11
|
+
export declare function renderToString<Name extends string, Props, Model, Msg, Events, R, V extends View.Elements, RF = never, RP = never>(component: Component.Members<Name, Props, Model, Msg, Events, R, V, any> & ReactHost.ReactRoot<V>, props: Component.PropsInput<Props, never, RP>, options?: MountOptions<V, RF, Events>): Effect.Effect<string, never, Exclude<R | RF | RP, React.Host>>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { renderToString as renderReactToString } from "react-dom/server";
|
|
4
|
+
import * as ReactHost from "../../ReactHost.js";
|
|
5
|
+
/**
|
|
6
|
+
* Mounts `component` with `props`, renders its first commit to an HTML string
|
|
7
|
+
* and unmounts it again. Hydrate the result on the client with
|
|
8
|
+
* `ReactDomHost.hydrate` and the same props.
|
|
9
|
+
*/
|
|
10
|
+
export function renderToString(component, props, options) {
|
|
11
|
+
return Effect.scoped(Effect.gen(function* () {
|
|
12
|
+
const host = ReactHost.makeHost();
|
|
13
|
+
yield* ReactHost.mountEffect(component, props, host, options);
|
|
14
|
+
return renderReactToString(createElement(host.Root));
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ReactDomServer from "./ReactDomServer.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ReactDomServer from "./ReactDomServer.js";
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { View, UiAtom } from "effect/ui";
|
|
2
|
+
import { type AtomRegistry } from "effect/reactivity";
|
|
3
|
+
import { type BoundaryStore, type DynamicNodeSource, type EffectComponentAny, type HostBridgeContext, type RenderSource, type SubtreeHandle } from "./react-component-bridge.js";
|
|
4
|
+
export interface PlacementHandle extends SubtreeHandle {
|
|
5
|
+
readonly attach: (store: BoundaryStore | undefined) => void;
|
|
6
|
+
}
|
|
7
|
+
interface Entry {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly component: EffectComponentAny;
|
|
10
|
+
readonly props: unknown;
|
|
11
|
+
readonly handle: PlacementHandle;
|
|
12
|
+
}
|
|
13
|
+
type Owners = ReadonlyMap<string, ReadonlyArray<Entry>>;
|
|
14
|
+
/** The hidden `reads` entry every `React.component` declares. */
|
|
15
|
+
export declare const placementsReadKey = "~reactPlacements";
|
|
16
|
+
export declare function placementsRead(name: string): {
|
|
17
|
+
owner: UiAtom.WritableFamily<"ReactPlacements", string, Owners, Owners>;
|
|
18
|
+
optional: boolean;
|
|
19
|
+
select: () => string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The bridge a component's `$boundary` provides to React descendants. Mounts
|
|
23
|
+
* become entries the owner's view declares as ordinary Effect children, so
|
|
24
|
+
* they keep the owner's environment, shared owners and collected reports.
|
|
25
|
+
*/
|
|
26
|
+
export declare function makePlacementBridge(name: string, owner: string, parent: HostBridgeContext, registry: AtomRegistry.AtomRegistry, shell: EffectComponentAny): HostBridgeContext;
|
|
27
|
+
export type PlacementTarget = {
|
|
28
|
+
readonly kind: "component";
|
|
29
|
+
readonly component: EffectComponentAny;
|
|
30
|
+
} | {
|
|
31
|
+
readonly kind: "render";
|
|
32
|
+
readonly source: RenderSource;
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "node";
|
|
35
|
+
readonly source: DynamicNodeSource;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Wraps a `React.component` view in its `$boundary`, appending the children
|
|
39
|
+
* React descendants placed through this instance's bridge.
|
|
40
|
+
*
|
|
41
|
+
* Content this view hands to a child (a headless `view` or `slots`) renders
|
|
42
|
+
* inside that child, so it gets a `$placementScope` with its own owner id:
|
|
43
|
+
* React mounts inside it are declared there and owned by the child instance,
|
|
44
|
+
* keeping the child's shared state and Layers in scope.
|
|
45
|
+
*/
|
|
46
|
+
export declare function boundaryView(name: string, h: View.Helper<any, any> & {
|
|
47
|
+
readonly reads?: Record<string, unknown>;
|
|
48
|
+
}, content: View.Input<any, any>): View.Content<any, any>;
|
|
49
|
+
export {};
|