@smart-cloud/ai-kit-ui 1.1.16 → 1.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart-cloud/ai-kit-ui",
3
- "version": "1.1.16",
3
+ "version": "1.1.18",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  import createCache from "@emotion/cache";
2
2
  import { CacheProvider } from "@emotion/react";
3
- import { useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import { SetStateAction, useLayoutEffect, useMemo, useRef, useState } from "react";
4
4
  import { createPortal } from "react-dom";
5
5
 
6
6
  export type ShadowBoundaryMode = "local" | "overlay";
@@ -27,6 +27,7 @@ export type ShadowBoundaryProps = {
27
27
  */
28
28
  overlayRootId?: string;
29
29
 
30
+ setHost: React.Dispatch<SetStateAction<HTMLElement | null>>;
30
31
  children: (api: {
31
32
  /** Portal target element inside the shadow root. */
32
33
  rootElement: HTMLDivElement;
@@ -89,6 +90,7 @@ export function ShadowBoundary({
89
90
  rootElementId,
90
91
  mode = "local",
91
92
  overlayRootId = "ai-kit-overlay-root",
93
+ setHost,
92
94
  }: ShadowBoundaryProps) {
93
95
  const hostRef = useRef<HTMLDivElement | null>(null);
94
96
 
@@ -131,6 +133,7 @@ export function ShadowBoundary({
131
133
  } else {
132
134
  host = hostRef.current;
133
135
  }
136
+ setHost(host);
134
137
 
135
138
  // 2) Ensure shadow root
136
139
  const shadow = host.shadowRoot ?? host.attachShadow({ mode: "open" });
@@ -14,7 +14,7 @@ import {
14
14
  } from "@smart-cloud/ai-kit-core";
15
15
  import { useSelect } from "@wordpress/data";
16
16
  import { I18n } from "aws-amplify/utils";
17
- import { type ComponentType, useCallback, useMemo, useState } from "react";
17
+ import { type ComponentType, useEffect, useMemo, useState } from "react";
18
18
  import { ShadowBoundary } from "./ShadowBoundary";
19
19
 
20
20
  export type AiKitShellInjectedProps = {
@@ -52,6 +52,7 @@ export function withAiKitShell<P extends object>(
52
52
  direction,
53
53
  } = { ...props, ...propOverrides } as AiWorkerProps & P;
54
54
 
55
+ const [host, setHost] = useState<HTMLElement | null>(null);
55
56
  const languageInStore: string | undefined | null = useSelect(() =>
56
57
  getStoreSelect(store).getLanguage(),
57
58
  );
@@ -178,23 +179,27 @@ export function withAiKitShell<P extends object>(
178
179
  return themeOverrides ? hashStringDjb2(themeOverrides) : "";
179
180
  }, [themeOverrides]);
180
181
 
181
- const injectStyle = useCallback(
182
- (rootElement: HTMLDivElement) => {
183
- const existingStyle = rootElement.ownerDocument.getElementById(
182
+ useEffect(
183
+ () => {
184
+
185
+ if (!host) {
186
+ return;
187
+ }
188
+
189
+ const existingStyle = host.shadowRoot!.getElementById(
184
190
  STYLE_TEXT_ID,
185
191
  ) as HTMLStyleElement | null;
186
192
 
187
193
  if (themeOverrides) {
188
194
  if (!existingStyle) {
189
- const s = rootElement.ownerDocument.createElement("style");
195
+ const s = host.shadowRoot!.ownerDocument.createElement("style");
190
196
  s.id = STYLE_TEXT_ID;
191
197
  s.setAttribute("data-hash", themeOverridesHash);
192
198
  s.textContent = themeOverrides;
193
- rootElement.appendChild(s);
199
+ host.shadowRoot!.appendChild(s);
194
200
  } else {
195
201
  const prevHash = existingStyle.getAttribute("data-hash") || "";
196
202
  if (prevHash !== themeOverridesHash) {
197
- existingStyle.setAttribute("data-hash", themeOverridesHash);
198
203
  existingStyle.textContent = themeOverrides;
199
204
  }
200
205
  }
@@ -202,7 +207,7 @@ export function withAiKitShell<P extends object>(
202
207
  existingStyle.remove();
203
208
  }
204
209
  },
205
- [themeOverrides, themeOverridesHash],
210
+ [host, themeOverrides, themeOverridesHash],
206
211
  );
207
212
 
208
213
  return (
@@ -211,6 +216,7 @@ export function withAiKitShell<P extends object>(
211
216
  variation={variation}
212
217
  overlayRootId="ai-kit-overlay-root"
213
218
  stylesheets={stylesheets}
219
+ setHost={setHost}
214
220
  rootElementId={
215
221
  variation === "modal" && !showOpenButton
216
222
  ? "ai-kit-portal-root"
@@ -218,7 +224,6 @@ export function withAiKitShell<P extends object>(
218
224
  }
219
225
  >
220
226
  {({ rootElement }) => {
221
- injectStyle(rootElement);
222
227
  rootElement.setAttribute(
223
228
  "data-ai-kit-variation",
224
229
  variation || "default",