@glasshome/widget-sdk 1.6.0 → 1.7.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.
@@ -0,0 +1 @@
1
+ export { instantiateWidget, type MountOptions, type WidgetInstanceHandle, } from "./instantiate-widget";
@@ -0,0 +1,51 @@
1
+ import { n as e, t } from "../tokens-Bdfb1xYe.js";
2
+ import { ErrorBoundary as n, createComponent as r } from "solid-js";
3
+ import { render as i } from "solid-js/web";
4
+ //#region src/host/instantiate-widget.ts
5
+ var a = /* @__PURE__ */ new Map();
6
+ function o(e) {
7
+ let t = a.get(e);
8
+ if (t) return t;
9
+ let n = new CSSStyleSheet();
10
+ return n.replaceSync(e), a.set(e, n), n;
11
+ }
12
+ var s = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Map(), l = !1;
13
+ function u(e, t) {
14
+ for (let n of t) e.classList.toggle(n, document.documentElement.classList.contains(n));
15
+ }
16
+ function d(e, t) {
17
+ for (let e of t) s.add(e);
18
+ return c.set(e, t), u(e, t), l || (l = !0, new MutationObserver(() => {
19
+ for (let [e, t] of c) u(e, t);
20
+ }).observe(document.documentElement, {
21
+ attributes: !0,
22
+ attributeFilter: ["class"]
23
+ })), () => {
24
+ c.delete(e);
25
+ };
26
+ }
27
+ function f(a, s) {
28
+ let c = a.attachShadow({ mode: "closed" }), l = d(a, s.mirrorClasses ?? ["dark"]);
29
+ t(c), s.cssText !== null && (c.adoptedStyleSheets = [...c.adoptedStyleSheets, o(s.cssText)]), s.extraSheets?.length && (c.adoptedStyleSheets = [...c.adoptedStyleSheets, ...s.extraSheets]);
30
+ let u = i(() => r(e.Provider, {
31
+ value: s.ctx,
32
+ get children() {
33
+ return r(n, {
34
+ fallback: (e) => {
35
+ let t = e instanceof Error ? e : Error(String(e));
36
+ return queueMicrotask(() => s.onCrash(t)), null;
37
+ },
38
+ get children() {
39
+ return r(s.definition.component, { get config() {
40
+ return s.config();
41
+ } });
42
+ }
43
+ });
44
+ }
45
+ }), c);
46
+ return { dispose: () => {
47
+ l(), u();
48
+ } };
49
+ }
50
+ //#endregion
51
+ export { f as instantiateWidget };
@@ -0,0 +1,48 @@
1
+ import { type Accessor } from "solid-js";
2
+ import type { ReactiveWidgetContext } from "../framework/hooks/use-widget-context";
3
+ import type { WidgetDefinition } from "../types";
4
+ export interface WidgetInstanceHandle {
5
+ dispose: () => void;
6
+ }
7
+ export interface MountOptions {
8
+ definition: WidgetDefinition;
9
+ /** Read inside the shadow render root so config changes propagate reactively across it. */
10
+ config: Accessor<Record<string, unknown>>;
11
+ /** Host context, re-provided inside the new render root (context does not cross roots). */
12
+ ctx: ReactiveWidgetContext;
13
+ /** Per-widget stylesheet text fetched by the host; null when the widget ships no CSS. */
14
+ cssText: string | null;
15
+ /** Called when the widget tree throws; the host owns crash counting and remount. */
16
+ onCrash: (error: Error) => void;
17
+ /**
18
+ * Extra sheets adopted AFTER the widget's own CSS, so their `!important` rules
19
+ * win ties. The host supplies these for effects it drives from outside the
20
+ * shadow (e.g. performant-blur / a11y overrides keyed on a mirrored class).
21
+ * Injection of SDK tokens is unchanged and never includes ui theme tokens.
22
+ */
23
+ extraSheets?: CSSStyleSheet[];
24
+ /**
25
+ * Document classes mirrored onto the shadow host so `:host(.x)` variants match
26
+ * (descendant selectors like `.dark *` cannot cross the boundary). Defaults to
27
+ * `["dark"]` — the SDK's dark variant. A host adds its own (reduce-blur,
28
+ * reduce-motion, perf-blur, …) when it ships sheets keyed on them.
29
+ */
30
+ mirrorClasses?: string[];
31
+ }
32
+ /**
33
+ * Mount a widget into a closed shadow root on `host`.
34
+ *
35
+ * The only ShadowRoot reference lives in this closure — `mode: "closed"` keeps
36
+ * it off `host.shadowRoot`, so neither the widget nor another bundle can reach
37
+ * into a foreign widget's DOM through the host element.
38
+ *
39
+ * This is the framework half of the widget host contract, shared by dash, the
40
+ * preview harness, and the hub render worker. It attaches the shadow, injects
41
+ * SDK tokens (tones + widget vars only — the ui theme reaches widgets by
42
+ * document inheritance, never pinned on `:host`), adopts the widget's CSS and
43
+ * any host `extraSheets`, mirrors the requested document classes, and renders
44
+ * the widget under a re-provided `WidgetCtx` and a crash boundary. Trust
45
+ * boundary steps that are host-specific (freeze-intrinsics, capability
46
+ * enforcement) stay in the host and are not part of this recipe.
47
+ */
48
+ export declare function instantiateWidget(host: HTMLElement, opts: MountOptions): WidgetInstanceHandle;