index.js
35 lines
| 1 | import { createElement } from '@wordpress/element'; |
| 2 | import { useSelect } from '@wordpress/data'; |
| 3 | import classnames from 'classnames'; |
| 4 | |
| 5 | export default function RootElement( { name, clientId, align, children } ) { |
| 6 | const { |
| 7 | getBlockRootClientId, |
| 8 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 9 | |
| 10 | const blockName = name.toString().replace( '/', '-' ); |
| 11 | |
| 12 | const blockProps = { |
| 13 | className: classnames( { |
| 14 | 'wp-block': true, |
| 15 | 'block-editor-block-list__block': true, |
| 16 | 'gb-is-root-block': true, |
| 17 | [ `gb-root-block-${ blockName }` ]: true, |
| 18 | } ), |
| 19 | 'data-align': align ? align : null, |
| 20 | 'data-block': clientId, |
| 21 | }; |
| 22 | |
| 23 | const parentBlock = getBlockRootClientId( clientId ); |
| 24 | |
| 25 | if ( parentBlock ) { |
| 26 | return children; |
| 27 | } |
| 28 | |
| 29 | return createElement( |
| 30 | 'div', |
| 31 | blockProps, |
| 32 | children |
| 33 | ); |
| 34 | } |
| 35 |