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

useBlockPreview: Try syncing local style overrides with parent block editor store #55241

Closed
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
41 changes: 39 additions & 2 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useDisabled, useMergeRefs } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { memo, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { memo, useEffect, useMemo } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
Expand All @@ -18,6 +18,7 @@ import { ExperimentalBlockEditorProvider } from '../provider';
import AutoHeightBlockPreview from './auto';
import { store as blockEditorStore } from '../../store';
import { BlockListItems } from '../block-list';
import { unlock } from '../../lock-unlock';

export function BlockPreview( {
blocks,
Expand Down Expand Up @@ -92,6 +93,33 @@ export function BlockPreview( {
*/
export default memo( BlockPreview );

/**
* Syncs style overrides with the parent component. This allows a child of
* an `ExperimentalBlockEditorProvider` to sync its internal style overrides with
* the parent store.
*
* @param {Object} props The component props.
* @param {Map} props.parentStyleOverrides The parent style overrides.
* @param {Function} props.setParentStyleOverride The parent's function to set a style override.
*/
function SyncStyleOverridesWithParent( {
parentStyleOverrides,
setParentStyleOverride,
} ) {
const overrides = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(),
[]
);

useEffect( () => {
for ( const [ id, override ] of overrides ) {
if ( parentStyleOverrides.get( id ) !== override ) {
setParentStyleOverride( id, override );
}
}
}, [ overrides, parentStyleOverrides, setParentStyleOverride ] );
}

/**
* This hook is used to lightly mark an element as a block preview wrapper
* element. Call this hook and pass the returned props to the element to mark as
Expand All @@ -108,6 +136,11 @@ export default memo( BlockPreview );
* @param {Object} options.layout Layout settings to be used in the preview.
*/
export function useBlockPreview( { blocks, props = {}, layout } ) {
const { setStyleOverride } = unlock( useDispatch( blockEditorStore ) );
const parentStyleOverrides = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(),
[]
);
const originalSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings(),
[]
Expand All @@ -128,6 +161,10 @@ export function useBlockPreview( { blocks, props = {}, layout } ) {
value={ renderedBlocks }
settings={ settings }
>
<SyncStyleOverridesWithParent
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to sync here, can't we just use EditorStyles component to render these overrides here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think syncing is wrong because if the "preview" is unmounted, why should its overrides linger. They are just the styles for the preview not for any parent editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can't we just use EditorStyles component to render these overrides here?

Oh, good question, I'll try that out!

parentStyleOverrides={ parentStyleOverrides }
setParentStyleOverride={ setStyleOverride }
/>
<BlockListItems renderAppender={ false } layout={ layout } />
</ExperimentalBlockEditorProvider>
);
Expand Down
Loading