Skip to content

Commit

Permalink
Refactor Separator block to use BlockRoot (new name)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 17, 2023
1 parent 70ec95d commit f0ce06c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ _Returns_

Undocumented declaration.

### BlockContent

Undocumented declaration.

### BlockContextProvider

Component which merges passed value with current consumed block context.
Expand Down Expand Up @@ -205,6 +201,10 @@ _Returns_

- `WPComponent`: The component to be rendered.

### BlockRoot

Undocumented declaration.

### BlockSelectionClearer

Undocumented declaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import { EnvironmentContext, forwardRef } from '@wordpress/element';
*/
import { useBlockProps } from '../block-list/use-block-props';

const BlockContentEdit = forwardRef(
const BlockRootEdit = forwardRef(
( { as: Component = 'div', className, style, ...props }, ref ) => {
const blockProps = useBlockProps( { ref, className, style } );
return <Component { ...props } { ...blockProps } />;
}
);

const BlockContentSave = ( {
const BlockRootSave = ( {
as: Component = 'div',
className,
style,
...props
} ) => {
const blockProps = useBlockProps.save( { className, style } );

return <Component { ...props } { ...blockProps } />;
};

export const BlockContent = forwardRef( ( props, ref ) => {
export const BlockRoot = forwardRef( ( props, ref ) => {
// We can't use the `useContext` hook here because it isn't supported in the save environment.
return (
<EnvironmentContext.Consumer>
{ ( env ) =>
env === 'save' ? (
<BlockContentSave { ...props } />
<BlockRootSave { ...props } />
) : (
<BlockContentEdit ref={ ref } { ...props } />
<BlockRootEdit ref={ ref } { ...props } />
)
}
</EnvironmentContext.Consumer>
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export { default as __experimentalSpacingSizesControl } from './spacing-sizes-co
/*
* Content Related Components
*/
export { BlockContent } from './block-content';
export { default as __unstableBlockSettingsMenuFirstItem } from './block-settings-menu/block-settings-menu-first-item';
export { default as __unstableBlockToolbarLastItem } from './block-toolbar/block-toolbar-last-item';
export { default as __unstableBlockNameContext } from './block-toolbar/block-name-context';
Expand All @@ -106,6 +105,7 @@ export { default as __experimentalUseResizeCanvas } from './use-resize-canvas';
export { default as BlockInspector } from './block-inspector';
export { default as BlockList } from './block-list';
export { useBlockProps } from './block-list/use-block-props';
export { BlockRoot } from './block-root';
export { LayoutStyle as __experimentalLayoutStyle } from './block-list/layout';
export { default as BlockMover } from './block-mover';
export {
Expand Down
15 changes: 6 additions & 9 deletions packages/block-library/src/separator/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import classnames from 'classnames';
*/
import { HorizontalRule } from '@wordpress/components';
import {
useBlockProps,
BlockRoot,
getColorClassName,
__experimentalUseColorProps as useColorProps,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -46,13 +46,10 @@ export default function SeparatorEdit( { attributes, setAttributes } ) {
};

return (
<>
<HorizontalRule
{ ...useBlockProps( {
className,
style: hasCustomColor ? styles : undefined,
} ) }
/>
</>
<BlockRoot
as={ HorizontalRule }
className={ className }
style={ hasCustomColor ? styles : undefined }
/>
);
}
4 changes: 2 additions & 2 deletions packages/block-library/src/separator/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import {
BlockRoot,
getColorClassName,
useBlockProps,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
} from '@wordpress/block-editor';

Expand Down Expand Up @@ -37,5 +37,5 @@ export default function separatorSave( { attributes } ) {
backgroundColor: colorProps?.style?.backgroundColor,
color: colorClass ? undefined : customColor,
};
return <hr { ...useBlockProps.save( { className, style: styles } ) } />;
return <BlockRoot as="hr" className={ className } style={ styles } />;
}
5 changes: 4 additions & 1 deletion packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
Component,
cloneElement,
EnvironmentContext,
renderToString,
RawHTML,
} from '@wordpress/element';
Expand Down Expand Up @@ -190,7 +191,9 @@ export function getSaveContent( blockTypeOrName, attributes, innerBlocks ) {
const blockType = normalizeBlockType( blockTypeOrName );

return renderToString(
getSaveElement( blockType, attributes, innerBlocks )
<EnvironmentContext.Provider value="save">
{ getSaveElement( blockType, attributes, innerBlocks ) }
</EnvironmentContext.Provider>
);
}

Expand Down

0 comments on commit f0ce06c

Please sign in to comment.