index.js
51 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 | import { applyFilters } from '@wordpress/hooks'; |
| 6 | |
| 7 | export default function RootElement( { |
| 8 | name, |
| 9 | clientId, |
| 10 | align, |
| 11 | children, |
| 12 | isBlockPreview, |
| 13 | } ) { |
| 14 | const { |
| 15 | getBlockRootClientId, |
| 16 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 17 | |
| 18 | const supportsLayout = useSelect( ( select ) => { |
| 19 | const { |
| 20 | getSettings, |
| 21 | } = select( blockEditorStore ); |
| 22 | |
| 23 | return getSettings().supportsLayout || false; |
| 24 | }, [] ); |
| 25 | |
| 26 | const blockName = name.toString().replace( '/', '-' ); |
| 27 | |
| 28 | const blockProps = { |
| 29 | className: classnames( { |
| 30 | 'wp-block': true, |
| 31 | 'gb-is-root-block': true, |
| 32 | [ `gb-root-block-${ blockName }` ]: true, |
| 33 | [ `align${ align }` ]: supportsLayout, |
| 34 | } ), |
| 35 | 'data-align': align && ! supportsLayout ? align : null, |
| 36 | 'data-block': clientId, |
| 37 | }; |
| 38 | |
| 39 | const parentBlockId = getBlockRootClientId( clientId ); |
| 40 | |
| 41 | if ( applyFilters( 'generateblocks.rootElement.disable', parentBlockId || isBlockPreview, { name } ) ) { |
| 42 | return children; |
| 43 | } |
| 44 | |
| 45 | return createElement( |
| 46 | 'div', |
| 47 | blockProps, |
| 48 | children |
| 49 | ); |
| 50 | } |
| 51 |