Skip to content

Commit 84ff7e6

Browse files
committed
Memo main component, improve initial loader bg and bump version
1 parent fc549de commit 84ff7e6

File tree

2 files changed

+78
-66
lines changed

2 files changed

+78
-66
lines changed

kits/connect/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openint/connect",
3-
"version": "0.2.19",
3+
"version": "0.2.22",
44
"sideEffects": false,
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

kits/connect/src/embed-react.tsx

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,82 @@ export interface OpenIntConnectEmbedProps
1010

1111
const DEFAULT_HEIGHT = 500
1212
const DEFAULT_WIDTH = 350
13-
export const OpenIntConnectEmbed = React.forwardRef(
14-
(
15-
{baseUrl, params, onReady, ...iframeProps}: OpenIntConnectEmbedProps,
16-
forwardedRef: React.ForwardedRef<HTMLIFrameElement>,
17-
) => {
18-
const url = getIFrameUrl({baseUrl, params})
19-
const [loading, setLoading] = React.useState(true)
13+
export const OpenIntConnectEmbed = React.memo(
14+
React.forwardRef(
15+
(
16+
props: OpenIntConnectEmbedProps,
17+
forwardedRef: React.ForwardedRef<HTMLIFrameElement>,
18+
) => {
19+
const {baseUrl, params, onReady, ...iframeProps} = props
2020

21-
React.useEffect(() => {
22-
if (
23-
typeof iframeProps.height === 'number' &&
24-
iframeProps.height < DEFAULT_HEIGHT
25-
) {
26-
console.warn(
27-
'Optimal height for Connect is 500px. Using 500px instead.',
28-
)
29-
}
30-
if (typeof iframeProps.width === 'number' && iframeProps.width < 350) {
31-
console.warn('Minimum width for Connect is 350px. Using 350px instead.')
32-
}
33-
}, [iframeProps.height, iframeProps.width])
21+
const url = React.useMemo(
22+
() => getIFrameUrl({baseUrl, params}),
23+
[baseUrl, params],
24+
)
3425

35-
const height =
36-
typeof iframeProps.height === 'number'
37-
? iframeProps.height > DEFAULT_HEIGHT
38-
? iframeProps.height
39-
: DEFAULT_HEIGHT
40-
: iframeProps.height
26+
const [loading, setLoading] = React.useState(true)
4127

42-
const width =
43-
typeof iframeProps.width === 'number'
44-
? iframeProps.width > DEFAULT_WIDTH
45-
? iframeProps.width
46-
: DEFAULT_WIDTH
47-
: iframeProps.width || DEFAULT_WIDTH
28+
React.useEffect(() => {
29+
if (
30+
typeof iframeProps.height === 'number' &&
31+
iframeProps.height < DEFAULT_HEIGHT
32+
) {
33+
console.warn(
34+
'Optimal height for Connect is 500px. Using 500px instead.',
35+
)
36+
}
37+
if (typeof iframeProps.width === 'number' && iframeProps.width < 350) {
38+
console.warn(
39+
'Minimum width for Connect is 350px. Using 350px instead.',
40+
)
41+
}
42+
}, [iframeProps.height, iframeProps.width])
4843

49-
// Add a more reliable way to know iframe has fully finished loading
50-
// by sending message from iframe to parent when ready
51-
return (
52-
<div className="connect-embed-wrapper">
53-
<div className={`spinner-container ${loading ? 'loading' : 'loaded'}`}>
54-
<svg className="spinner" viewBox="0 0 50 50">
55-
<circle
56-
className="path"
57-
cx="25"
58-
cy="25"
59-
r="20"
60-
fill="none"
61-
strokeWidth="5"></circle>
62-
</svg>
63-
</div>
64-
<iframe
65-
name="openint-connect-frame"
66-
id="openint-connect-frame"
67-
{...iframeProps}
68-
ref={forwardedRef}
69-
onLoad={() => {
70-
setLoading(false)
71-
onReady?.()
72-
}}
73-
src={url}
74-
width={width}
75-
height={height}
76-
/>
44+
const height =
45+
typeof iframeProps.height === 'number'
46+
? iframeProps.height > DEFAULT_HEIGHT
47+
? iframeProps.height
48+
: DEFAULT_HEIGHT
49+
: iframeProps.height
50+
51+
const width =
52+
typeof iframeProps.width === 'number'
53+
? iframeProps.width > DEFAULT_WIDTH
54+
? iframeProps.width
55+
: DEFAULT_WIDTH
56+
: iframeProps.width || DEFAULT_WIDTH
7757

78-
<style>{`
58+
// Add a more reliable way to know iframe has fully finished loading
59+
// by sending message from iframe to parent when ready
60+
return (
61+
<div className="connect-embed-wrapper">
62+
<div
63+
className={`spinner-container ${loading ? 'loading' : 'loaded'}`}>
64+
<svg className="spinner" viewBox="0 0 50 50">
65+
<circle
66+
className="path"
67+
cx="25"
68+
cy="25"
69+
r="20"
70+
fill="none"
71+
strokeWidth="5"></circle>
72+
</svg>
73+
</div>
74+
<iframe
75+
name="openint-connect-frame"
76+
id="openint-connect-frame"
77+
{...iframeProps}
78+
ref={forwardedRef}
79+
onLoad={() => {
80+
setLoading(false)
81+
onReady?.()
82+
}}
83+
src={url}
84+
width={width}
85+
height={height}
86+
/>
87+
88+
<style>{`
7989
.connect-embed-wrapper {
8090
display: flex;
8191
height: ${height}px;
@@ -91,7 +101,7 @@ export const OpenIntConnectEmbed = React.forwardRef(
91101
justify-content: center;
92102
align-items: center;
93103
height: 100%;
94-
background: white;
104+
background: ${params?.theme === 'dark' ? '#1C1C1C' : 'white'};
95105
transition: opacity 0.3s ease;
96106
max-width: ${width}px;
97107
max-height: ${height}px;
@@ -130,8 +140,10 @@ export const OpenIntConnectEmbed = React.forwardRef(
130140
}
131141
}
132142
`}</style>
133-
</div>
134-
)
135-
},
143+
</div>
144+
)
145+
},
146+
),
136147
)
148+
137149
OpenIntConnectEmbed.displayName = 'OpenIntConnectEmbed'

0 commit comments

Comments
 (0)