|
1 |
| -import { useCallback, useEffect, useState, forwardRef, useImperativeHandle } from 'react'; |
| 1 | +import { useCallback, useEffect, useState, forwardRef, useImperativeHandle, useMemo, useRef } from 'react'; |
2 | 2 | import { createPortal } from 'react-dom';
|
3 | 3 | import { FrameContext } from './Context';
|
4 | 4 |
|
@@ -46,16 +46,27 @@ const IFrame = forwardRef<HTMLIFrameElement, IFrameProps>(
|
46 | 46 | }
|
47 | 47 | return doc?.body;
|
48 | 48 | };
|
49 |
| - const handleLoad = useCallback(() => { |
50 |
| - /** |
51 |
| - * In certain situations on a cold cache DOMContentLoaded never gets called |
52 |
| - * fallback to an interval to check if that's the case |
53 |
| - */ |
54 |
| - const loadCheck = () => setInterval(() => handleLoad(), 500); |
55 |
| - clearInterval(loadCheck()); |
56 |
| - // Bail update as some browsers will trigger on both DOMContentLoaded & onLoad ala firefox |
57 |
| - if (!iframeLoaded) { |
58 |
| - setIframeLoaded(true); |
| 49 | + const evnRef = useRef<React.SyntheticEvent<HTMLIFrameElement, Event>>(); |
| 50 | + const handleLoad = useCallback<(evn: React.SyntheticEvent<HTMLIFrameElement, Event> | Event) => void>( |
| 51 | + (evn) => { |
| 52 | + evnRef.current = evn as React.SyntheticEvent<HTMLIFrameElement, Event>; |
| 53 | + /** |
| 54 | + * In certain situations on a cold cache DOMContentLoaded never gets called |
| 55 | + * fallback to an interval to check if that's the case |
| 56 | + */ |
| 57 | + const loadCheck = () => setInterval(() => handleLoad(evn), 500); |
| 58 | + clearInterval(loadCheck()); |
| 59 | + // Bail update as some browsers will trigger on both DOMContentLoaded & onLoad ala firefox |
| 60 | + if (!iframeLoaded) { |
| 61 | + setIframeLoaded(true); |
| 62 | + } |
| 63 | + }, |
| 64 | + [iframeLoaded], |
| 65 | + ); |
| 66 | + |
| 67 | + useMemo(() => { |
| 68 | + if (!src && other.onLoad && iframeLoaded) { |
| 69 | + other.onLoad(evnRef.current!); |
59 | 70 | }
|
60 | 71 | }, [iframeLoaded]);
|
61 | 72 |
|
|
0 commit comments