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