@opsydyn/astro-effect-ui 0.0.0-stage → 1.0.0-next.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.
- package/client.d.mts +18 -0
- package/client.mjs +158 -0
- package/component.d.mts +37 -0
- package/component.mjs +22 -0
- package/directive-interaction.d.mts +25 -0
- package/directive-interaction.mjs +173 -0
- package/index.d.mts +20 -0
- package/index.mjs +89 -0
- package/package.json +36 -4
- package/runtime.d.mts +49 -0
- package/runtime.mjs +19 -0
- package/server.d.mts +32 -0
- package/server.mjs +42 -0
- package/slots-ELUCsLv9.mjs +105 -0
- package/README.md +0 -3
package/client.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IslandProps } from "./component.mjs";
|
|
2
|
+
import { AnyClientRuntime } from "./runtime.mjs";
|
|
3
|
+
import { Component } from "@effect/ui";
|
|
4
|
+
|
|
5
|
+
//#region src/client.d.ts
|
|
6
|
+
/** The client renderer, with every island run in the app's runtime when it has one. */
|
|
7
|
+
declare function makeClientRenderer(runtime?: AnyClientRuntime): (island: HTMLElement) => (component: Component.Any, props: IslandProps, slots: Record<string, string>, {
|
|
8
|
+
client
|
|
9
|
+
}: {
|
|
10
|
+
client: string;
|
|
11
|
+
}) => Promise<void>;
|
|
12
|
+
declare const _default: (island: HTMLElement) => (component: Component.Any, props: IslandProps, slots: Record<string, string>, {
|
|
13
|
+
client
|
|
14
|
+
}: {
|
|
15
|
+
client: string;
|
|
16
|
+
}) => Promise<void>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { _default as default, makeClientRenderer };
|
package/client.mjs
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
+
import { a as slotFiller, i as keepSlotNodes, o as slotsOf, r as fillSlotElements, s as withSlots } from "./slots-ELUCsLv9.mjs";
|
|
3
|
+
import { Cause, Effect, Exit, ManagedRuntime, Scope } from "effect";
|
|
4
|
+
import { BrowserHost, HtmlHydrate } from "@effect/ui/html";
|
|
5
|
+
//#region src/client.ts
|
|
6
|
+
/**
|
|
7
|
+
* Dispatched on an island whose component failed. Its detail holds the `error` and the
|
|
8
|
+
* `phase`: `activate` when the island never came alive, `run` when it failed later.
|
|
9
|
+
*/
|
|
10
|
+
const ERROR_EVENT = "effect-ui:error";
|
|
11
|
+
/**
|
|
12
|
+
* Starts the component in the island. `fresh` builds it from its props, as `client:only`
|
|
13
|
+
* does and as a kept island does when its props change; otherwise the server's markup
|
|
14
|
+
* is adopted.
|
|
15
|
+
*/
|
|
16
|
+
const activate = (host, island, component, props, slots, fresh) => {
|
|
17
|
+
const scope = Scope.makeUnsafe();
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
let closing;
|
|
20
|
+
let restore = () => {};
|
|
21
|
+
let live = false;
|
|
22
|
+
let failure;
|
|
23
|
+
const dispose = () => {
|
|
24
|
+
closing ??= Promise.resolve().then(() => {
|
|
25
|
+
controller.abort();
|
|
26
|
+
return Effect.runPromise(Scope.close(scope, Exit.void));
|
|
27
|
+
});
|
|
28
|
+
return closing;
|
|
29
|
+
};
|
|
30
|
+
const report = (detail) => {
|
|
31
|
+
island.setAttribute("data-effect-ui-failed", "");
|
|
32
|
+
const view = island.ownerDocument.defaultView;
|
|
33
|
+
if (view !== null) island.dispatchEvent(new view.CustomEvent(ERROR_EVENT, {
|
|
34
|
+
bubbles: true,
|
|
35
|
+
composed: true,
|
|
36
|
+
detail
|
|
37
|
+
}));
|
|
38
|
+
return host.notify?.(detail);
|
|
39
|
+
};
|
|
40
|
+
const fail = async (detail) => {
|
|
41
|
+
if (failure === void 0 || host.fallback === void 0) {
|
|
42
|
+
await dispose().catch((cause) => console.error("Could not dispose Effect UI island.", cause));
|
|
43
|
+
restore();
|
|
44
|
+
}
|
|
45
|
+
await Promise.resolve(report(detail)).catch((cause) => console.error("Could not report the Effect UI island failure.", cause));
|
|
46
|
+
};
|
|
47
|
+
/** The component failed: Effect UI calls this through the fallback it is given. */
|
|
48
|
+
const componentFailed = (cause) => {
|
|
49
|
+
if (failure !== void 0) return;
|
|
50
|
+
const error = new IslandRendererError({
|
|
51
|
+
message: `The ${component.definition.name} island failed.`,
|
|
52
|
+
cause: Cause.squash(cause)
|
|
53
|
+
});
|
|
54
|
+
failure = error;
|
|
55
|
+
if (live) queueMicrotask(() => void fail({
|
|
56
|
+
error,
|
|
57
|
+
phase: "run"
|
|
58
|
+
}));
|
|
59
|
+
};
|
|
60
|
+
const fallback = (result, h) => {
|
|
61
|
+
componentFailed(result.cause);
|
|
62
|
+
return host.fallback === void 0 ? h.span.attr("hidden", "") : host.fallback(result, h);
|
|
63
|
+
};
|
|
64
|
+
const options = {
|
|
65
|
+
head: false,
|
|
66
|
+
fallback
|
|
67
|
+
};
|
|
68
|
+
const traced = Effect.gen(function* () {
|
|
69
|
+
const definition = asIslandComponent(component);
|
|
70
|
+
const filled = slotsOf(component, slots);
|
|
71
|
+
const root = island.querySelector(":scope > [data-effect-ui-root]");
|
|
72
|
+
const fill = filled === void 0 ? void 0 : slotFiller(filled, fresh ? /* @__PURE__ */ new Map() : keepSlotNodes(island));
|
|
73
|
+
const target = filled === void 0 ? definition : withSlots(component, props, filled, {
|
|
74
|
+
fill,
|
|
75
|
+
fallback
|
|
76
|
+
});
|
|
77
|
+
const given = filled === void 0 ? props : {};
|
|
78
|
+
if (fresh) {
|
|
79
|
+
const before = [...island.childNodes];
|
|
80
|
+
const container = root ?? island.ownerDocument.createElement("div");
|
|
81
|
+
island.querySelector(":scope > script[data-effect-ui-state]")?.remove();
|
|
82
|
+
if (root === null) {
|
|
83
|
+
island.replaceChildren(container);
|
|
84
|
+
restore = () => island.replaceChildren(...before);
|
|
85
|
+
} else container.replaceChildren();
|
|
86
|
+
yield* BrowserHost.mountEffect(target, given, container, options);
|
|
87
|
+
} else {
|
|
88
|
+
const state = island.querySelector(":scope > script[data-effect-ui-state]")?.textContent;
|
|
89
|
+
if (!root || !state) return yield* new IslandRendererError({ message: "Missing Effect UI hydration markup or state." });
|
|
90
|
+
const markup = root.innerHTML;
|
|
91
|
+
restore = () => {
|
|
92
|
+
root.innerHTML = markup;
|
|
93
|
+
};
|
|
94
|
+
const hydrationState = JSON.parse(state);
|
|
95
|
+
const hydrated = yield* HtmlHydrate.hydrateEffect(target, given, root, hydrationState, options);
|
|
96
|
+
if (fill !== void 0) fillSlotElements(island, fill);
|
|
97
|
+
yield* hydrated.adopted;
|
|
98
|
+
}
|
|
99
|
+
if (fill !== void 0) fillSlotElements(island, fill);
|
|
100
|
+
if (failure !== void 0) return yield* failure;
|
|
101
|
+
live = true;
|
|
102
|
+
}).pipe(Scope.provide(scope), Effect.withSpan("astro-effect-ui.activate", { attributes: { component: component.definition?.name ?? "unknown" } }));
|
|
103
|
+
return {
|
|
104
|
+
ready: host.run(traced, { signal: controller.signal }).catch(async (error) => {
|
|
105
|
+
if (controller.signal.aborted) return;
|
|
106
|
+
await fail({
|
|
107
|
+
error,
|
|
108
|
+
phase: "activate"
|
|
109
|
+
});
|
|
110
|
+
throw error;
|
|
111
|
+
}),
|
|
112
|
+
dispose
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/** The client renderer, with every island run in the app's runtime when it has one. */
|
|
116
|
+
function makeClientRenderer(runtime = {}) {
|
|
117
|
+
const { onError } = runtime;
|
|
118
|
+
const run = runtime.layer === void 0 ? Effect.runPromise : ManagedRuntime.make(runtime.layer).runPromise;
|
|
119
|
+
const host = {
|
|
120
|
+
run,
|
|
121
|
+
fallback: runtime.fallback,
|
|
122
|
+
notify: onError === void 0 ? void 0 : (failure) => run(onError(failure))
|
|
123
|
+
};
|
|
124
|
+
const entries = /* @__PURE__ */ new WeakMap();
|
|
125
|
+
return function renderer(island) {
|
|
126
|
+
return (component, props, slots, { client }) => {
|
|
127
|
+
const requested = island.getAttribute("props");
|
|
128
|
+
const current = entries.get(island);
|
|
129
|
+
if (current !== void 0 && current.props === requested) return current.ready;
|
|
130
|
+
let run;
|
|
131
|
+
if (current === void 0) {
|
|
132
|
+
const first = activate(host, island, component, props, slots, client === "only");
|
|
133
|
+
run = Promise.resolve(first);
|
|
134
|
+
const onUnmount = () => {
|
|
135
|
+
const last = entries.get(island);
|
|
136
|
+
entries.delete(island);
|
|
137
|
+
last?.run.then((started) => started.dispose()).catch((error) => console.error("Could not dispose Effect UI island.", error));
|
|
138
|
+
};
|
|
139
|
+
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
140
|
+
} else run = current.run.then(async (last) => {
|
|
141
|
+
await last.ready.catch(() => void 0);
|
|
142
|
+
await last.dispose();
|
|
143
|
+
island.removeAttribute("data-effect-ui-failed");
|
|
144
|
+
return activate(host, island, component, props, slots, true);
|
|
145
|
+
});
|
|
146
|
+
const entry = {
|
|
147
|
+
props: requested,
|
|
148
|
+
run,
|
|
149
|
+
ready: run.then((started) => started.ready)
|
|
150
|
+
};
|
|
151
|
+
entries.set(island, entry);
|
|
152
|
+
return entry.ready;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
var client_default = makeClientRenderer();
|
|
157
|
+
//#endregion
|
|
158
|
+
export { client_default as default, makeClientRenderer };
|
package/component.d.mts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
readonly cause?: unknown;
|
|
13
|
+
}> {}
|
|
14
|
+
type IslandProps = object;
|
|
15
|
+
type IslandComponent = Component.Members<string, IslandProps, unknown, never, never, Html.Host, Html.Elements>;
|
|
16
|
+
declare function asIslandComponent(value: Component.Any): IslandComponent;
|
|
17
|
+
/**
|
|
18
|
+
* What a component still needs once the HTML host and `Provided` are taken away.
|
|
19
|
+
* Requirements typed as `any` are let through, since nothing can be read from them.
|
|
20
|
+
*/
|
|
21
|
+
type Missing<C, Provided> = Exclude<Component.Requirements<C>, Html.Host | Provided>;
|
|
22
|
+
/** Nothing for a component whose services are all provided, and a named error otherwise. */
|
|
23
|
+
type Accepted<C, Provided = never> = 0 extends 1 & Component.Requirements<C> ? unknown : [Missing<C, Provided>] extends [never] ? unknown : {
|
|
24
|
+
readonly 'astroComponent: no island runtime provides': Missing<C, Provided>;
|
|
25
|
+
};
|
|
26
|
+
/** What Astro sees: a component that takes these props. */
|
|
27
|
+
type AstroComponent<C> = (props: Component.Props<C>) => void;
|
|
28
|
+
/** The component as Astro sees it. Only the renderer calls the original view. */
|
|
29
|
+
declare function toAstro<C>(component: Component.Any): AstroComponent<C>;
|
|
30
|
+
/**
|
|
31
|
+
* Describes the props Astro passes while preserving the component the renderer detects.
|
|
32
|
+
* A component that needs a service is a type error here: declare the services with
|
|
33
|
+
* `defineIslands` and use the `astroComponent` it returns.
|
|
34
|
+
*/
|
|
35
|
+
declare function astroComponent<C extends Component.Any>(component: C & Accepted<C>): AstroComponent<C>;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { Accepted, AstroComponent, IslandComponent, IslandProps, IslandRendererError, asIslandComponent, astroComponent, toAstro };
|
package/component.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
/** The component as Astro sees it. Only the renderer calls the original view. */
|
|
10
|
+
function toAstro(component) {
|
|
11
|
+
return component;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Describes the props Astro passes while preserving the component the renderer detects.
|
|
15
|
+
* A component that needs a service is a type error here: declare the services with
|
|
16
|
+
* `defineIslands` and use the `astroComponent` it returns.
|
|
17
|
+
*/
|
|
18
|
+
function astroComponent(component) {
|
|
19
|
+
return toAstro(component);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { IslandRendererError, asIslandComponent, astroComponent, toAstro };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClientDirective } from "astro";
|
|
2
|
+
|
|
3
|
+
//#region src/directive-interaction.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Hydrate on the reader's first press, focus, key or (with `hover`) mouse-over.
|
|
6
|
+
*
|
|
7
|
+
* Clicks made while the island loads are held and replayed once Effect UI has adopted
|
|
8
|
+
* the tree; Effect UI itself reads pending form edits from adopted browser controls.
|
|
9
|
+
*
|
|
10
|
+
* A mouse resting on the island is replayed too. The browser sent its
|
|
11
|
+
* `mouseenter` events before anything listened, and sends no more until the
|
|
12
|
+
* pointer crosses another element, so the adopted tree is told what the
|
|
13
|
+
* pointer is already over: one `mouseenter` for each element under it, the
|
|
14
|
+
* outermost first, as the browser sends them.
|
|
15
|
+
*
|
|
16
|
+
* With `preload`, the island's code is fetched when the browser is idle or the island
|
|
17
|
+
* nears the viewport, so the first interaction only has to hydrate.
|
|
18
|
+
*
|
|
19
|
+
* Code that fails to load is asked for again on the next interaction. A component that
|
|
20
|
+
* fails to hydrate is not retried. Either way the island keeps its server markup, and
|
|
21
|
+
* the failure is raised as `astro:hydration-error`, as Astro's own directives do.
|
|
22
|
+
*/
|
|
23
|
+
declare const interaction: ClientDirective;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { interaction as default };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
//#region src/directive-interaction.ts
|
|
2
|
+
const optionsOf = (value) => value instanceof Object ? value : {};
|
|
3
|
+
const triggers = [
|
|
4
|
+
"pointerdown",
|
|
5
|
+
"focusin",
|
|
6
|
+
"keydown",
|
|
7
|
+
"pointerover"
|
|
8
|
+
];
|
|
9
|
+
/** How many times an island's code is asked for before the island is left as markup. */
|
|
10
|
+
const ATTEMPTS = 3;
|
|
11
|
+
/**
|
|
12
|
+
* Islands this directive is waiting on. Astro runs a directive again whenever its island
|
|
13
|
+
* is put back in the document, as slot content is while its parent island hydrates.
|
|
14
|
+
*/
|
|
15
|
+
const armed = /* @__PURE__ */ new WeakSet();
|
|
16
|
+
/**
|
|
17
|
+
* Hydrate on the reader's first press, focus, key or (with `hover`) mouse-over.
|
|
18
|
+
*
|
|
19
|
+
* Clicks made while the island loads are held and replayed once Effect UI has adopted
|
|
20
|
+
* the tree; Effect UI itself reads pending form edits from adopted browser controls.
|
|
21
|
+
*
|
|
22
|
+
* A mouse resting on the island is replayed too. The browser sent its
|
|
23
|
+
* `mouseenter` events before anything listened, and sends no more until the
|
|
24
|
+
* pointer crosses another element, so the adopted tree is told what the
|
|
25
|
+
* pointer is already over: one `mouseenter` for each element under it, the
|
|
26
|
+
* outermost first, as the browser sends them.
|
|
27
|
+
*
|
|
28
|
+
* With `preload`, the island's code is fetched when the browser is idle or the island
|
|
29
|
+
* nears the viewport, so the first interaction only has to hydrate.
|
|
30
|
+
*
|
|
31
|
+
* Code that fails to load is asked for again on the next interaction. A component that
|
|
32
|
+
* fails to hydrate is not retried. Either way the island keeps its server markup, and
|
|
33
|
+
* the failure is raised as `astro:hydration-error`, as Astro's own directives do.
|
|
34
|
+
*/
|
|
35
|
+
const interaction = (load, options, island) => {
|
|
36
|
+
const root = island.querySelector(":scope > [data-effect-ui-root]");
|
|
37
|
+
const window = island.ownerDocument.defaultView;
|
|
38
|
+
const { hover = false, preload } = optionsOf(options.value);
|
|
39
|
+
if (!root || !window) {
|
|
40
|
+
load().then((hydrate) => hydrate());
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (armed.has(island) || !island.hasAttribute("ssr")) return;
|
|
44
|
+
armed.add(island);
|
|
45
|
+
const clicks = [];
|
|
46
|
+
let started = false;
|
|
47
|
+
let removed = false;
|
|
48
|
+
let attempts = 0;
|
|
49
|
+
let loading;
|
|
50
|
+
let cancelPreload = () => {};
|
|
51
|
+
let rest;
|
|
52
|
+
const stop = () => {
|
|
53
|
+
armed.delete(island);
|
|
54
|
+
cancelPreload();
|
|
55
|
+
for (const type of triggers) island.removeEventListener(type, start, true);
|
|
56
|
+
island.removeEventListener("pointerover", trackRest, true);
|
|
57
|
+
island.removeEventListener("pointerout", trackRest, true);
|
|
58
|
+
island.removeEventListener("click", captureClick, true);
|
|
59
|
+
island.removeEventListener("astro:unmount", onUnmount);
|
|
60
|
+
};
|
|
61
|
+
const onUnmount = () => {
|
|
62
|
+
removed = true;
|
|
63
|
+
stop();
|
|
64
|
+
};
|
|
65
|
+
const gone = () => removed || !island.isConnected;
|
|
66
|
+
const report = (detail) => {
|
|
67
|
+
const event = new window.CustomEvent("astro:hydration-error", {
|
|
68
|
+
cancelable: true,
|
|
69
|
+
bubbles: true,
|
|
70
|
+
composed: true,
|
|
71
|
+
detail: {
|
|
72
|
+
...detail,
|
|
73
|
+
componentUrl: island.getAttribute("component-url")
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (island.dispatchEvent(event)) console.error("Could not activate the Effect UI island.", detail.error);
|
|
77
|
+
};
|
|
78
|
+
/** The island's code, fetched once until a fetch fails. */
|
|
79
|
+
const fetchCode = () => loading ??= load();
|
|
80
|
+
const targetInsideRoot = (event) => {
|
|
81
|
+
const target = event.target;
|
|
82
|
+
return target instanceof window.Element && root.contains(target) && target.closest("astro-island") === island ? target : null;
|
|
83
|
+
};
|
|
84
|
+
const trackRest = (event) => {
|
|
85
|
+
if (!(event instanceof window.MouseEvent) || !("pointerType" in event) || event.pointerType !== "mouse") return;
|
|
86
|
+
const target = event.type === "pointerover" ? targetInsideRoot(event) : null;
|
|
87
|
+
rest = target === null ? void 0 : {
|
|
88
|
+
target,
|
|
89
|
+
x: event.clientX,
|
|
90
|
+
y: event.clientY
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const replayHover = () => {
|
|
94
|
+
if (rest === void 0 || !root.contains(rest.target)) return;
|
|
95
|
+
const entered = [];
|
|
96
|
+
for (let element = rest.target; element !== null && element !== root; element = element.parentElement) entered.unshift(element);
|
|
97
|
+
for (const element of entered) element.dispatchEvent(new window.MouseEvent("mouseenter", {
|
|
98
|
+
clientX: rest.x,
|
|
99
|
+
clientY: rest.y
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
102
|
+
const replayClicks = () => {
|
|
103
|
+
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));
|
|
104
|
+
};
|
|
105
|
+
/** The code did not arrive: wait for another interaction, up to the limit. */
|
|
106
|
+
const retryLater = () => {
|
|
107
|
+
loading = void 0;
|
|
108
|
+
clicks.length = 0;
|
|
109
|
+
started = false;
|
|
110
|
+
if (attempts >= ATTEMPTS) stop();
|
|
111
|
+
};
|
|
112
|
+
const hydrated = () => {
|
|
113
|
+
if (gone()) return;
|
|
114
|
+
if (island.hasAttribute("ssr")) return retryLater();
|
|
115
|
+
stop();
|
|
116
|
+
replayHover();
|
|
117
|
+
replayClicks();
|
|
118
|
+
};
|
|
119
|
+
const hydrateNow = () => fetchCode().then((hydrate) => gone() ? void 0 : Promise.resolve().then(hydrate).then(hydrated, (error) => {
|
|
120
|
+
stop();
|
|
121
|
+
report({ error });
|
|
122
|
+
}), (error) => {
|
|
123
|
+
if (!gone()) report({ error });
|
|
124
|
+
retryLater();
|
|
125
|
+
}).catch((error) => console.error("Could not activate the Effect UI island.", error));
|
|
126
|
+
const start = (event) => {
|
|
127
|
+
if (started || !targetInsideRoot(event)) return;
|
|
128
|
+
if (event.type === "pointerover" && (!hover || !("pointerType" in event) || event.pointerType !== "mouse")) return;
|
|
129
|
+
started = true;
|
|
130
|
+
attempts += 1;
|
|
131
|
+
hydrateNow();
|
|
132
|
+
};
|
|
133
|
+
const captureClick = (event) => {
|
|
134
|
+
const target = targetInsideRoot(event);
|
|
135
|
+
if (!target) return;
|
|
136
|
+
start(event);
|
|
137
|
+
if (target.closest("a[href], label, input[type=\"checkbox\"], input[type=\"radio\"]")) return;
|
|
138
|
+
event.preventDefault();
|
|
139
|
+
event.stopImmediatePropagation();
|
|
140
|
+
clicks.push({
|
|
141
|
+
target,
|
|
142
|
+
event
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
const preloadCode = () => {
|
|
146
|
+
cancelPreload = () => {};
|
|
147
|
+
if (!started) fetchCode().catch(() => loading = void 0);
|
|
148
|
+
};
|
|
149
|
+
island.addEventListener("pointerover", trackRest, true);
|
|
150
|
+
island.addEventListener("pointerout", trackRest, true);
|
|
151
|
+
for (const type of triggers) island.addEventListener(type, start, true);
|
|
152
|
+
island.addEventListener("click", captureClick, true);
|
|
153
|
+
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
154
|
+
if (preload === "idle") if ("requestIdleCallback" in window) {
|
|
155
|
+
const id = window.requestIdleCallback(preloadCode);
|
|
156
|
+
cancelPreload = () => window.cancelIdleCallback(id);
|
|
157
|
+
} else {
|
|
158
|
+
const id = setTimeout(preloadCode, 200);
|
|
159
|
+
cancelPreload = () => clearTimeout(id);
|
|
160
|
+
}
|
|
161
|
+
else if (preload === "visible" && "IntersectionObserver" in window) {
|
|
162
|
+
const observer = new window.IntersectionObserver((entries) => {
|
|
163
|
+
if (entries.some((entry) => entry.isIntersecting)) {
|
|
164
|
+
observer.disconnect();
|
|
165
|
+
preloadCode();
|
|
166
|
+
}
|
|
167
|
+
}, { rootMargin: "200px" });
|
|
168
|
+
for (const child of island.children) observer.observe(child);
|
|
169
|
+
cancelPreload = () => observer.disconnect();
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
//#endregion
|
|
173
|
+
export { interaction as default };
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AstroIntegration } from "astro";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type EffectUiOptions = {
|
|
5
|
+
readonly interaction?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Modules that describe what islands share, as paths from the project root. Each
|
|
8
|
+
* default-exports what `serverRuntime` or `clientRuntime` of `defineIslands` returns.
|
|
9
|
+
*/
|
|
10
|
+
readonly runtime?: {
|
|
11
|
+
readonly server?: string;
|
|
12
|
+
readonly client?: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
declare function effectUi({
|
|
16
|
+
interaction,
|
|
17
|
+
runtime
|
|
18
|
+
}?: EffectUiOptions): AstroIntegration;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { EffectUiOptions, effectUi as default };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const extension = import.meta.url.endsWith(".ts") ? "ts" : "mjs";
|
|
5
|
+
function entrypoint(name) {
|
|
6
|
+
return fileURLToPath(new URL(`./${name}.${extension}`, import.meta.url));
|
|
7
|
+
}
|
|
8
|
+
const factories = {
|
|
9
|
+
client: "makeClientRenderer",
|
|
10
|
+
server: "makeServerRenderer"
|
|
11
|
+
};
|
|
12
|
+
/** A path as an import specifier, whatever the platform's separator. */
|
|
13
|
+
const specifier = (path) => JSON.stringify(path.replaceAll("\\", "/"));
|
|
14
|
+
/**
|
|
15
|
+
* The renderer entry for one side when the app has a runtime module for it: this
|
|
16
|
+
* package's renderer, made with that module's default export.
|
|
17
|
+
*/
|
|
18
|
+
function entryWith(side, runtime) {
|
|
19
|
+
const factory = factories[side];
|
|
20
|
+
return `import { ${factory} } from ${specifier(entrypoint(side))};
|
|
21
|
+
import runtime from ${specifier(runtime)};
|
|
22
|
+
|
|
23
|
+
export default ${factory}(runtime);
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The `client:interaction` attribute, for Astro's type check. Astro 5, 6 and 7 all read
|
|
28
|
+
* custom directives from `Astro.ClientDirectives`.
|
|
29
|
+
*/
|
|
30
|
+
const directiveTypes = `declare namespace Astro {
|
|
31
|
+
interface ClientDirectives {
|
|
32
|
+
/**
|
|
33
|
+
* Hydrate when a reader first presses, focuses or types in the island and, with
|
|
34
|
+
* \`hover\`, when a mouse pointer enters it. \`preload\` fetches the island's code
|
|
35
|
+
* ahead of that, when the browser is idle or the island nears the viewport.
|
|
36
|
+
*/
|
|
37
|
+
'client:interaction'?:
|
|
38
|
+
| boolean
|
|
39
|
+
| { readonly hover?: boolean; readonly preload?: 'idle' | 'visible' };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
/**
|
|
44
|
+
* Effect and Effect UI are prebundled up front, so the dev server does not find them on
|
|
45
|
+
* the first island and reload the page, and are resolved once, so the renderer and the
|
|
46
|
+
* app's components share one copy of each.
|
|
47
|
+
*/
|
|
48
|
+
const vite = {
|
|
49
|
+
optimizeDeps: { include: [
|
|
50
|
+
"effect",
|
|
51
|
+
"@effect/ui",
|
|
52
|
+
"@effect/ui/html"
|
|
53
|
+
] },
|
|
54
|
+
resolve: { dedupe: ["effect", "@effect/ui"] }
|
|
55
|
+
};
|
|
56
|
+
function effectUi({ interaction = true, runtime = {} } = {}) {
|
|
57
|
+
return {
|
|
58
|
+
name: "astro-effect-ui",
|
|
59
|
+
hooks: {
|
|
60
|
+
"astro:config:done": ({ injectTypes }) => {
|
|
61
|
+
if (interaction) injectTypes({
|
|
62
|
+
filename: "client-directives.d.ts",
|
|
63
|
+
content: directiveTypes
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
"astro:config:setup": ({ addClientDirective, addRenderer, config, createCodegenDir, updateConfig }) => {
|
|
67
|
+
updateConfig({ vite });
|
|
68
|
+
if (interaction) addClientDirective({
|
|
69
|
+
name: "interaction",
|
|
70
|
+
entrypoint: entrypoint("directive-interaction")
|
|
71
|
+
});
|
|
72
|
+
const entryFor = (side) => {
|
|
73
|
+
const module = runtime[side];
|
|
74
|
+
if (module === void 0) return entrypoint(side);
|
|
75
|
+
const file = new URL(`${side}-entry.mjs`, createCodegenDir());
|
|
76
|
+
writeFileSync(file, entryWith(side, fileURLToPath(new URL(module, config.root))));
|
|
77
|
+
return fileURLToPath(file);
|
|
78
|
+
};
|
|
79
|
+
addRenderer({
|
|
80
|
+
name: "astro-effect-ui",
|
|
81
|
+
serverEntrypoint: entryFor("server"),
|
|
82
|
+
clientEntrypoint: entryFor("client")
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
export { effectUi as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opsydyn/astro-effect-ui",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-next.1",
|
|
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
|
+
"./runtime": {
|
|
17
|
+
"types": "./runtime.d.mts",
|
|
18
|
+
"import": "./runtime.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@effect/ui": "*",
|
|
23
|
+
"astro": ">=5.0.0 <8",
|
|
24
|
+
"effect": "~4.1.0"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.0.0"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/opsydyn/fold-kit-experiments.git",
|
|
32
|
+
"directory": "packages/astro-effect-ui"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"tag": "next"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/runtime.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Accepted, AstroComponent } from "./component.mjs";
|
|
2
|
+
import { Component } from "@effect/ui";
|
|
3
|
+
import { Duration, Effect, Layer } from "effect";
|
|
4
|
+
import { BrowserHost, Html } from "@effect/ui/html";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime.d.ts
|
|
7
|
+
/** An island whose component failed: as it started (`activate`) or later (`run`). */
|
|
8
|
+
type IslandFailure = Readonly<{
|
|
9
|
+
error: unknown;
|
|
10
|
+
phase: 'activate' | 'run';
|
|
11
|
+
}>;
|
|
12
|
+
/** What Effect UI shows in a root's place when the root fails, as `BrowserHost.mount` takes it. */
|
|
13
|
+
type IslandFallback = NonNullable<BrowserHost.MountOptions<Html.Elements>['fallback']>;
|
|
14
|
+
/** The Layer of a runtime: required as soon as the app's islands declare a service. */
|
|
15
|
+
type Wiring<Services> = [Services] extends [never] ? {
|
|
16
|
+
readonly layer?: Layer.Layer<never, unknown>;
|
|
17
|
+
} : {
|
|
18
|
+
readonly layer: Layer.Layer<Services, unknown>;
|
|
19
|
+
};
|
|
20
|
+
/** What every server render shares. */
|
|
21
|
+
type ServerRuntime<Services = never> = Wiring<Services> & Readonly<{
|
|
22
|
+
/** How long a render waits for Layers and queries. Effect UI's default is 500 ms. */budget?: Duration.Input;
|
|
23
|
+
}>;
|
|
24
|
+
/** What every island on a page shares. */
|
|
25
|
+
type ClientRuntime<Services = never> = Wiring<Services> & Readonly<{
|
|
26
|
+
/** Shown in place of an island that failed. Without it the island keeps its earlier markup. */fallback?: IslandFallback; /** Run in the page's runtime when an island fails, as well as the `effect-ui:error` event. */
|
|
27
|
+
onError?: (failure: IslandFailure) => Effect.Effect<void, never, Services>;
|
|
28
|
+
}>;
|
|
29
|
+
/** A server runtime as the renderer receives it: checked where it was defined. */
|
|
30
|
+
type AnyServerRuntime = ServerRuntime;
|
|
31
|
+
/** A client runtime as the renderer receives it, whatever services its hook uses. */
|
|
32
|
+
type AnyClientRuntime = Omit<ClientRuntime, 'onError'> & Readonly<{
|
|
33
|
+
onError?: (failure: IslandFailure) => Effect.Effect<void, never, unknown>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Names the services an app's islands may use, once. Both runtimes must then provide
|
|
37
|
+
* them, and `astroComponent` accepts a component only if they cover what it needs.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* export const islands = defineIslands<DeleteTodo | Flags>();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function defineIslands<Services = never>(): {
|
|
44
|
+
astroComponent: <C extends Component.Any>(component: C & Accepted<C, Services>) => AstroComponent<C>;
|
|
45
|
+
serverRuntime: (runtime: ServerRuntime<Services>) => ServerRuntime<Services>;
|
|
46
|
+
clientRuntime: (runtime: ClientRuntime<Services>) => ClientRuntime<Services>;
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { AnyClientRuntime, AnyServerRuntime, ClientRuntime, IslandFailure, IslandFallback, ServerRuntime, defineIslands };
|
package/runtime.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { toAstro } from "./component.mjs";
|
|
2
|
+
//#region src/runtime.ts
|
|
3
|
+
/**
|
|
4
|
+
* Names the services an app's islands may use, once. Both runtimes must then provide
|
|
5
|
+
* them, and `astroComponent` accepts a component only if they cover what it needs.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export const islands = defineIslands<DeleteTodo | Flags>();
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
function defineIslands() {
|
|
12
|
+
return {
|
|
13
|
+
astroComponent: (component) => toAstro(component),
|
|
14
|
+
serverRuntime: (runtime) => runtime,
|
|
15
|
+
clientRuntime: (runtime) => runtime
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { defineIslands };
|
package/server.d.mts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { IslandProps } from "./component.mjs";
|
|
2
|
+
import { AnyServerRuntime } from "./runtime.mjs";
|
|
3
|
+
import { Component } from "@effect/ui";
|
|
4
|
+
import { AstroComponentMetadata } from "astro";
|
|
5
|
+
|
|
6
|
+
//#region src/server.d.ts
|
|
7
|
+
type RenderContext = Readonly<{
|
|
8
|
+
result?: Readonly<{
|
|
9
|
+
createAstro?: (props: IslandProps, slots: null) => Readonly<{
|
|
10
|
+
isPrerendered: boolean;
|
|
11
|
+
}>;
|
|
12
|
+
}>;
|
|
13
|
+
}>;
|
|
14
|
+
/** The server renderer, with every render run in the app's runtime when it has one. */
|
|
15
|
+
declare function makeServerRenderer(runtime?: AnyServerRuntime): {
|
|
16
|
+
name: string;
|
|
17
|
+
supportsAstroStaticSlot: true;
|
|
18
|
+
check: (component: any) => Promise<boolean>;
|
|
19
|
+
renderToStaticMarkup(this: RenderContext | void, component: Component.Any, props: IslandProps, slots: Record<string, string>, metadata?: AstroComponentMetadata): Promise<{
|
|
20
|
+
html: string;
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
23
|
+
declare const _default: {
|
|
24
|
+
name: string;
|
|
25
|
+
supportsAstroStaticSlot: true;
|
|
26
|
+
check: (component: any) => Promise<boolean>;
|
|
27
|
+
renderToStaticMarkup(this: RenderContext | void, component: Component.Any, props: IslandProps, slots: Record<string, string>, metadata?: AstroComponentMetadata): Promise<{
|
|
28
|
+
html: string;
|
|
29
|
+
}>;
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { _default as default, makeServerRenderer };
|
package/server.mjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
+
import { n as fillMarkup, o as slotsOf, s as withSlots, t as deferredUnderSlots } from "./slots-ELUCsLv9.mjs";
|
|
3
|
+
import { Component } from "@effect/ui";
|
|
4
|
+
import { Effect, ManagedRuntime } from "effect";
|
|
5
|
+
import { HtmlServer } from "@effect/ui/html";
|
|
6
|
+
//#region src/server.ts
|
|
7
|
+
/** The server renderer, with every render run in the app's runtime when it has one. */
|
|
8
|
+
function makeServerRenderer(runtime = {}) {
|
|
9
|
+
const run = runtime.layer === void 0 ? Effect.runPromise : ManagedRuntime.make(runtime.layer).runPromise;
|
|
10
|
+
return {
|
|
11
|
+
name: "astro-effect-ui",
|
|
12
|
+
supportsAstroStaticSlot: true,
|
|
13
|
+
check: async (component) => Component.isComponent(component),
|
|
14
|
+
async renderToStaticMarkup(component, props, slots, metadata) {
|
|
15
|
+
const definition = asIslandComponent(component);
|
|
16
|
+
const name = component.definition.name;
|
|
17
|
+
const isPrerendered = this?.result?.createAstro?.({}, null)?.isPrerendered ?? false;
|
|
18
|
+
const hydrate = Boolean(metadata?.hydrate);
|
|
19
|
+
const filled = slotsOf(component, slots);
|
|
20
|
+
const rendered = await run((filled === void 0 ? HtmlServer.renderToHtml(definition, props, {
|
|
21
|
+
static: isPrerendered,
|
|
22
|
+
budget: runtime.budget
|
|
23
|
+
}) : HtmlServer.renderToHtml(withSlots(component, props, filled), {}, {
|
|
24
|
+
static: isPrerendered,
|
|
25
|
+
budget: runtime.budget
|
|
26
|
+
})).pipe(Effect.withSpan("astro-effect-ui.render", { attributes: { component: name } })));
|
|
27
|
+
const html = filled === void 0 ? rendered.html : fillMarkup(rendered.html, filled, hydrate);
|
|
28
|
+
if (!hydrate) {
|
|
29
|
+
if (filled === void 0 ? rendered.model === void 0 : deferredUnderSlots(rendered.state["~deferred"])) {
|
|
30
|
+
const message = `${metadata?.displayName ?? name} rendered nothing on the server and has no client directive.`;
|
|
31
|
+
if (isPrerendered) throw new IslandRendererError({ message });
|
|
32
|
+
await run(Effect.logError(`[astro-effect-ui] ${message}`));
|
|
33
|
+
}
|
|
34
|
+
return { html };
|
|
35
|
+
}
|
|
36
|
+
return { html: `<div data-effect-ui-root>${html}</div><script type="application/json" data-effect-ui-state>${HtmlServer.serializeState(rendered.state)}<\/script>` };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var server_default = makeServerRenderer();
|
|
41
|
+
//#endregion
|
|
42
|
+
export { server_default as default, makeServerRenderer };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { IslandRendererError } from "./component.mjs";
|
|
2
|
+
import { Component, Mount } from "@effect/ui";
|
|
3
|
+
import { Effect, Schema } from "effect";
|
|
4
|
+
import { Html } from "@effect/ui/html";
|
|
5
|
+
//#region src/slots.ts
|
|
6
|
+
/** The element Astro reads an island's slots back from. The default slot has no name. */
|
|
7
|
+
const SLOT = "astro-slot";
|
|
8
|
+
const DEFAULT = "default";
|
|
9
|
+
const placeholder = /<astro-slot(?: name="([^"]*)")?><\/astro-slot>/g;
|
|
10
|
+
/**
|
|
11
|
+
* The slots to fill: those with content that the component declares. Content for a slot
|
|
12
|
+
* it does not declare is an error, except Astro's `fallback`, which is not a slot of the
|
|
13
|
+
* component.
|
|
14
|
+
*/
|
|
15
|
+
function slotsOf(component, slots) {
|
|
16
|
+
const declared = component.definition.slots;
|
|
17
|
+
const given = Object.entries(slots).filter(([name, html]) => html.trim() !== "" && (declared.includes(name) || name !== "fallback"));
|
|
18
|
+
const unknown = given.find(([name]) => !declared.includes(name));
|
|
19
|
+
if (unknown !== void 0) {
|
|
20
|
+
const names = declared.length === 0 ? "none" : declared.join(", ");
|
|
21
|
+
throw new IslandRendererError({ message: `${component.definition.name} has no slot named "${unknown[0]}". It declares: ${names}.` });
|
|
22
|
+
}
|
|
23
|
+
return given.length === 0 ? void 0 : Object.fromEntries(given);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A root that renders the component with Astro's slots in place. A root has no parent to
|
|
27
|
+
* fill its slots, so this is that parent. Each slot is an empty `astro-slot` element:
|
|
28
|
+
* Effect UI has no node for markup it did not build, so the content is put in around it,
|
|
29
|
+
* by `fillMarkup` on the server and by `fill` in the browser.
|
|
30
|
+
*/
|
|
31
|
+
function withSlots(component, props, slots, { fill, fallback } = {}) {
|
|
32
|
+
const Fill = Mount.make("FillAstroSlot", { args: { name: Schema.String } }, (element, { name }) => Effect.sync(() => {
|
|
33
|
+
fill?.(element, name);
|
|
34
|
+
return Component.noop;
|
|
35
|
+
}));
|
|
36
|
+
const declare = component;
|
|
37
|
+
return Html.component({
|
|
38
|
+
name: "AstroIsland",
|
|
39
|
+
model: Schema.Null,
|
|
40
|
+
message: Schema.Never
|
|
41
|
+
}).make({
|
|
42
|
+
init: () => null,
|
|
43
|
+
update: ($) => $.model,
|
|
44
|
+
view: (h) => declare(h, {
|
|
45
|
+
props,
|
|
46
|
+
slots: Object.fromEntries(Object.keys(slots).map((name) => {
|
|
47
|
+
return [name, (name === DEFAULT ? h.el(SLOT) : h.el(SLOT).attr("name", name)).onMount(Fill({ name }))];
|
|
48
|
+
})),
|
|
49
|
+
fallback: (result) => result._tag === "Failure" && fallback !== void 0 ? fallback(result, h) : h.span.attr("hidden", "")
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Whether the component, rendered under `withSlots`, was left to the browser: the paths
|
|
55
|
+
* the server deferred include a child of the wrapper itself.
|
|
56
|
+
*/
|
|
57
|
+
function deferredUnderSlots(deferred) {
|
|
58
|
+
return (deferred ?? []).some((path) => path.split("/").length === 3);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Puts each slot's HTML where the server left its empty element. An island that will
|
|
62
|
+
* hydrate keeps the element, which is how Astro finds the slot again; a static one gets
|
|
63
|
+
* the content alone. The render holds no markup but Effect UI's own, so one pass cannot
|
|
64
|
+
* be misled by what a slot contains.
|
|
65
|
+
*/
|
|
66
|
+
function fillMarkup(html, slots, hydrate) {
|
|
67
|
+
return html.replace(placeholder, (empty, name) => {
|
|
68
|
+
const content = slots[name ?? DEFAULT];
|
|
69
|
+
if (content === void 0) return empty;
|
|
70
|
+
if (!hydrate) return content;
|
|
71
|
+
return name === void 0 ? `<${SLOT}>${content}</${SLOT}>` : `<${SLOT} name="${name}">${content}</${SLOT}>`;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const slotElements = (island) => [...island.querySelectorAll(SLOT)].filter((slot) => slot.closest("astro-island") === island);
|
|
75
|
+
const nameOf = (slot) => slot.getAttribute("name") ?? DEFAULT;
|
|
76
|
+
/**
|
|
77
|
+
* Keeps the nodes the server rendered into each slot. Hydration removes whatever a view
|
|
78
|
+
* does not describe, and a slot's content is exactly that, so the same nodes are handed
|
|
79
|
+
* back afterwards rather than parsed again.
|
|
80
|
+
*/
|
|
81
|
+
function keepSlotNodes(island) {
|
|
82
|
+
return new Map(slotElements(island).map((slot) => [nameOf(slot), [...slot.childNodes]]));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A `fill` for the browser. A slot's nodes are reused whenever they are free, so content
|
|
86
|
+
* survives hydration, and survives its slot being taken out of the view and placed again:
|
|
87
|
+
* an island nested in it keeps running. Only a slot never filled before is parsed.
|
|
88
|
+
*/
|
|
89
|
+
function slotFiller(slots, kept) {
|
|
90
|
+
return (element, name) => {
|
|
91
|
+
if (element.childNodes.length > 0) return;
|
|
92
|
+
const nodes = kept.get(name);
|
|
93
|
+
if (nodes !== void 0 && nodes.every((node) => node.parentNode?.isConnected !== true)) element.append(...nodes);
|
|
94
|
+
else {
|
|
95
|
+
element.innerHTML = slots[name] ?? "";
|
|
96
|
+
kept.set(name, [...element.childNodes]);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** Fills every slot element of the island that is still empty. */
|
|
101
|
+
function fillSlotElements(island, fill) {
|
|
102
|
+
for (const slot of slotElements(island)) fill(slot, nameOf(slot));
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { slotFiller as a, keepSlotNodes as i, fillMarkup as n, slotsOf as o, fillSlotElements as r, withSlots as s, deferredUnderSlots as t };
|
package/README.md
DELETED