1
1
"use client" ;
2
2
3
3
import { type Settings , initialize } from "@open-iframe-resizer/core" ;
4
- import { type IframeHTMLAttributes , useEffect , useRef } from "react" ;
4
+ import { type IframeHTMLAttributes , forwardRef , useCallback , useEffect , useRef } from "react" ;
5
5
6
6
interface Props extends IframeHTMLAttributes < HTMLIFrameElement > , Partial < Settings > { }
7
7
8
- export function IframeResizer ( props : Props ) {
8
+ export const IframeResizer = forwardRef < HTMLIFrameElement , Props > ( ( props , forwardedRef ) => {
9
9
const iframeRef = useRef < HTMLIFrameElement > ( null ) ;
10
10
const { settings, iframeAttributes } = filterProps ( props ) ;
11
11
@@ -17,8 +17,23 @@ export function IframeResizer(props: Props) {
17
17
return ( ) => results . forEach ( ( value ) => value . unsubscribe ( ) ) ;
18
18
} , [ ] ) ;
19
19
20
- return < iframe { ...iframeAttributes } ref = { iframeRef } /> ;
21
- }
20
+ const composedRef = useCallback (
21
+ ( el : HTMLIFrameElement ) => {
22
+ iframeRef . current = el ;
23
+
24
+ if ( typeof forwardedRef === "function" ) {
25
+ forwardedRef ( el ) ;
26
+ } else if ( forwardedRef ) {
27
+ forwardedRef . current = el ;
28
+ }
29
+ } ,
30
+ [ forwardedRef ] ,
31
+ ) ;
32
+
33
+ return < iframe { ...iframeAttributes } ref = { composedRef } /> ;
34
+ } ) ;
35
+
36
+ IframeResizer . displayName = "IframeResizer" ;
22
37
23
38
function filterProps ( props : Props ) : { iframeAttributes : IframeHTMLAttributes < HTMLIFrameElement > ; settings : Partial < Settings > } {
24
39
const {
0 commit comments