@gechiui/block-editor
Version:
81 lines (71 loc) • 1.93 kB
JavaScript
/**
* GeChiUI dependencies
*/
import { _x } from '@gechiui/i18n';
import { ToolbarGroup, ToolbarDropdownMenu } from '@gechiui/components';
/**
* Internal dependencies
*/
import { alignTop, alignCenter, alignBottom } from './icons';
const BLOCK_ALIGNMENTS_CONTROLS = {
top: {
icon: alignTop,
title: _x( 'Align top', 'Block vertical alignment setting' ),
},
center: {
icon: alignCenter,
title: _x( 'Align middle', 'Block vertical alignment setting' ),
},
bottom: {
icon: alignBottom,
title: _x( 'Align bottom', 'Block vertical alignment setting' ),
},
};
const DEFAULT_CONTROLS = [ 'top', 'center', 'bottom' ];
const DEFAULT_CONTROL = 'top';
const POPOVER_PROPS = {
isAlternate: true,
};
function BlockVerticalAlignmentUI( {
value,
onChange,
controls = DEFAULT_CONTROLS,
isCollapsed = true,
isToolbar,
} ) {
function applyOrUnset( align ) {
return () => onChange( value === align ? undefined : align );
}
const activeAlignment = BLOCK_ALIGNMENTS_CONTROLS[ value ];
const defaultAlignmentControl =
BLOCK_ALIGNMENTS_CONTROLS[ DEFAULT_CONTROL ];
const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu;
const extraProps = isToolbar ? { isCollapsed } : {};
return (
<UIComponent
popoverProps={ POPOVER_PROPS }
icon={
activeAlignment
? activeAlignment.icon
: defaultAlignmentControl.icon
}
label={ _x(
'Change vertical alignment',
'Block vertical alignment setting label'
) }
controls={ controls.map( ( control ) => {
return {
...BLOCK_ALIGNMENTS_CONTROLS[ control ],
isActive: value === control,
role: isCollapsed ? 'menuitemradio' : undefined,
onClick: applyOrUnset( control ),
};
} ) }
{ ...extraProps }
/>
);
}
/**
* @see https://github.com/GeChiUI/gutenberg/blob/HEAD/packages/block-editor/src/components/block-vertical-alignment-toolbar/README.md
*/
export default BlockVerticalAlignmentUI;