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

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.17",
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,31 +179,40 @@ 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
 
193
+ console.log('injectStyle called', host, host.shadowRoot, existingStyle);
194
+
187
195
  if (themeOverrides) {
188
196
  if (!existingStyle) {
189
- const s = rootElement.ownerDocument.createElement("style");
197
+ console.log('Creating new style element');
198
+ const s = host.shadowRoot!.ownerDocument.createElement("style");
190
199
  s.id = STYLE_TEXT_ID;
191
200
  s.setAttribute("data-hash", themeOverridesHash);
192
201
  s.textContent = themeOverrides;
193
- rootElement.appendChild(s);
202
+ host.shadowRoot!.appendChild(s);
194
203
  } else {
195
204
  const prevHash = existingStyle.getAttribute("data-hash") || "";
196
205
  if (prevHash !== themeOverridesHash) {
197
- existingStyle.setAttribute("data-hash", themeOverridesHash);
206
+ console.log('Updating existing style element');
198
207
  existingStyle.textContent = themeOverrides;
199
208
  }
200
209
  }
201
210
  } else if (existingStyle) {
211
+ console.log('Removing existing style element');
202
212
  existingStyle.remove();
203
213
  }
204
214
  },
205
- [themeOverrides, themeOverridesHash],
215
+ [host, themeOverrides, themeOverridesHash],
206
216
  );
207
217
 
208
218
  return (
@@ -211,6 +221,7 @@ export function withAiKitShell<P extends object>(
211
221
  variation={variation}
212
222
  overlayRootId="ai-kit-overlay-root"
213
223
  stylesheets={stylesheets}
224
+ setHost={setHost}
214
225
  rootElementId={
215
226
  variation === "modal" && !showOpenButton
216
227
  ? "ai-kit-portal-root"
@@ -218,7 +229,6 @@ export function withAiKitShell<P extends object>(
218
229
  }
219
230
  >
220
231
  {({ rootElement }) => {
221
- injectStyle(rootElement);
222
232
  rootElement.setAttribute(
223
233
  "data-ai-kit-variation",
224
234
  variation || "default",