Skip to content
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

Try to fix auto resizing in template part focus mode #37394

Merged
merged 5 commits into from
Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions packages/edit-site/src/components/block-editor/resizable-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,49 @@ function ResizableEditor( { enableResizing, settings, ...props } ) {
return;
}

const resizeObserver = new iframe.contentWindow.ResizeObserver(
() => {
setHeight(
iframe.contentDocument.querySelector(
`.edit-site-block-editor__block-list`
).offsetHeight
let animationFrame = null;

function resizeHeight() {
if ( ! animationFrame ) {
// Throttle the updates on animation frame.
animationFrame = iframe.contentWindow.requestAnimationFrame(
() => {
setHeight(
iframe.contentDocument.documentElement
.scrollHeight
);
animationFrame = null;
}
);
}
);
}

let resizeObserver;

function registerObserver() {
resizeObserver?.disconnect();

resizeObserver = new iframe.contentWindow.ResizeObserver(
resizeHeight
);
// Observing the <html> rather than the <body> because the latter
// gets destroyed and remounted after initialization in <Iframe>.
resizeObserver.observe(
iframe.contentDocument.documentElement
);

resizeHeight();
}

// Observing the <html> rather than the <body> because the latter
// gets destroyed and remounted after initialization in <Iframe>.
resizeObserver.observe( iframe.contentDocument.documentElement );
// This is only required in Firefox for some unknown reasons.
iframe.addEventListener( 'load', registerObserver );
// This is required in Chrome and Safari.
registerObserver();

return () => {
resizeObserver.disconnect();
iframe.contentWindow?.cancelAnimationFrame( animationFrame );
resizeObserver?.disconnect();
iframe.removeEventListener( 'load', registerObserver );
};
},
[ enableResizing ]
Expand Down Expand Up @@ -133,8 +160,21 @@ function ResizableEditor( { enableResizing, settings, ...props } ) {
<style>{
// Forming a "block formatting context" to prevent margin collapsing.
// @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
`.edit-site-block-editor__block-list { display: flow-root; }`
`.is-root-container { display: flow-root; }`
}</style>
{ enableResizing && (
<style>
{
// Force the <html> and <body>'s heights to fit the content.
`html, body { height: -moz-fit-content !important; height: fit-content !important; min-height: 0 !important; }`
}
{
// Some themes will have `min-height: 100vh` for the root container,
// which isn't a requirement in auto resize mode.
`.is-root-container { min-height: 0 !important; }`
}
</style>
) }
</>
}
assets={ settings.__unstableResolvedAssets }
Expand Down