@adobe/uix-host-react 0.7.1-nightly.20230124 → 0.7.1-nightly.20230126
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
|
|
1
1
|
import type { VirtualApi } from "@adobe/uix-core";
|
2
2
|
import type { PropsWithChildren, IframeHTMLAttributes } from "react";
|
3
|
+
import type { AttrTokens, SandboxToken } from "@adobe/uix-host";
|
3
4
|
/**
|
4
5
|
* @public
|
5
6
|
*/
|
6
|
-
declare type FrameProps = IframeHTMLAttributes<HTMLIFrameElement>;
|
7
|
+
declare type FrameProps = Omit<IframeHTMLAttributes<HTMLIFrameElement>, "sandbox">;
|
7
8
|
/** @public */
|
8
9
|
export interface GuestUIProps extends FrameProps {
|
9
10
|
guestId: string;
|
@@ -19,6 +20,10 @@ export interface GuestUIProps extends FrameProps {
|
|
19
20
|
* Called when the connection process throws an exception
|
20
21
|
*/
|
21
22
|
onConnectionError?: (error: Error) => void;
|
23
|
+
/**
|
24
|
+
* Additional sandbox attributes GuestUIFrame might need.
|
25
|
+
*/
|
26
|
+
sandbox: AttrTokens<SandboxToken>;
|
22
27
|
/**
|
23
28
|
* Optional custom URL or path.
|
24
29
|
*/
|
@@ -38,6 +43,6 @@ export interface GuestUIProps extends FrameProps {
|
|
38
43
|
* delivered by the Extension server.
|
39
44
|
* @public
|
40
45
|
*/
|
41
|
-
export declare function GuestUIFrame({ guestId, src, onConnect, onDisconnect, onConnectionError, methods, ...customFrameProps }: PropsWithChildren<GuestUIProps>): JSX.Element;
|
46
|
+
export declare function GuestUIFrame({ guestId, src, onConnect, onDisconnect, onConnectionError, methods, sandbox, ...customFrameProps }: PropsWithChildren<GuestUIProps>): JSX.Element;
|
42
47
|
export default GuestUIFrame;
|
43
48
|
//# sourceMappingURL=GuestUIFrame.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"GuestUIFrame.d.ts","sourceRoot":"","sources":["../../src/components/GuestUIFrame.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;
|
1
|
+
{"version":3,"file":"GuestUIFrame.d.ts","sourceRoot":"","sources":["../../src/components/GuestUIFrame.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAErE,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGhE;;GAEG;AACH,aAAK,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,SAAS,CAAC,CAAC;AAE3E,cAAc;AACd,MAAM,WAAW,YAAa,SAAQ,UAAU;IAC9C,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB;;OAEG;IACH,YAAY,EAAE,MAAM,OAAO,CAAC;IAC5B;;OAEG;IACH,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAC3C;;OAEG;IACH,OAAO,EAAE,UAAU,CAAC,YAAY,CAAC,CAAC;IAClC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,CAAC,EAAE,UAAU,CAAC;IAErB;;;OAGG;IACH,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC5B;AAYD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAC3B,OAAO,EACP,GAAQ,EACR,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,OAAO,EACP,OAAY,EACZ,GAAG,gBAAgB,EACpB,EAAE,iBAAiB,CAAC,YAAY,CAAC,eAoDjC;AACD,eAAe,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
@@ -77,16 +77,14 @@ function useHost() {
|
|
77
77
|
}
|
78
78
|
return { error: void 0, host };
|
79
79
|
}
|
80
|
-
|
81
|
-
// src/components/GuestUIFrame.tsx
|
82
80
|
var defaultFrameProps = {
|
83
81
|
width: "100%",
|
84
82
|
height: "100%",
|
85
|
-
sandbox: "allow-scripts",
|
86
83
|
style: {
|
87
84
|
border: "none"
|
88
85
|
}
|
89
86
|
};
|
87
|
+
var defaultSandbox = "allow-scripts";
|
90
88
|
function GuestUIFrame({
|
91
89
|
guestId,
|
92
90
|
src = "",
|
@@ -94,6 +92,7 @@ function GuestUIFrame({
|
|
94
92
|
onDisconnect,
|
95
93
|
onConnectionError,
|
96
94
|
methods,
|
95
|
+
sandbox = "",
|
97
96
|
...customFrameProps
|
98
97
|
}) {
|
99
98
|
const { host } = useHost();
|
@@ -126,13 +125,17 @@ function GuestUIFrame({
|
|
126
125
|
};
|
127
126
|
}
|
128
127
|
}, []);
|
129
|
-
const frameProps = {
|
128
|
+
const frameProps = {
|
129
|
+
...defaultFrameProps,
|
130
|
+
...customFrameProps,
|
131
|
+
...uixHost.requiredIframeProps
|
132
|
+
};
|
130
133
|
return /* @__PURE__ */ React.createElement("iframe", {
|
131
134
|
ref,
|
132
135
|
src: frameUrl.href,
|
133
136
|
name: `uix-guest-${guest.id}`,
|
134
|
-
|
135
|
-
|
137
|
+
sandbox: sandbox ? uixHost.makeSandboxAttrs(defaultSandbox, sandbox).join(" ") : defaultSandbox,
|
138
|
+
...frameProps
|
136
139
|
});
|
137
140
|
}
|
138
141
|
var NO_EXTENSIONS = [];
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/components/Extensible.tsx","../src/extension-context.ts","../src/components/GuestUIFrame.tsx","../src/hooks/useHost.ts","../src/hooks/useExtensions.ts"],"names":["host","React","Host","useCallback","useEffect","useMemo","useState"],"mappings":";AAYA,cAAc;;;ACAd,OAAO,SAAS,UAAU,iBAA0B;AASpD,SAAS,YAAY;;;AC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AAA,IACvE;AACA,WAAO,cAAc,WAAW,IAAI,gBAAgB;AAAA,EACtD,GAAG,CAAC,GAAG,UAAU,QAAQ,CAAC;AAE1B,QAAM,YAAYA;AAAA,IAChB,CAAC,YAA2B;AAC1B,YAAM,YAAY,aAAa,QAAQ,kBAAkB;AACzD,WAAK,iBAAiB,WAAW,OAAO;AACxC,aAAO,MAAM,KAAK,oBAAoB,WAAW,OAAO;AAAA,IAC1D;AAAA,IACA,CAAC,GAAG,UAAU,QAAQ;AAAA,EACxB;AAEA,QAAM,CAAC,YAAY,aAAa,IAAIG,UAAS,MAAM,cAAc,CAAC;AAClE,EAAAF,WAAU,MAAM;AACd,WAAO,UAAU,MAAM;AACrB,oBAAc,cAAc,CAAC;AAAA,IAC/B,CAAC;AAAA,EACH,GAAG,CAAC,WAAW,aAAa,CAAC;AAE7B,QAAM,CAAC,WAAW,YAAY,IAAIE,UAAgB;AAClD,EAAAF;AAAA,IACE,MACE,KAAK;AAAA,MACH;AAAA,MACA,CAAC,UACC,aAAa,MAAM,OAAO,KAAK;AAAA,IACnC;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,YAAY,SAAS,CAAC,KAAK,SAAS,OAAO,UAAU;AAChE","sourcesContent":["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nexport * from \"@adobe/uix-host\";\nexport * from \"./components/index.js\";\nexport * from \"./hooks/index.js\";\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport React, { useState, useEffect, useMemo } from \"react\";\nimport type { PropsWithChildren } from \"react\";\nimport type {\n InstalledExtensions,\n ExtensionsProvider,\n HostConfig,\n PortOptions,\n SharedContextValues,\n} from \"@adobe/uix-host\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/** @public */\nexport interface ExtensibleProps extends Omit<HostConfig, \"hostName\"> {\n /**\n * Unique name for identifying this extensible app. May be used as metadata in\n * extension registry in the future.\n */\n appName?: string;\n /**\n * Function which returns a promise for the full list of extensions.\n */\n extensionsProvider: ExtensionsProvider;\n /**\n * {@inheritDoc HostConfig.guestOptions}\n */\n guestOptions?: PortOptions;\n /**\n * {@inheritDoc HostConfig.sharedContext}\n */\n sharedContext?: SharedContextValues;\n}\n\nfunction areExtensionsDifferent(\n set1: InstalledExtensions,\n set2: InstalledExtensions\n) {\n const ids1 = Object.keys(set1).sort();\n const ids2 = Object.keys(set2).sort();\n return (\n ids1.length !== ids2.length || ids1.some((id, index) => id !== ids2[index])\n );\n}\n\n/**\n * Declares an extensible area in an app, and provides host and extension\n * objects to all descendents. The {@link useExtensions} hook can only be called\n * in a descendent of this component.\n *\n * @remarks\n * For many apps, there will be only one Extensible provider, fairly high in the\n * component tree. It is a context provider component that may be bundled with\n * other context providers in your app architecture.\n *\n * Each Extensible element creates one {@link @adobe/uix-host#Host} object and\n * one call to an extensions provider. If multiple Extensible elements are used,\n * this may cause redundant calls. Such a design should be carefully considered.\n *\n * @public\n */\nexport function Extensible({\n appName,\n children,\n extensionsProvider,\n guestOptions,\n runtimeContainer,\n debug,\n sharedContext,\n}: PropsWithChildren<ExtensibleProps>) {\n const hostName = appName || window.location.host || \"mainframe\";\n\n const [extensions, setExtensions] = useState({});\n useEffect(() => {\n extensionsProvider()\n .then((loaded: InstalledExtensions) => {\n setExtensions((prev) =>\n areExtensionsDifferent(prev, loaded) ? loaded : prev\n );\n })\n .catch((e: Error | unknown) => {\n console.error(\"Fetching list of extensions failed!\", e);\n });\n }, [extensionsProvider]);\n\n const [host, setHost] = useState<Host>();\n useEffect(() => {\n function logError(msg: string) {\n return (e: Error | unknown) => {\n const error = e instanceof Error ? e : new Error(String(e));\n console.error(msg, error, extensions, guestOptions);\n };\n }\n\n const host = new Host({ debug, hostName, runtimeContainer, sharedContext });\n setHost(host);\n\n if (!Object.entries(extensions).length) {\n return;\n }\n\n host\n .load(extensions, guestOptions)\n .catch(logError(\"Load of extensions failed!\"));\n return () => {\n host.unload().catch(logError(\"Unload of extensions failed!\"));\n };\n }, [debug, hostName, runtimeContainer, sharedContext, extensions]);\n\n // skip render before host is initialized\n if (!host) {\n return null;\n }\n\n return (\n <ExtensionContext.Provider value={host}>\n {children}\n </ExtensionContext.Provider>\n );\n}\nexport default Extensible;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\n/**\n * @hidden\n */\nimport { createContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\n\nexport const ExtensionContext = createContext<Host>({} as unknown as Host);\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { VirtualApi } from \"@adobe/uix-core\";\nimport React, { useCallback } from \"react\";\nimport type { PropsWithChildren, IframeHTMLAttributes } from \"react\";\nimport { useHost } from \"../hooks/useHost.js\";\n\n/**\n * @public\n */\ntype FrameProps = IframeHTMLAttributes<HTMLIFrameElement>;\n\n/** @public */\nexport interface GuestUIProps extends FrameProps {\n guestId: string;\n /**\n * Receives the Penpal context when the frame is mounted.\n */\n onConnect: () => unknown;\n /**\n * Called when the frame disconnects and unmounts.\n */\n onDisconnect: () => unknown;\n /**\n * Called when the connection process throws an exception\n */\n onConnectionError?: (error: Error) => void;\n /**\n * Optional custom URL or path.\n */\n src: string;\n /**\n * Host methods to provide only to the guest inside this iframe.\n */\n methods?: VirtualApi;\n\n /**\n *\n * @defaultValue \"100%\"\n */\n width: FrameProps[\"width\"];\n}\n\nconst defaultFrameProps: FrameProps = {\n width: \"100%\",\n height: \"100%\",\n sandbox: \"allow-scripts\",\n style: {\n border: \"none\",\n },\n};\n\n/**\n * An iframe that attaches to a running GuestServer, to display visible UI pages\n * delivered by the Extension server.\n * @public\n */\nexport function GuestUIFrame({\n guestId,\n src = \"\",\n onConnect,\n onDisconnect,\n onConnectionError,\n methods,\n ...customFrameProps\n}: PropsWithChildren<GuestUIProps>) {\n const { host } = useHost();\n if (!host) {\n return null;\n }\n const guest = host.guests.get(guestId);\n const frameUrl = new URL(src, guest.url.href);\n\n const ref = useCallback((iframe: HTMLIFrameElement) => {\n if (iframe) {\n if (methods) {\n guest.provide(methods);\n }\n const connection = guest.attachUI(iframe);\n connection\n .then(() => {\n if (onConnect) {\n onConnect();\n }\n })\n .catch((error: Error) => {\n if (onConnectionError) onConnectionError(error);\n else throw error;\n });\n return async () => {\n if (onDisconnect) {\n await onDisconnect();\n return guest.unload();\n }\n };\n }\n }, []);\n\n const frameProps = { ...defaultFrameProps, ...customFrameProps };\n\n return (\n <iframe\n ref={ref}\n src={frameUrl.href}\n name={`uix-guest-${guest.id}`}\n {...frameProps}\n sandbox=\"allow-scripts allow-downloads allow-same-origin allow-presentation\"\n />\n );\n}\nexport default GuestUIFrame;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/**\n * @public\n */\nexport class OutsideOfExtensionContextError extends Error {\n outsideOfExtensionContext: boolean;\n constructor(msg: string) {\n super(msg);\n this.outsideOfExtensionContext = true;\n Object.setPrototypeOf(this, OutsideOfExtensionContextError.prototype);\n }\n}\n\n/** @public */\ntype UseHostResponse =\n | { host: undefined; error: Error }\n | { host: Host; error: undefined };\n\n/**\n * Retrieve the {@link @adobe/uix-host#Host} object hosting all extensions inside the current parent provider.\n *\n * @remarks Returns a `{ host, error }` tuple, not the host object directly.\n * @beta\n */\nexport function useHost(): UseHostResponse {\n const host = useContext(ExtensionContext);\n if (!(host instanceof Host)) {\n const error = new OutsideOfExtensionContextError(\n \"Attempt to use extensions outside of ExtensionContext. Wrap extensible part of application with Extensible component.\"\n );\n return {\n host: undefined,\n error,\n };\n }\n return { error: undefined, host };\n}\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useCallback, useEffect, useMemo, useState } from \"react\";\nimport type {\n GuestConnection,\n GuestApis,\n RemoteGuestApis,\n VirtualApi,\n} from \"@adobe/uix-core\";\n\nimport { Host, HostEvents } from \"@adobe/uix-host\";\nimport type { CapabilitySpec } from \"@adobe/uix-host\";\nimport { useHost } from \"./useHost.js\";\n\n/**\n * @internal\n */\nexport interface TypedGuestConnection<T extends GuestApis>\n extends GuestConnection {\n id: GuestConnection[\"id\"];\n apis: RemoteGuestApis<T>;\n}\n\n/** @public */\nexport interface UseExtensionsConfig<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n> {\n /**\n * A {@link @adobe/uix-host#CapabilitySpec} describing the namespaced methods\n * extensions must implement to be used by this component.\n *\n * @remarks\n * This declaration is used to filter the extensions that will be\n * returned; if they don't implement those methods, they will be filtered out.\n */\n requires?: CapabilitySpec<Incoming>;\n /**\n * A namespaced object of methods which extensions will be able to call.\n *\n * @remarks This is the counterpart of `requires`; in `requires`, the you\n * describes methods the extension must implement that your host code will\n * call, and in `provides`, you implement host methods that extensions will be\n * able to call.\n *\n * Most cases for host-side methods will use the state of the component. This\n * can cause unexpected bugs in React if the config callback is run on every\n * render. **useExtensions caches the config callback by default!**\n * So remember to pass a deps array, so that the config callback re-runs under\n * the right conditions.\n */\n provides?: Outgoing;\n /**\n * Sets when re-render is triggered on extension load.\n *\n * @remarks\n * Set to `each` to trigger a component re-render every time an individual\n * extension loads, which may result in multiple UI updates. Set to `all` to\n * wait until all extensions have loaded to re-render the component.\n * @defaultValue \"each\"\n */\n updateOn?: \"each\" | \"all\";\n}\n\n/** @public */\nexport interface UseExtensionsResult<T extends GuestApis> {\n /**\n * A list of loaded guests which implement the methods specified in\n * `requires`, represented as {@link @adobe/uix-host#Port} objects which\n * present methods to be called.\n */\n extensions: TypedGuestConnection<T>[];\n /**\n * This is `true` until all extensions are loaded. Use for rendering spinners\n * or other intermediate UI.\n */\n loading: boolean;\n /**\n * Populated with an Error if there were any problems during the load process.\n */\n error?: Error;\n}\n\nconst NO_EXTENSIONS: [] = [];\n\n/**\n * Fetch extensions which implement an API, provide them methods, and use them.\n *\n * @remarks `useExtensions` does three things at once:\n * - Gets all extensions which implement the APIs described in the `require` field\n * - Exposes any functions defined in the `provide` field to those extensions\n * - Returns an object whose `extensions` property is a list of `Port` objects representing those extensions\n *\n * useExtensions will trigger a re-render when extensions load. You can choose whether it triggers that rerender as each extension loads, or only after all extensions have loaded.\n * @public\n */\nexport function useExtensions<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n>(\n configFactory: (host: Host) => UseExtensionsConfig<Incoming, Outgoing>,\n deps: unknown[] = []\n): UseExtensionsResult<Incoming> {\n const { host, error } = useHost();\n if (error) {\n return {\n extensions: NO_EXTENSIONS,\n loading: false,\n error,\n };\n }\n\n const baseDeps = [host, ...deps];\n const {\n requires,\n provides,\n updateOn = \"each\",\n } = useMemo(() => configFactory(host), baseDeps);\n\n const getExtensions = useCallback(() => {\n const newExtensions = [];\n const guests = host.getLoadedGuests(requires);\n for (const guest of guests) {\n if (provides) {\n guest.provide(provides);\n }\n newExtensions.push(guest as unknown as TypedGuestConnection<Incoming>);\n }\n return newExtensions.length === 0 ? NO_EXTENSIONS : newExtensions;\n }, [...baseDeps, requires]);\n\n const subscribe = useCallback(\n (handler: EventListener) => {\n const eventName = updateOn === \"all\" ? \"loadallguests\" : \"guestload\";\n host.addEventListener(eventName, handler);\n return () => host.removeEventListener(eventName, handler);\n },\n [...baseDeps, updateOn]\n );\n\n const [extensions, setExtensions] = useState(() => getExtensions());\n useEffect(() => {\n return subscribe(() => {\n setExtensions(getExtensions());\n });\n }, [subscribe, getExtensions]);\n\n const [hostError, setHostError] = useState<Error>();\n useEffect(\n () =>\n host.addEventListener(\n \"error\",\n (event: Extract<HostEvents, { detail: { error: Error } }>) =>\n setHostError(event.detail.error)\n ),\n baseDeps\n );\n\n return { extensions, loading: !host.loading, error: hostError };\n}\n"]}
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/components/Extensible.tsx","../src/extension-context.ts","../src/components/GuestUIFrame.tsx","../src/hooks/useHost.ts","../src/hooks/useExtensions.ts"],"names":["host","React","Host","useCallback","useEffect","useMemo","useState"],"mappings":";AAYA,cAAc;;;ACAd,OAAO,SAAS,UAAU,iBAA0B;AASpD,SAAS,YAAY;;;ACNrB,SAAS,qBAAqB;AAGvB,IAAM,mBAAmB,cAAoB,CAAC,CAAoB;;;AD2BzE,SAAS,uBACP,MACA,MACA;AACA,QAAM,OAAO,OAAO,KAAK,IAAI,EAAE,KAAK;AACpC,QAAM,OAAO,OAAO,KAAK,IAAI,EAAE,KAAK;AACpC,SACE,KAAK,WAAW,KAAK,UAAU,KAAK,KAAK,CAAC,IAAI,UAAU,OAAO,KAAK,MAAM;AAE9E;AAkBO,SAAS,WAAW;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAuC;AACrC,QAAM,WAAW,WAAW,OAAO,SAAS,QAAQ;AAEpD,QAAM,CAAC,YAAY,aAAa,IAAI,SAAS,CAAC,CAAC;AAC/C,YAAU,MAAM;AACd,uBAAmB,EAChB,KAAK,CAAC,WAAgC;AACrC;AAAA,QAAc,CAAC,SACb,uBAAuB,MAAM,MAAM,IAAI,SAAS;AAAA,MAClD;AAAA,IACF,CAAC,EACA,MAAM,CAAC,MAAuB;AAC7B,cAAQ,MAAM,uCAAuC,CAAC;AAAA,IACxD,CAAC;AAAA,EACL,GAAG,CAAC,kBAAkB,CAAC;AAEvB,QAAM,CAAC,MAAM,OAAO,IAAI,SAAe;AACvC,YAAU,MAAM;AACd,aAAS,SAAS,KAAa;AAC7B,aAAO,CAAC,MAAuB;AAC7B,cAAM,QAAQ,aAAa,QAAQ,IAAI,IAAI,MAAM,OAAO,CAAC,CAAC;AAC1D,gBAAQ,MAAM,KAAK,OAAO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SAAS;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,QAAM,WAAW,CAAC,MAAM,GAAG,IAAI;AAC/B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,WAAW;AAAA,EACb,IAAID,SAAQ,MAAM,cAAc,IAAI,GAAG,QAAQ;AAE/C,QAAM,gBAAgBF,aAAY,MAAM;AACtC,UAAM,gBAAgB,CAAC;AACvB,UAAM,SAAS,KAAK,gBAAgB,QAAQ;AAC5C,eAAW,SAAS,QAAQ;AAC1B,UAAI,UAAU;AACZ,cAAM,QAAQ,QAAQ;AAAA,MACxB;AACA,oBAAc,KAAK,KAAkD;AAAA,IACvE;AACA,WAAO,cAAc,WAAW,IAAI,gBAAgB;AAAA,EACtD,GAAG,CAAC,GAAG,UAAU,QAAQ,CAAC;AAE1B,QAAM,YAAYA;AAAA,IAChB,CAAC,YAA2B;AAC1B,YAAM,YAAY,aAAa,QAAQ,kBAAkB;AACzD,WAAK,iBAAiB,WAAW,OAAO;AACxC,aAAO,MAAM,KAAK,oBAAoB,WAAW,OAAO;AAAA,IAC1D;AAAA,IACA,CAAC,GAAG,UAAU,QAAQ;AAAA,EACxB;AAEA,QAAM,CAAC,YAAY,aAAa,IAAIG,UAAS,MAAM,cAAc,CAAC;AAClE,EAAAF,WAAU,MAAM;AACd,WAAO,UAAU,MAAM;AACrB,oBAAc,cAAc,CAAC;AAAA,IAC/B,CAAC;AAAA,EACH,GAAG,CAAC,WAAW,aAAa,CAAC;AAE7B,QAAM,CAAC,WAAW,YAAY,IAAIE,UAAgB;AAClD,EAAAF;AAAA,IACE,MACE,KAAK;AAAA,MACH;AAAA,MACA,CAAC,UACC,aAAa,MAAM,OAAO,KAAK;AAAA,IACnC;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,YAAY,SAAS,CAAC,KAAK,SAAS,OAAO,UAAU;AAChE","sourcesContent":["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nexport * from \"@adobe/uix-host\";\nexport * from \"./components/index.js\";\nexport * from \"./hooks/index.js\";\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport React, { useState, useEffect, useMemo } from \"react\";\nimport type { PropsWithChildren } from \"react\";\nimport type {\n InstalledExtensions,\n ExtensionsProvider,\n HostConfig,\n PortOptions,\n SharedContextValues,\n} from \"@adobe/uix-host\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/** @public */\nexport interface ExtensibleProps extends Omit<HostConfig, \"hostName\"> {\n /**\n * Unique name for identifying this extensible app. May be used as metadata in\n * extension registry in the future.\n */\n appName?: string;\n /**\n * Function which returns a promise for the full list of extensions.\n */\n extensionsProvider: ExtensionsProvider;\n /**\n * {@inheritDoc HostConfig.guestOptions}\n */\n guestOptions?: PortOptions;\n /**\n * {@inheritDoc HostConfig.sharedContext}\n */\n sharedContext?: SharedContextValues;\n}\n\nfunction areExtensionsDifferent(\n set1: InstalledExtensions,\n set2: InstalledExtensions\n) {\n const ids1 = Object.keys(set1).sort();\n const ids2 = Object.keys(set2).sort();\n return (\n ids1.length !== ids2.length || ids1.some((id, index) => id !== ids2[index])\n );\n}\n\n/**\n * Declares an extensible area in an app, and provides host and extension\n * objects to all descendents. The {@link useExtensions} hook can only be called\n * in a descendent of this component.\n *\n * @remarks\n * For many apps, there will be only one Extensible provider, fairly high in the\n * component tree. It is a context provider component that may be bundled with\n * other context providers in your app architecture.\n *\n * Each Extensible element creates one {@link @adobe/uix-host#Host} object and\n * one call to an extensions provider. If multiple Extensible elements are used,\n * this may cause redundant calls. Such a design should be carefully considered.\n *\n * @public\n */\nexport function Extensible({\n appName,\n children,\n extensionsProvider,\n guestOptions,\n runtimeContainer,\n debug,\n sharedContext,\n}: PropsWithChildren<ExtensibleProps>) {\n const hostName = appName || window.location.host || \"mainframe\";\n\n const [extensions, setExtensions] = useState({});\n useEffect(() => {\n extensionsProvider()\n .then((loaded: InstalledExtensions) => {\n setExtensions((prev) =>\n areExtensionsDifferent(prev, loaded) ? loaded : prev\n );\n })\n .catch((e: Error | unknown) => {\n console.error(\"Fetching list of extensions failed!\", e);\n });\n }, [extensionsProvider]);\n\n const [host, setHost] = useState<Host>();\n useEffect(() => {\n function logError(msg: string) {\n return (e: Error | unknown) => {\n const error = e instanceof Error ? e : new Error(String(e));\n console.error(msg, error, extensions, guestOptions);\n };\n }\n\n const host = new Host({ debug, hostName, runtimeContainer, sharedContext });\n setHost(host);\n\n if (!Object.entries(extensions).length) {\n return;\n }\n\n host\n .load(extensions, guestOptions)\n .catch(logError(\"Load of extensions failed!\"));\n return () => {\n host.unload().catch(logError(\"Unload of extensions failed!\"));\n };\n }, [debug, hostName, runtimeContainer, sharedContext, extensions]);\n\n // skip render before host is initialized\n if (!host) {\n return null;\n }\n\n return (\n <ExtensionContext.Provider value={host}>\n {children}\n </ExtensionContext.Provider>\n );\n}\nexport default Extensible;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\n/**\n * @hidden\n */\nimport { createContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\n\nexport const ExtensionContext = createContext<Host>({} as unknown as Host);\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { VirtualApi } from \"@adobe/uix-core\";\nimport React, { useCallback } from \"react\";\nimport type { PropsWithChildren, IframeHTMLAttributes } from \"react\";\nimport { useHost } from \"../hooks/useHost.js\";\nimport type { AttrTokens, SandboxToken } from \"@adobe/uix-host\";\nimport { makeSandboxAttrs, requiredIframeProps } from \"@adobe/uix-host\";\n\n/**\n * @public\n */\ntype FrameProps = Omit<IframeHTMLAttributes<HTMLIFrameElement>, \"sandbox\">;\n\n/** @public */\nexport interface GuestUIProps extends FrameProps {\n guestId: string;\n /**\n * Receives the Penpal context when the frame is mounted.\n */\n onConnect: () => unknown;\n /**\n * Called when the frame disconnects and unmounts.\n */\n onDisconnect: () => unknown;\n /**\n * Called when the connection process throws an exception\n */\n onConnectionError?: (error: Error) => void;\n /**\n * Additional sandbox attributes GuestUIFrame might need.\n */\n sandbox: AttrTokens<SandboxToken>;\n /**\n * Optional custom URL or path.\n */\n src: string;\n /**\n * Host methods to provide only to the guest inside this iframe.\n */\n methods?: VirtualApi;\n\n /**\n *\n * @defaultValue \"100%\"\n */\n width: FrameProps[\"width\"];\n}\n\nconst defaultFrameProps: FrameProps = {\n width: \"100%\",\n height: \"100%\",\n style: {\n border: \"none\",\n },\n};\n\nconst defaultSandbox = \"allow-scripts\";\n\n/**\n * An iframe that attaches to a running GuestServer, to display visible UI pages\n * delivered by the Extension server.\n * @public\n */\nexport function GuestUIFrame({\n guestId,\n src = \"\",\n onConnect,\n onDisconnect,\n onConnectionError,\n methods,\n sandbox = \"\",\n ...customFrameProps\n}: PropsWithChildren<GuestUIProps>) {\n const { host } = useHost();\n if (!host) {\n return null;\n }\n const guest = host.guests.get(guestId);\n const frameUrl = new URL(src, guest.url.href);\n\n const ref = useCallback((iframe: HTMLIFrameElement) => {\n if (iframe) {\n if (methods) {\n guest.provide(methods);\n }\n const connection = guest.attachUI(iframe);\n connection\n .then(() => {\n if (onConnect) {\n onConnect();\n }\n })\n .catch((error: Error) => {\n if (onConnectionError) onConnectionError(error);\n else throw error;\n });\n return async () => {\n if (onDisconnect) {\n await onDisconnect();\n return guest.unload();\n }\n };\n }\n }, []);\n\n const frameProps = {\n ...defaultFrameProps,\n ...customFrameProps,\n ...requiredIframeProps,\n };\n\n return (\n <iframe\n ref={ref}\n src={frameUrl.href}\n name={`uix-guest-${guest.id}`}\n sandbox={\n sandbox\n ? makeSandboxAttrs(defaultSandbox, sandbox).join(\" \")\n : defaultSandbox\n }\n {...frameProps}\n />\n );\n}\nexport default GuestUIFrame;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/**\n * @public\n */\nexport class OutsideOfExtensionContextError extends Error {\n outsideOfExtensionContext: boolean;\n constructor(msg: string) {\n super(msg);\n this.outsideOfExtensionContext = true;\n Object.setPrototypeOf(this, OutsideOfExtensionContextError.prototype);\n }\n}\n\n/** @public */\ntype UseHostResponse =\n | { host: undefined; error: Error }\n | { host: Host; error: undefined };\n\n/**\n * Retrieve the {@link @adobe/uix-host#Host} object hosting all extensions inside the current parent provider.\n *\n * @remarks Returns a `{ host, error }` tuple, not the host object directly.\n * @beta\n */\nexport function useHost(): UseHostResponse {\n const host = useContext(ExtensionContext);\n if (!(host instanceof Host)) {\n const error = new OutsideOfExtensionContextError(\n \"Attempt to use extensions outside of ExtensionContext. Wrap extensible part of application with Extensible component.\"\n );\n return {\n host: undefined,\n error,\n };\n }\n return { error: undefined, host };\n}\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useCallback, useEffect, useMemo, useState } from \"react\";\nimport type {\n GuestConnection,\n GuestApis,\n RemoteGuestApis,\n VirtualApi,\n} from \"@adobe/uix-core\";\n\nimport { Host, HostEvents } from \"@adobe/uix-host\";\nimport type { CapabilitySpec } from \"@adobe/uix-host\";\nimport { useHost } from \"./useHost.js\";\n\n/**\n * @internal\n */\nexport interface TypedGuestConnection<T extends GuestApis>\n extends GuestConnection {\n id: GuestConnection[\"id\"];\n apis: RemoteGuestApis<T>;\n}\n\n/** @public */\nexport interface UseExtensionsConfig<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n> {\n /**\n * A {@link @adobe/uix-host#CapabilitySpec} describing the namespaced methods\n * extensions must implement to be used by this component.\n *\n * @remarks\n * This declaration is used to filter the extensions that will be\n * returned; if they don't implement those methods, they will be filtered out.\n */\n requires?: CapabilitySpec<Incoming>;\n /**\n * A namespaced object of methods which extensions will be able to call.\n *\n * @remarks This is the counterpart of `requires`; in `requires`, the you\n * describes methods the extension must implement that your host code will\n * call, and in `provides`, you implement host methods that extensions will be\n * able to call.\n *\n * Most cases for host-side methods will use the state of the component. This\n * can cause unexpected bugs in React if the config callback is run on every\n * render. **useExtensions caches the config callback by default!**\n * So remember to pass a deps array, so that the config callback re-runs under\n * the right conditions.\n */\n provides?: Outgoing;\n /**\n * Sets when re-render is triggered on extension load.\n *\n * @remarks\n * Set to `each` to trigger a component re-render every time an individual\n * extension loads, which may result in multiple UI updates. Set to `all` to\n * wait until all extensions have loaded to re-render the component.\n * @defaultValue \"each\"\n */\n updateOn?: \"each\" | \"all\";\n}\n\n/** @public */\nexport interface UseExtensionsResult<T extends GuestApis> {\n /**\n * A list of loaded guests which implement the methods specified in\n * `requires`, represented as {@link @adobe/uix-host#Port} objects which\n * present methods to be called.\n */\n extensions: TypedGuestConnection<T>[];\n /**\n * This is `true` until all extensions are loaded. Use for rendering spinners\n * or other intermediate UI.\n */\n loading: boolean;\n /**\n * Populated with an Error if there were any problems during the load process.\n */\n error?: Error;\n}\n\nconst NO_EXTENSIONS: [] = [];\n\n/**\n * Fetch extensions which implement an API, provide them methods, and use them.\n *\n * @remarks `useExtensions` does three things at once:\n * - Gets all extensions which implement the APIs described in the `require` field\n * - Exposes any functions defined in the `provide` field to those extensions\n * - Returns an object whose `extensions` property is a list of `Port` objects representing those extensions\n *\n * useExtensions will trigger a re-render when extensions load. You can choose whether it triggers that rerender as each extension loads, or only after all extensions have loaded.\n * @public\n */\nexport function useExtensions<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n>(\n configFactory: (host: Host) => UseExtensionsConfig<Incoming, Outgoing>,\n deps: unknown[] = []\n): UseExtensionsResult<Incoming> {\n const { host, error } = useHost();\n if (error) {\n return {\n extensions: NO_EXTENSIONS,\n loading: false,\n error,\n };\n }\n\n const baseDeps = [host, ...deps];\n const {\n requires,\n provides,\n updateOn = \"each\",\n } = useMemo(() => configFactory(host), baseDeps);\n\n const getExtensions = useCallback(() => {\n const newExtensions = [];\n const guests = host.getLoadedGuests(requires);\n for (const guest of guests) {\n if (provides) {\n guest.provide(provides);\n }\n newExtensions.push(guest as unknown as TypedGuestConnection<Incoming>);\n }\n return newExtensions.length === 0 ? NO_EXTENSIONS : newExtensions;\n }, [...baseDeps, requires]);\n\n const subscribe = useCallback(\n (handler: EventListener) => {\n const eventName = updateOn === \"all\" ? \"loadallguests\" : \"guestload\";\n host.addEventListener(eventName, handler);\n return () => host.removeEventListener(eventName, handler);\n },\n [...baseDeps, updateOn]\n );\n\n const [extensions, setExtensions] = useState(() => getExtensions());\n useEffect(() => {\n return subscribe(() => {\n setExtensions(getExtensions());\n });\n }, [subscribe, getExtensions]);\n\n const [hostError, setHostError] = useState<Error>();\n useEffect(\n () =>\n host.addEventListener(\n \"error\",\n (event: Extract<HostEvents, { detail: { error: Error } }>) =>\n setHostError(event.detail.error)\n ),\n baseDeps\n );\n\n return { extensions, loading: !host.loading, error: hostError };\n}\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@adobe/uix-host-react",
|
3
|
-
"version": "0.7.1-nightly.
|
3
|
+
"version": "0.7.1-nightly.20230126",
|
4
4
|
"publishConfig": {
|
5
5
|
"access": "public"
|
6
6
|
},
|
@@ -18,8 +18,8 @@
|
|
18
18
|
],
|
19
19
|
"bugs": "https://github.com/adobe/uix-sdk/issues",
|
20
20
|
"dependencies": {
|
21
|
-
"@adobe/uix-core": "^0.7.1-nightly.
|
22
|
-
"@adobe/uix-host": "^0.7.1-nightly.
|
21
|
+
"@adobe/uix-core": "^0.7.1-nightly.20230126",
|
22
|
+
"@adobe/uix-host": "^0.7.1-nightly.20230126"
|
23
23
|
},
|
24
24
|
"files": [
|
25
25
|
"README.md",
|
@@ -14,11 +14,13 @@ import type { VirtualApi } from "@adobe/uix-core";
|
|
14
14
|
import React, { useCallback } from "react";
|
15
15
|
import type { PropsWithChildren, IframeHTMLAttributes } from "react";
|
16
16
|
import { useHost } from "../hooks/useHost.js";
|
17
|
+
import type { AttrTokens, SandboxToken } from "@adobe/uix-host";
|
18
|
+
import { makeSandboxAttrs, requiredIframeProps } from "@adobe/uix-host";
|
17
19
|
|
18
20
|
/**
|
19
21
|
* @public
|
20
22
|
*/
|
21
|
-
type FrameProps = IframeHTMLAttributes<HTMLIFrameElement>;
|
23
|
+
type FrameProps = Omit<IframeHTMLAttributes<HTMLIFrameElement>, "sandbox">;
|
22
24
|
|
23
25
|
/** @public */
|
24
26
|
export interface GuestUIProps extends FrameProps {
|
@@ -35,6 +37,10 @@ export interface GuestUIProps extends FrameProps {
|
|
35
37
|
* Called when the connection process throws an exception
|
36
38
|
*/
|
37
39
|
onConnectionError?: (error: Error) => void;
|
40
|
+
/**
|
41
|
+
* Additional sandbox attributes GuestUIFrame might need.
|
42
|
+
*/
|
43
|
+
sandbox: AttrTokens<SandboxToken>;
|
38
44
|
/**
|
39
45
|
* Optional custom URL or path.
|
40
46
|
*/
|
@@ -54,12 +60,13 @@ export interface GuestUIProps extends FrameProps {
|
|
54
60
|
const defaultFrameProps: FrameProps = {
|
55
61
|
width: "100%",
|
56
62
|
height: "100%",
|
57
|
-
sandbox: "allow-scripts",
|
58
63
|
style: {
|
59
64
|
border: "none",
|
60
65
|
},
|
61
66
|
};
|
62
67
|
|
68
|
+
const defaultSandbox = "allow-scripts";
|
69
|
+
|
63
70
|
/**
|
64
71
|
* An iframe that attaches to a running GuestServer, to display visible UI pages
|
65
72
|
* delivered by the Extension server.
|
@@ -72,6 +79,7 @@ export function GuestUIFrame({
|
|
72
79
|
onDisconnect,
|
73
80
|
onConnectionError,
|
74
81
|
methods,
|
82
|
+
sandbox = "",
|
75
83
|
...customFrameProps
|
76
84
|
}: PropsWithChildren<GuestUIProps>) {
|
77
85
|
const { host } = useHost();
|
@@ -106,15 +114,23 @@ export function GuestUIFrame({
|
|
106
114
|
}
|
107
115
|
}, []);
|
108
116
|
|
109
|
-
const frameProps = {
|
117
|
+
const frameProps = {
|
118
|
+
...defaultFrameProps,
|
119
|
+
...customFrameProps,
|
120
|
+
...requiredIframeProps,
|
121
|
+
};
|
110
122
|
|
111
123
|
return (
|
112
124
|
<iframe
|
113
125
|
ref={ref}
|
114
126
|
src={frameUrl.href}
|
115
127
|
name={`uix-guest-${guest.id}`}
|
128
|
+
sandbox={
|
129
|
+
sandbox
|
130
|
+
? makeSandboxAttrs(defaultSandbox, sandbox).join(" ")
|
131
|
+
: defaultSandbox
|
132
|
+
}
|
116
133
|
{...frameProps}
|
117
|
-
sandbox="allow-scripts allow-downloads allow-same-origin allow-presentation"
|
118
134
|
/>
|
119
135
|
);
|
120
136
|
}
|