-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Iframe: preload style assets to avoid flash of unstyled content #46706
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
forwardRef, | ||
useMemo, | ||
useReducer, | ||
renderToString, | ||
} from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
|
@@ -20,13 +21,15 @@ import { | |
useRefEffect, | ||
} from '@wordpress/compose'; | ||
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockSelectionClearer } from '../block-selection-clearer'; | ||
import { useWritingFlow } from '../writing-flow'; | ||
import { useCompatibilityStyles } from './use-compatibility-styles'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
/** | ||
* Bubbles some event types (keydown, keypress, and dragover) to parent document | ||
|
@@ -97,20 +100,22 @@ async function loadScript( head, { id, src } ) { | |
} ); | ||
} | ||
|
||
function Iframe( | ||
{ | ||
contentRef, | ||
children, | ||
head, | ||
tabIndex = 0, | ||
assets, | ||
scale = 1, | ||
frameSize = 0, | ||
readonly, | ||
...props | ||
}, | ||
ref | ||
) { | ||
function Iframe( { | ||
contentRef, | ||
children, | ||
head, | ||
tabIndex = 0, | ||
scale = 1, | ||
frameSize = 0, | ||
readonly, | ||
forwardedRef: ref, | ||
...props | ||
} ) { | ||
const assets = useSelect( | ||
( select ) => | ||
select( blockEditorStore ).getSettings().__unstableResolvedAssets, | ||
[] | ||
); | ||
const [ , forceRender ] = useReducer( () => ( {} ) ); | ||
const [ iframeDocument, setIframeDocument ] = useState(); | ||
const [ bodyClasses, setBodyClasses ] = useState( [] ); | ||
|
@@ -204,7 +209,7 @@ function Iframe( | |
}, [] ); | ||
const bodyRef = useMergeRefs( [ contentRef, clearerRef, writingFlowRef ] ); | ||
|
||
head = ( | ||
const styleAssets = ( | ||
<> | ||
<style>{ 'html{height:auto!important;}body{margin:0}' }</style> | ||
{ [ ...styles, ...neededCompatStyles ].map( | ||
|
@@ -224,25 +229,34 @@ function Iframe( | |
); | ||
} | ||
) } | ||
{ head } | ||
</> | ||
); | ||
|
||
// Correct doctype is required to enable rendering in standards | ||
// mode. Also preload the styles to avoid a flash of unstyled | ||
// content. | ||
const srcDoc = useMemo( () => { | ||
return '<!doctype html>' + renderToString( styleAssets ); | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, [] ); | ||
|
||
return ( | ||
<> | ||
{ tabIndex >= 0 && before } | ||
<iframe | ||
{ ...props } | ||
ref={ useMergeRefs( [ ref, setRef ] ) } | ||
tabIndex={ tabIndex } | ||
// Correct doctype is required to enable rendering in standards mode | ||
srcDoc="<!doctype html>" | ||
// Correct doctype is required to enable rendering in standards | ||
// mode. Also preload the styles to avoid a flash of unstyled | ||
// content. | ||
srcDoc={ srcDoc } | ||
title={ __( 'Editor canvas' ) } | ||
> | ||
{ iframeDocument && | ||
createPortal( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we do something like hydrateRoot in the future so existing HTML is reused. |
||
<> | ||
<head ref={ headRef }> | ||
{ styleAssets } | ||
{ head } | ||
<style> | ||
{ `html { transition: background 5s; ${ | ||
|
@@ -286,4 +300,23 @@ function Iframe( | |
); | ||
} | ||
|
||
export default forwardRef( Iframe ); | ||
function IframeIfReady( props, ref ) { | ||
const isInitialised = useSelect( | ||
( select ) => | ||
select( blockEditorStore ).getSettings().__internalIsInitialized, | ||
[] | ||
); | ||
|
||
// We shouldn't render the iframe until the editor settings are initialised. | ||
// The initial settings are needed to get the styles for the srcDoc, which | ||
// cannot be changed after the iframe is mounted. srcDoc is used to to set | ||
// the initial iframe HTML, which is required to avoid a flash of unstyled | ||
// content. | ||
if ( ! isInitialised ) { | ||
return null; | ||
} | ||
|
||
return <Iframe { ...props } forwardedRef={ ref } />; | ||
} | ||
|
||
export default forwardRef( IframeIfReady ); |
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
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.
It makes me wonder about these two remaining things: styles, duotone. How are they being handled in the case of the regular editors? Wondering if there's a way to move them to the iframe automatically?
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.
The styles are passed to
<EditorStyles>
, which is created separately from the Iframe and then passed to it. I guess at some point we can also integrate this.Don't know about duotone. It seems to create SVG filters and then renders it in the content area. Tbh, that doesn't feel like the iframe's responsibility, Ideally this should be isolated to the duotone hook.