@@ -6,6 +6,34 @@ import { preloadMethods } from './api';
66export let penpalParent : PromisifiedPenpalParentMethods | null = null ;
77let 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+
937const 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