AlignmentToolbar.jsx
120 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { ToolbarButton, Dropdown, MenuGroup, MenuItem } from '@wordpress/components'; |
| 3 | import { BlockControls } from '@wordpress/block-editor'; |
| 4 | import { alignLeft, alignCenter, alignRight, alignJustify } from '@wordpress/icons'; |
| 5 | import { useBlockStyles } from '@hooks/useBlockStyles'; |
| 6 | import { BlockWidth } from './BlockWidth'; |
| 7 | |
| 8 | const POPOVER_PROPS = { |
| 9 | position: 'bottom right', |
| 10 | }; |
| 11 | |
| 12 | export function AlignmentToolbar( { |
| 13 | getStyleValue, |
| 14 | onStyleChange, |
| 15 | align, |
| 16 | setAttributes, |
| 17 | clientId, |
| 18 | withTextAlign = false, |
| 19 | withBlockWidth = false, |
| 20 | } ) { |
| 21 | const { |
| 22 | currentAtRule, |
| 23 | } = useBlockStyles(); |
| 24 | |
| 25 | function getAlignmentIcon() { |
| 26 | if ( withTextAlign ) { |
| 27 | const alignment = getStyleValue( 'textAlign', currentAtRule ); |
| 28 | |
| 29 | switch ( alignment ) { |
| 30 | case 'center': |
| 31 | return alignCenter; |
| 32 | case 'right': |
| 33 | return alignRight; |
| 34 | case 'justify': |
| 35 | return alignJustify; |
| 36 | case 'left': |
| 37 | default: |
| 38 | return alignLeft; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const textAlignments = [ |
| 44 | { |
| 45 | icon: alignLeft, |
| 46 | value: 'left', |
| 47 | label: __( 'Left', 'generateblocks' ), |
| 48 | }, |
| 49 | { |
| 50 | icon: alignCenter, |
| 51 | value: 'center', |
| 52 | label: __( 'Center', 'generateblocks' ), |
| 53 | }, |
| 54 | { |
| 55 | icon: alignRight, |
| 56 | value: 'right', |
| 57 | label: __( 'Right', 'generateblocks' ), |
| 58 | }, |
| 59 | { |
| 60 | icon: alignJustify, |
| 61 | value: 'justify', |
| 62 | label: __( 'Justify', 'generateblocks' ), |
| 63 | }, |
| 64 | ]; |
| 65 | |
| 66 | if ( ! withTextAlign && ! withBlockWidth ) { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | return ( |
| 71 | <BlockControls group="inline"> |
| 72 | <Dropdown |
| 73 | popoverProps={ POPOVER_PROPS } |
| 74 | renderToggle={ ( { isOpen, onToggle } ) => ( |
| 75 | <ToolbarButton |
| 76 | icon={ getAlignmentIcon() } |
| 77 | label={ __( 'Alignment', 'generateblocks' ) } |
| 78 | onClick={ onToggle } |
| 79 | aria-expanded={ isOpen } |
| 80 | isPressed={ !! isOpen } |
| 81 | /> |
| 82 | ) } |
| 83 | renderContent={ () => ( |
| 84 | <> |
| 85 | { !! withTextAlign && ( |
| 86 | <MenuGroup label={ __( 'Text Alignment', 'generateblocks' ) }> |
| 87 | { textAlignments.map( ( { icon, value, label } ) => ( |
| 88 | <MenuItem |
| 89 | key={ value } |
| 90 | icon={ icon } |
| 91 | isPressed={ value === getStyleValue( 'textAlign', currentAtRule ) } |
| 92 | onClick={ () => { |
| 93 | if ( value === getStyleValue( 'textAlign', currentAtRule ) ) { |
| 94 | onStyleChange( 'textAlign', '', currentAtRule ); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | onStyleChange( 'textAlign', value, currentAtRule ); |
| 99 | } } |
| 100 | > |
| 101 | { label } |
| 102 | </MenuItem> |
| 103 | ) ) } |
| 104 | </MenuGroup> |
| 105 | ) } |
| 106 | |
| 107 | { !! withBlockWidth && '' === currentAtRule && undefined !== align && ( |
| 108 | <BlockWidth |
| 109 | align={ align } |
| 110 | onChange={ ( value ) => setAttributes( { align: value } ) } |
| 111 | clientId={ clientId } |
| 112 | /> |
| 113 | ) } |
| 114 | </> |
| 115 | ) } |
| 116 | /> |
| 117 | </BlockControls> |
| 118 | ); |
| 119 | } |
| 120 |