UNPKG

@gechiui/block-editor

Version:
85 lines (74 loc) 1.75 kB
/** * External dependencies */ import { find } from 'lodash'; /** * GeChiUI dependencies */ import { __, isRTL } from '@gechiui/i18n'; import { ToolbarDropdownMenu, ToolbarGroup } from '@gechiui/components'; import { alignLeft, alignRight, alignCenter } from '@gechiui/icons'; const DEFAULT_ALIGNMENT_CONTROLS = [ { icon: alignLeft, title: __( '左对齐文字' ), align: 'left', }, { icon: alignCenter, title: __( '居中文字' ), align: 'center', }, { icon: alignRight, title: __( '右对齐文字' ), align: 'right', }, ]; const POPOVER_PROPS = { position: 'bottom right', isAlternate: true, }; function AlignmentUI( { value, onChange, alignmentControls = DEFAULT_ALIGNMENT_CONTROLS, label = __( '对齐' ), describedBy = __( '修改文字对齐' ), isCollapsed = true, isToolbar, } ) { function applyOrUnset( align ) { return () => onChange( value === align ? undefined : align ); } const activeAlignment = find( alignmentControls, ( control ) => control.align === value ); function setIcon() { if ( activeAlignment ) return activeAlignment.icon; return isRTL() ? alignRight : alignLeft; } const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu; const extraProps = isToolbar ? { isCollapsed } : {}; return ( <UIComponent icon={ setIcon() } label={ label } toggleProps={ { describedBy } } popoverProps={ POPOVER_PROPS } controls={ alignmentControls.map( ( control ) => { const { align } = control; const isActive = value === align; return { ...control, isActive, role: isCollapsed ? 'menuitemradio' : undefined, onClick: applyOrUnset( align ), }; } ) } { ...extraProps } /> ); } export default AlignmentUI;