Skip to content

Commit 7ddc018

Browse files
authored
Attempt storybook setup in onlook containers (#3042)
* Attempt storybook setup * Coderabbit fix
1 parent 7b121d7 commit 7ddc018

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="module" src="/onlook-preload-script.js" id="onlook-preload-script"></script>

apps/web/client/public/onlook-preload-script.js

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/web/preload/script/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@ import { preloadMethods } from './api';
66
export let penpalParent: PromisifiedPenpalParentMethods | null = null;
77
let isConnecting = false;
88

9+
/**
10+
* Find the correct parent window for Onlook connection.
11+
* Handles both direct iframes (Next.js) and nested iframes (Storybook).
12+
*/
13+
const findOnlookParent = (): Window => {
14+
// If we're not in an iframe, something is wrong
15+
if (window === window.top) {
16+
console.warn(`${PENPAL_CHILD_CHANNEL} - Not in an iframe, using window.parent as fallback`);
17+
return window.parent;
18+
}
19+
20+
// Check if we're in a direct iframe (parent is the top window)
21+
// This is the Next.js case: Onlook -> Next.js iframe
22+
if (window.parent === window.top) {
23+
return window.parent;
24+
}
25+
26+
// We're in a nested iframe (parent is NOT the top window)
27+
// This is the Storybook case: Onlook -> CodeSandbox -> Storybook preview iframe
28+
if (window.top) {
29+
console.log(`${PENPAL_CHILD_CHANNEL} - Using window.top for nested iframe scenario`);
30+
return window.top;
31+
}
32+
33+
// Final fallback
34+
return window.parent;
35+
};
36+
937
const createMessageConnection = async () => {
1038
if (isConnecting || penpalParent) {
1139
return penpalParent;
@@ -15,7 +43,7 @@ const createMessageConnection = async () => {
1543
console.log(`${PENPAL_CHILD_CHANNEL} - Creating penpal connection`);
1644

1745
const messenger = new WindowMessenger({
18-
remoteWindow: window.parent,
46+
remoteWindow: findOnlookParent(),
1947
// TODO: Use a proper origin
2048
allowedOrigins: ['*'],
2149
});

0 commit comments

Comments
 (0)