-
-
Notifications
You must be signed in to change notification settings - Fork 977
/
index.jsx
43 lines (36 loc) · 1.52 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import connectResizer from '@iframe-resizer/core'
import React, { useEffect, useImperativeHandle, useRef } from 'react'
import warning from 'warning'
import filterIframeAttribs from '../common/filter-iframe-attribs'
function IframeResizer(props) {
// eslint-disable-next-line react/prop-types
const { title, forwardRef, ...rest } = props
const filteredProps = filterIframeAttribs(rest)
const iframeRef = useRef(null)
const onClose = () => {
warning(
!iframeRef.current,
`[iframe-resizer/react][${iframeRef?.current?.id}] Close event ignored, to remove the iframe update your React component.`,
)
return !iframeRef.current // Allow React to close this
}
// This hook is only run once, as once iframe-resizer is bound, it will
// deal with changes to the element and does not need recalling
useEffect(() => {
const iframe = iframeRef.current
const resizer = connectResizer({ ...rest, onClose })(iframe)
return () => resizer?.disconnect()
}, []) // eslint-disable-line react-hooks/exhaustive-deps
useImperativeHandle(forwardRef, () => ({
getRef: () => iframeRef,
getElement: () => iframeRef.current,
resize: () => iframeRef.current.iframeResizer.resize(),
moveToAnchor: (anchor) =>
iframeRef.current.iframeResizer.moveToAnchor(anchor),
sendMessage: (message, targetOrigin) => {
iframeRef.current.iframeResizer.sendMessage(message, targetOrigin)
},
}))
return <iframe title={title} {...filteredProps} ref={iframeRef} />
}
export default IframeResizer