index.js
265 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | import './editor.scss'; |
| 5 | import socialSvgs from './svgs-social'; |
| 6 | import generalSvgs from './svgs-general'; |
| 7 | import sanitizeSVG from '../../utils/sanitize-svg'; |
| 8 | |
| 9 | /** |
| 10 | * WordPress dependencies |
| 11 | */ |
| 12 | const { |
| 13 | __, |
| 14 | } = wp.i18n; |
| 15 | |
| 16 | const { |
| 17 | Component, |
| 18 | Fragment, |
| 19 | renderToString, |
| 20 | } = wp.element; |
| 21 | |
| 22 | const { |
| 23 | BaseControl, |
| 24 | SelectControl, |
| 25 | ToggleControl, |
| 26 | TextControl, |
| 27 | Tooltip, |
| 28 | Button, |
| 29 | PanelBody, |
| 30 | PanelRow, |
| 31 | } = wp.components; |
| 32 | |
| 33 | /** |
| 34 | * Typography Component |
| 35 | */ |
| 36 | class IconPicker extends Component { |
| 37 | constructor() { |
| 38 | super( ...arguments ); |
| 39 | |
| 40 | this.state = { |
| 41 | showIcons: false, |
| 42 | showGeneralIcons: false, |
| 43 | showSocialIcons: false, |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | render() { |
| 48 | const { |
| 49 | attributes, |
| 50 | setAttributes, |
| 51 | attrIcon, |
| 52 | attrIconLocation, |
| 53 | locationOptions, |
| 54 | attrRemoveText, |
| 55 | attrAriaLabel, |
| 56 | } = this.props; |
| 57 | |
| 58 | return ( |
| 59 | <Fragment> |
| 60 | <BaseControl className="gb-svg-html"> |
| 61 | <TextControl |
| 62 | label={ __( 'Icon SVG HTML', 'generateblocks' ) } |
| 63 | value={ attributes[ attrIcon ] } |
| 64 | onChange={ ( value ) => { |
| 65 | setAttributes( { |
| 66 | [ this.props[ 'attrIcon' ] ]: sanitizeSVG( value ), // eslint-disable-line dot-notation |
| 67 | } ); |
| 68 | |
| 69 | if ( '' !== value ) { |
| 70 | setAttributes( { |
| 71 | 'hasIcon': true, // eslint-disable-line quote-props |
| 72 | } ); |
| 73 | } else { |
| 74 | setAttributes( { |
| 75 | 'hasIcon': false, // eslint-disable-line quote-props |
| 76 | } ); |
| 77 | } |
| 78 | } } |
| 79 | /> |
| 80 | |
| 81 | <div className="gb-icon-preview"> |
| 82 | <span dangerouslySetInnerHTML={ { __html: sanitizeSVG( attributes[ attrIcon ] ) } } /> |
| 83 | |
| 84 | <Button |
| 85 | isSmall |
| 86 | className="reset-icon is-secondary" |
| 87 | onClick={ () => { |
| 88 | setAttributes( { |
| 89 | [ this.props[ 'attrIcon' ] ]: '', // eslint-disable-line dot-notation |
| 90 | 'hasIcon': false, // eslint-disable-line quote-props |
| 91 | } ); |
| 92 | } } |
| 93 | > |
| 94 | <span className="editor-block-types-list__item-icon"> |
| 95 | { __( 'Clear', 'generateblocks' ) } |
| 96 | </span> |
| 97 | </Button> |
| 98 | </div> |
| 99 | </BaseControl> |
| 100 | |
| 101 | <BaseControl className="gb-icon-chooser"> |
| 102 | <PanelBody title={ __( 'General Icons', 'generateblocks' ) } initialOpen={ false }> |
| 103 | <PanelRow> |
| 104 | <BaseControl> |
| 105 | <ul className="gblocks-icon-chooser"> |
| 106 | { |
| 107 | Object.keys( generalSvgs ).map( ( svg, i ) => { |
| 108 | return ( |
| 109 | <li key={ `editor-pblock-types-list-item-${ i }` }> |
| 110 | <Tooltip text={ ( generalSvgs[ svg ].label ) }> |
| 111 | <Button |
| 112 | isLarge |
| 113 | className="editor-block-list-item-button" |
| 114 | onClick={ () => { |
| 115 | setAttributes( { |
| 116 | [ this.props[ 'attrIcon' ] ]: renderToString( generalSvgs[ svg ][ 'icon' ] ), // eslint-disable-line dot-notation |
| 117 | 'hasIcon': true, // eslint-disable-line quote-props |
| 118 | } ); |
| 119 | } } |
| 120 | > |
| 121 | <span className="editor-block-types-list__item-icon"> |
| 122 | { generalSvgs[ svg ].icon } |
| 123 | </span> |
| 124 | </Button> |
| 125 | </Tooltip> |
| 126 | </li> |
| 127 | ); |
| 128 | } ) |
| 129 | } |
| 130 | </ul> |
| 131 | </BaseControl> |
| 132 | </PanelRow> |
| 133 | </PanelBody> |
| 134 | |
| 135 | <PanelBody title={ __( 'Social Icons', 'generateblocks' ) } initialOpen={ false }> |
| 136 | <PanelRow> |
| 137 | <BaseControl> |
| 138 | <ul className="gblocks-icon-chooser"> |
| 139 | { |
| 140 | Object.keys( socialSvgs ).map( ( svg, i ) => { |
| 141 | return ( |
| 142 | <li key={ `editor-pblock-types-list-item-${ i }` }> |
| 143 | <Tooltip text={ ( socialSvgs[ svg ].label ) }> |
| 144 | <Button |
| 145 | isLarge |
| 146 | className="editor-block-list-item-button" |
| 147 | onClick={ () => { |
| 148 | setAttributes( { |
| 149 | [ this.props[ 'attrIcon' ] ]: renderToString( socialSvgs[ svg ][ 'icon' ] ), // eslint-disable-line dot-notation |
| 150 | 'hasIcon': true, // eslint-disable-line quote-props |
| 151 | } ); |
| 152 | } } |
| 153 | > |
| 154 | <span className="editor-block-types-list__item-icon"> |
| 155 | { socialSvgs[ svg ].icon } |
| 156 | </span> |
| 157 | </Button> |
| 158 | </Tooltip> |
| 159 | </li> |
| 160 | ); |
| 161 | } ) |
| 162 | } |
| 163 | </ul> |
| 164 | </BaseControl> |
| 165 | </PanelRow> |
| 166 | </PanelBody> |
| 167 | </BaseControl> |
| 168 | |
| 169 | { ( typeof attributes[ attrIconLocation ] !== 'undefined' && ! attributes[ attrRemoveText ] && !! attributes[ attrIcon ] ) && |
| 170 | <SelectControl |
| 171 | label={ __( 'Icon Location', 'generateblocks' ) } |
| 172 | value={ attributes[ attrIconLocation ] } |
| 173 | options={ locationOptions } |
| 174 | onChange={ ( value ) => { |
| 175 | const leftPadding = attributes.iconPaddingLeft, |
| 176 | rightPadding = attributes.iconPaddingRight, |
| 177 | rightPaddingTablet = attributes.iconPaddingRightTablet, |
| 178 | leftPaddingTablet = attributes.iconPaddingLeftTablet, |
| 179 | rightPaddingMobile = attributes.iconPaddingRightMobile, |
| 180 | leftPaddingMobile = attributes.iconPaddingLeftMobile; |
| 181 | |
| 182 | if ( 'right' === value ) { |
| 183 | if ( ! leftPadding && rightPadding ) { |
| 184 | setAttributes( { |
| 185 | iconPaddingLeft: rightPadding, |
| 186 | iconPaddingRight: '', |
| 187 | } ); |
| 188 | } |
| 189 | |
| 190 | if ( ! leftPaddingTablet && rightPaddingTablet ) { |
| 191 | setAttributes( { |
| 192 | iconPaddingLeftTablet: rightPaddingTablet, |
| 193 | iconPaddingRightTablet: '', |
| 194 | } ); |
| 195 | } |
| 196 | |
| 197 | if ( ! leftPaddingMobile && rightPaddingMobile ) { |
| 198 | setAttributes( { |
| 199 | iconPaddingLeftMobile: rightPaddingMobile, |
| 200 | iconPaddingRightMobile: '', |
| 201 | } ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if ( 'left' === value ) { |
| 206 | if ( ! rightPadding && leftPadding ) { |
| 207 | setAttributes( { |
| 208 | iconPaddingRight: leftPadding, |
| 209 | iconPaddingLeft: '', |
| 210 | } ); |
| 211 | } |
| 212 | |
| 213 | if ( ! rightPaddingTablet && leftPaddingTablet ) { |
| 214 | setAttributes( { |
| 215 | iconPaddingRightTablet: leftPaddingTablet, |
| 216 | iconPaddingLeftTablet: '', |
| 217 | } ); |
| 218 | } |
| 219 | |
| 220 | if ( ! rightPaddingMobile && leftPaddingMobile ) { |
| 221 | setAttributes( { |
| 222 | iconPaddingRightMobile: leftPaddingMobile, |
| 223 | iconPaddingLeftMobile: '', |
| 224 | } ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | setAttributes( { |
| 229 | [ this.props[ 'attrIconLocation' ] ]: value, // eslint-disable-line dot-notation |
| 230 | } ); |
| 231 | } } |
| 232 | /> |
| 233 | } |
| 234 | |
| 235 | { ( typeof attributes[ attrRemoveText ] !== 'undefined' && !! attributes[ attrIcon ] ) && |
| 236 | <ToggleControl |
| 237 | label={ __( 'Remove Text', 'generateblocks' ) } |
| 238 | checked={ !! attributes[ attrRemoveText ] } |
| 239 | onChange={ ( value ) => { |
| 240 | setAttributes( { |
| 241 | [ this.props[ 'attrRemoveText' ] ]: value, // eslint-disable-line dot-notation |
| 242 | } ); |
| 243 | } } |
| 244 | /> |
| 245 | } |
| 246 | |
| 247 | { typeof attributes[ attrAriaLabel ] !== 'undefined' && !! attributes[ attrRemoveText ] && !! attributes[ attrIcon ] && |
| 248 | <TextControl |
| 249 | label={ __( 'ARIA Label', 'generateblocks' ) } |
| 250 | help={ __( 'Helpful to people using screen readers.', 'generateblocks' ) } |
| 251 | value={ attributes[ attrAriaLabel ] } |
| 252 | onChange={ ( value ) => { |
| 253 | setAttributes( { |
| 254 | [ this.props[ 'attrAriaLabel' ] ]: value, // eslint-disable-line dot-notation |
| 255 | } ); |
| 256 | } } |
| 257 | /> |
| 258 | } |
| 259 | </Fragment> |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | export default IconPicker; |
| 265 |