vitral 0.0.0-stage → 0.2.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +357 -2
  3. package/dist/angular.d.ts +38 -0
  4. package/dist/angular.js +90 -0
  5. package/dist/auto.d.ts +2 -0
  6. package/dist/auto.js +12 -0
  7. package/dist/chunk-2XF5BKV3.js +190 -0
  8. package/dist/chunk-6Y3VMWHH.js +181 -0
  9. package/dist/chunk-DXORSF4X.js +42 -0
  10. package/dist/chunk-GNPFWISJ.js +80 -0
  11. package/dist/chunk-HP6QWU6K.js +51 -0
  12. package/dist/chunk-NCONW6WS.js +606 -0
  13. package/dist/chunk-PX7PBCCT.js +89 -0
  14. package/dist/chunk-S5MZT5IT.js +20 -0
  15. package/dist/chunk-SFL6LTP3.js +56 -0
  16. package/dist/chunk-UTYAX6MU.js +152 -0
  17. package/dist/cli.d.ts +1 -0
  18. package/dist/cli.js +88 -0
  19. package/dist/dedupe-IWFIXRWG.js +81 -0
  20. package/dist/devtools.d.ts +12 -0
  21. package/dist/devtools.js +86 -0
  22. package/dist/element.d.ts +88 -0
  23. package/dist/element.js +18 -0
  24. package/dist/errors-KKB8FGCz.d.ts +11 -0
  25. package/dist/events-DUdjhfps.d.ts +6 -0
  26. package/dist/expose-53BEJB34.js +14 -0
  27. package/dist/host-slot-Cz6EiGsB.d.ts +6 -0
  28. package/dist/iframe.d.ts +28 -0
  29. package/dist/iframe.js +236 -0
  30. package/dist/index.d.ts +186 -0
  31. package/dist/index.js +65 -0
  32. package/dist/navigate-BRG1WJUM.d.ts +9 -0
  33. package/dist/overrides-NZAIUGK5.js +15 -0
  34. package/dist/preact.d.ts +45 -0
  35. package/dist/preact.js +71 -0
  36. package/dist/react.d.ts +63 -0
  37. package/dist/react.js +167 -0
  38. package/dist/routes.d.ts +56 -0
  39. package/dist/routes.js +143 -0
  40. package/dist/shadow-MYRJQLLY.js +72 -0
  41. package/dist/slot-pB276oPt.d.ts +36 -0
  42. package/dist/solid.d.ts +47 -0
  43. package/dist/solid.js +83 -0
  44. package/dist/sri-BVKZNX5Q.js +114 -0
  45. package/dist/state-BDdCwc21.d.ts +4 -0
  46. package/dist/svelte.d.ts +43 -0
  47. package/dist/svelte.js +42 -0
  48. package/dist/testing.d.ts +28 -0
  49. package/dist/testing.js +52 -0
  50. package/dist/types-BV-hUrms.d.ts +265 -0
  51. package/dist/vite.d.ts +78 -0
  52. package/dist/vite.js +786 -0
  53. package/dist/vue.d.ts +133 -0
  54. package/dist/vue.js +148 -0
  55. package/package.json +331 -3
