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 2 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
43 changes: 36 additions & 7 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,44 @@ 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.querySelector(
'.is-root-container'
).scrollHeight
);
animationFrame = null;
}
);
}
}

const resizeObserver = new iframe.contentWindow.ResizeObserver(
resizeHeight
);
const mutationObserver = new iframe.contentWindow.MutationObserver(
resizeHeight
);

// Observing the <html> rather than the <body> because the latter
// gets destroyed and remounted after initialization in <Iframe>.
resizeObserver.observe( iframe.contentDocument.documentElement );
mutationObserver.observe( iframe.contentDocument.documentElement, {
subtree: true,
childList: true,
characterData: true,
} );

return () => {
iframe.contentWindow?.cancelAnimationFrame( animationFrame );
resizeObserver.disconnect();
mutationObserver.disconnect();
};
},
[ enableResizing ]
Expand Down Expand Up @@ -133,8 +155,15 @@ 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 && (
// Some themes will have `min-height: 100vh` for the root container,
// which isn't a requirement in auto resize mode.
<style>
{ `.is-root-container { min-height: 0 !important; }` }
Copy link
Member Author

Choose a reason for hiding this comment

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

Not entirely sure if this makes sense in all cases though 🤔 .

</style>
) }
</>
}
assets={ settings.__unstableResolvedAssets }
Expand Down