1
- import { forwardRef , FunctionComponent , PropsWithChildren } from 'react'
1
+ import {
2
+ ForwardedRef ,
3
+ forwardRef ,
4
+ FunctionComponent ,
5
+ PropsWithChildren ,
6
+ } from 'react'
2
7
import {
3
8
Alert ,
4
9
AlertTitle ,
5
10
Backdrop ,
6
11
Box ,
12
+ Dialog ,
7
13
SxProps ,
8
14
Typography ,
9
15
} from '@mui/material'
@@ -68,6 +74,57 @@ const FieldTypeSandbox: FunctionComponent<
68
74
</ Box >
69
75
)
70
76
77
+ const FieldPluginIframe = forwardRef <
78
+ HTMLIFrameElement ,
79
+ {
80
+ src : string | undefined
81
+ fullHeight : boolean
82
+ modal : boolean
83
+ height : number
84
+ }
85
+ > ( function FieldPluginIframe ( props , ref ) {
86
+ const { src, fullHeight, modal, height } = props
87
+
88
+ if ( typeof src === 'undefined' ) {
89
+ return (
90
+ < Alert
91
+ severity = "error"
92
+ sx = { {
93
+ width : '100%' ,
94
+ } }
95
+ >
96
+ < AlertTitle > Unable to Load Field Plugin</ AlertTitle >
97
+ < Typography > Please enter a valid URL.</ Typography >
98
+ </ Alert >
99
+ )
100
+ }
101
+
102
+ return (
103
+ < Box
104
+ ref = { ref }
105
+ component = "iframe"
106
+ src = { src }
107
+ title = "Field Plugin Preview"
108
+ style = { {
109
+ height : fullHeight && modal ? 'auto' : height ,
110
+ width : '100%' ,
111
+ flex : 1 ,
112
+ border : 'none' ,
113
+ } }
114
+ />
115
+ )
116
+ } )
117
+
118
+ const setRef = < T , > ( ref : ForwardedRef < T > , value : T | null ) => {
119
+ if ( ref === null ) {
120
+ return
121
+ } else if ( typeof ref === 'function' ) {
122
+ ref ( value )
123
+ } else {
124
+ ref . current = value
125
+ }
126
+ }
127
+
71
128
export const FieldTypePreview = forwardRef <
72
129
HTMLIFrameElement ,
73
130
{
@@ -86,39 +143,57 @@ export const FieldTypePreview = forwardRef<
86
143
const isNonPortalModalOpen = ! enablePortalModal && isModal
87
144
const isPortalModalOpen = enablePortalModal && isModal
88
145
146
+ const setTeleported = ( el : HTMLIFrameElement | null ) => {
147
+ if ( isPortalModalOpen ) {
148
+ setRef ( ref , el )
149
+ }
150
+ }
151
+ const setNonTeleported = ( el : HTMLIFrameElement | null ) => {
152
+ if ( ! isPortalModalOpen ) {
153
+ setRef ( ref , el )
154
+ }
155
+ }
156
+
89
157
return (
90
158
< Box sx = { props . sx } >
91
159
< DisableShieldsNotification />
160
+ < Dialog
161
+ open = { isPortalModalOpen }
162
+ fullScreen
163
+ sx = { {
164
+ padding : 10 ,
165
+ } }
166
+ PaperProps = { {
167
+ sx : {
168
+ borderRadius : 2 ,
169
+ overflow : 'hidden' ,
170
+ } ,
171
+ } }
172
+ >
173
+ < FieldPluginIframe
174
+ key = { props . iframeKey }
175
+ src = { props . src }
176
+ ref = { setTeleported }
177
+ fullHeight = { fullHeight }
178
+ modal = { isModal }
179
+ height = { height }
180
+ />
181
+ </ Dialog >
92
182
< Backdrop
93
- open = { props . isModal }
183
+ open = { isNonPortalModalOpen }
94
184
sx = { { zIndex : ( { zIndex } ) => zIndex . drawer } }
95
185
/>
96
186
< NonPortalModal isNonPortalModalOpen = { isNonPortalModalOpen } >
97
187
< FieldTypeSandbox isNonPortalModalOpen = { isNonPortalModalOpen } >
98
- { typeof props . src !== 'undefined' ? (
99
- < Box
100
- ref = { ref }
188
+ { ! isPortalModalOpen && (
189
+ < FieldPluginIframe
101
190
key = { props . iframeKey }
102
- component = "iframe"
103
191
src = { props . src }
104
- title = "Field Plugin Preview"
105
- style = { {
106
- height : fullHeight && isModal ? 'auto' : height ,
107
- width : '100%' ,
108
- flex : 1 ,
109
- border : 'none' ,
110
- } }
192
+ ref = { setNonTeleported }
193
+ fullHeight = { fullHeight }
194
+ modal = { isModal }
195
+ height = { height }
111
196
/>
112
- ) : (
113
- < Alert
114
- severity = "error"
115
- sx = { {
116
- width : '100%' ,
117
- } }
118
- >
119
- < AlertTitle > Unable to Load Field Plugin</ AlertTitle >
120
- < Typography > Please enter a valid URL.</ Typography >
121
- </ Alert >
122
197
) }
123
198
</ FieldTypeSandbox >
124
199
</ NonPortalModal >
0 commit comments