@@ -0,0 +1,63 @@
1
+ import { ReactNode, ComponentType } from 'react';
2
+ import { L as Loading, P as Prefetch } from './host-slot-Cz6EiGsB.js';
3
+ import * as JSXRuntime from 'react/jsx-runtime';
4
+ import { P as Props, I as Isolation, C as CheckedProps, a as RemoteModule, S as Shared, R as RemoteContext } from './types-BV-hUrms.js';
5
+ export { E as EventBus } from './types-BV-hUrms.js';
6
+ export { e as events } from './events-DUdjhfps.js';
7
+
8
+ /**
9
+ * Adaptador React (funciona em Vite, CRA e Next.js App/Pages Router).
10
+ *
11
+ * Host: <Remote name="carrinho" props={{ userId }} />
12
+ * <Remote src="https://cdn.exemplo.com/carrinho/vitral.json" props={{ userId }} />
13
+ * Remote: export default reactRemote(App)
14
+ */
15
+
16
+ /** Transforma um componente React em um remote. */
17
+ declare function reactRemote<P extends Props>(Component: ComponentType<P>): RemoteModule<P>;
18
+ /** Dentro de um remote: dá acesso ao contexto (nome, eventos, baseUrl). */
19
+ declare function useVitral(): RemoteContext | null;
20
+ /** Assina um evento do barramento global enquanto o componente estiver montado. */
21
+ declare function useEvent<T = unknown>(type: string, handler: (payload: T) => void): void;
22
+ /** Estado derivado do último valor emitido em `type`. */
23
+ declare function useEventValue<T = unknown>(type: string): T | undefined;
24
+ /**
25
+ * Lê uma chave compartilhada (`provide`/`shared`) e re-renderiza só quando
26
+ * ela muda. Com seletor, só quando o resultado do seletor muda (`Object.is`).
27
+ * No servidor e na hidratação o valor é `undefined`.
28
+ *
29
+ * const nome = useShared('usuario', (u) => u?.nome)
30
+ */
31
+ declare function useShared<K extends keyof Shared, S = Shared[K] | undefined>(key: K, select?: (value: Shared[K] | undefined) => S): S;
32
+ declare function useShared<T = unknown, S = T | undefined>(key: string, select?: (value: T | undefined) => S): S;
33
+
34
+ interface RemoteProps<P extends Props = Props> {
35
+ /** Nome registrado com `configure()`. Com `src`, o nome sob o qual a URL é registrada. */
36
+ name?: string;
37
+ /** URL do `vitral.json`. Dispensa `configure()`. */
38
+ src?: string;
39
+ props?: P;
40
+ isolate?: Isolation;
41
+ /** Mostrado enquanto o remote baixa. */
42
+ fallback?: ReactNode;
43
+ /** Mostrado se o remote falhar. Recebe o erro e uma função para tentar de novo. */
44
+ error?: ReactNode | ((error: Error, retry: () => void) => ReactNode);
45
+ onError?: (error: Error) => void;
46
+ className?: string;
47
+ /** `'lazy'`: monta perto de aparecer na tela; `'idle'`: com o navegador ocioso. */
48
+ loading?: Loading;
49
+ /** Baixa antes de montar: `'hover'`, `'visible'` ou `'idle'`. */
50
+ prefetch?: Prefetch;
51
+ /** `'high'` para o remote que aparece logo ao abrir a página. */
52
+ priority?: RequestPriority;
53
+ /** Ao sair da página, guarda o remote montado (LRU, padrão 5) e na volta só atualiza as props. */
54
+ keepAlive?: boolean | number;
55
+ }
56
+ /**
57
+ * Renderiza um remote. No servidor (Next SSR) renderiza só o fallback; o
58
+ * remote monta no cliente, então não há erro de hidratação. Com o nome em
59
+ * `Remotes` (`npx vitral types`), as props são checadas.
60
+ */
61
+ declare function Remote<P extends Props = Props, N extends string = string>({ name, src, props, isolate, fallback, error, onError, className, loading, prefetch, priority, keepAlive, }: RemoteProps<P> & CheckedProps<N>): JSXRuntime.JSX.Element;
62
+
63
+ export { Remote, type RemoteProps, reactRemote, useEvent, useEventValue, useShared, useVitral };
package/dist/react.js ADDED
@@ -0,0 +1,167 @@
1
+ "use client";
2
+ import {
3
+ hostSlot
4
+ } from "./chunk-UTYAX6MU.js";
5
+ import {
6
+ defineRemote,
7
+ events,
8
+ memoSelect,
9
+ same,
10
+ shared
11
+ } from "./chunk-NCONW6WS.js";
12
+ import "./chunk-2XF5BKV3.js";
13
+ import "./chunk-SFL6LTP3.js";
14
+ import "./chunk-PX7PBCCT.js";
15
+ import {
16
+ protocol
17
+ } from "./chunk-GNPFWISJ.js";
18
+
19
+ // src/react.tsx
20
+ import {
21
+ Component as ReactComponent,
22
+ createContext,
23
+ useContext,
24
+ useEffect,
25
+ useMemo,
26
+ useRef,
27
+ useState,
28
+ useSyncExternalStore
29
+ } from "react";
30
+ import { createRoot } from "react-dom/client";
31
+ import * as ReactAll from "react";
32
+ import * as ReactDOM from "react-dom";
33
+ import * as ReactDOMClient from "react-dom/client";
34
+ import * as JSXRuntime from "react/jsx-runtime";
35
+ import * as JSXDevRuntime from "react/jsx-dev-runtime";
36
+ import { jsx, jsxs } from "react/jsx-runtime";
37
+ var VitralContext = createContext(null);
38
+ var Boundary = class extends ReactComponent {
39
+ constructor() {
40
+ super(...arguments);
41
+ this.state = { failed: false };
42
+ }
43
+ static getDerivedStateFromError() {
44
+ return { failed: true };
45
+ }
46
+ componentDidCatch(error) {
47
+ this.props.ctx.reportError(error);
48
+ }
49
+ render() {
50
+ return this.state.failed ? null : this.props.children;
51
+ }
52
+ };
53
+ function reactRemote(Component) {
54
+ return defineRemote({
55
+ mount(el, props, ctx) {
56
+ const root = createRoot(el, { onUncaughtError: (e) => ctx.reportError(e) });
57
+ const render = (p) => root.render(
58
+ /* @__PURE__ */ jsx(VitralContext.Provider, { value: ctx, children: /* @__PURE__ */ jsx(Boundary, { ctx, children: /* @__PURE__ */ jsx(Component, { ...p }) }) })
59
+ );
60
+ render(props);
61
+ return { update: render, unmount: () => root.unmount() };
62
+ }
63
+ });
64
+ }
65
+ function useVitral() {
66
+ return useContext(VitralContext);
67
+ }
68
+ function useEvent(type, handler) {
69
+ const ref = useRef(handler);
70
+ ref.current = handler;
71
+ useEffect(() => events.on(type, (p) => ref.current(p)), [type]);
72
+ }
73
+ function useEventValue(type) {
74
+ const [value, setValue] = useState(() => events.last(type));
75
+ useEvent(type, setValue);
76
+ return value;
77
+ }
78
+ function useShared(key, select = same) {
79
+ const handle = useMemo(() => shared(key), [key]);
80
+ const [pick] = useState(memoSelect);
81
+ return useSyncExternalStore(
82
+ handle.subscribe,
83
+ () => pick(handle.get(), select),
84
+ () => pick(void 0, select)
85
+ );
86
+ }
87
+ var offeredTo;
88
+ function offerReact() {
89
+ const p = protocol();
90
+ if (offeredTo === p) return;
91
+ offeredTo = p;
92
+ const compiler = ReactAll.__COMPILER_RUNTIME;
93
+ const modules = {
94
+ react: ReactAll,
95
+ "react-dom": ReactDOM,
96
+ "react-dom/client": ReactDOMClient,
97
+ "react/jsx-runtime": JSXRuntime,
98
+ "react/jsx-dev-runtime": JSXDevRuntime,
99
+ // O React 19 expõe o react/compiler-runtime pelo próprio react.
100
+ ...compiler && { "react/compiler-runtime": compiler }
101
+ };
102
+ p.pending(import("./dedupe-IWFIXRWG.js").then((m) => m.offer(p, { react: { version: ReactAll.version, modules } })).catch(() => {
103
+ }));
104
+ }
105
+ function Remote({
106
+ name,
107
+ src,
108
+ props,
109
+ isolate,
110
+ fallback = null,
111
+ error,
112
+ onError,
113
+ className,
114
+ loading,
115
+ prefetch,
116
+ priority,
117
+ keepAlive
118
+ }) {
119
+ const target = useRef(null);
120
+ const slot = useRef(null);
121
+ const [state, setState] = useState({ status: "loading", error: null });
122
+ const onErrorRef = useRef(onError);
123
+ onErrorRef.current = onError;
124
+ useEffect(() => {
125
+ offerReact();
126
+ const s = hostSlot(target.current, {
127
+ name,
128
+ src,
129
+ props: props ?? {},
130
+ isolate,
131
+ loading,
132
+ prefetch,
133
+ priority,
134
+ keepAlive,
135
+ onStatus(status2, e) {
136
+ setState({ status: status2, error: e });
137
+ if (e) {
138
+ if (onErrorRef.current) onErrorRef.current(e);
139
+ else console.error(e);
140
+ }
141
+ }
142
+ });
143
+ slot.current = s;
144
+ return () => {
145
+ slot.current = null;
146
+ queueMicrotask(() => s.destroy());
147
+ };
148
+ }, []);
149
+ useEffect(() => slot.current?.set({ name, src, isolate }), [name, src, isolate]);
150
+ useEffect(() => slot.current?.update(props ?? {}), [props]);
151
+ const retry = () => slot.current?.retry();
152
+ const { status, error: err } = state;
153
+ return /* @__PURE__ */ jsxs("div", { className, "data-vitral": name ?? src, "data-vitral-status": status, children: [
154
+ status === "loading" && fallback,
155
+ status === "error" && err && (typeof error === "function" ? error(err, retry) : error ?? null),
156
+ /* @__PURE__ */ jsx("div", { ref: target })
157
+ ] });
158
+ }
159
+ export {
160
+ Remote,
161
+ events,
162
+ reactRemote,
163
+ useEvent,
164
+ useEventValue,
165
+ useShared,
166
+ useVitral
167
+ };
@@ -0,0 +1,56 @@
1
+ import { P as Props } from './types-BV-hUrms.js';
2
+ import { S as SlotStatus } from './slot-pB276oPt.js';
3
+ import './errors-KKB8FGCz.js';
4
+
5
+ /** Padrão de URL → nome do remote. A primeira rota que bate vence. */
6
+ type Routes = Record<string, string>;
7
+ interface RoutesOptions {
8
+ /**
9
+ * Links `<a href>` da mesma origem que batem com uma rota navegam sem
10
+ * recarregar a página (via `navigate()`). Padrão: true.
11
+ */
12
+ links?: boolean;
13
+ }
14
+ interface RouteMatch {
15
+ /** Nome do remote. */
16
+ name: string;
17
+ /** Padrão que bateu. */
18
+ pattern: string;
19
+ /** Segmentos `:nome` do padrão. */
20
+ params: Record<string, string>;
21
+ }
22
+ /**
23
+ * Remote da rota que bate com `path`, ou `null`. Padrões: `/conta` (exato),
24
+ * `/checkout/*` (o prefixo e tudo abaixo dele), `/produto/:id` e `*` (qualquer).
25
+ */
26
+ declare function matchRoute(path: string, routes?: Routes): RouteMatch | null;
27
+ /**
28
+ * Define as rotas, registra `<vitral-outlet>` e, com `links` (padrão), faz os
29
+ * links internos navegarem sem recarregar. Devolve a função que desfaz tudo.
30
+ */
31
+ declare function routes(map: Routes, options?: RoutesOptions): () => void;
32
+ declare const Base: typeof HTMLElement;
33
+ declare class VitralOutletElement extends Base {
34
+ #private;
35
+ static observedAttributes: string[];
36
+ get props(): Props;
37
+ set props(value: Props);
38
+ /** Remote montado agora, ou `null`. */
39
+ get route(): string | null;
40
+ get status(): SlotStatus | "empty";
41
+ retry(): void;
42
+ connectedCallback(): void;
43
+ disconnectedCallback(): void;
44
+ attributeChangedCallback(attr: string, _old: string | null, value: string | null): void;
45
+ /** Confere a URL atual e troca de remote se a rota mudou. */
46
+ sync(): void;
47
+ }
48
+ /** Registra `<vitral-outlet>` (ou outro nome). `routes()` já chama. */
49
+ declare function defineVitralOutlet(tag?: string): void;
50
+ declare global {
51
+ interface HTMLElementTagNameMap {
52
+ 'vitral-outlet': VitralOutletElement;
53
+ }
54
+ }
55
+
56
+ export { type RouteMatch, type Routes, type RoutesOptions, VitralOutletElement, defineVitralOutlet, matchRoute, routes };
package/dist/routes.js ADDED
@@ -0,0 +1,143 @@
1
+ import {
2
+ createSlot,
3
+ navigate
4
+ } from "./chunk-NCONW6WS.js";
5
+ import "./chunk-2XF5BKV3.js";
6
+ import "./chunk-SFL6LTP3.js";
7
+ import "./chunk-PX7PBCCT.js";
8
+ import "./chunk-GNPFWISJ.js";
9
+
10
+ // src/routes.ts
11
+ var table = {};
12
+ var outlets = /* @__PURE__ */ new Set();
13
+ var compiled = /* @__PURE__ */ new Map();
14
+ function compile(pattern) {
15
+ let re = compiled.get(pattern);
16
+ if (!re) {
17
+ const body = pattern.replace(/\/\*$|^\*$/, "\0").replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/:(\w+)/g, "(?<$1>[^/]+)").replace(/\/$/, "").replace("\0", "(?:/.*)?");
18
+ compiled.set(pattern, re = new RegExp(`^${body}/?$`));
19
+ }
20
+ return re;
21
+ }
22
+ function matchRoute(path, routes2 = table) {
23
+ for (const [pattern, name] of Object.entries(routes2)) {
24
+ const m = compile(pattern).exec(path);
25
+ if (m) return { name, pattern, params: { ...m.groups } };
26
+ }
27
+ return null;
28
+ }
29
+ function routes(map, options = {}) {
30
+ table = map;
31
+ defineVitralOutlet();
32
+ for (const o of outlets) o.sync();
33
+ const intercept = options.links !== false && typeof document !== "undefined";
34
+ if (intercept) document.addEventListener("click", onClick);
35
+ return () => {
36
+ if (intercept) document.removeEventListener("click", onClick);
37
+ if (table === map) table = {};
38
+ for (const o of outlets) o.sync();
39
+ };
40
+ }
41
+ function onClick(e) {
42
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
43
+ const a = e.composedPath().find((n) => n.localName === "a" && !!n.href);
44
+ if (!a || a.target && a.target !== "_self" || a.hasAttribute("download")) return;
45
+ const url = new URL(a.href);
46
+ if (url.origin !== location.origin || !matchRoute(url.pathname)) return;
47
+ e.preventDefault();
48
+ navigate(url.href);
49
+ }
50
+ var Base = typeof HTMLElement === "undefined" ? class {
51
+ } : HTMLElement;
52
+ var VitralOutletElement = class extends Base {
53
+ static {
54
+ this.observedAttributes = ["props", "isolate"];
55
+ }
56
+ #slot = null;
57
+ #props = {};
58
+ // `undefined` até a primeira checagem, para o estado `empty` aparecer já de início.
59
+ #route;
60
+ get props() {
61
+ return this.#props;
62
+ }
63
+ set props(value) {
64
+ this.#props = value ?? {};
65
+ this.#slot?.update(this.#props);
66
+ }
67
+ /** Remote montado agora, ou `null`. */
68
+ get route() {
69
+ return this.#route ?? null;
70
+ }
71
+ get status() {
72
+ return this.#slot?.status ?? (this.#route ? "loading" : "empty");
73
+ }
74
+ retry() {
75
+ this.#slot?.retry();
76
+ }
77
+ #onNavigate = () => this.sync();
78
+ connectedCallback() {
79
+ outlets.add(this);
80
+ addEventListener("popstate", this.#onNavigate);
81
+ globalThis.navigation?.addEventListener("navigatesuccess", this.#onNavigate);
82
+ this.sync();
83
+ }
84
+ disconnectedCallback() {
85
+ outlets.delete(this);
86
+ removeEventListener("popstate", this.#onNavigate);
87
+ globalThis.navigation?.removeEventListener("navigatesuccess", this.#onNavigate);
88
+ queueMicrotask(() => !this.isConnected && this.#show(null));
89
+ }
90
+ attributeChangedCallback(attr, _old, value) {
91
+ if (attr === "props") {
92
+ try {
93
+ this.props = value ? JSON.parse(value) : {};
94
+ } catch {
95
+ console.error("[vitral] atributo props n\xE3o \xE9 JSON v\xE1lido:", value);
96
+ }
97
+ } else this.#slot?.set({ isolate: value || void 0 });
98
+ }
99
+ /** Confere a URL atual e troca de remote se a rota mudou. */
100
+ sync() {
101
+ if (this.isConnected) this.#show(matchRoute(location.pathname)?.name ?? null);
102
+ }
103
+ #show(name) {
104
+ if (name === this.#route) return;
105
+ this.#route = name;
106
+ if (name) this.setAttribute("data-vitral-route", name);
107
+ else this.removeAttribute("data-vitral-route");
108
+ if (!name) {
109
+ this.#slot?.destroy();
110
+ this.#slot = null;
111
+ this.#status("empty", null);
112
+ } else if (this.#slot) {
113
+ this.#slot.set({ name });
114
+ } else {
115
+ this.#slot = createSlot(this, {
116
+ name,
117
+ props: this.#props,
118
+ isolate: this.getAttribute("isolate") || void 0,
119
+ onStatus: (s, e) => this.#status(s, e)
120
+ });
121
+ }
122
+ }
123
+ #status(status, error) {
124
+ this.setAttribute("data-vitral-status", status);
125
+ for (const [slot, on] of [["fallback", "loading"], ["error", "error"], ["empty", "empty"]]) {
126
+ for (const el of this.querySelectorAll(`:scope > [slot="${slot}"]`)) el.hidden = status !== on;
127
+ }
128
+ if (status === "ready") this.dispatchEvent(new CustomEvent("vitral-ready", { bubbles: true, detail: { route: this.#route } }));
129
+ if (status === "error") this.dispatchEvent(new CustomEvent("vitral-error", { bubbles: true, detail: { error } }));
130
+ }
131
+ };
132
+ function defineVitralOutlet(tag = "vitral-outlet") {
133
+ if (typeof customElements !== "undefined" && !customElements.get(tag)) {
134
+ customElements.define(tag, class extends VitralOutletElement {
135
+ });
136
+ }
137
+ }
138
+ export {
139
+ VitralOutletElement,
140
+ defineVitralOutlet,
141
+ matchRoute,
142
+ routes
143
+ };
@@ -0,0 +1,72 @@
1
+ import {
2
+ fetchCssText,
3
+ injectCss
4
+ } from "./chunk-SFL6LTP3.js";
5
+ import "./chunk-PX7PBCCT.js";
6
+ import "./chunk-GNPFWISJ.js";
7
+
8
+ // src/core/shadow.ts
9
+ var sheets = /* @__PURE__ */ new Map();
10
+ var hoisted = /* @__PURE__ */ new Set();
11
+ var RELATIVE_URL = /url\(\s*(['"]?)(?![a-z]+:|\/\/|#)([^'")]+)\1\s*\)/gi;
12
+ var GLOBAL_RULES = /@property\s+(--[\w-]+)\s*\{[^}]*\}|@font-face\s*\{[^}]*\}|@import\s+(?:url\(\s*)?(?:(['"])(.*?)\2|([^'"\s);]+))\s*\)?[^;]*;/gi;
13
+ async function attachStyles(root, el, css, global = {}, local = {}, sri = {}) {
14
+ const urls = [...global.inherit ?? [], ...local.inherit ?? []].map((u) => new URL(u, document.baseURI).href).concat(css);
15
+ if (typeof CSSStyleSheet !== "undefined" && "replaceSync" in CSSStyleSheet.prototype) {
16
+ const loaded = await Promise.all(urls.map((u) => loadSheet(u, [], sri[u])));
17
+ root.adoptedStyleSheets = [.../* @__PURE__ */ new Set([...root.adoptedStyleSheets, ...loaded.flat()])];
18
+ } else {
19
+ await injectCss(urls, root, sri);
20
+ }
21
+ return (local.mirrorTheme ?? global.mirrorTheme) === false ? () => {
22
+ } : mirrorTheme(el);
23
+ }
24
+ function loadSheet(href, chain, integrity) {
25
+ let p = sheets.get(href);
26
+ if (!p) {
27
+ p = fetchCssText(href, integrity).then((text) => {
28
+ const up = [];
29
+ const imports = [];
30
+ const rest = text.replace(RELATIVE_URL, (_, q, u) => `url(${q}${new URL(u, href).href}${q})`).replace(GLOBAL_RULES, (rule, prop, _q, quoted, bare) => {
31
+ const url = quoted ?? bare;
32
+ if (url) imports.push(new URL(url, href).href);
33
+ else if (!hoisted.has(prop ?? rule)) {
34
+ hoisted.add(prop ?? rule);
35
+ up.push(rule);
36
+ }
37
+ return "";
38
+ });
39
+ if (up.length) {
40
+ const style = document.createElement("style");
41
+ style.setAttribute("data-vitral-properties", "");
42
+ style.textContent = up.join("\n");
43
+ document.head.appendChild(style);
44
+ }
45
+ const sheet = new CSSStyleSheet();
46
+ sheet.replaceSync(rest);
47
+ const next = [...chain, href];
48
+ return Promise.all(imports.filter((u) => !next.includes(u)).map((u) => loadSheet(u, next))).then((found) => [...found.flat(), sheet]);
49
+ }).catch(() => []);
50
+ sheets.set(href, p);
51
+ }
52
+ return p;
53
+ }
54
+ function mirrorTheme(el) {
55
+ const html = document.documentElement;
56
+ let added = [];
57
+ const sync = () => {
58
+ el.classList.remove(...added);
59
+ added = [...html.classList].filter((c) => !el.classList.contains(c));
60
+ el.classList.add(...added);
61
+ const theme = html.getAttribute("data-theme");
62
+ if (theme === null) el.removeAttribute("data-theme");
63
+ else el.setAttribute("data-theme", theme);
64
+ };
65
+ sync();
66
+ const observer = new MutationObserver(sync);
67
+ observer.observe(html, { attributeFilter: ["class", "data-theme"] });
68
+ return () => observer.disconnect();
69
+ }
70
+ export {
71
+ attachStyles
72
+ };
@@ -0,0 +1,36 @@
1
+ import { R as RemoteError } from './errors-KKB8FGCz.js';
2
+ import { P as Props, I as Isolation, b as StyleOptions } from './types-BV-hUrms.js';
3
+
4
+ type SlotStatus = 'loading' | 'ready' | 'error';
5
+ interface SlotOptions<P extends Props = Props> {
6
+ /** Nome registrado. Com `src`, é o nome sob o qual a URL é registrada (opcional). */
7
+ name?: string;
8
+ /** URL do `vitral.json` (ou do JS). Registra o remote sem `configure()`. */
9
+ src?: string;
10
+ props?: P;
11
+ isolate?: Isolation;
12
+ /** `'high'` para o remote que aparece logo ao abrir a página. */
13
+ priority?: RequestPriority;
14
+ /** Estilos do host no Shadow DOM (somados aos do `configure`). */
15
+ styles?: StyleOptions;
16
+ /** Chamado a cada mudança de estado (para mostrar fallback / erro). */
17
+ onStatus?: (status: SlotStatus, error: RemoteError | null) => void;
18
+ }
19
+ interface RemoteSlot<P extends Props = Props> {
20
+ /** Atualiza as props sem remontar. */
21
+ update(props: P): void;
22
+ /** Troca de remote ou de isolamento (remonta). */
23
+ set(options: Partial<Pick<SlotOptions<P>, 'name' | 'src' | 'isolate'>>): void;
24
+ /** Remonta do zero (botão "tentar de novo"). */
25
+ retry(): void;
26
+ destroy(): void;
27
+ readonly status: SlotStatus;
28
+ }
29
+ /**
30
+ * Cuida do ciclo de vida de um remote dentro de `target`: monta, atualiza,
31
+ * remonta ao trocar de nome, ignora respostas atrasadas e limpa tudo no fim.
32
+ * Os adaptadores de host (Web Component, Svelte, Solid, Preact…) usam isto.
33
+ */
34
+ declare function createSlot<P extends Props = Props>(target: HTMLElement, options: SlotOptions<P>): RemoteSlot<P>;
35
+
36
+ export { type RemoteSlot as R, type SlotStatus as S, type SlotOptions as a, createSlot as c };
@@ -0,0 +1,47 @@
1
+ import { JSX, Component, Accessor } from 'solid-js';
2
+ import { P as Props, I as Isolation, C as CheckedProps, a as RemoteModule, S as Shared } from './types-BV-hUrms.js';
3
+ export { R as RemoteContext } from './types-BV-hUrms.js';
4
+ import { R as RemoteError } from './errors-KKB8FGCz.js';
5
+ export { e as events } from './events-DUdjhfps.js';
6
+ export { n as navigate } from './navigate-BRG1WJUM.js';
7
+ import { L as Loading, P as Prefetch } from './host-slot-Cz6EiGsB.js';
8
+
9
+ /**
10
+ * Adaptador Solid (e SolidStart).
11
+ *
12
+ * Remote: export default solidRemote(App)
13
+ * Host: <Remote name="carrinho" props={{ userId: id() }} />
14
+ */
15
+
16
+ /** Transforma um componente Solid em um remote. O contexto chega na prop `vitral`. */
17
+ declare function solidRemote<P extends Props>(Comp: Component<any>): RemoteModule<P>;
18
+ interface RemoteProps<P extends Props = Props> {
19
+ name?: string;
20
+ /** URL do `vitral.json`. Dispensa `configure()`. */
21
+ src?: string;
22
+ props?: P;
23
+ isolate?: Isolation;
24
+ fallback?: JSX.Element;
25
+ error?: (error: RemoteError, retry: () => void) => JSX.Element;
26
+ class?: string;
27
+ /** `'lazy'`: monta perto de aparecer na tela; `'idle'`: com o navegador ocioso. */
28
+ loading?: Loading;
29
+ /** Baixa antes de montar: `'hover'`, `'visible'` ou `'idle'`. */
30
+ prefetch?: Prefetch;
31
+ /** `'high'` para o remote que aparece logo ao abrir a página. */
32
+ priority?: RequestPriority;
33
+ /** Ao sair da página, guarda o remote montado (LRU, padrão 5) e na volta só atualiza as props. */
34
+ keepAlive?: boolean | number;
35
+ }
36
+ declare function Remote<P extends Props = Props, N extends string = string>(p: RemoteProps<P> & CheckedProps<N>): JSX.Element;
37
+ /**
38
+ * Accessor de uma chave compartilhada (`provide`/`shared`). Com seletor, só
39
+ * muda quando o resultado do seletor muda (`Object.is`). A assinatura acaba
40
+ * junto com o dono reativo (componente ou `createRoot`).
41
+ *
42
+ * const nome = useShared('usuario', (u) => u?.nome)
43
+ */
44
+ declare function useShared<K extends keyof Shared, S = Shared[K] | undefined>(key: K, select?: (value: Shared[K] | undefined) => S): Accessor<S>;
45
+ declare function useShared<T = unknown, S = T | undefined>(key: string, select?: (value: T | undefined) => S): Accessor<S>;
46
+
47
+ export { Remote, type RemoteProps, solidRemote, useShared };
package/dist/solid.js ADDED
@@ -0,0 +1,83 @@
1
+ import {
2
+ hostSlot
3
+ } from "./chunk-UTYAX6MU.js";
4
+ import {
5
+ defineRemote,
6
+ events,
7
+ memoSelect,
8
+ navigate,
9
+ same,
10
+ shared
11
+ } from "./chunk-NCONW6WS.js";
12
+ import "./chunk-2XF5BKV3.js";
13
+ import "./chunk-SFL6LTP3.js";
14
+ import "./chunk-PX7PBCCT.js";
15
+ import "./chunk-GNPFWISJ.js";
16
+
17
+ // src/solid.ts
18
+ import { createComponent, createEffect, createSignal, getOwner, mergeProps, on, onCleanup, onMount } from "solid-js";
19
+ import { createStore, reconcile } from "solid-js/store";
20
+ import { isServer, render } from "solid-js/web";
21
+ function solidRemote(Comp) {
22
+ return defineRemote({
23
+ mount(el, props, ctx) {
24
+ const [state, setState] = createStore({ ...props });
25
+ const dispose = render(() => createComponent(Comp, mergeProps(state, { vitral: ctx })), el);
26
+ return {
27
+ update: (next) => setState(reconcile({ ...next })),
28
+ unmount: dispose
29
+ };
30
+ }
31
+ });
32
+ }
33
+ function Remote(p) {
34
+ if (isServer) return p.fallback ?? null;
35
+ const [status, setStatus] = createSignal("loading");
36
+ const [error, setError] = createSignal(null);
37
+ const host = document.createElement("div");
38
+ host.setAttribute("data-vitral", p.name ?? p.src ?? "");
39
+ if (p.class) host.className = p.class;
40
+ const target = document.createElement("div");
41
+ let slot = null;
42
+ onMount(() => {
43
+ slot = hostSlot(target, {
44
+ name: p.name,
45
+ src: p.src,
46
+ props: p.props ?? {},
47
+ isolate: p.isolate,
48
+ loading: p.loading,
49
+ prefetch: p.prefetch,
50
+ priority: p.priority,
51
+ keepAlive: p.keepAlive,
52
+ onStatus: (s, e) => {
53
+ host.setAttribute("data-vitral-status", s);
54
+ setError(() => e);
55
+ setStatus(s);
56
+ }
57
+ });
58
+ });
59
+ createEffect(on(() => [p.name, p.src, p.isolate], ([name, src, isolate]) => slot?.set({ name, src, isolate }), { defer: true }));
60
+ createEffect(on(() => ({ ...p.props ?? {} }), (props) => slot?.update(props), { defer: true }));
61
+ onCleanup(() => slot?.destroy());
62
+ const retry = () => slot?.retry();
63
+ return [
64
+ () => status() === "loading" ? p.fallback : null,
65
+ () => status() === "error" && error() && p.error ? p.error(error(), retry) : null,
66
+ (host.appendChild(target), host)
67
+ ];
68
+ }
69
+ function useShared(key, select = same) {
70
+ const handle = shared(key);
71
+ const pick = memoSelect();
72
+ const [value, setValue] = createSignal(pick(handle.get(), select), { equals: Object.is });
73
+ const off = handle.subscribe((v) => setValue(() => pick(v, select)));
74
+ if (getOwner()) onCleanup(off);
75
+ return value;
76
+ }
77
+ export {
78
+ Remote,
79
+ events,
80
+ navigate,
81
+ solidRemote,
82
+ useShared
83
+ };