index.js
251 lines
| 1 | import hexToRGBA from '../../utils/hex-to-rgba'; |
| 2 | import getIcon from '../../utils/get-icon'; |
| 3 | |
| 4 | // Import CSS |
| 5 | import './editor.scss'; |
| 6 | |
| 7 | import { |
| 8 | Component, |
| 9 | } from '@wordpress/element'; |
| 10 | |
| 11 | import { |
| 12 | __, |
| 13 | } from '@wordpress/i18n'; |
| 14 | |
| 15 | import { |
| 16 | Tooltip, |
| 17 | BaseControl, |
| 18 | ColorPicker, |
| 19 | RangeControl, |
| 20 | Popover, |
| 21 | Button, |
| 22 | TextControl, |
| 23 | } from '@wordpress/components'; |
| 24 | |
| 25 | import { |
| 26 | ColorPalette, |
| 27 | } from '@wordpress/block-editor'; |
| 28 | |
| 29 | export default class GenerateBlocksColorPicker extends Component { |
| 30 | constructor() { |
| 31 | super( ...arguments ); |
| 32 | |
| 33 | this.state = { |
| 34 | colorKey: false, |
| 35 | }; |
| 36 | |
| 37 | this.timer = null; |
| 38 | } |
| 39 | |
| 40 | componentWillUnmount() { |
| 41 | clearTimeout( this.timer ); |
| 42 | } |
| 43 | |
| 44 | render() { |
| 45 | const { |
| 46 | value, |
| 47 | onChange, |
| 48 | onOpacityChange, |
| 49 | label, |
| 50 | alpha = false, |
| 51 | valueOpacity, |
| 52 | } = this.props; |
| 53 | |
| 54 | const { |
| 55 | colorKey, |
| 56 | } = this.state; |
| 57 | |
| 58 | const toggleVisible = () => { |
| 59 | this.setState( { isVisible: true } ); |
| 60 | }; |
| 61 | |
| 62 | const toggleClose = () => { |
| 63 | if ( this.state.isVisible === true ) { |
| 64 | this.setState( { isVisible: false } ); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | const isHex = ( hex ) => { |
| 69 | return /^([0-9A-F]{3}){1,2}$/i.test( hex ); |
| 70 | }; |
| 71 | |
| 72 | const getPaletteValue = ( colorValue ) => { |
| 73 | if ( String( colorValue ).startsWith( 'var(' ) ) { |
| 74 | const variableName = colorValue.match( /\(([^)]+)\)/ ); |
| 75 | |
| 76 | if ( variableName ) { |
| 77 | const variableValue = getComputedStyle( document.documentElement ).getPropertyValue( variableName[ 1 ] ); |
| 78 | |
| 79 | if ( variableValue ) { |
| 80 | colorValue = variableValue; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return colorValue; |
| 86 | }; |
| 87 | |
| 88 | return ( |
| 89 | <BaseControl |
| 90 | className="gblocks-component-color-picker-wrapper" |
| 91 | > |
| 92 | { !! label && |
| 93 | <div className="gblocks-color-component-label"> |
| 94 | <span>{ label }</span> |
| 95 | </div> |
| 96 | } |
| 97 | |
| 98 | <div className="gblocks-color-picker-area"> |
| 99 | { ! this.state.isVisible && |
| 100 | <div className="components-circular-option-picker__option-wrapper"> |
| 101 | <Tooltip text={ __( 'Choose Color', 'generateblocks' ) }> |
| 102 | <button |
| 103 | type="button" |
| 104 | aria-expanded={ this.state.isVisible } |
| 105 | className="components-color-palette__item components-circular-option-picker__option" |
| 106 | onClick={ toggleVisible } |
| 107 | aria-label={ __( 'Custom color picker', 'generateblocks' ) } |
| 108 | style={ { color: value ? hexToRGBA( value, valueOpacity ) : 'transparent' } } |
| 109 | > |
| 110 | <span className="components-color-palette__custom-color-gradient" /> |
| 111 | </button> |
| 112 | </Tooltip> |
| 113 | </div> |
| 114 | } |
| 115 | |
| 116 | { this.state.isVisible && |
| 117 | <div className="components-circular-option-picker__option-wrapper"> |
| 118 | <Tooltip text={ __( 'Choose Color', 'generateblocks' ) }> |
| 119 | <button |
| 120 | type="button" |
| 121 | aria-expanded={ this.state.isVisible } |
| 122 | className="components-color-palette__item components-circular-option-picker__option" |
| 123 | onClick={ toggleClose } |
| 124 | aria-label={ __( 'Custom color picker', 'generateblocks' ) } |
| 125 | style={ { color: value ? hexToRGBA( value, valueOpacity ) : 'transparent' } } |
| 126 | > |
| 127 | <span className="components-color-palette__custom-color-gradient" /> |
| 128 | </button> |
| 129 | </Tooltip> |
| 130 | </div> |
| 131 | } |
| 132 | |
| 133 | { this.state.isVisible && |
| 134 | <Popover position="top left" className="gblocks-component-color-picker" onClose={ toggleClose }> |
| 135 | <BaseControl key={ colorKey }> |
| 136 | <ColorPicker |
| 137 | key={ colorKey } |
| 138 | color={ getPaletteValue( value ) || '' } |
| 139 | onChangeComplete={ ( color ) => { |
| 140 | let colorString; |
| 141 | |
| 142 | if ( 'undefined' === typeof color.rgb || color.rgb.a === 1 ) { |
| 143 | colorString = color.hex; |
| 144 | } else { |
| 145 | const { r, g, b, a } = color.rgb; |
| 146 | colorString = `rgba(${ r }, ${ g }, ${ b }, ${ a })`; |
| 147 | } |
| 148 | |
| 149 | onChange( colorString ); |
| 150 | } } |
| 151 | disableAlpha={ ! alpha || 1 !== valueOpacity } |
| 152 | /> |
| 153 | |
| 154 | <div className="gblocks-color-input-wrapper"> |
| 155 | <TextControl |
| 156 | className="gblocks-color-input" |
| 157 | type={ 'text' } |
| 158 | value={ value || '' } |
| 159 | onChange={ ( color ) => { |
| 160 | if ( ! color.startsWith( '#' ) && isHex( color ) ) { |
| 161 | color = '#' + color; |
| 162 | } |
| 163 | |
| 164 | onChange( color ); |
| 165 | |
| 166 | clearTimeout( this.timer ); |
| 167 | |
| 168 | this.timer = setTimeout( () => { |
| 169 | this.setState( { |
| 170 | colorKey: color, |
| 171 | } ); |
| 172 | |
| 173 | const input = document.querySelector( '.gblocks-color-input-wrapper input' ); |
| 174 | |
| 175 | if ( input ) { |
| 176 | input.focus(); |
| 177 | } |
| 178 | }, 350 ); |
| 179 | } } |
| 180 | /> |
| 181 | |
| 182 | <Button |
| 183 | isSmall |
| 184 | isSecondary |
| 185 | className="components-color-clear-color" |
| 186 | onClick={ () => { |
| 187 | onChange( '' ); |
| 188 | |
| 189 | if ( alpha && 1 !== valueOpacity ) { |
| 190 | onOpacityChange( 1 ); |
| 191 | } |
| 192 | |
| 193 | this.setState( { |
| 194 | colorKey: false, |
| 195 | } ); |
| 196 | |
| 197 | setTimeout( function() { |
| 198 | document.querySelector( '.gblocks-color-input-wrapper input' ).focus(); |
| 199 | }, 10 ); |
| 200 | } } |
| 201 | > |
| 202 | { __( 'Clear Color', 'generateblocks' ) } |
| 203 | </Button> |
| 204 | </div> |
| 205 | </BaseControl> |
| 206 | |
| 207 | { alpha && 1 !== valueOpacity && |
| 208 | <div className="gblocks-component-color-opacity"> |
| 209 | <Tooltip text={ __( 'Opacity', 'generateblocks' ) }> |
| 210 | { getIcon( 'gradient' ) } |
| 211 | </Tooltip> |
| 212 | |
| 213 | <RangeControl |
| 214 | value={ valueOpacity ? valueOpacity : 0 } |
| 215 | onChange={ ( opacityValue ) => onOpacityChange( opacityValue ) } |
| 216 | min={ 0 } |
| 217 | max={ 1 } |
| 218 | step={ 0.01 } |
| 219 | initialPosition={ 1 } |
| 220 | /> |
| 221 | </div> |
| 222 | } |
| 223 | |
| 224 | <BaseControl |
| 225 | className="gblocks-component-color-picker-palette" |
| 226 | > |
| 227 | <ColorPalette |
| 228 | value={ value } |
| 229 | onChange={ ( color ) => { |
| 230 | onChange( color ); |
| 231 | |
| 232 | this.setState( { |
| 233 | colorKey: color, |
| 234 | } ); |
| 235 | |
| 236 | setTimeout( function() { |
| 237 | document.querySelector( '.gblocks-color-input-wrapper input' ).focus(); |
| 238 | }, 10 ); |
| 239 | } } |
| 240 | disableCustomColors={ true } |
| 241 | clearable={ false } |
| 242 | /> |
| 243 | </BaseControl> |
| 244 | </Popover> |
| 245 | } |
| 246 | </div> |
| 247 | </BaseControl> |
| 248 | ); |
| 249 | } |
| 250 | } |
| 251 |