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

Blocks: Add block environment and directives support (edit vs save) #48590

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"sideEffects": [
"build-style/**",
"src/**/*.scss",
"{src,build,build-module}/{index.js,store/index.js,hooks/**}"
"{src,build,build-module}/{index.js,store/index.js,directives/**,hooks/**}"
],
"dependencies": {
"@babel/runtime": "^7.16.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import BlockHtml from './block-html';
import { useBlockProps } from './use-block-props';
import { store as blockEditorStore } from '../../store';
import { useLayout } from './layout';
export const BlockListBlockContext = createContext();
export const BlockListBlockContext = createContext( {} );

/**
* Merges wrapper props with special handling for classNames and styles.
Expand Down
42 changes: 42 additions & 0 deletions packages/block-editor/src/components/block-root/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { EnvironmentContext, forwardRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useBlockProps } from '../block-list/use-block-props';

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

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

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

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' ? (
<BlockRootSave { ...props } />
) : (
<BlockRootEdit ref={ ref } { ...props } />
)
}
</EnvironmentContext.Consumer>
);
} );
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export {
/*
* Content Related Components
*/

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 Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/directives/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { registerDirective } from '@wordpress/element';

/**
* Internal dependencies
*/
import { BlockRoot } from '../components/block-root';

registerDirective(
'block-root',
function ( [ type, originalProps, ...children ], attributeName ) {
const { [ attributeName ]: _, ...props } = originalProps;
return [ BlockRoot, { ...props, as: type }, ...children ];
}
);
1 change: 1 addition & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import './directives';
import './hooks';
export {
getBorderClassesAndStyles as __experimentalGetBorderClassesAndStyles,
Expand Down
14 changes: 5 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,6 @@ import classnames from 'classnames';
*/
import { HorizontalRule } from '@wordpress/components';
import {
useBlockProps,
getColorClassName,
__experimentalUseColorProps as useColorProps,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -46,13 +45,10 @@ export default function SeparatorEdit( { attributes, setAttributes } ) {
};

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

Expand Down Expand Up @@ -37,5 +36,5 @@ export default function separatorSave( { attributes } ) {
backgroundColor: colorProps?.style?.backgroundColor,
color: colorClass ? undefined : customColor,
};
return <hr { ...useBlockProps.save( { className, style: styles } ) } />;
return <hr data-wp-block-root className={ className } style={ styles } />;
}
13 changes: 9 additions & 4 deletions packages/block-library/src/separator/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { useBlockProps } from '@wordpress/block-editor';
*/
import SeparatorEdit from '../edit';

jest.mock( '@wordpress/block-editor', () => ( {
...jest.requireActual( '@wordpress/block-editor' ),
useBlockProps: jest.fn(),
} ) );
jest.mock(
'@wordpress/block-editor/src/components/block-list/use-block-props',
() => ( { useBlockProps: jest.fn() } )
);

const defaultAttributes = {
backgroundColor: undefined,
Expand All @@ -38,6 +38,7 @@ describe( 'Separator block edit method', () => {
render( <SeparatorEdit { ...defaultProps } /> );
expect( useBlockProps ).toHaveBeenCalledWith( {
className: 'has-alpha-channel-opacity',
ref: null,
style: undefined,
} );
} );
Expand All @@ -50,6 +51,7 @@ describe( 'Separator block edit method', () => {
render( <SeparatorEdit { ...props } /> );
expect( useBlockProps ).toHaveBeenCalledWith( {
className: 'has-css-opacity',
ref: null,
style: undefined,
} );
} );
Expand All @@ -68,6 +70,7 @@ describe( 'Separator block edit method', () => {
// is-style-dots as this class was always added to v1 blocks, so may be expected by themes and plugins.
className:
'has-text-color has-alpha-channel-opacity has-background',
ref: null,
style: {
backgroundColor: '#ff0000',
color: '#ff0000',
Expand All @@ -88,6 +91,7 @@ describe( 'Separator block edit method', () => {
expect( useBlockProps ).toHaveBeenCalledWith( {
className:
'has-text-color has-alpha-channel-opacity has-background',
ref: null,
style: {
backgroundColor: '#ff0000',
color: '#ff0000',
Expand All @@ -109,6 +113,7 @@ describe( 'Separator block edit method', () => {
expect( useBlockProps ).toHaveBeenCalledWith( {
className:
'has-text-color has-banana-color has-alpha-channel-opacity has-banana-background-color has-background',
ref: null,
style: undefined,
} );
} );
Expand Down
8 changes: 7 additions & 1 deletion packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import {
Component,
cloneElement,
createElement,
EnvironmentContext,
renderToString,
RawHTML,
} from '@wordpress/element';
Expand Down Expand Up @@ -190,7 +192,11 @@ export function getSaveContent( blockTypeOrName, attributes, innerBlocks ) {
const blockType = normalizeBlockType( blockTypeOrName );

return renderToString(
getSaveElement( blockType, attributes, innerBlocks )
createElement(
EnvironmentContext.Provider,
{ value: 'save' },
getSaveElement( blockType, attributes, innerBlocks )
)
);
}

Expand Down
Loading
Loading