Skip to content

Commit

Permalink
Iframe: skip scoping styles (#46752)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Sep 11, 2023
1 parent ef5bd83 commit b18a270
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/block-editor/src/components/block-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function ExperimentalBlockCanvas( {
if ( ! shouldIframe ) {
return (
<>
<EditorStyles styles={ styles } />
<EditorStyles
styles={ styles }
scope=".editor-styles-wrapper"
/>
<WritingFlow
ref={ contentRef }
className="editor-styles-wrapper"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@
}

.block-editor-iframe__body {
background-color: $white;
transition: all 0.3s;
transform-origin: top center;
}
17 changes: 7 additions & 10 deletions packages/block-editor/src/components/editor-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import { useCallback, useMemo } from '@wordpress/element';
*/
import transformStyles from '../../utils/transform-styles';

const EDITOR_STYLES_SELECTOR = '.editor-styles-wrapper';
extend( [ namesPlugin, a11yPlugin ] );

function useDarkThemeBodyClassName( styles ) {
function useDarkThemeBodyClassName( styles, scope ) {
return useCallback(
( node ) => {
if ( ! node ) {
Expand All @@ -28,9 +27,7 @@ function useDarkThemeBodyClassName( styles ) {

const { ownerDocument } = node;
const { defaultView, body } = ownerDocument;
const canvas = ownerDocument.querySelector(
EDITOR_STYLES_SELECTOR
);
const canvas = scope ? ownerDocument.querySelector( scope ) : body;

let backgroundColor;

Expand Down Expand Up @@ -63,11 +60,11 @@ function useDarkThemeBodyClassName( styles ) {
body.classList.add( 'is-dark-theme' );
}
},
[ styles ]
[ styles, scope ]
);
}

export default function EditorStyles( { styles } ) {
export default function EditorStyles( { styles, scope } ) {
const stylesArray = useMemo(
() => Object.values( styles ?? [] ),
[ styles ]
Expand All @@ -76,9 +73,9 @@ export default function EditorStyles( { styles } ) {
() =>
transformStyles(
stylesArray.filter( ( style ) => style?.css ),
EDITOR_STYLES_SELECTOR
scope
),
[ stylesArray ]
[ stylesArray, scope ]
);

const transformedSvgs = useMemo(
Expand All @@ -94,7 +91,7 @@ export default function EditorStyles( { styles } ) {
<>
{ /* Use an empty style element to have a document reference,
but this could be any element. */ }
<style ref={ useDarkThemeBodyClassName( stylesArray ) } />
<style ref={ useDarkThemeBodyClassName( stylesArray, scope ) } />
{ transformedStyles.map( ( css, index ) => (
<style key={ index }>{ css }</style>
) ) }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// We use :where to keep specificity minimal.
// https://css-tricks.com/almanac/selectors/w/where/
html :where(.editor-styles-wrapper) {
:where(.editor-styles-wrapper) {
/**
* The following styles revert to the browser defaults overriding the WPAdmin styles.
* This is only needed while the block editor is not being loaded in an iframe.
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/block-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
display: block;
width: 100%;
height: 100%;
background: $white;
}

.edit-site-visual-editor__editor-canvas {
Expand Down

0 comments on commit b18a270

Please sign in to comment.