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