Skip to content

Commit ebbde4f

Browse files
committed
Make api simpler
1 parent 8e4019b commit ebbde4f

File tree

2 files changed

+10
-61
lines changed

2 files changed

+10
-61
lines changed

src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ const App = () => {
3131
<br />
3232
<br />
3333
<Retool
34-
url="https://support.retool.com/embedded/public/cb9e15f0-5d7c-43a7-a746-cdec870dde9a"
34+
url="https://retoolin.tryretool.com/embedded/public/9e6c7390-922d-41bd-b038-2e9c0476867c"
3535
data={data}
3636
height="700px"
3737
width="1000px"
3838
onData={setRetoolData}
39-
></Retool>
39+
sandbox
40+
/>
4041
<h1> {JSON.stringify(retoolData)} </h1>
4142
</div>
4243
);

src/components/Retool.js

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,11 @@
11
import { useEffect, useRef, useState } from "react";
22

3-
const IFramePermissionKeys = set(['downloads', 'forms', 'modals', 'popups', 'fullscreen', 'camera', 'microphone', 'geolocation', 'payment', 'topNavigation', 'sameOrigin'])
3+
const MINIMUM_SANDBOX_PERMISSIONS = 'allow-scripts allow-same-origin'
44

5-
const constructPermissionStrings = (permissions) => {
6-
if (!permissions || permissions.length === 0) {
7-
return { allowString: undefined, sandboxString: undefined }
8-
}
9-
10-
const allowArray = []
11-
const sandboxArray = []
12-
13-
permissions.filter(permission => IFramePermissionKeys.has(permission)).forEach(permission => {
14-
switch (permission) {
15-
case 'downloads':
16-
sandboxArray.push('allow-downloads')
17-
break
18-
case 'forms':
19-
sandboxArray.push('allow-forms')
20-
break
21-
case 'modals':
22-
sandboxArray.push('allow-modals')
23-
break
24-
case 'popups':
25-
sandboxArray.push('allow-popups', 'allow-popups-to-escape-sandbox')
26-
break
27-
case 'fullscreen':
28-
allowArray.push('fullscreen')
29-
break
30-
case 'camera':
31-
allowArray.push('camera')
32-
break
33-
case 'microphone':
34-
allowArray.push('microphone')
35-
break
36-
case 'geolocation':
37-
allowArray.push('geolocation')
38-
break
39-
case 'payment':
40-
allowArray.push('payment')
41-
break
42-
case 'topNavigation':
43-
sandboxArray.push('allow-top-navigation', 'allow-top-navigation-by-user-activation')
44-
break
45-
case 'sameOrigin':
46-
sandboxArray.push('allow-same-origin')
47-
break
48-
}
49-
})
50-
51-
const allowString = allowArray.length > 0 ? allowArray.join(';') : undefined
52-
const sandboxString = sandboxArray.length > 0 ? sandboxArray.join(' ') : undefined
53-
54-
return { allowString, sandboxString }
55-
}
56-
57-
const Retool = ({ data, url, height, width, onData, permissions }) => {
5+
const Retool = ({ data, url, height, width, onData, sandbox, allow }) => {
586
const embeddedIframe = useRef(null);
597
const [elementWatchers, setElementWatchers] = useState({});
608

61-
const { allowString, sandboxString } = constructPermissionStrings(permissions)
62-
639
/* Retool passes up the list of elements to watch on page load */
6410
useEffect(() => {
6511
for (const key in elementWatchers) {
@@ -141,17 +87,19 @@ const Retool = ({ data, url, height, width, onData, permissions }) => {
14187
}
14288
};
14389

90+
const sandboxAttrib = typeof sandbox === 'string' ? `${MINIMUM_SANDBOX_PERMISSIONS} ${sandbox}` : sandbox === true ? MINIMUM_SANDBOX_PERMISSIONS : sandbox
91+
14492
return (
14593
<iframe
146-
allow={allowString}
147-
sandbox={sandboxString}
94+
allow={allow}
95+
sandbox={sandboxAttrib}
14896
height={height ?? "100%"}
14997
width={width ?? "100%"}
15098
frameBorder="none"
15199
src={url}
152100
ref={embeddedIframe}
153101
title="retool"
154-
></iframe>
102+
/>
155103
);
156104
};
157105

0 commit comments

Comments
 (0)