-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Only render visible frames, avoid extra history entries (#106)
- Loading branch information
1 parent
3232344
commit 462f612
Showing
6 changed files
with
324 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,69 @@ | ||
import React, { Component, AllHTMLAttributes } from 'react'; | ||
import React, { | ||
useState, | ||
useEffect, | ||
useRef, | ||
AllHTMLAttributes, | ||
MutableRefObject | ||
} from 'react'; | ||
import { useIntersection } from 'react-use'; | ||
|
||
interface State { | ||
loaded: boolean; | ||
interface IframeProps extends AllHTMLAttributes<HTMLIFrameElement> { | ||
src: string; | ||
intersectionRootRef: MutableRefObject<Element | null>; | ||
} | ||
interface Props extends AllHTMLAttributes<HTMLIFrameElement> {} | ||
|
||
export default class Iframe extends Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
|
||
this.state = { | ||
loaded: false | ||
}; | ||
} | ||
|
||
handleLoad = () => { | ||
this.setState({ loaded: true }); | ||
}; | ||
|
||
render() { | ||
const { style, ...props } = this.props; | ||
const { loaded } = this.state; | ||
|
||
const combinedStyles = { | ||
...style, | ||
transition: 'opacity .3s ease', | ||
opacity: loaded ? 1 : 0 | ||
}; | ||
|
||
return ( | ||
<iframe onLoad={this.handleLoad} {...props} style={combinedStyles} /> | ||
); | ||
} | ||
|
||
export default function Iframe({ | ||
intersectionRootRef, | ||
style, | ||
src, | ||
...restProps | ||
}: IframeProps) { | ||
const [loaded, setLoaded] = useState(false); | ||
const [renderedSrc, setRenderedSrc] = useState<string | null>(null); | ||
const iframeRef = useRef<HTMLIFrameElement | null>(null); | ||
const intersection = useIntersection(iframeRef, { | ||
root: intersectionRootRef.current, | ||
rootMargin: '800px', | ||
threshold: 0 | ||
}); | ||
|
||
const intersectionRatio = intersection?.intersectionRatio ?? null; | ||
|
||
useEffect(() => { | ||
if (intersectionRatio === null) { | ||
return; | ||
} | ||
|
||
if (intersectionRatio > 0 && src !== renderedSrc) { | ||
setRenderedSrc(src); | ||
} | ||
}, [intersectionRatio, src, renderedSrc]); | ||
|
||
useEffect(() => { | ||
if (renderedSrc !== null) { | ||
const location = iframeRef.current?.contentWindow?.location; | ||
|
||
if (location) { | ||
location.replace(renderedSrc); | ||
} | ||
} | ||
}, [renderedSrc]); | ||
|
||
return ( | ||
<iframe | ||
ref={iframeRef} | ||
onLoad={() => setLoaded(true)} | ||
onMouseEnter={() => { | ||
if (src !== renderedSrc) { | ||
setRenderedSrc(src); | ||
} | ||
}} | ||
style={{ | ||
...style, | ||
transition: 'opacity .3s ease', | ||
opacity: loaded ? 1 : 0 | ||
}} | ||
{...restProps} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.