Skip to content

Commit

Permalink
Add the directive for handling block root
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 14, 2023
1 parent fe199a1 commit de8bd7a
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 24 deletions.
4 changes: 0 additions & 4 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ _Returns_

- `WPComponent`: The component to be rendered.

### BlockRoot

Undocumented declaration.

### BlockSelectionClearer

Undocumented declaration.
Expand Down
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
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 @@ -108,7 +108,6 @@ 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
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export { default as DefaultBlockAppender } from './default-block-appender';
export { default as __unstableEditorStyles } from './editor-styles';
export { default as Inserter } from './inserter';
export { useBlockProps } from './block-list/use-block-props';
export { BlockRoot } from './block-root';
export { default as FloatingToolbar } from './floating-toolbar';

// State Related Components.
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
5 changes: 2 additions & 3 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 {
BlockRoot,
getColorClassName,
__experimentalUseColorProps as useColorProps,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -46,8 +45,8 @@ export default function SeparatorEdit( { attributes, setAttributes } ) {
};

return (
<BlockRoot
as={ HorizontalRule }
<HorizontalRule
data-wp-block-root
className={ className }
style={ hasCustomColor ? styles : undefined }
/>
Expand Down
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 @@ -7,7 +7,6 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import {
BlockRoot,
getColorClassName,
__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 <BlockRoot as="hr" className={ className } style={ styles } />;
return <hr data-wp-block-root className={ className } style={ styles } />;
}
9 changes: 9 additions & 0 deletions packages/element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ _Returns_

- `JSX.Element`: Dangerously-rendering component.

### registerDirective

Registers a new directive.

_Parameters_

- _name_ `string`: Name of the directive.
- _handler_ `Function`: Handler for the directive.

### render

> **Deprecated** since WordPress 6.2.0. Use `createRoot` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@ export const directives = new Map();
export function registerDirective( name, handler ) {
directives.set( DIRECTIVE_PREFIX + name, handler );
}

registerDirective( 'debug', function ( type, originalProps, ...children ) {
const key = DIRECTIVE_PREFIX + 'debug';
const { [ key ]: value, ...props } = originalProps;
return [ type, props, ...children ];
} );
1 change: 1 addition & 0 deletions packages/element/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as createInterpolateElement } from './create-interpolate-element';
export { registerDirective } from './directives';
export * from './react';
export * from './react-platform';
export * from './utils';
Expand Down
11 changes: 5 additions & 6 deletions packages/element/src/react/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import { createElement as createElementReact } from 'react';
/**
* Internal dependencies
*/
import { directives } from './directives';
import { directives } from '../directives';

export function createElement( type, props, ...children ) {
// Limiting the application of directives to HTML elements with attributes.
if ( typeof type !== 'string' || ! props || props.length === 0 ) {
// Limiting the application of directives to components with props.
if ( ! props || props.length === 0 ) {
return createElementReact( type, props, ...children );
}

for ( const [ attributeName, directiveHandler ] of directives ) {
if ( attributeName in props ) {
[ type, props, ...children ] = directiveHandler(
type,
props,
...children
[ type, props, ...children ],
attributeName
);
}
}
Expand Down

0 comments on commit de8bd7a

Please sign in to comment.