Skip to content

Commit c603fcb

Browse files
fix(sandbox): implementation for sandbox
1 parent 7ede7bf commit c603fcb

File tree

1 file changed

+98
-23
lines changed

1 file changed

+98
-23
lines changed

packages/sandbox/src/components/FieldTypePreview.tsx

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { forwardRef, FunctionComponent, PropsWithChildren } from 'react'
1+
import {
2+
ForwardedRef,
3+
forwardRef,
4+
FunctionComponent,
5+
PropsWithChildren,
6+
} from 'react'
27
import {
38
Alert,
49
AlertTitle,
510
Backdrop,
611
Box,
12+
Dialog,
713
SxProps,
814
Typography,
915
} from '@mui/material'
@@ -68,6 +74,57 @@ const FieldTypeSandbox: FunctionComponent<
6874
</Box>
6975
)
7076

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+
71128
export const FieldTypePreview = forwardRef<
72129
HTMLIFrameElement,
73130
{
@@ -86,39 +143,57 @@ export const FieldTypePreview = forwardRef<
86143
const isNonPortalModalOpen = !enablePortalModal && isModal
87144
const isPortalModalOpen = enablePortalModal && isModal
88145

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+
89157
return (
90158
<Box sx={props.sx}>
91159
<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>
92182
<Backdrop
93-
open={props.isModal}
183+
open={isNonPortalModalOpen}
94184
sx={{ zIndex: ({ zIndex }) => zIndex.drawer }}
95185
/>
96186
<NonPortalModal isNonPortalModalOpen={isNonPortalModalOpen}>
97187
<FieldTypeSandbox isNonPortalModalOpen={isNonPortalModalOpen}>
98-
{typeof props.src !== 'undefined' ? (
99-
<Box
100-
ref={ref}
188+
{!isPortalModalOpen && (
189+
<FieldPluginIframe
101190
key={props.iframeKey}
102-
component="iframe"
103191
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}
111196
/>
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>
122197
)}
123198
</FieldTypeSandbox>
124199
</NonPortalModal>

0 commit comments

Comments
 (0)