Skip to content

Commit

Permalink
Layout: use override editor style API (#54466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Sep 15, 2023
1 parent d30cdd0 commit bc90aed
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@ import classnames from 'classnames';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
ButtonGroup,
ToggleControl,
PanelBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useContext, createPortal } from '@wordpress/element';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { InspectorControls } from '../components';
import useSetting from '../components/use-setting';
import { LayoutStyle } from '../components/block-list/layout';
import BlockList from '../components/block-list';
import { getLayoutType, getLayoutTypes } from '../layouts';
import { useBlockEditingMode } from '../components/block-editing-mode';
import { LAYOUT_DEFINITIONS } from '../layouts/definitions';
import { kebabCase } from '../utils/object';
import { useBlockSettings } from './utils';
import { unlock } from '../lock-unlock';

const layoutBlockSupportKey = 'layout';

Expand Down Expand Up @@ -364,7 +363,6 @@ export const withLayoutStyles = createHigherOrderComponent(
const shouldRenderLayoutStyles =
blockSupportsLayout && ! disableLayoutStyles;
const id = useInstanceId( BlockListBlock );
const element = useContext( BlockList.__unstableElementContext );
const { layout } = attributes;
const { default: defaultBlockLayout } =
getBlockSupport( name, layoutBlockSupportKey ) || {};
Expand Down Expand Up @@ -404,26 +402,23 @@ export const withLayoutStyles = createHigherOrderComponent(
layoutClasses
);

const { setStyleOverride, deleteStyleOverride } = unlock(
useDispatch( blockEditorStore )
);

useEffect( () => {
if ( ! css ) return;
setStyleOverride( id, { css } );
return () => {
deleteStyleOverride( id );
};
}, [ id, css, setStyleOverride, deleteStyleOverride ] );

return (
<>
{ shouldRenderLayoutStyles &&
element &&
!! css &&
createPortal(
<LayoutStyle
blockName={ name }
selector={ selector }
css={ css }
layout={ usedLayout }
style={ attributes?.style }
/>,
element
) }
<BlockListBlock
{ ...props }
__unstableLayoutClassNames={ layoutClassNames }
/>
</>
<BlockListBlock
{ ...props }
__unstableLayoutClassNames={ layoutClassNames }
/>
);
},
'withLayoutStyles'
Expand All @@ -449,7 +444,6 @@ export const withChildLayoutStyles = createHigherOrderComponent(
const shouldRenderChildLayoutStyles =
hasChildLayout && ! disableLayoutStyles;

const element = useContext( BlockList.__unstableElementContext );
const id = useInstanceId( BlockListBlock );
const selector = `.wp-container-content-${ id }`;

Expand All @@ -472,15 +466,19 @@ export const withChildLayoutStyles = createHigherOrderComponent(
shouldRenderChildLayoutStyles && !! css, // Only attach a container class if there is generated CSS to be attached.
} );

return (
<>
{ shouldRenderChildLayoutStyles &&
element &&
!! css &&
createPortal( <style>{ css }</style>, element ) }
<BlockListBlock { ...props } className={ className } />
</>
const { setStyleOverride, deleteStyleOverride } = unlock(
useDispatch( blockEditorStore )
);

useEffect( () => {
if ( ! css ) return;
setStyleOverride( id, { css } );
return () => {
deleteStyleOverride( id );
};
}, [ id, css, setStyleOverride, deleteStyleOverride ] );

return <BlockListBlock { ...props } className={ className } />;
},
'withChildLayoutStyles'
);
Expand Down

1 comment on commit bc90aed

@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 in bc90aed.
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/6195845479
📝 Reported issues:

Please sign in to comment.