import clsx from 'clsx';
import type { BlockEditProps } from '@wordpress/blocks';
import {
InnerBlocks,
BlockControls,
BlockVerticalAlignmentToolbar,
InspectorControls,
useBlockProps,
useSettings,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { sprintf, __ } from '@wordpress/i18n';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import type { ColumnAttributes, ColumnInspectorControlsProps } from './types';
function ColumnInspectorControls( {
width,
setAttributes,
}: ColumnInspectorControlsProps ) {
const [ availableUnits ] = useSettings( 'spacing.units' );
const units = useCustomUnits( {
availableUnits: ( availableUnits as string[] | undefined ) || [
'%',
'px',
'em',
'rem',
'vw',
],
} );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return (
{
setAttributes( { width: undefined } );
} }
dropdownMenuProps={ dropdownMenuProps }
>
width !== undefined }
label={ __( 'Width' ) }
onDeselect={ () => setAttributes( { width: undefined } ) }
isShownByDefault
>
{
nextWidth =
0 > parseFloat( nextWidth! ) ? '0' : nextWidth;
setAttributes( { width: nextWidth } );
} }
units={ units }
/>
);
}
/**
* Renders the `core/column` block in the editor.
*
* @param props Component props.
*
* @return React element.
*/
function ColumnEdit( props: BlockEditProps< ColumnAttributes > ) {
const {
attributes: { verticalAlignment, width, templateLock, allowedBlocks },
setAttributes,
clientId,
} = props;
const classes = clsx( 'block-core-columns', {
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
} );
const { columnsIds, hasChildBlocks, rootClientId } = useSelect(
( select ) => {
const { getBlockOrder, getBlockRootClientId } =
select( blockEditorStore );
const rootId = getBlockRootClientId( clientId );
return {
hasChildBlocks: getBlockOrder( clientId ).length > 0,
rootClientId: rootId,
columnsIds: getBlockOrder( rootId ),
};
},
[ clientId ]
);
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const updateAlignment = ( value: string | undefined ) => {
// Update own alignment.
setAttributes( { verticalAlignment: value } );
// Reset parent Columns block.
updateBlockAttributes( rootClientId!, {
verticalAlignment: null,
} );
};
const widthWithUnit = Number.isFinite( width ) ? width + '%' : width;
// State, not a ref, so that the element is there on the next render.
const [ dropZoneElement, setDropZoneElement ] =
useState< HTMLElement | null >( null );
const blockProps = useBlockProps( {
ref: setDropZoneElement,
className: classes,
style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
} );
const columnsCount = columnsIds.length;
const currentColumnPosition = columnsIds.indexOf( clientId ) + 1;
const label = sprintf(
/* translators: 1: Block label (i.e. "Block: Column"), 2: Position of the selected block, 3: Total number of sibling blocks of the same type */
__( '%1$s (%2$d of %3$d)' ),
blockProps[ 'aria-label' ] as string,
currentColumnPosition,
columnsCount
);
const innerBlocksProps = useInnerBlocksProps(
{ ...blockProps, 'aria-label': label },
{
// Allows dropping before or after the column, not only inside it.
dropZoneElement,
templateLock,
allowedBlocks,
renderAppender: hasChildBlocks
? false
: InnerBlocks.ButtonBlockAppender,
}
);
return (
<>
>
);
}
export default ColumnEdit;