@wordpress/block-library
Version:
Block library for the WordPress editor.
107 lines (99 loc) • 3.36 kB
JavaScript
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
InnerBlocks,
useBlockProps,
InspectorControls,
useInnerBlocksProps,
useSetting,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
const htmlElementMessages = {
header: __(
'The <header> element should represent introductory content, typically a group of introductory or navigational aids.'
),
main: __(
'The <main> element should be used for the primary content of your document only. '
),
section: __(
"The <section> element should represent a standalone portion of the document that can't be better represented by another element."
),
article: __(
'The <article> element should represent a self contained, syndicatable portion of the document.'
),
aside: __(
"The <aside> element should represent a portion of a document whose content is only indirectly related to the document's main content."
),
footer: __(
'The <footer> element should represent a footer for its nearest sectioning element (e.g.: <section>, <article>, <main> etc.).'
),
};
function GroupEdit( { attributes, setAttributes, clientId } ) {
const { hasInnerBlocks, themeSupportsLayout } = useSelect(
( select ) => {
const { getBlock, getSettings } = select( blockEditorStore );
const block = getBlock( clientId );
return {
hasInnerBlocks: !! ( block && block.innerBlocks.length ),
themeSupportsLayout: getSettings()?.supportsLayout,
};
},
[ clientId ]
);
const defaultLayout = useSetting( 'layout' ) || {};
const { tagName: TagName = 'div', templateLock, layout = {} } = attributes;
const usedLayout = ! layout?.type
? { ...defaultLayout, ...layout, type: 'default' }
: { ...defaultLayout, ...layout };
const { type = 'default' } = usedLayout;
const layoutSupportEnabled = themeSupportsLayout || type !== 'default';
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps(
layoutSupportEnabled
? blockProps
: { className: 'wp-block-group__inner-container' },
{
templateLock,
renderAppender: hasInnerBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
__experimentalLayout: layoutSupportEnabled ? usedLayout : undefined,
}
);
return (
<>
<InspectorControls __experimentalGroup="advanced">
<SelectControl
label={ __( 'HTML element' ) }
options={ [
{ label: __( 'Default (<div>)' ), value: 'div' },
{ label: '<header>', value: 'header' },
{ label: '<main>', value: 'main' },
{ label: '<section>', value: 'section' },
{ label: '<article>', value: 'article' },
{ label: '<aside>', value: 'aside' },
{ label: '<footer>', value: 'footer' },
] }
value={ TagName }
onChange={ ( value ) =>
setAttributes( { tagName: value } )
}
help={ htmlElementMessages[ TagName ] }
/>
</InspectorControls>
{ layoutSupportEnabled && <TagName { ...innerBlocksProps } /> }
{ /* Ideally this is not needed but it's there for backward compatibility reason
to keep this div for themes that might rely on its presence */ }
{ ! layoutSupportEnabled && (
<TagName { ...blockProps }>
<div { ...innerBlocksProps } />
</TagName>
) }
</>
);
}
export default GroupEdit;