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