Skip to content

Commit

Permalink
Iframe: preload style assets to avoid flash of unstyled content
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 21, 2022
1 parent 16a2fea commit 1ad7137
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
18 changes: 14 additions & 4 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
forwardRef,
useMemo,
useReducer,
renderToString,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Expand Down Expand Up @@ -290,7 +291,7 @@ function Iframe(
const bodyRef = useMergeRefs( [ contentRef, clearerRef, writingFlowRef ] );
const styleCompatibilityRef = useStylesCompatibility();

head = (
const styleAssets = (
<>
<style>{ 'html{height:auto!important;}body{margin:0}' }</style>
{ styles.map(
Expand All @@ -310,25 +311,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 );
}, [] );

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(
<>
<head ref={ headRef }>
{ styleAssets }
{ head }
<style>
{ `html { transition: background 5s; ${
Expand Down
28 changes: 14 additions & 14 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default function VisualEditor( { styles } ) {
wrapperBlockName,
wrapperUniqueId,
isBlockBasedTheme,
assets,
} = useSelect( ( select ) => {
const {
isFeatureActive,
Expand Down Expand Up @@ -158,27 +159,26 @@ export default function VisualEditor( { styles } ) {
wrapperBlockName: _wrapperBlockName,
wrapperUniqueId: getCurrentPostId(),
isBlockBasedTheme: editorSettings.__unstableIsBlockBasedTheme,
// WARNING: use getEditorSettings from the editor store, not the
// block editor store. The settings on the block editor store set
// with a delay.
assets: editorSettings.__unstableResolvedAssets,
};
}, [] );
const { isCleanNewPost } = useSelect( editorStore );
const hasMetaBoxes = useSelect(
( select ) => select( editPostStore ).hasMetaBoxes(),
[]
);
const {
themeHasDisabledLayoutStyles,
themeSupportsLayout,
assets,
isFocusMode,
} = useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeHasDisabledLayoutStyles: _settings.disableLayoutStyles,
themeSupportsLayout: _settings.supportsLayout,
assets: _settings.__unstableResolvedAssets,
isFocusMode: _settings.focusMode,
};
}, [] );
const { themeHasDisabledLayoutStyles, themeSupportsLayout, isFocusMode } =
useSelect( ( select ) => {
const _settings = select( blockEditorStore ).getSettings();
return {
themeHasDisabledLayoutStyles: _settings.disableLayoutStyles,
themeSupportsLayout: _settings.supportsLayout,
isFocusMode: _settings.focusMode,
};
}, [] );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { setIsEditingTemplate } = useDispatch( editPostStore );
const desktopCanvasStyles = {
Expand Down

1 comment on commit 1ad7137

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3750057641
📝 Reported issues:

Please sign in to comment.