1
+ import { Size } from 'noya-geometry' ;
1
2
import { assignRef } from 'noya-react-utils' ;
2
3
import React , {
3
- HTMLProps ,
4
4
forwardRef ,
5
5
memo ,
6
6
useCallback ,
7
7
useEffect ,
8
+ useMemo ,
8
9
useRef ,
9
10
useState ,
10
11
} from 'react' ;
11
12
12
- interface Props extends HTMLProps < HTMLIFrameElement > {
13
+ interface Props {
13
14
onReady ?: ( ) => void ;
15
+ onResize ?: ( size : Size ) => void ;
14
16
title : string ;
15
17
}
16
18
17
19
export const ControlledFrame = memo (
18
20
forwardRef ( function ControlledFrame (
19
- { onReady, ... props } : Props ,
21
+ { onReady, onResize , title } : Props ,
20
22
forwardedRef : React . ForwardedRef < HTMLIFrameElement > ,
21
23
) {
22
24
const ref = useRef < HTMLIFrameElement | null > ( null ) ;
@@ -34,6 +36,10 @@ export const ControlledFrame = memo(
34
36
ok = true ;
35
37
break ;
36
38
}
39
+ case 'resize' : {
40
+ onResize ?.( event . data . size ) ;
41
+ break ;
42
+ }
37
43
case 'keydown' : {
38
44
const customEvent = new KeyboardEvent ( 'keydown' , {
39
45
key : event . data . command . key ,
@@ -61,7 +67,7 @@ export const ControlledFrame = memo(
61
67
return ( ) => {
62
68
window . removeEventListener ( 'message' , listener ) ;
63
69
} ;
64
- } , [ id , onReady ] ) ;
70
+ } , [ id , onReady , onResize ] ) ;
65
71
66
72
const handleRef = useCallback (
67
73
( value : HTMLIFrameElement | null ) => {
@@ -71,16 +77,14 @@ export const ControlledFrame = memo(
71
77
[ forwardedRef ] ,
72
78
) ;
73
79
80
+ const style = useMemo ( ( ) => ( { width : '100%' , height : '100%' } ) , [ ] ) ;
81
+
74
82
return (
75
83
< iframe
76
84
ref = { handleRef }
77
85
tabIndex = { - 1 }
78
- style = { {
79
- width : '100%' ,
80
- height : '100%' ,
81
- } }
82
- { ...props }
83
- title = { props . title }
86
+ title = { title }
87
+ style = { style }
84
88
// Ensure html5 doctype for proper styling
85
89
srcDoc = { `<!DOCTYPE html>
86
90
<head>
@@ -139,6 +143,15 @@ export const ControlledFrame = memo(
139
143
document.addEventListener('DOMContentLoaded', callback);
140
144
}
141
145
146
+ // Handle window resize events
147
+ window.addEventListener('resize', function(event) {
148
+ parent.postMessage({
149
+ id,
150
+ type: 'resize',
151
+ size: { width: window.innerWidth, height: window.innerHeight }
152
+ }, '*');
153
+ });
154
+
142
155
// Propagate keyboard shortcuts (keydown events) to the parent window
143
156
window.addEventListener('keydown', function(event) {
144
157
// Check if Cmd (for Mac) or Ctrl (for other OS) is pressed
0 commit comments