index.js
241 lines
| 1 | /** |
| 2 | * Internal dependencies |
| 3 | */ |
| 4 | import './editor.scss'; |
| 5 | import hasNumericValue from '../../utils/has-numeric-value'; |
| 6 | |
| 7 | /** |
| 8 | * WordPress dependencies |
| 9 | */ |
| 10 | import { |
| 11 | __, |
| 12 | } from '@wordpress/i18n'; |
| 13 | |
| 14 | import { |
| 15 | Component, |
| 16 | Fragment, |
| 17 | } from '@wordpress/element'; |
| 18 | |
| 19 | import { |
| 20 | BaseControl, |
| 21 | ToggleControl, |
| 22 | TextControl, |
| 23 | RangeControl, |
| 24 | SelectControl, |
| 25 | } from '@wordpress/components'; |
| 26 | |
| 27 | /** |
| 28 | * External dependencies |
| 29 | */ |
| 30 | import { ColorPicker } from '@edge22/components'; |
| 31 | |
| 32 | /** |
| 33 | * Typography Component |
| 34 | */ |
| 35 | class GradientControl extends Component { |
| 36 | render() { |
| 37 | const { |
| 38 | attributes, |
| 39 | setAttributes, |
| 40 | attrGradient, |
| 41 | attrGradientDirection, |
| 42 | attrGradientColorOne, |
| 43 | attrGradientColorOneOpacity, |
| 44 | attrGradientColorStopOne, |
| 45 | attrGradientColorTwo, |
| 46 | attrGradientColorTwoOpacity, |
| 47 | attrGradientColorStopTwo, |
| 48 | defaultColorOne, |
| 49 | defaultColorTwo, |
| 50 | } = this.props; |
| 51 | |
| 52 | const { |
| 53 | gradientSelector, |
| 54 | innerZindex, |
| 55 | useInnerContainer, |
| 56 | position, |
| 57 | } = attributes; |
| 58 | |
| 59 | const selectorHelp = 'element' === gradientSelector ? __( 'Displays behind the background image.', 'generateblocks' ) : __( 'Displays in front of the background image.', 'generateblocks' ); |
| 60 | |
| 61 | return ( |
| 62 | <Fragment> |
| 63 | <ToggleControl |
| 64 | label={ __( 'Use Gradient', 'generateblocks' ) } |
| 65 | checked={ !! attributes[ attrGradient ] } |
| 66 | onChange={ ( value ) => { |
| 67 | const props = this.props; |
| 68 | let gradientDirection = attributes[ attrGradientDirection ], |
| 69 | gradientColorOne = attributes[ attrGradientColorOne ], |
| 70 | gradientColorTwo = attributes[ attrGradientColorTwo ]; |
| 71 | |
| 72 | if ( value ) { |
| 73 | gradientDirection = gradientDirection || 90; |
| 74 | gradientColorOne = gradientColorOne || 'rgba(255, 255, 255, 0.1)'; |
| 75 | gradientColorTwo = gradientColorTwo || 'rgba(0, 0, 0, 0.30)'; |
| 76 | } |
| 77 | |
| 78 | setAttributes( { |
| 79 | [ props[ 'attrGradient' ] ]: value, // eslint-disable-line dot-notation |
| 80 | [ props[ 'attrGradientDirection' ] ]: gradientDirection, // eslint-disable-line dot-notation |
| 81 | [ props[ 'attrGradientColorOne' ] ]: gradientColorOne, // eslint-disable-line dot-notation |
| 82 | [ props[ 'attrGradientColorTwo' ] ]: gradientColorTwo, // eslint-disable-line dot-notation |
| 83 | } ); |
| 84 | } } |
| 85 | /> |
| 86 | |
| 87 | { !! attributes[ attrGradient ] && ( |
| 88 | <Fragment> |
| 89 | { 'undefined' !== typeof gradientSelector && |
| 90 | <SelectControl |
| 91 | label={ __( 'Selector', 'generateblocks' ) } |
| 92 | help={ selectorHelp } |
| 93 | value={ gradientSelector } |
| 94 | options={ [ |
| 95 | { label: __( 'Element', 'generateblocks' ), value: 'element' }, |
| 96 | { label: __( 'Pseudo Element', 'generateblocks' ), value: 'pseudo-element' }, |
| 97 | ] } |
| 98 | onChange={ ( value ) => { |
| 99 | setAttributes( { |
| 100 | gradientSelector: value, |
| 101 | } ); |
| 102 | |
| 103 | if ( useInnerContainer && ! hasNumericValue( innerZindex ) && 'pseudo-element' === value ) { |
| 104 | setAttributes( { |
| 105 | innerZindex: 1, |
| 106 | } ); |
| 107 | } |
| 108 | |
| 109 | if ( ! useInnerContainer && 'pseudo-element' === value && ! position ) { |
| 110 | setAttributes( { position: 'relative' } ); |
| 111 | } |
| 112 | } } |
| 113 | /> |
| 114 | } |
| 115 | |
| 116 | <BaseControl> |
| 117 | <span className="components-base-control__label">{ __( 'Direction', 'generateblocks' ) }</span> |
| 118 | |
| 119 | <RangeControl |
| 120 | value={ attributes[ attrGradientDirection ] ? attributes[ attrGradientDirection ] : 0 } |
| 121 | onChange={ ( value ) => { |
| 122 | setAttributes( { |
| 123 | [ attrGradientDirection ]: value, |
| 124 | } ); |
| 125 | } } |
| 126 | min={ 0 } |
| 127 | max={ 360 } |
| 128 | step={ 1 } |
| 129 | initialPosition={ 90 } |
| 130 | /> |
| 131 | </BaseControl> |
| 132 | |
| 133 | <BaseControl> |
| 134 | <span className="components-base-control__label">{ __( 'Color One', 'generateblocks' ) }</span> |
| 135 | |
| 136 | <div className="gblocks-component-gradient-control"> |
| 137 | <ColorPicker |
| 138 | value={ attributes[ attrGradientColorOne ] } |
| 139 | alpha={ true } |
| 140 | valueOpacity={ attributes[ attrGradientColorOneOpacity ] || 0 === attributes[ attrGradientColorOneOpacity ] ? attributes[ attrGradientColorOneOpacity ] : 1 } |
| 141 | attrOpacity={ 'gradientColorOneOpacity' } |
| 142 | onChange={ ( value ) => |
| 143 | setAttributes( { |
| 144 | [ attrGradientColorOne ]: value, |
| 145 | } ) |
| 146 | } |
| 147 | onOpacityChange={ ( value ) => |
| 148 | setAttributes( { |
| 149 | [ attrGradientColorOneOpacity ]: value, |
| 150 | } ) |
| 151 | } |
| 152 | onClear={ () => |
| 153 | setAttributes( { |
| 154 | [ attrGradientColorOne ]: defaultColorOne, |
| 155 | } ) |
| 156 | } |
| 157 | /> |
| 158 | |
| 159 | <TextControl |
| 160 | className={ 'gblocks-component-gradient-stop-value' } |
| 161 | type={ 'text' } |
| 162 | value={ attributes[ attrGradientColorStopOne ] || 0 === attributes[ attrGradientColorStopOne ] ? attributes[ attrGradientColorStopOne ] : '' } |
| 163 | placeholder={ __( 'Stop position (%)', 'generateblocks' ) } |
| 164 | onChange={ ( value ) => { |
| 165 | setAttributes( { |
| 166 | [ attrGradientColorStopOne ]: value, |
| 167 | } ); |
| 168 | } } |
| 169 | onBlur={ () => { |
| 170 | if ( attributes[ attrGradientColorStopOne ] || 0 === attributes[ attrGradientColorStopOne ] ) { |
| 171 | setAttributes( { |
| 172 | [ attrGradientColorStopOne ]: parseFloat( attributes[ attrGradientColorStopOne ] ), |
| 173 | } ); |
| 174 | } |
| 175 | } } |
| 176 | onClick={ ( e ) => { |
| 177 | // Make sure onBlur fires in Firefox. |
| 178 | e.currentTarget.focus(); |
| 179 | } } |
| 180 | /> |
| 181 | </div> |
| 182 | </BaseControl> |
| 183 | |
| 184 | <BaseControl> |
| 185 | <span className="components-base-control__label">{ __( 'Color Two', 'generateblocks' ) }</span> |
| 186 | <div className="gblocks-component-gradient-control"> |
| 187 | <ColorPicker |
| 188 | value={ attributes[ attrGradientColorTwo ] } |
| 189 | alpha={ true } |
| 190 | valueOpacity={ attributes[ attrGradientColorTwoOpacity ] || 0 === attributes[ attrGradientColorTwoOpacity ] ? attributes[ attrGradientColorTwoOpacity ] : 1 } |
| 191 | attrOpacity={ 'gradientColorTwoOpacity' } |
| 192 | onChange={ ( value ) => |
| 193 | setAttributes( { |
| 194 | [ attrGradientColorTwo ]: value, |
| 195 | } ) |
| 196 | } |
| 197 | onOpacityChange={ ( value ) => |
| 198 | setAttributes( { |
| 199 | [ attrGradientColorTwoOpacity ]: value, |
| 200 | } ) |
| 201 | } |
| 202 | onClear={ () => |
| 203 | setAttributes( { |
| 204 | [ attrGradientColorTwo ]: defaultColorTwo, |
| 205 | } ) |
| 206 | } |
| 207 | /> |
| 208 | |
| 209 | <TextControl |
| 210 | className={ 'gblocks-component-gradient-stop-value' } |
| 211 | type={ 'text' } |
| 212 | value={ attributes[ attrGradientColorStopTwo ] || 0 === attributes[ attrGradientColorStopTwo ] ? attributes[ attrGradientColorStopTwo ] : '' } |
| 213 | placeholder={ __( 'Stop position (%)', 'generateblocks' ) } |
| 214 | onChange={ ( value ) => { |
| 215 | setAttributes( { |
| 216 | [ attrGradientColorStopTwo ]: value, |
| 217 | } ); |
| 218 | } } |
| 219 | onBlur={ () => { |
| 220 | if ( attributes[ attrGradientColorStopTwo ] || 0 === attributes[ attrGradientColorStopTwo ] ) { |
| 221 | setAttributes( { |
| 222 | [ attrGradientColorStopTwo ]: parseFloat( attributes[ attrGradientColorStopTwo ] ), |
| 223 | } ); |
| 224 | } |
| 225 | } } |
| 226 | onClick={ ( e ) => { |
| 227 | // Make sure onBlur fires in Firefox. |
| 228 | e.currentTarget.focus(); |
| 229 | } } |
| 230 | /> |
| 231 | </div> |
| 232 | </BaseControl> |
| 233 | </Fragment> |
| 234 | ) } |
| 235 | </Fragment> |
| 236 | ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | export default GradientControl; |
| 241 |