@opsydyn/astro-effect-ui 0.0.0-stage → 1.0.0-next.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/client.d.mts +11 -0
- package/client.mjs +47 -0
- package/component.d.mts +19 -0
- package/component.mjs +14 -0
- package/directive-interaction.d.mts +10 -0
- package/directive-interaction.mjs +67 -0
- package/index.d.mts +11 -0
- package/index.mjs +24 -0
- package/package.json +34 -4
- package/server.d.mts +21 -0
- package/server.mjs +19 -0
- package/README.md +0 -3
package/client.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IslandProps } from "./component.mjs";
|
|
2
|
+
import { Component } from "@effect/ui";
|
|
3
|
+
|
|
4
|
+
//#region src/client.d.ts
|
|
5
|
+
declare function renderer(island: HTMLElement): (component: Component.Any, props: IslandProps, slots: Record<string, string>, {
|
|
6
|
+
client
|
|
7
|
+
}: {
|
|
8
|
+
client: string;
|
|
9
|
+
}) => Promise<void>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { renderer as default };
|
package/client.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
+
import { Effect, Exit, Scope } from "effect";
|
|
3
|
+
import { BrowserHost, HtmlHydrate } from "@effect/ui/html";
|
|
4
|
+
//#region src/client.ts
|
|
5
|
+
const activations = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
function renderer(island) {
|
|
7
|
+
return (component, props, slots, { client }) => {
|
|
8
|
+
const existing = activations.get(island);
|
|
9
|
+
if (existing) return existing;
|
|
10
|
+
const scope = Scope.makeUnsafe();
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
const dispose = async () => {
|
|
13
|
+
island.removeEventListener("astro:unmount", onUnmount);
|
|
14
|
+
controller.abort();
|
|
15
|
+
await Effect.runPromise(Scope.close(scope, Exit.void));
|
|
16
|
+
};
|
|
17
|
+
const onUnmount = () => {
|
|
18
|
+
dispose().catch((error) => console.error("Could not dispose Effect UI island.", error)).finally(() => activations.delete(island));
|
|
19
|
+
};
|
|
20
|
+
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
21
|
+
const activate = Effect.gen(function* () {
|
|
22
|
+
if (Object.keys(slots).length > 0) return yield* new IslandRendererError({ message: "Astro Effect UI slots are unsupported." });
|
|
23
|
+
const definition = asIslandComponent(component);
|
|
24
|
+
const options = { head: false };
|
|
25
|
+
if (client === "only") {
|
|
26
|
+
const container = island.ownerDocument.createElement("div");
|
|
27
|
+
island.replaceChildren(container);
|
|
28
|
+
yield* BrowserHost.mountEffect(definition, props, container, options);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const container = island.querySelector(":scope > [data-effect-ui-root]");
|
|
32
|
+
const state = island.querySelector(":scope > script[data-effect-ui-state]")?.textContent;
|
|
33
|
+
if (!container || !state) return yield* new IslandRendererError({ message: "Missing Effect UI hydration markup or state." });
|
|
34
|
+
const hydrationState = JSON.parse(state);
|
|
35
|
+
yield* (yield* HtmlHydrate.hydrateEffect(definition, props, container, hydrationState, options)).adopted;
|
|
36
|
+
});
|
|
37
|
+
const ready = Effect.runPromise(activate.pipe(Scope.provide(scope)), { signal: controller.signal }).catch(async (error) => {
|
|
38
|
+
if (controller.signal.aborted) return;
|
|
39
|
+
await dispose().finally(() => activations.delete(island));
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
activations.set(island, ready);
|
|
43
|
+
return ready;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { renderer as default };
|
package/component.d.mts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Component } from "@effect/ui";
|
|
2
|
+
import { Html } from "@effect/ui/html";
|
|
3
|
+
import * as _$effect_Types0 from "effect/Types";
|
|
4
|
+
import * as _$effect_Cause0 from "effect/Cause";
|
|
5
|
+
|
|
6
|
+
//#region src/component.d.ts
|
|
7
|
+
declare const IslandRendererError_base: new <A extends Record<string, any> = {}>(args: _$effect_Types0.VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P] }>) => _$effect_Cause0.YieldableError & {
|
|
8
|
+
readonly _tag: "IslandRendererError";
|
|
9
|
+
} & Readonly<A>;
|
|
10
|
+
declare class IslandRendererError extends IslandRendererError_base<{
|
|
11
|
+
readonly message: string;
|
|
12
|
+
}> {}
|
|
13
|
+
type IslandProps = object;
|
|
14
|
+
type IslandComponent = Component.Members<string, IslandProps, unknown, never, never, Html.Host, Html.Elements>;
|
|
15
|
+
declare function asIslandComponent(value: Component.Any): IslandComponent;
|
|
16
|
+
/** Describes the props Astro passes while preserving the component the renderer detects. */
|
|
17
|
+
declare function astroComponent<C extends Component.Any>(component: C): (props: Component.Props<C>) => void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { IslandComponent, IslandProps, IslandRendererError, asIslandComponent, astroComponent };
|
package/component.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Component } from "@effect/ui";
|
|
2
|
+
import { Data } from "effect";
|
|
3
|
+
//#region src/component.ts
|
|
4
|
+
var IslandRendererError = class extends Data.TaggedError("IslandRendererError") {};
|
|
5
|
+
function asIslandComponent(value) {
|
|
6
|
+
if (!Component.isComponent(value)) throw new IslandRendererError({ message: "Expected an Effect UI component." });
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
/** Describes the props Astro passes while preserving the component the renderer detects. */
|
|
10
|
+
function astroComponent(component) {
|
|
11
|
+
return component;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { IslandRendererError, asIslandComponent, astroComponent };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ClientDirective } from "astro";
|
|
2
|
+
|
|
3
|
+
//#region src/directive-interaction.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Preserve clicks while the renderer waits for Effect UI to adopt the tree.
|
|
6
|
+
* Effect UI itself reads pending form edits from adopted browser controls.
|
|
7
|
+
*/
|
|
8
|
+
declare const interaction: ClientDirective;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { interaction as default };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//#region src/directive-interaction.ts
|
|
2
|
+
const enablesHover = (value) => value instanceof Object && value.hover === true;
|
|
3
|
+
const triggers = [
|
|
4
|
+
"pointerdown",
|
|
5
|
+
"focusin",
|
|
6
|
+
"keydown",
|
|
7
|
+
"pointerover"
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* Preserve clicks while the renderer waits for Effect UI to adopt the tree.
|
|
11
|
+
* Effect UI itself reads pending form edits from adopted browser controls.
|
|
12
|
+
*/
|
|
13
|
+
const interaction = (load, options, island) => {
|
|
14
|
+
const root = island.querySelector(":scope > [data-effect-ui-root]");
|
|
15
|
+
const window = island.ownerDocument.defaultView;
|
|
16
|
+
const hover = enablesHover(options.value);
|
|
17
|
+
if (!root || !window) {
|
|
18
|
+
load().then((hydrate) => hydrate());
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const clicks = [];
|
|
22
|
+
let started = false;
|
|
23
|
+
let removed = false;
|
|
24
|
+
const stop = () => {
|
|
25
|
+
for (const type of triggers) island.removeEventListener(type, start, true);
|
|
26
|
+
island.removeEventListener("click", captureClick, true);
|
|
27
|
+
island.removeEventListener("astro:unmount", onUnmount);
|
|
28
|
+
};
|
|
29
|
+
const onUnmount = () => {
|
|
30
|
+
removed = true;
|
|
31
|
+
stop();
|
|
32
|
+
};
|
|
33
|
+
const targetInsideRoot = (event) => {
|
|
34
|
+
const target = event.target;
|
|
35
|
+
return target instanceof window.Element && root.contains(target) && target.closest("astro-island") === island ? target : null;
|
|
36
|
+
};
|
|
37
|
+
const start = (event) => {
|
|
38
|
+
if (started || !targetInsideRoot(event)) return;
|
|
39
|
+
if (event.type === "pointerover" && (!hover || !("pointerType" in event) || event.pointerType !== "mouse")) return;
|
|
40
|
+
started = true;
|
|
41
|
+
load().then((hydrate) => removed || !island.isConnected ? void 0 : hydrate()).then(() => {
|
|
42
|
+
stop();
|
|
43
|
+
if (removed || !island.isConnected) return;
|
|
44
|
+
for (const { target, event } of clicks) if (root.contains(target) && target.closest("astro-island") === island && target.closest(":disabled") === null) target.dispatchEvent(new window.MouseEvent("click", event));
|
|
45
|
+
}).catch((error) => {
|
|
46
|
+
stop();
|
|
47
|
+
console.error("Could not activate the Effect UI island.", error);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
const captureClick = (event) => {
|
|
51
|
+
const target = targetInsideRoot(event);
|
|
52
|
+
if (!target) return;
|
|
53
|
+
start(event);
|
|
54
|
+
if (target.closest("a[href], label, input[type=\"checkbox\"], input[type=\"radio\"]")) return;
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
event.stopImmediatePropagation();
|
|
57
|
+
clicks.push({
|
|
58
|
+
target,
|
|
59
|
+
event
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
for (const type of triggers) island.addEventListener(type, start, true);
|
|
63
|
+
island.addEventListener("click", captureClick, true);
|
|
64
|
+
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
export { interaction as default };
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AstroIntegration } from "astro";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type EffectUiOptions = {
|
|
5
|
+
readonly interaction?: boolean;
|
|
6
|
+
};
|
|
7
|
+
declare function effectUi({
|
|
8
|
+
interaction
|
|
9
|
+
}?: EffectUiOptions): AstroIntegration;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { EffectUiOptions, effectUi as default };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
const extension = import.meta.url.endsWith(".ts") ? "ts" : "mjs";
|
|
4
|
+
function entrypoint(name) {
|
|
5
|
+
return fileURLToPath(new URL(`./${name}.${extension}`, import.meta.url));
|
|
6
|
+
}
|
|
7
|
+
function effectUi({ interaction = true } = {}) {
|
|
8
|
+
return {
|
|
9
|
+
name: "astro-effect-ui",
|
|
10
|
+
hooks: { "astro:config:setup": ({ addClientDirective, addRenderer }) => {
|
|
11
|
+
if (interaction) addClientDirective({
|
|
12
|
+
name: "interaction",
|
|
13
|
+
entrypoint: entrypoint("directive-interaction")
|
|
14
|
+
});
|
|
15
|
+
addRenderer({
|
|
16
|
+
name: "astro-effect-ui",
|
|
17
|
+
serverEntrypoint: entrypoint("server"),
|
|
18
|
+
clientEntrypoint: entrypoint("client")
|
|
19
|
+
});
|
|
20
|
+
} }
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { effectUi as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opsydyn/astro-effect-ui",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-next.0",
|
|
4
|
+
"description": "Standalone Astro integration for Effect UI HTML components.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.mts",
|
|
10
|
+
"import": "./index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"./component": {
|
|
13
|
+
"types": "./component.d.mts",
|
|
14
|
+
"import": "./component.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@effect/ui": "https://pkg.pr.new/Effect-TS/ui/@effect/ui@77dcd92"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"astro": ">=5.0.0 <8",
|
|
22
|
+
"effect": "~4.1.0"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22.0.0"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/opsydyn/fold-kit-experiments.git",
|
|
30
|
+
"directory": "packages/astro-effect-ui"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"tag": "next"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/server.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IslandProps } from "./component.mjs";
|
|
2
|
+
import { Component } from "@effect/ui";
|
|
3
|
+
import { AstroComponentMetadata } from "astro";
|
|
4
|
+
|
|
5
|
+
//#region src/server.d.ts
|
|
6
|
+
type RenderContext = Readonly<{
|
|
7
|
+
result?: Readonly<{
|
|
8
|
+
createAstro?: (props: IslandProps, slots: null) => Readonly<{
|
|
9
|
+
isPrerendered: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
}>;
|
|
12
|
+
}>;
|
|
13
|
+
declare const _default: {
|
|
14
|
+
name: string;
|
|
15
|
+
check: (component: any) => Promise<boolean>;
|
|
16
|
+
renderToStaticMarkup(this: RenderContext | void, component: Component.Any, props: IslandProps, slots: Record<string, string>, metadata?: AstroComponentMetadata): Promise<{
|
|
17
|
+
html: string;
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
export { _default as default };
|
package/server.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
+
import { Component } from "@effect/ui";
|
|
3
|
+
import { Effect } from "effect";
|
|
4
|
+
import { HtmlServer } from "@effect/ui/html";
|
|
5
|
+
//#region src/server.ts
|
|
6
|
+
var server_default = {
|
|
7
|
+
name: "astro-effect-ui",
|
|
8
|
+
check: async (component) => Component.isComponent(component),
|
|
9
|
+
async renderToStaticMarkup(component, props, slots, metadata) {
|
|
10
|
+
if (Object.keys(slots).length > 0) throw new IslandRendererError({ message: "Astro Effect UI slots are unsupported." });
|
|
11
|
+
const isPrerendered = this?.result?.createAstro?.({}, null)?.isPrerendered ?? false;
|
|
12
|
+
const rendered = await Effect.runPromise(HtmlServer.renderToHtml(asIslandComponent(component), props, { static: isPrerendered }));
|
|
13
|
+
if (!metadata?.hydrate) return { html: rendered.html };
|
|
14
|
+
const state = HtmlServer.serializeState(rendered.state);
|
|
15
|
+
return { html: `<div data-effect-ui-root>${rendered.html}</div><script type="application/json" data-effect-ui-state>${state}<\/script>` };
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { server_default as default };
|
package/README.md
DELETED