Skip to content

Commit 080435b

Browse files
feat: add hook check for if the dojo is iframed from cpk docs
1 parent 8d09050 commit 080435b

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

typescript-sdk/apps/dojo/src/components/code-viewer/code-viewer.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { useMemo, useState } from "react";
1+
import { useMemo } from "react";
22
import { FileTree } from "@/components/file-tree/file-tree";
33
import { CodeEditor } from "./code-editor";
44
import { FeatureFile } from "@/types/feature";
55
import { useURLParams } from "@/contexts/url-params-context";
66
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
7+
import { useIsInsideIframe } from "@/utils/use-in-iframe";
8+
import { cn } from "@/lib/utils";
79

810
export default function CodeViewer({ codeFiles }: { codeFiles: FeatureFile[] }) {
911
const { file, setCodeFile, codeLayout } = useURLParams();
12+
const isInsideIframe = useIsInsideIframe();
13+
const isInsideCpkFrame = isInsideIframe && (document.referrer.includes("copilotkit.com") || document.referrer.includes("localhost"));
1014

1115
const selectedFile = useMemo(
1216
() => codeFiles.find((f) => f.name === file) ?? codeFiles[0],
@@ -38,7 +42,10 @@ export default function CodeViewer({ codeFiles }: { codeFiles: FeatureFile[] })
3842
value={file.name}
3943
className="flex-1 mt-0 data-[state=inactive]:hidden"
4044
>
41-
<div className="h-full rounded-xl overflow-hidden border border-b-0 border-cpk-docs-dark-bg/8 dark:border-white/6 -mx-px">
45+
<div className={cn(
46+
"h-full border border-b-0 border-cpk-docs-dark-bg/8 dark:border-white/6 -mx-px",
47+
isInsideCpkFrame && "rounded-xl overflow-hidden",
48+
)}>
4249
<CodeEditor file={file} />
4350
</div>
4451
</TabsContent>
@@ -49,17 +56,23 @@ export default function CodeViewer({ codeFiles }: { codeFiles: FeatureFile[] })
4956
}
5057

5158
return (
52-
<div className="flex h-full bg-white">
59+
<div className="flex h-full w-full bg-white">
5360
{/* wrapper div to mix the parent bg-white with bg-cpk-docs-dark-bg */}
54-
<div className="bg-cpk-docs-dark-bg/3 dark:bg-cpk-docs-dark-bg/95">
61+
<div className="flex h-full w-full bg-cpk-docs-dark-bg/3 dark:bg-cpk-docs-dark-bg/95">
5562
<div className="w-72 border-r border-gray-200 dark:border-neutral-700 flex flex-col">
5663
<div className="flex-1 overflow-auto">
5764
<FileTree files={codeFiles} selectedFile={selectedFile} onFileSelect={setCodeFile} />
5865
</div>
5966
</div>
60-
<div className="flex-1 h-full py-5 bg-gray-50 dark:bg-[#1e1e1e] rounded-xl overflow-hidden">
67+
<div className={cn(
68+
"flex-1 h-full bg-gray-50 dark:bg-[#1e1e1e]",
69+
isInsideCpkFrame && "rounded-xl overflow-hidden",
70+
)}>
6171
{selectedFile ? (
62-
<div className="h-full rounded-xl overflow-hidden border border-b-0 border-cpk-docs-dark-bg/8 dark:border-white/6 -mx-px">
72+
<div className={cn(
73+
"h-full border-cpk-docs-dark-bg/8 dark:border-white/6",
74+
isInsideCpkFrame && "rounded-xl overflow-hidden",
75+
)}>
6376
<CodeEditor file={selectedFile} />
6477
</div>
6578
) : (
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useEffect, useState } from "react";
2+
3+
export function useIsInsideIframe() {
4+
const [isInside, setIsInside] = useState(false);
5+
6+
useEffect(() => {
7+
const check = () => {
8+
// Check if the window is a self-reference and not the top-level window
9+
setIsInside(window.self !== window.top);
10+
};
11+
check();
12+
// Optionally, you could add an event listener for resize if the iframe behavior needs to be tracked dynamically.
13+
}, []);
14+
15+
return isInside;
16+
}

0 commit comments

Comments
 (0)