Skip to content

Commit f76b493

Browse files
committed
fix: lint warnings and refactor some exports
1 parent 79df882 commit f76b493

5 files changed

Lines changed: 37 additions & 33 deletions

File tree

src/Context.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
import { createContext, useContext } from 'react';
2-
3-
type FrameContextValue = {
4-
document?: Document;
5-
window?: Window;
6-
};
7-
8-
const defaultValue: FrameContextValue = {
9-
document: undefined,
10-
window: undefined
11-
};
12-
13-
export const FrameContext = createContext<FrameContextValue>(defaultValue);
14-
15-
export const useFrame = () => useContext(FrameContext);
1+
import { FrameContext } from './FrameContext';
162

173
export const FrameContextConsumer = FrameContext.Consumer;
184

src/Frame.tsx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, {
33
ReactNode,
44
RefObject,
55
forwardRef,
6+
useCallback,
67
useEffect,
78
useImperativeHandle,
89
useRef,
@@ -60,9 +61,9 @@ function Frame({
6061
const isMounted = useRef(false);
6162
const loadCheckInterval = useRef<ReturnType<typeof setInterval> | null>(null);
6263

63-
const getDoc = (): Document | null => {
64+
const getDoc = useCallback((): Document | null => {
6465
return nodeRef.current ? nodeRef.current.contentDocument : null;
65-
};
66+
}, [nodeRef]);
6667

6768
const getMountTarget = (): Element | null => {
6869
const doc = getDoc();
@@ -74,42 +75,40 @@ function Frame({
7475
return doc.body.children[0];
7576
};
7677

77-
const handleLoad = () => {
78+
const handleLoad = useCallback(() => {
7879
if (loadCheckInterval.current) {
7980
clearInterval(loadCheckInterval.current);
8081
}
8182
if (!iframeLoaded) {
8283
setIframeLoaded(true);
8384
}
84-
};
85+
}, [iframeLoaded]);
8586

8687
useEffect(() => {
8788
isMounted.current = true;
8889

8990
const doc = getDoc();
91+
const frame = nodeRef.current;
92+
const interval = loadCheckInterval.current;
9093

91-
if (doc && nodeRef.current?.contentWindow) {
92-
nodeRef.current.contentWindow.addEventListener(
93-
'DOMContentLoaded',
94-
handleLoad
95-
);
94+
if (doc && frame?.contentWindow) {
95+
frame.contentWindow.addEventListener('DOMContentLoaded', handleLoad);
9696
}
9797

9898
return () => {
9999
isMounted.current = false;
100100

101-
if (nodeRef.current?.contentWindow) {
102-
nodeRef.current.contentWindow.removeEventListener(
103-
'DOMContentLoaded',
104-
handleLoad
105-
);
101+
if (frame?.contentWindow) {
102+
frame.contentWindow.removeEventListener('DOMContentLoaded', handleLoad);
106103
}
107104

108-
if (loadCheckInterval.current) {
109-
clearInterval(loadCheckInterval.current);
105+
if (interval) {
106+
clearInterval(interval);
110107
}
111108
};
112-
}, []);
109+
// nodeRef is stable (either internalRef from useRef or external ref from props)
110+
// eslint-disable-next-line react-hooks/exhaustive-deps
111+
}, [getDoc, handleLoad]);
113112

114113
const renderFrameContents = (): ReactNode => {
115114
if (!isMounted.current) {

src/FrameContext.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createContext } from 'react';
2+
3+
type FrameContextValue = {
4+
document?: Document;
5+
window?: Window;
6+
};
7+
8+
const defaultValue: FrameContextValue = {
9+
document: undefined,
10+
window: undefined
11+
};
12+
13+
export const FrameContext = createContext<FrameContextValue>(defaultValue);

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export { Frame, default } from './Frame';
22
export type { FrameProps } from './Frame';
3-
export { FrameContext, FrameContextConsumer, useFrame } from './Context';
3+
export { FrameContext } from './FrameContext';
4+
export { FrameContextConsumer, FrameContextProvider } from './Context';
5+
export { useFrame } from './useFrame';

src/useFrame.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { useContext } from 'react';
2+
import { FrameContext } from './FrameContext';
3+
4+
export const useFrame = () => useContext(FrameContext);

0 commit comments

Comments
 (0)