@ox-content/islands 3.0.0-alpha.1 → 3.0.0-alpha.10
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/dist/index.d.mts +16 -1
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -2
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +1 -260
- package/dist/runtime2.mjs +299 -0
- package/dist/runtime2.mjs.map +1 -0
- package/package.json +1 -1
- package/dist/runtime.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
1
|
import { a as HydrateFunction, c as IslandController, i as ComponentRegistry, l as IslandInstance, n as initIslands, o as InitIslandsOptions, r as isIslandsSupported, s as IslandConfig, t as createDeferredInit, u as LoadStrategy } from "./runtime.mjs";
|
|
2
|
-
|
|
2
|
+
//#region src/payload.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Flatten the Rust MDX island payload (`{ props, expressions, spreads }`)
|
|
5
|
+
* into the literal props object hydrate functions expect. Legacy flat
|
|
6
|
+
* `data-ox-props` objects from the Markdown regex path are returned as-is.
|
|
7
|
+
*/
|
|
8
|
+
declare function unwrapIslandProps(parsed: unknown): Record<string, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Slot markup for an island: `data-ox-content` when the regex path set it,
|
|
11
|
+
* otherwise inner HTML with the Rust JSON payload script stripped.
|
|
12
|
+
*/
|
|
13
|
+
declare function stripIslandPayloadScript(innerHTML: string): string;
|
|
14
|
+
declare function readIslandSlotHtml(element: Pick<HTMLElement, "dataset" | "innerHTML">): string;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { type ComponentRegistry, type HydrateFunction, type InitIslandsOptions, type IslandConfig, type IslandController, type IslandInstance, type LoadStrategy, createDeferredInit, initIslands, isIslandsSupported, readIslandSlotHtml, stripIslandPayloadScript, unwrapIslandProps };
|
|
17
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/payload.ts"],"mappings":";;;;;;;iBAQgB,kBAAkB,kBAAkB;;;;;iBAwBpC,yBAAyB;iBAIzB,mBAAmB,SAAS,KAAK"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { createDeferredInit, initIslands, isIslandsSupported };
|
|
1
|
+
import { a as stripIslandPayloadScript, i as readIslandSlotHtml, n as initIslands, o as unwrapIslandProps, r as isIslandsSupported, t as createDeferredInit } from "./runtime2.mjs";
|
|
2
|
+
export { createDeferredInit, initIslands, isIslandsSupported, readIslandSlotHtml, stripIslandPayloadScript, unwrapIslandProps };
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/types.ts","../src/runtime.ts"],"mappings":";;;;;;;;;;;;;;KAcY;;;;UAKK;;EAEf;;EAEA;;EAEA,MAAM;;EAEN;;EAEA,OAAO;;;;;;;;KASG,mBACV,SAAS,aACT,OAAO;;;;KAMG,oBAAoB,YAAY;;;;UAK3B;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,kBAAkB,SAAS,aAAa,QAAQ;;EAEhD,gBAAgB,SAAS,aAAa,QAAQ;;EAE9C,kBAAkB,SAAS,aAAa,QAAQ,cAAc,OAAO;;;;;UAMtD;EACf,SAAS;EACT,QAAQ;EACR;EACA;;;;;UAMe;;EAEf,WAAW;;EAEX,UAAU,SAAS;;EAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/types.ts","../src/runtime.ts"],"mappings":";;;;;;;;;;;;;;KAcY;;;;UAKK;;EAEf;;EAEA;;EAEA,MAAM;;EAEN;;EAEA,OAAO;;;;;;;;KASG,mBACV,SAAS,aACT,OAAO;;;;KAMG,oBAAoB,YAAY;;;;UAK3B;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,kBAAkB,SAAS,aAAa,QAAQ;;EAEhD,gBAAgB,SAAS,aAAa,QAAQ;;EAE9C,kBAAkB,SAAS,aAAa,QAAQ,cAAc,OAAO;;;;;UAMtD;EACf,SAAS;EACT,QAAQ;EACR;EACA;;;;;UAMe;;EAEf,WAAW;;EAEX,UAAU,SAAS;;EAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCgGc,YACd,SAAS,iBACT,UAAU,qBACT;;;;;;;;;;;;;;;;;;;iBAyJa,mBACd,UAAU,sBACR,SAAS,oBAAoB;;;;iBAOjB"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -1,261 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
const defaultOptions = {
|
|
3
|
-
rootMargin: "200px",
|
|
4
|
-
threshold: 0,
|
|
5
|
-
idleTimeout: 200,
|
|
6
|
-
selector: "[data-ox-island]"
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Parse island configuration from element attributes.
|
|
10
|
-
*/
|
|
11
|
-
function parseIslandConfig(element) {
|
|
12
|
-
const component = element.dataset.oxIsland || "";
|
|
13
|
-
const load = element.dataset.oxLoad || "eager";
|
|
14
|
-
const mediaQuery = element.dataset.oxMedia;
|
|
15
|
-
let props = {};
|
|
16
|
-
try {
|
|
17
|
-
const propsJson = element.dataset.oxProps;
|
|
18
|
-
if (propsJson) props = JSON.parse(propsJson);
|
|
19
|
-
} catch (e) {
|
|
20
|
-
console.warn("[ox-islands] Failed to parse props for", element, e);
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
id: element.id || `island-${Math.random().toString(36).slice(2, 9)}`,
|
|
24
|
-
component,
|
|
25
|
-
load,
|
|
26
|
-
mediaQuery,
|
|
27
|
-
props
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Observe element visibility using IntersectionObserver.
|
|
32
|
-
*/
|
|
33
|
-
function observeVisibility(element, callback, options) {
|
|
34
|
-
const observer = new IntersectionObserver((entries) => {
|
|
35
|
-
if (entries[0].isIntersecting) {
|
|
36
|
-
observer.disconnect();
|
|
37
|
-
callback();
|
|
38
|
-
}
|
|
39
|
-
}, {
|
|
40
|
-
rootMargin: options.rootMargin,
|
|
41
|
-
threshold: options.threshold
|
|
42
|
-
});
|
|
43
|
-
observer.observe(element);
|
|
44
|
-
return () => observer.disconnect();
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Observe media query changes.
|
|
48
|
-
*/
|
|
49
|
-
function observeMedia(query, callback) {
|
|
50
|
-
const mql = matchMedia(query);
|
|
51
|
-
if (mql.matches) {
|
|
52
|
-
callback();
|
|
53
|
-
return () => {};
|
|
54
|
-
}
|
|
55
|
-
const handler = () => {
|
|
56
|
-
if (mql.matches) {
|
|
57
|
-
mql.removeEventListener("change", handler);
|
|
58
|
-
callback();
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
mql.addEventListener("change", handler);
|
|
62
|
-
return () => mql.removeEventListener("change", handler);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Schedule callback for browser idle time.
|
|
66
|
-
*/
|
|
67
|
-
function scheduleIdle(callback, timeout) {
|
|
68
|
-
if ("requestIdleCallback" in window) {
|
|
69
|
-
const id = requestIdleCallback(callback, { timeout });
|
|
70
|
-
return () => cancelIdleCallback(id);
|
|
71
|
-
}
|
|
72
|
-
const id = setTimeout(callback, timeout);
|
|
73
|
-
return () => clearTimeout(id);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Initialize islands with a hydration function.
|
|
77
|
-
*
|
|
78
|
-
* This is the main entry point for the island system.
|
|
79
|
-
* Pass a hydrate function that knows how to mount your components.
|
|
80
|
-
*
|
|
81
|
-
* @example Vue
|
|
82
|
-
* ```ts
|
|
83
|
-
* import { initIslands } from '@ox-content/islands';
|
|
84
|
-
* import { createApp, h } from 'vue';
|
|
85
|
-
* import Counter from './Counter.vue';
|
|
86
|
-
*
|
|
87
|
-
* const components = { Counter };
|
|
88
|
-
*
|
|
89
|
-
* initIslands((el, props) => {
|
|
90
|
-
* const name = el.dataset.oxIsland!;
|
|
91
|
-
* const Component = components[name];
|
|
92
|
-
* if (!Component) return;
|
|
93
|
-
*
|
|
94
|
-
* const app = createApp({ render: () => h(Component, props) });
|
|
95
|
-
* app.mount(el);
|
|
96
|
-
*
|
|
97
|
-
* return () => app.unmount();
|
|
98
|
-
* });
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* @example React
|
|
102
|
-
* ```ts
|
|
103
|
-
* import { initIslands } from '@ox-content/islands';
|
|
104
|
-
* import { createRoot } from 'react-dom/client';
|
|
105
|
-
* import Counter from './Counter';
|
|
106
|
-
*
|
|
107
|
-
* const components = { Counter };
|
|
108
|
-
*
|
|
109
|
-
* initIslands((el, props) => {
|
|
110
|
-
* const name = el.dataset.oxIsland!;
|
|
111
|
-
* const Component = components[name];
|
|
112
|
-
* if (!Component) return;
|
|
113
|
-
*
|
|
114
|
-
* const root = createRoot(el);
|
|
115
|
-
* root.render(<Component {...props} />);
|
|
116
|
-
*
|
|
117
|
-
* return () => root.unmount();
|
|
118
|
-
* });
|
|
119
|
-
* ```
|
|
120
|
-
*
|
|
121
|
-
* @example Vanilla JS
|
|
122
|
-
* ```ts
|
|
123
|
-
* import { initIslands } from '@ox-content/islands';
|
|
124
|
-
*
|
|
125
|
-
* initIslands((el, props) => {
|
|
126
|
-
* const name = el.dataset.oxIsland!;
|
|
127
|
-
*
|
|
128
|
-
* if (name === 'Counter') {
|
|
129
|
-
* let count = props.initial || 0;
|
|
130
|
-
* const button = el.querySelector('button')!;
|
|
131
|
-
* const handler = () => {
|
|
132
|
-
* count++;
|
|
133
|
-
* button.textContent = String(count);
|
|
134
|
-
* };
|
|
135
|
-
* button.addEventListener('click', handler);
|
|
136
|
-
* return () => button.removeEventListener('click', handler);
|
|
137
|
-
* }
|
|
138
|
-
* });
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
function initIslands(hydrate, options) {
|
|
142
|
-
const opts = {
|
|
143
|
-
...defaultOptions,
|
|
144
|
-
...options
|
|
145
|
-
};
|
|
146
|
-
const instances = [];
|
|
147
|
-
const cleanups = [];
|
|
148
|
-
/**
|
|
149
|
-
* Hydrate a single island element.
|
|
150
|
-
*/
|
|
151
|
-
function hydrateIsland(element, config) {
|
|
152
|
-
const instance = instances.find((i) => i.element === element);
|
|
153
|
-
if (instance?.hydrated) return;
|
|
154
|
-
try {
|
|
155
|
-
opts.onHydrateStart?.(element, config);
|
|
156
|
-
element.classList.add("ox-island-loading");
|
|
157
|
-
const cleanup = hydrate(element, config.props);
|
|
158
|
-
element.dataset.oxHydrated = "true";
|
|
159
|
-
element.classList.remove("ox-island-loading");
|
|
160
|
-
if (instance) {
|
|
161
|
-
instance.cleanup = cleanup || void 0;
|
|
162
|
-
instance.hydrated = true;
|
|
163
|
-
} else instances.push({
|
|
164
|
-
element,
|
|
165
|
-
config,
|
|
166
|
-
cleanup: cleanup || void 0,
|
|
167
|
-
hydrated: true
|
|
168
|
-
});
|
|
169
|
-
opts.onHydrateEnd?.(element, config);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
element.classList.remove("ox-island-loading");
|
|
172
|
-
element.classList.add("ox-island-error");
|
|
173
|
-
element.dataset.oxError = error instanceof Error ? error.message : String(error);
|
|
174
|
-
opts.onHydrateError?.(element, config, error instanceof Error ? error : new Error(String(error)));
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Schedule hydration based on load strategy.
|
|
179
|
-
*/
|
|
180
|
-
function scheduleHydration(element, config) {
|
|
181
|
-
switch (config.load) {
|
|
182
|
-
case "eager":
|
|
183
|
-
hydrateIsland(element, config);
|
|
184
|
-
break;
|
|
185
|
-
case "idle": {
|
|
186
|
-
const cancel = scheduleIdle(() => hydrateIsland(element, config), opts.idleTimeout);
|
|
187
|
-
cleanups.push(cancel);
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
case "visible": {
|
|
191
|
-
const cancel = observeVisibility(element, () => hydrateIsland(element, config), {
|
|
192
|
-
rootMargin: opts.rootMargin,
|
|
193
|
-
threshold: opts.threshold
|
|
194
|
-
});
|
|
195
|
-
cleanups.push(cancel);
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
case "media":
|
|
199
|
-
if (config.mediaQuery) {
|
|
200
|
-
const cancel = observeMedia(config.mediaQuery, () => hydrateIsland(element, config));
|
|
201
|
-
cleanups.push(cancel);
|
|
202
|
-
} else hydrateIsland(element, config);
|
|
203
|
-
break;
|
|
204
|
-
default: hydrateIsland(element, config);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
document.querySelectorAll(opts.selector).forEach((element) => {
|
|
208
|
-
const config = parseIslandConfig(element);
|
|
209
|
-
instances.push({
|
|
210
|
-
element,
|
|
211
|
-
config,
|
|
212
|
-
hydrated: false
|
|
213
|
-
});
|
|
214
|
-
scheduleHydration(element, config);
|
|
215
|
-
});
|
|
216
|
-
return {
|
|
217
|
-
instances,
|
|
218
|
-
hydrate(element) {
|
|
219
|
-
hydrateIsland(element, parseIslandConfig(element));
|
|
220
|
-
},
|
|
221
|
-
destroy() {
|
|
222
|
-
cleanups.forEach((cleanup) => cleanup());
|
|
223
|
-
cleanups.length = 0;
|
|
224
|
-
instances.forEach((instance) => {
|
|
225
|
-
if (instance.cleanup) instance.cleanup();
|
|
226
|
-
});
|
|
227
|
-
instances.length = 0;
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Create a deferred hydration wrapper.
|
|
233
|
-
*
|
|
234
|
-
* Returns a function that can be called to hydrate islands later.
|
|
235
|
-
* Useful for frameworks that need to register components first.
|
|
236
|
-
*
|
|
237
|
-
* @example
|
|
238
|
-
* ```ts
|
|
239
|
-
* const deferredInit = createDeferredInit();
|
|
240
|
-
*
|
|
241
|
-
* // Later, after components are ready
|
|
242
|
-
* const components = await loadComponents();
|
|
243
|
-
* deferredInit((el, props) => {
|
|
244
|
-
* const Component = components[el.dataset.oxIsland!];
|
|
245
|
-
* // ...
|
|
246
|
-
* });
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
function createDeferredInit(options) {
|
|
250
|
-
return (hydrate) => initIslands(hydrate, options);
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Check if islands are supported in the current environment.
|
|
254
|
-
*/
|
|
255
|
-
function isIslandsSupported() {
|
|
256
|
-
return typeof window !== "undefined" && typeof document !== "undefined" && typeof IntersectionObserver !== "undefined" && typeof MutationObserver !== "undefined";
|
|
257
|
-
}
|
|
258
|
-
//#endregion
|
|
1
|
+
import { n as initIslands, r as isIslandsSupported, t as createDeferredInit } from "./runtime2.mjs";
|
|
259
2
|
export { createDeferredInit, initIslands, isIslandsSupported };
|
|
260
|
-
|
|
261
|
-
//# sourceMappingURL=runtime.mjs.map
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
//#region src/payload.ts
|
|
2
|
+
const RUST_PAYLOAD_KEYS = /* @__PURE__ */ new Set([
|
|
3
|
+
"props",
|
|
4
|
+
"expressions",
|
|
5
|
+
"spreads"
|
|
6
|
+
]);
|
|
7
|
+
const ISLAND_JSON_SCRIPT = /^\s*<script type="application\/json">[\s\S]*?<\/script>/;
|
|
8
|
+
/**
|
|
9
|
+
* Flatten the Rust MDX island payload (`{ props, expressions, spreads }`)
|
|
10
|
+
* into the literal props object hydrate functions expect. Legacy flat
|
|
11
|
+
* `data-ox-props` objects from the Markdown regex path are returned as-is.
|
|
12
|
+
*/
|
|
13
|
+
function unwrapIslandProps(parsed) {
|
|
14
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
|
|
15
|
+
const record = parsed;
|
|
16
|
+
const keys = Object.keys(record);
|
|
17
|
+
if (keys.length > 0 && keys.every((key) => RUST_PAYLOAD_KEYS.has(key)) && record.props && typeof record.props === "object" && !Array.isArray(record.props)) return record.props;
|
|
18
|
+
return record;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Slot markup for an island: `data-ox-content` when the regex path set it,
|
|
22
|
+
* otherwise inner HTML with the Rust JSON payload script stripped.
|
|
23
|
+
*/
|
|
24
|
+
function stripIslandPayloadScript(innerHTML) {
|
|
25
|
+
return innerHTML.replace(ISLAND_JSON_SCRIPT, "");
|
|
26
|
+
}
|
|
27
|
+
function readIslandSlotHtml(element) {
|
|
28
|
+
const fromAttr = element.dataset.oxContent;
|
|
29
|
+
if (fromAttr) return fromAttr;
|
|
30
|
+
return stripIslandPayloadScript(element.innerHTML);
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/runtime.ts
|
|
34
|
+
/**
|
|
35
|
+
* Island Architecture Runtime
|
|
36
|
+
*
|
|
37
|
+
* Framework-agnostic Island controller using pure Vanilla JavaScript.
|
|
38
|
+
* No framework dependencies - works with Vue, React, Svelte, or plain JS.
|
|
39
|
+
*/
|
|
40
|
+
const defaultOptions = {
|
|
41
|
+
rootMargin: "200px",
|
|
42
|
+
threshold: 0,
|
|
43
|
+
idleTimeout: 200,
|
|
44
|
+
selector: "[data-ox-island]"
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Parse island configuration from element attributes.
|
|
48
|
+
*/
|
|
49
|
+
function parseIslandConfig(element) {
|
|
50
|
+
const component = element.dataset.oxIsland || "";
|
|
51
|
+
const load = element.dataset.oxLoad || "eager";
|
|
52
|
+
const mediaQuery = element.dataset.oxMedia;
|
|
53
|
+
let props = {};
|
|
54
|
+
try {
|
|
55
|
+
const propsJson = element.dataset.oxProps;
|
|
56
|
+
if (propsJson) props = unwrapIslandProps(JSON.parse(propsJson));
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.warn("[ox-islands] Failed to parse props for", element, e);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
id: element.id || `island-${Math.random().toString(36).slice(2, 9)}`,
|
|
62
|
+
component,
|
|
63
|
+
load,
|
|
64
|
+
mediaQuery,
|
|
65
|
+
props
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Observe element visibility using IntersectionObserver.
|
|
70
|
+
*/
|
|
71
|
+
function observeVisibility(element, callback, options) {
|
|
72
|
+
const observer = new IntersectionObserver((entries) => {
|
|
73
|
+
if (entries[0].isIntersecting) {
|
|
74
|
+
observer.disconnect();
|
|
75
|
+
callback();
|
|
76
|
+
}
|
|
77
|
+
}, {
|
|
78
|
+
rootMargin: options.rootMargin,
|
|
79
|
+
threshold: options.threshold
|
|
80
|
+
});
|
|
81
|
+
observer.observe(element);
|
|
82
|
+
return () => observer.disconnect();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Observe media query changes.
|
|
86
|
+
*/
|
|
87
|
+
function observeMedia(query, callback) {
|
|
88
|
+
const mql = matchMedia(query);
|
|
89
|
+
if (mql.matches) {
|
|
90
|
+
callback();
|
|
91
|
+
return () => {};
|
|
92
|
+
}
|
|
93
|
+
const handler = () => {
|
|
94
|
+
if (mql.matches) {
|
|
95
|
+
mql.removeEventListener("change", handler);
|
|
96
|
+
callback();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
mql.addEventListener("change", handler);
|
|
100
|
+
return () => mql.removeEventListener("change", handler);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Schedule callback for browser idle time.
|
|
104
|
+
*/
|
|
105
|
+
function scheduleIdle(callback, timeout) {
|
|
106
|
+
if ("requestIdleCallback" in window) {
|
|
107
|
+
const id = requestIdleCallback(callback, { timeout });
|
|
108
|
+
return () => cancelIdleCallback(id);
|
|
109
|
+
}
|
|
110
|
+
const id = setTimeout(callback, timeout);
|
|
111
|
+
return () => clearTimeout(id);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Initialize islands with a hydration function.
|
|
115
|
+
*
|
|
116
|
+
* This is the main entry point for the island system.
|
|
117
|
+
* Pass a hydrate function that knows how to mount your components.
|
|
118
|
+
*
|
|
119
|
+
* @example Vue
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { initIslands } from '@ox-content/islands';
|
|
122
|
+
* import { createApp, h } from 'vue';
|
|
123
|
+
* import Counter from './Counter.vue';
|
|
124
|
+
*
|
|
125
|
+
* const components = { Counter };
|
|
126
|
+
*
|
|
127
|
+
* initIslands((el, props) => {
|
|
128
|
+
* const name = el.dataset.oxIsland!;
|
|
129
|
+
* const Component = components[name];
|
|
130
|
+
* if (!Component) return;
|
|
131
|
+
*
|
|
132
|
+
* const app = createApp({ render: () => h(Component, props) });
|
|
133
|
+
* app.mount(el);
|
|
134
|
+
*
|
|
135
|
+
* return () => app.unmount();
|
|
136
|
+
* });
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @example React
|
|
140
|
+
* ```ts
|
|
141
|
+
* import { initIslands } from '@ox-content/islands';
|
|
142
|
+
* import { createRoot } from 'react-dom/client';
|
|
143
|
+
* import Counter from './Counter';
|
|
144
|
+
*
|
|
145
|
+
* const components = { Counter };
|
|
146
|
+
*
|
|
147
|
+
* initIslands((el, props) => {
|
|
148
|
+
* const name = el.dataset.oxIsland!;
|
|
149
|
+
* const Component = components[name];
|
|
150
|
+
* if (!Component) return;
|
|
151
|
+
*
|
|
152
|
+
* const root = createRoot(el);
|
|
153
|
+
* root.render(<Component {...props} />);
|
|
154
|
+
*
|
|
155
|
+
* return () => root.unmount();
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @example Vanilla JS
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { initIslands } from '@ox-content/islands';
|
|
162
|
+
*
|
|
163
|
+
* initIslands((el, props) => {
|
|
164
|
+
* const name = el.dataset.oxIsland!;
|
|
165
|
+
*
|
|
166
|
+
* if (name === 'Counter') {
|
|
167
|
+
* let count = props.initial || 0;
|
|
168
|
+
* const button = el.querySelector('button')!;
|
|
169
|
+
* const handler = () => {
|
|
170
|
+
* count++;
|
|
171
|
+
* button.textContent = String(count);
|
|
172
|
+
* };
|
|
173
|
+
* button.addEventListener('click', handler);
|
|
174
|
+
* return () => button.removeEventListener('click', handler);
|
|
175
|
+
* }
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function initIslands(hydrate, options) {
|
|
180
|
+
const opts = {
|
|
181
|
+
...defaultOptions,
|
|
182
|
+
...options
|
|
183
|
+
};
|
|
184
|
+
const instances = [];
|
|
185
|
+
const cleanups = [];
|
|
186
|
+
/**
|
|
187
|
+
* Hydrate a single island element.
|
|
188
|
+
*/
|
|
189
|
+
function hydrateIsland(element, config) {
|
|
190
|
+
const instance = instances.find((i) => i.element === element);
|
|
191
|
+
if (instance?.hydrated) return;
|
|
192
|
+
try {
|
|
193
|
+
opts.onHydrateStart?.(element, config);
|
|
194
|
+
element.classList.add("ox-island-loading");
|
|
195
|
+
const cleanup = hydrate(element, config.props);
|
|
196
|
+
element.dataset.oxHydrated = "true";
|
|
197
|
+
element.classList.remove("ox-island-loading");
|
|
198
|
+
if (instance) {
|
|
199
|
+
instance.cleanup = cleanup || void 0;
|
|
200
|
+
instance.hydrated = true;
|
|
201
|
+
} else instances.push({
|
|
202
|
+
element,
|
|
203
|
+
config,
|
|
204
|
+
cleanup: cleanup || void 0,
|
|
205
|
+
hydrated: true
|
|
206
|
+
});
|
|
207
|
+
opts.onHydrateEnd?.(element, config);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
element.classList.remove("ox-island-loading");
|
|
210
|
+
element.classList.add("ox-island-error");
|
|
211
|
+
element.dataset.oxError = error instanceof Error ? error.message : String(error);
|
|
212
|
+
opts.onHydrateError?.(element, config, error instanceof Error ? error : new Error(String(error)));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Schedule hydration based on load strategy.
|
|
217
|
+
*/
|
|
218
|
+
function scheduleHydration(element, config) {
|
|
219
|
+
switch (config.load) {
|
|
220
|
+
case "eager":
|
|
221
|
+
hydrateIsland(element, config);
|
|
222
|
+
break;
|
|
223
|
+
case "idle": {
|
|
224
|
+
const cancel = scheduleIdle(() => hydrateIsland(element, config), opts.idleTimeout);
|
|
225
|
+
cleanups.push(cancel);
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
case "visible": {
|
|
229
|
+
const cancel = observeVisibility(element, () => hydrateIsland(element, config), {
|
|
230
|
+
rootMargin: opts.rootMargin,
|
|
231
|
+
threshold: opts.threshold
|
|
232
|
+
});
|
|
233
|
+
cleanups.push(cancel);
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case "media":
|
|
237
|
+
if (config.mediaQuery) {
|
|
238
|
+
const cancel = observeMedia(config.mediaQuery, () => hydrateIsland(element, config));
|
|
239
|
+
cleanups.push(cancel);
|
|
240
|
+
} else hydrateIsland(element, config);
|
|
241
|
+
break;
|
|
242
|
+
default: hydrateIsland(element, config);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
document.querySelectorAll(opts.selector).forEach((element) => {
|
|
246
|
+
const config = parseIslandConfig(element);
|
|
247
|
+
instances.push({
|
|
248
|
+
element,
|
|
249
|
+
config,
|
|
250
|
+
hydrated: false
|
|
251
|
+
});
|
|
252
|
+
scheduleHydration(element, config);
|
|
253
|
+
});
|
|
254
|
+
return {
|
|
255
|
+
instances,
|
|
256
|
+
hydrate(element) {
|
|
257
|
+
hydrateIsland(element, parseIslandConfig(element));
|
|
258
|
+
},
|
|
259
|
+
destroy() {
|
|
260
|
+
cleanups.forEach((cleanup) => cleanup());
|
|
261
|
+
cleanups.length = 0;
|
|
262
|
+
instances.forEach((instance) => {
|
|
263
|
+
if (instance.cleanup) instance.cleanup();
|
|
264
|
+
});
|
|
265
|
+
instances.length = 0;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Create a deferred hydration wrapper.
|
|
271
|
+
*
|
|
272
|
+
* Returns a function that can be called to hydrate islands later.
|
|
273
|
+
* Useful for frameworks that need to register components first.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* const deferredInit = createDeferredInit();
|
|
278
|
+
*
|
|
279
|
+
* // Later, after components are ready
|
|
280
|
+
* const components = await loadComponents();
|
|
281
|
+
* deferredInit((el, props) => {
|
|
282
|
+
* const Component = components[el.dataset.oxIsland!];
|
|
283
|
+
* // ...
|
|
284
|
+
* });
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
function createDeferredInit(options) {
|
|
288
|
+
return (hydrate) => initIslands(hydrate, options);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Check if islands are supported in the current environment.
|
|
292
|
+
*/
|
|
293
|
+
function isIslandsSupported() {
|
|
294
|
+
return typeof window !== "undefined" && typeof document !== "undefined" && typeof IntersectionObserver !== "undefined" && typeof MutationObserver !== "undefined";
|
|
295
|
+
}
|
|
296
|
+
//#endregion
|
|
297
|
+
export { stripIslandPayloadScript as a, readIslandSlotHtml as i, initIslands as n, unwrapIslandProps as o, isIslandsSupported as r, createDeferredInit as t };
|
|
298
|
+
|
|
299
|
+
//# sourceMappingURL=runtime2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime2.mjs","names":[],"sources":["../src/payload.ts","../src/runtime.ts"],"sourcesContent":["const RUST_PAYLOAD_KEYS = new Set([\"props\", \"expressions\", \"spreads\"]);\nconst ISLAND_JSON_SCRIPT = /^\\s*<script type=\"application\\/json\">[\\s\\S]*?<\\/script>/;\n\n/**\n * Flatten the Rust MDX island payload (`{ props, expressions, spreads }`)\n * into the literal props object hydrate functions expect. Legacy flat\n * `data-ox-props` objects from the Markdown regex path are returned as-is.\n */\nexport function unwrapIslandProps(parsed: unknown): Record<string, unknown> {\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return {};\n }\n\n const record = parsed as Record<string, unknown>;\n const keys = Object.keys(record);\n if (\n keys.length > 0 &&\n keys.every((key) => RUST_PAYLOAD_KEYS.has(key)) &&\n record.props &&\n typeof record.props === \"object\" &&\n !Array.isArray(record.props)\n ) {\n return record.props as Record<string, unknown>;\n }\n\n return record;\n}\n\n/**\n * Slot markup for an island: `data-ox-content` when the regex path set it,\n * otherwise inner HTML with the Rust JSON payload script stripped.\n */\nexport function stripIslandPayloadScript(innerHTML: string): string {\n return innerHTML.replace(ISLAND_JSON_SCRIPT, \"\");\n}\n\nexport function readIslandSlotHtml(element: Pick<HTMLElement, \"dataset\" | \"innerHTML\">): string {\n const fromAttr = element.dataset.oxContent;\n if (fromAttr) return fromAttr;\n return stripIslandPayloadScript(element.innerHTML);\n}\n","/**\n * Island Architecture Runtime\n *\n * Framework-agnostic Island controller using pure Vanilla JavaScript.\n * No framework dependencies - works with Vue, React, Svelte, or plain JS.\n */\n\nimport { unwrapIslandProps } from \"./payload\";\nimport type {\n HydrateFunction,\n InitIslandsOptions,\n IslandConfig,\n IslandInstance,\n IslandController,\n LoadStrategy,\n} from \"./types\";\n\nconst defaultOptions: Required<\n Omit<InitIslandsOptions, \"onHydrateStart\" | \"onHydrateEnd\" | \"onHydrateError\">\n> &\n InitIslandsOptions = {\n rootMargin: \"200px\",\n threshold: 0,\n idleTimeout: 200,\n selector: \"[data-ox-island]\",\n};\n\n/**\n * Parse island configuration from element attributes.\n */\nfunction parseIslandConfig(element: HTMLElement): IslandConfig {\n const component = element.dataset.oxIsland || \"\";\n const load = (element.dataset.oxLoad as LoadStrategy) || \"eager\";\n const mediaQuery = element.dataset.oxMedia;\n\n let props: Record<string, unknown> = {};\n try {\n const propsJson = element.dataset.oxProps;\n if (propsJson) {\n props = unwrapIslandProps(JSON.parse(propsJson));\n }\n } catch (e) {\n console.warn(\"[ox-islands] Failed to parse props for\", element, e);\n }\n\n return {\n id: element.id || `island-${Math.random().toString(36).slice(2, 9)}`,\n component,\n load,\n mediaQuery,\n props,\n };\n}\n\n/**\n * Observe element visibility using IntersectionObserver.\n */\nfunction observeVisibility(\n element: HTMLElement,\n callback: () => void,\n options: { rootMargin: string; threshold: number },\n): () => void {\n const observer = new IntersectionObserver(\n (entries) => {\n if (entries[0].isIntersecting) {\n observer.disconnect();\n callback();\n }\n },\n {\n rootMargin: options.rootMargin,\n threshold: options.threshold,\n },\n );\n\n observer.observe(element);\n\n return () => observer.disconnect();\n}\n\n/**\n * Observe media query changes.\n */\nfunction observeMedia(query: string, callback: () => void): () => void {\n const mql = matchMedia(query);\n\n if (mql.matches) {\n callback();\n return () => {};\n }\n\n const handler = () => {\n if (mql.matches) {\n mql.removeEventListener(\"change\", handler);\n callback();\n }\n };\n\n mql.addEventListener(\"change\", handler);\n\n return () => mql.removeEventListener(\"change\", handler);\n}\n\n/**\n * Schedule callback for browser idle time.\n */\nfunction scheduleIdle(callback: () => void, timeout: number): () => void {\n if (\"requestIdleCallback\" in window) {\n const id = requestIdleCallback(callback, { timeout });\n return () => cancelIdleCallback(id);\n }\n\n // Fallback for Safari and older browsers\n const id = setTimeout(callback, timeout);\n return () => clearTimeout(id);\n}\n\n/**\n * Initialize islands with a hydration function.\n *\n * This is the main entry point for the island system.\n * Pass a hydrate function that knows how to mount your components.\n *\n * @example Vue\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n * import { createApp, h } from 'vue';\n * import Counter from './Counter.vue';\n *\n * const components = { Counter };\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n * const Component = components[name];\n * if (!Component) return;\n *\n * const app = createApp({ render: () => h(Component, props) });\n * app.mount(el);\n *\n * return () => app.unmount();\n * });\n * ```\n *\n * @example React\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n * import { createRoot } from 'react-dom/client';\n * import Counter from './Counter';\n *\n * const components = { Counter };\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n * const Component = components[name];\n * if (!Component) return;\n *\n * const root = createRoot(el);\n * root.render(<Component {...props} />);\n *\n * return () => root.unmount();\n * });\n * ```\n *\n * @example Vanilla JS\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n *\n * if (name === 'Counter') {\n * let count = props.initial || 0;\n * const button = el.querySelector('button')!;\n * const handler = () => {\n * count++;\n * button.textContent = String(count);\n * };\n * button.addEventListener('click', handler);\n * return () => button.removeEventListener('click', handler);\n * }\n * });\n * ```\n */\nexport function initIslands(\n hydrate: HydrateFunction,\n options?: InitIslandsOptions,\n): IslandController {\n const opts = { ...defaultOptions, ...options };\n const instances: IslandInstance[] = [];\n const cleanups: (() => void)[] = [];\n\n /**\n * Hydrate a single island element.\n */\n function hydrateIsland(element: HTMLElement, config: IslandConfig): void {\n // Find existing instance\n const instance = instances.find((i) => i.element === element);\n if (instance?.hydrated) return;\n\n try {\n opts.onHydrateStart?.(element, config);\n\n // Mark as loading\n element.classList.add(\"ox-island-loading\");\n\n // Call the hydrate function\n const cleanup = hydrate(element, config.props);\n\n // Mark as hydrated\n element.dataset.oxHydrated = \"true\";\n element.classList.remove(\"ox-island-loading\");\n\n // Update instance\n if (instance) {\n instance.cleanup = cleanup || undefined;\n instance.hydrated = true;\n } else {\n instances.push({\n element,\n config,\n cleanup: cleanup || undefined,\n hydrated: true,\n });\n }\n\n opts.onHydrateEnd?.(element, config);\n } catch (error) {\n element.classList.remove(\"ox-island-loading\");\n element.classList.add(\"ox-island-error\");\n element.dataset.oxError = error instanceof Error ? error.message : String(error);\n\n opts.onHydrateError?.(\n element,\n config,\n error instanceof Error ? error : new Error(String(error)),\n );\n }\n }\n\n /**\n * Schedule hydration based on load strategy.\n */\n function scheduleHydration(element: HTMLElement, config: IslandConfig): void {\n switch (config.load) {\n case \"eager\":\n hydrateIsland(element, config);\n break;\n\n case \"idle\": {\n const cancel = scheduleIdle(() => hydrateIsland(element, config), opts.idleTimeout);\n cleanups.push(cancel);\n break;\n }\n\n case \"visible\": {\n const cancel = observeVisibility(element, () => hydrateIsland(element, config), {\n rootMargin: opts.rootMargin,\n threshold: opts.threshold,\n });\n cleanups.push(cancel);\n break;\n }\n\n case \"media\": {\n if (config.mediaQuery) {\n const cancel = observeMedia(config.mediaQuery, () => hydrateIsland(element, config));\n cleanups.push(cancel);\n } else {\n // No media query specified, fall back to eager\n hydrateIsland(element, config);\n }\n break;\n }\n\n default:\n hydrateIsland(element, config);\n }\n }\n\n // Find and process all island elements\n const elements = document.querySelectorAll<HTMLElement>(opts.selector);\n\n elements.forEach((element) => {\n const config = parseIslandConfig(element);\n\n // Track instance\n instances.push({\n element,\n config,\n hydrated: false,\n });\n\n // Schedule hydration\n scheduleHydration(element, config);\n });\n\n // Return controller\n return {\n instances,\n\n hydrate(element: HTMLElement): void {\n const config = parseIslandConfig(element);\n hydrateIsland(element, config);\n },\n\n destroy(): void {\n // Cancel pending hydrations\n cleanups.forEach((cleanup) => cleanup());\n cleanups.length = 0;\n\n // Cleanup hydrated instances\n instances.forEach((instance) => {\n if (instance.cleanup) {\n instance.cleanup();\n }\n });\n instances.length = 0;\n },\n };\n}\n\n/**\n * Create a deferred hydration wrapper.\n *\n * Returns a function that can be called to hydrate islands later.\n * Useful for frameworks that need to register components first.\n *\n * @example\n * ```ts\n * const deferredInit = createDeferredInit();\n *\n * // Later, after components are ready\n * const components = await loadComponents();\n * deferredInit((el, props) => {\n * const Component = components[el.dataset.oxIsland!];\n * // ...\n * });\n * ```\n */\nexport function createDeferredInit(\n options?: InitIslandsOptions,\n): (hydrate: HydrateFunction) => IslandController {\n return (hydrate: HydrateFunction) => initIslands(hydrate, options);\n}\n\n/**\n * Check if islands are supported in the current environment.\n */\nexport function isIslandsSupported(): boolean {\n return (\n typeof window !== \"undefined\" &&\n typeof document !== \"undefined\" &&\n typeof IntersectionObserver !== \"undefined\" &&\n typeof MutationObserver !== \"undef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,CAAC;CAGD,OAAO;EACL;EAEA,QAAQ,SAA4B;GAElC,cAAc,SADC,kBAAkB,OACL,CAAC;EAC/B;EAEA,UAAgB;GAEd,SAAS,SAAS,YAAY,QAAQ,CAAC;GACvC,SAAS,SAAS;GAGlB,UAAU,SAAS,aAAa;IAC9B,IAAI,SAAS,SACX,SAAS,QAAQ;GAErB,CAAC;GACD,UAAU,SAAS;EACrB;CACF;AACF;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,mBACd,SACgD;CAChD,QAAQ,YAA6B,YAAY,SAAS,OAAO;AACnE;;;;AAKA,SAAgB,qBAA8B;CAC5C,OACE,OAAO,WAAW,eAClB,OAAO,aAAa,eACpB,OAAO,yBAAyB,eAChC,OAAO,qBAAqB;AAEhC"}
|
package/package.json
CHANGED
package/dist/runtime.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":[],"sources":["../src/runtime.ts"],"sourcesContent":["/**\n * Island Architecture Runtime\n *\n * Framework-agnostic Island controller using pure Vanilla JavaScript.\n * No framework dependencies - works with Vue, React, Svelte, or plain JS.\n */\n\nimport type {\n HydrateFunction,\n InitIslandsOptions,\n IslandConfig,\n IslandInstance,\n IslandController,\n LoadStrategy,\n} from \"./types\";\n\nconst defaultOptions: Required<\n Omit<InitIslandsOptions, \"onHydrateStart\" | \"onHydrateEnd\" | \"onHydrateError\">\n> &\n InitIslandsOptions = {\n rootMargin: \"200px\",\n threshold: 0,\n idleTimeout: 200,\n selector: \"[data-ox-island]\",\n};\n\n/**\n * Parse island configuration from element attributes.\n */\nfunction parseIslandConfig(element: HTMLElement): IslandConfig {\n const component = element.dataset.oxIsland || \"\";\n const load = (element.dataset.oxLoad as LoadStrategy) || \"eager\";\n const mediaQuery = element.dataset.oxMedia;\n\n let props: Record<string, unknown> = {};\n try {\n const propsJson = element.dataset.oxProps;\n if (propsJson) {\n props = JSON.parse(propsJson);\n }\n } catch (e) {\n console.warn(\"[ox-islands] Failed to parse props for\", element, e);\n }\n\n return {\n id: element.id || `island-${Math.random().toString(36).slice(2, 9)}`,\n component,\n load,\n mediaQuery,\n props,\n };\n}\n\n/**\n * Observe element visibility using IntersectionObserver.\n */\nfunction observeVisibility(\n element: HTMLElement,\n callback: () => void,\n options: { rootMargin: string; threshold: number },\n): () => void {\n const observer = new IntersectionObserver(\n (entries) => {\n if (entries[0].isIntersecting) {\n observer.disconnect();\n callback();\n }\n },\n {\n rootMargin: options.rootMargin,\n threshold: options.threshold,\n },\n );\n\n observer.observe(element);\n\n return () => observer.disconnect();\n}\n\n/**\n * Observe media query changes.\n */\nfunction observeMedia(query: string, callback: () => void): () => void {\n const mql = matchMedia(query);\n\n if (mql.matches) {\n callback();\n return () => {};\n }\n\n const handler = () => {\n if (mql.matches) {\n mql.removeEventListener(\"change\", handler);\n callback();\n }\n };\n\n mql.addEventListener(\"change\", handler);\n\n return () => mql.removeEventListener(\"change\", handler);\n}\n\n/**\n * Schedule callback for browser idle time.\n */\nfunction scheduleIdle(callback: () => void, timeout: number): () => void {\n if (\"requestIdleCallback\" in window) {\n const id = requestIdleCallback(callback, { timeout });\n return () => cancelIdleCallback(id);\n }\n\n // Fallback for Safari and older browsers\n const id = setTimeout(callback, timeout);\n return () => clearTimeout(id);\n}\n\n/**\n * Initialize islands with a hydration function.\n *\n * This is the main entry point for the island system.\n * Pass a hydrate function that knows how to mount your components.\n *\n * @example Vue\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n * import { createApp, h } from 'vue';\n * import Counter from './Counter.vue';\n *\n * const components = { Counter };\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n * const Component = components[name];\n * if (!Component) return;\n *\n * const app = createApp({ render: () => h(Component, props) });\n * app.mount(el);\n *\n * return () => app.unmount();\n * });\n * ```\n *\n * @example React\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n * import { createRoot } from 'react-dom/client';\n * import Counter from './Counter';\n *\n * const components = { Counter };\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n * const Component = components[name];\n * if (!Component) return;\n *\n * const root = createRoot(el);\n * root.render(<Component {...props} />);\n *\n * return () => root.unmount();\n * });\n * ```\n *\n * @example Vanilla JS\n * ```ts\n * import { initIslands } from '@ox-content/islands';\n *\n * initIslands((el, props) => {\n * const name = el.dataset.oxIsland!;\n *\n * if (name === 'Counter') {\n * let count = props.initial || 0;\n * const button = el.querySelector('button')!;\n * const handler = () => {\n * count++;\n * button.textContent = String(count);\n * };\n * button.addEventListener('click', handler);\n * return () => button.removeEventListener('click', handler);\n * }\n * });\n * ```\n */\nexport function initIslands(\n hydrate: HydrateFunction,\n options?: InitIslandsOptions,\n): IslandController {\n const opts = { ...defaultOptions, ...options };\n const instances: IslandInstance[] = [];\n const cleanups: (() => void)[] = [];\n\n /**\n * Hydrate a single island element.\n */\n function hydrateIsland(element: HTMLElement, config: IslandConfig): void {\n // Find existing instance\n const instance = instances.find((i) => i.element === element);\n if (instance?.hydrated) return;\n\n try {\n opts.onHydrateStart?.(element, config);\n\n // Mark as loading\n element.classList.add(\"ox-island-loading\");\n\n // Call the hydrate function\n const cleanup = hydrate(element, config.props);\n\n // Mark as hydrated\n element.dataset.oxHydrated = \"true\";\n element.classList.remove(\"ox-island-loading\");\n\n // Update instance\n if (instance) {\n instance.cleanup = cleanup || undefined;\n instance.hydrated = true;\n } else {\n instances.push({\n element,\n config,\n cleanup: cleanup || undefined,\n hydrated: true,\n });\n }\n\n opts.onHydrateEnd?.(element, config);\n } catch (error) {\n element.classList.remove(\"ox-island-loading\");\n element.classList.add(\"ox-island-error\");\n element.dataset.oxError = error instanceof Error ? error.message : String(error);\n\n opts.onHydrateError?.(\n element,\n config,\n error instanceof Error ? error : new Error(String(error)),\n );\n }\n }\n\n /**\n * Schedule hydration based on load strategy.\n */\n function scheduleHydration(element: HTMLElement, config: IslandConfig): void {\n switch (config.load) {\n case \"eager\":\n hydrateIsland(element, config);\n break;\n\n case \"idle\": {\n const cancel = scheduleIdle(() => hydrateIsland(element, config), opts.idleTimeout);\n cleanups.push(cancel);\n break;\n }\n\n case \"visible\": {\n const cancel = observeVisibility(element, () => hydrateIsland(element, config), {\n rootMargin: opts.rootMargin,\n threshold: opts.threshold,\n });\n cleanups.push(cancel);\n break;\n }\n\n case \"media\": {\n if (config.mediaQuery) {\n const cancel = observeMedia(config.mediaQuery, () => hydrateIsland(element, config));\n cleanups.push(cancel);\n } else {\n // No media query specified, fall back to eager\n hydrateIsland(element, config);\n }\n break;\n }\n\n default:\n hydrateIsland(element, config);\n }\n }\n\n // Find and process all island elements\n const elements = document.querySelectorAll<HTMLElement>(opts.selector);\n\n elements.forEach((element) => {\n const config = parseIslandConfig(element);\n\n // Track instance\n instances.push({\n element,\n config,\n hydrated: false,\n });\n\n // Schedule hydration\n scheduleHydration(element, config);\n });\n\n // Return controller\n return {\n instances,\n\n hydrate(element: HTMLElement): void {\n const config = parseIslandConfig(element);\n hydrateIsland(element, config);\n },\n\n destroy(): void {\n // Cancel pending hydrations\n cleanups.forEach((cleanup) => cleanup());\n cleanups.length = 0;\n\n // Cleanup hydrated instances\n instances.forEach((instance) => {\n if (instance.cleanup) {\n instance.cleanup();\n }\n });\n instances.length = 0;\n },\n };\n}\n\n/**\n * Create a deferred hydration wrapper.\n *\n * Returns a function that can be called to hydrate islands later.\n * Useful for frameworks that need to register components first.\n *\n * @example\n * ```ts\n * const deferredInit = createDeferredInit();\n *\n * // Later, after components are ready\n * const components = await loadComponents();\n * deferredInit((el, props) => {\n * const Component = components[el.dataset.oxIsland!];\n * // ...\n * });\n * ```\n */\nexport function createDeferredInit(\n options?: InitIslandsOptions,\n): (hydrate: HydrateFunction) => IslandController {\n return (hydrate: HydrateFunction) => initIslands(hydrate, options);\n}\n\n/**\n * Check if islands are supported in the current environment.\n */\nexport function isIslandsSupported(): boolean {\n return (\n typeof window !== \"undefined\" &&\n typeof document !== \"undefined\" &&\n typeof IntersectionObserver !== \"undefined\" &&\n typeof MutationObserver !== \"undefined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