-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: migrate IFrame component to functional component #21677
Open
crutchcorn
wants to merge
1
commit into
TryGhost:main
Choose a base branch
from
crutchcorn:frame-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,58 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import {Component} from 'react'; | ||
import React from 'react'; | ||
import {createPortal} from 'react-dom'; | ||
|
||
/** | ||
* This is still a class component because it causes issues with the behaviour (DOM recreation and layout glitches) if we switch to a functional component. Feel free to refactor. | ||
*/ | ||
export default class IFrame extends Component<any> { | ||
node: any; | ||
iframeHtml: any; | ||
iframeHead: any; | ||
iframeRoot: any; | ||
|
||
constructor(props: {onResize?: (el: HTMLElement) => void, children: any}) { | ||
super(props); | ||
this.setNode = this.setNode.bind(this); | ||
this.node = null; | ||
} | ||
|
||
componentDidMount() { | ||
this.node.addEventListener('load', this.handleLoad); | ||
} | ||
|
||
handleLoad = () => { | ||
this.setupFrameBaseStyle(); | ||
}; | ||
|
||
componentWillUnmount() { | ||
this.node.removeEventListener('load', this.handleLoad); | ||
} | ||
|
||
setupFrameBaseStyle() { | ||
if (this.node.contentDocument) { | ||
this.iframeHtml = this.node.contentDocument.documentElement; | ||
this.iframeHead = this.node.contentDocument.head; | ||
this.iframeRoot = this.node.contentDocument.body; | ||
this.forceUpdate(); | ||
|
||
if (this.props.onResize) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
(new ResizeObserver(_ => this.props.onResize(this.iframeRoot)))?.observe?.(this.iframeRoot); | ||
type IFrameProps = React.PropsWithChildren<{ | ||
style: React.CSSProperties | ||
head: React.ReactNode; | ||
title: string; | ||
onResize?: (el: HTMLElement) => void; | ||
} & Omit<React.ComponentProps<'iframe'>, 'onResize'>> | ||
|
||
const IFrame: React.FC<IFrameProps> = ({children, head, title = '', onResize, style = {}, ...rest}) => { | ||
const [iframeHead, setIframeHead] = React.useState<HTMLHeadElement>(); | ||
const [iframeRoot, setIframeRoot] = React.useState<HTMLElement>(); | ||
|
||
// TODO: Migrate to callback ref when React 19 cleanup refs are available: https://react.dev/blog/2024/04/25/react-19#cleanup-functions-for-refs | ||
const node = React.useRef<HTMLIFrameElement>(null); | ||
React.useEffect(() => { | ||
function setupFrameBaseStyle() { | ||
if (node.current?.contentDocument) { | ||
const iframeRootLocal = node.current.contentDocument.body; | ||
// This is safe because of batched updates, new to React 18 | ||
setIframeHead(node.current.contentDocument.head); | ||
setIframeRoot(iframeRootLocal); | ||
|
||
if (onResize) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
(new ResizeObserver(_ => onResize(iframeRootLocal)))?.observe?.(iframeRootLocal); | ||
} | ||
|
||
// This is a bit hacky, but prevents us to need to attach even listeners to all the iframes we have | ||
// because when we want to listen for keydown events, those are only send in the window of iframe that is focused | ||
// To get around this, we pass down the keydown events to the main window | ||
// No need to detach, because the iframe would get removed | ||
node.current.contentWindow?.addEventListener('keydown', (e: KeyboardEvent | undefined) => { | ||
// dispatch a new event | ||
window.dispatchEvent( | ||
new KeyboardEvent('keydown', e) | ||
); | ||
}); | ||
} | ||
|
||
// This is a bit hacky, but prevents us to need to attach even listeners to all the iframes we have | ||
// because when we want to listen for keydown events, those are only send in the window of iframe that is focused | ||
// To get around this, we pass down the keydown events to the main window | ||
// No need to detach, because the iframe would get removed | ||
this.node.contentWindow.addEventListener('keydown', (e: KeyboardEvent | undefined) => { | ||
// dispatch a new event | ||
window.dispatchEvent( | ||
new KeyboardEvent('keydown', e) | ||
); | ||
}); | ||
} | ||
} | ||
|
||
setNode(node: any) { | ||
this.node = node; | ||
} | ||
node.current?.addEventListener('load', setupFrameBaseStyle); | ||
|
||
return () => { | ||
node.current?.removeEventListener('load', setupFrameBaseStyle); | ||
}; | ||
}, [onResize]); | ||
|
||
return ( | ||
<iframe srcDoc={`<!DOCTYPE html>`} {...rest} ref={node} frameBorder="0" style={style} title={title}> | ||
{iframeHead && createPortal(head, iframeHead)} | ||
{iframeRoot && createPortal(children, iframeRoot)} | ||
</iframe> | ||
); | ||
}; | ||
|
||
render() { | ||
const {children, head, title = '', style = {}, ...rest} = this.props; | ||
return ( | ||
<iframe srcDoc={`<!DOCTYPE html>`} {...rest} ref={this.setNode} frameBorder="0" style={style} title={title}> | ||
{this.iframeHead && createPortal(head, this.iframeHead)} | ||
{this.iframeRoot && createPortal(children, this.iframeRoot)} | ||
</iframe> | ||
); | ||
} | ||
} | ||
export default IFrame; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to recall
this.forceUpdate
anymore, since the batched updates will trigger a re-render, updating theiframeHead
andiframeRoot
usages in the JSX