UNPKG

@gechiui/block-editor

Version:
88 lines (81 loc) 1.94 kB
/** * GeChiUI dependencies */ import { ToolbarDropdownMenu, ToolbarGroup } from '@gechiui/components'; import { justifyLeft, justifyCenter, justifyRight, justifySpaceBetween, } from '@gechiui/icons'; import { __ } from '@gechiui/i18n'; const icons = { left: justifyLeft, center: justifyCenter, right: justifyRight, 'space-between': justifySpaceBetween, }; function JustifyContentUI( { allowedControls = [ 'left', 'center', 'right', 'space-between' ], isCollapsed = true, onChange, value, popoverProps, isToolbar, } ) { // If the control is already selected we want a click // again on the control to deselect the item, so we // call onChange( undefined ) const handleClick = ( next ) => { if ( next === value ) { onChange( undefined ); } else { onChange( next ); } }; const icon = value ? icons[ value ] : icons.left; const allControls = [ { name: 'left', icon: justifyLeft, title: __( '左对齐项' ), isActive: 'left' === value, onClick: () => handleClick( 'left' ), }, { name: 'center', icon: justifyCenter, title: __( '居中对齐项' ), isActive: 'center' === value, onClick: () => handleClick( 'center' ), }, { name: 'right', icon: justifyRight, title: __( '右对齐项' ), isActive: 'right' === value, onClick: () => handleClick( 'right' ), }, { name: 'space-between', icon: justifySpaceBetween, title: __( '项目间距' ), isActive: 'space-between' === value, onClick: () => handleClick( 'space-between' ), }, ]; const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu; const extraProps = isToolbar ? { isCollapsed } : {}; return ( <UIComponent icon={ icon } popoverProps={ popoverProps } label={ __( '更改项目对齐' ) } controls={ allControls.filter( ( elem ) => allowedControls.includes( elem.name ) ) } { ...extraProps } /> ); } export default JustifyContentUI;