BlockSettings.jsx
176 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { SelectControl, BaseControl, ToggleControl } from '@wordpress/components'; |
| 3 | import { applyFilters } from '@wordpress/hooks'; |
| 4 | |
| 5 | import { OpenPanel, IconControl, ColorPicker } from '@edge22/components'; |
| 6 | |
| 7 | import { |
| 8 | ApplyFilters, |
| 9 | URLControls, |
| 10 | TagNameControl, |
| 11 | DynamicTagsOnboarder, |
| 12 | } from '@components/index.js'; |
| 13 | import { useBlockStyles } from '@hooks/useBlockStyles'; |
| 14 | |
| 15 | export function BlockSettings( { |
| 16 | getStyleValue, |
| 17 | onStyleChange, |
| 18 | name, |
| 19 | attributes, |
| 20 | setAttributes, |
| 21 | htmlAttributes, |
| 22 | context, |
| 23 | } ) { |
| 24 | const { |
| 25 | tagName, |
| 26 | icon, |
| 27 | iconLocation, |
| 28 | iconOnly, |
| 29 | } = attributes; |
| 30 | |
| 31 | const { |
| 32 | atRule, |
| 33 | } = useBlockStyles(); |
| 34 | |
| 35 | const panelProps = { |
| 36 | name, |
| 37 | attributes, |
| 38 | setAttributes, |
| 39 | onStyleChange, |
| 40 | getStyleValue, |
| 41 | }; |
| 42 | |
| 43 | const icons = applyFilters( |
| 44 | 'generateblocks.editor.iconSVGSets', |
| 45 | {}, |
| 46 | { attributes } |
| 47 | ); |
| 48 | |
| 49 | return ( |
| 50 | <ApplyFilters |
| 51 | name="generateblocks.editor.blockControls" |
| 52 | blockName={ name } |
| 53 | getStyleValue={ getStyleValue } |
| 54 | onStyleChange={ onStyleChange } |
| 55 | currentAtRule={ atRule } |
| 56 | attributes={ attributes } |
| 57 | setAttributes={ setAttributes } |
| 58 | > |
| 59 | <OpenPanel |
| 60 | { ...panelProps } |
| 61 | shouldRender={ 'a' === tagName && '' === atRule } |
| 62 | panelId="link-destination" |
| 63 | > |
| 64 | <URLControls |
| 65 | label={ __( 'Link Destination', 'generateblocks' ) } |
| 66 | setAttributes={ setAttributes } |
| 67 | htmlAttributes={ htmlAttributes } |
| 68 | context={ context } |
| 69 | tagName={ tagName } |
| 70 | /> |
| 71 | </OpenPanel> |
| 72 | |
| 73 | <OpenPanel |
| 74 | { ...panelProps } |
| 75 | shouldRender={ '' === atRule } |
| 76 | panelId="icon" |
| 77 | > |
| 78 | <IconControl |
| 79 | label={ __( 'Icon', 'generateblocks' ) } |
| 80 | value={ icon } |
| 81 | onChange={ ( value ) => { |
| 82 | // If the user hasn't done this before, align the icon and text. |
| 83 | if ( ! icon ) { |
| 84 | if ( ! getStyleValue( 'display' ) ) { |
| 85 | onStyleChange( 'display', 'inline-flex' ); |
| 86 | } |
| 87 | |
| 88 | if ( ! getStyleValue( 'alignItems' ) ) { |
| 89 | onStyleChange( 'alignItems', 'center' ); |
| 90 | } |
| 91 | |
| 92 | if ( ! getStyleValue( 'columnGap' ) ) { |
| 93 | onStyleChange( 'columnGap', '0.5em' ); |
| 94 | } |
| 95 | |
| 96 | if ( ! getStyleValue( 'width', '', '.gb-shape svg' ) ) { |
| 97 | onStyleChange( 'width', '1em', '', '.gb-shape svg' ); |
| 98 | } |
| 99 | |
| 100 | if ( ! getStyleValue( 'height', '', '.gb-shape svg' ) ) { |
| 101 | onStyleChange( 'height', '1em', '', '.gb-shape svg' ); |
| 102 | } |
| 103 | |
| 104 | if ( ! getStyleValue( 'fill', '', '.gb-shape svg' ) ) { |
| 105 | onStyleChange( 'fill', 'currentColor', '', '.gb-shape svg' ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | setAttributes( { icon: value } ); |
| 110 | } } |
| 111 | onClear={ () => setAttributes( { icon: '' } ) } |
| 112 | icons={ icons } |
| 113 | clearLabel={ __( 'Clear', 'generateblocks' ) } |
| 114 | openLabel={ __( 'Open Library', 'generateblocks' ) } |
| 115 | modalTitle={ __( 'Icon Library', 'generateblocks' ) } |
| 116 | /> |
| 117 | |
| 118 | { !! icon && ( |
| 119 | <> |
| 120 | <ColorPicker |
| 121 | label={ __( 'Icon Color', 'generateblocks' ) } |
| 122 | value={ getStyleValue( 'color', atRule, '.gb-shape svg' ) } |
| 123 | onChange={ ( value ) => onStyleChange( 'color', value, atRule, '.gb-shape svg' ) } |
| 124 | /> |
| 125 | |
| 126 | { ! iconOnly && ( |
| 127 | <SelectControl |
| 128 | label={ __( 'Icon Location', 'generateblocks' ) } |
| 129 | value={ iconLocation } |
| 130 | options={ [ |
| 131 | { label: __( 'Before', 'generateblocks' ), value: 'before' }, |
| 132 | { label: __( 'After', 'generateblocks' ), value: 'after' }, |
| 133 | ] } |
| 134 | onChange={ ( value ) => setAttributes( { iconLocation: value } ) } |
| 135 | /> |
| 136 | ) } |
| 137 | |
| 138 | <BaseControl |
| 139 | label={ __( 'Icon Display', 'generateblocks' ) } |
| 140 | id="gb-icon-only" |
| 141 | > |
| 142 | <ToggleControl |
| 143 | id="gb-icon-only" |
| 144 | label={ __( 'Show the icon by itself', 'generateblocks' ) } |
| 145 | checked={ !! iconOnly } |
| 146 | onChange={ () => setAttributes( { iconOnly: ! iconOnly } ) } |
| 147 | /> |
| 148 | </BaseControl> |
| 149 | </> |
| 150 | ) } |
| 151 | </OpenPanel> |
| 152 | |
| 153 | <OpenPanel |
| 154 | { ...panelProps } |
| 155 | panelId="settings" |
| 156 | > |
| 157 | { '' === atRule && ( |
| 158 | <TagNameControl |
| 159 | blockName="generateblocks/text" |
| 160 | value={ tagName } |
| 161 | onChange={ ( value ) => { |
| 162 | setAttributes( { tagName: value } ); |
| 163 | |
| 164 | if ( 'a' === value && ! getStyleValue( 'display', atRule ) ) { |
| 165 | onStyleChange( 'display', 'block' ); |
| 166 | } |
| 167 | } } |
| 168 | /> |
| 169 | ) } |
| 170 | </OpenPanel> |
| 171 | |
| 172 | <DynamicTagsOnboarder /> |
| 173 | </ApplyFilters> |
| 174 | ); |
| 175 | } |
| 176 |