PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.6.0
GenerateBlocks v1.6.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / components / color-picker / index.js
generateblocks / src / components / color-picker Last commit date
editor.scss 4 years ago index.js 3 years ago
index.js
200 lines
1 /**
2 * External dependencies
3 */
4 import { RgbStringColorPicker, RgbaStringColorPicker } from 'react-colorful';
5 import { colord } from 'colord';
6 import { useDebounce } from '@wordpress/compose';
7 import { __ } from '@wordpress/i18n';
8 import { ColorPalette } from '@wordpress/block-editor';
9 import { useState, useEffect, useMemo, useRef } from '@wordpress/element';
10 import {
11 Tooltip,
12 BaseControl,
13 RangeControl,
14 Dropdown,
15 Button,
16 TextControl,
17 } from '@wordpress/components';
18
19 /**
20 * Internal dependencies
21 */
22 import hexToRGBA from '../../utils/hex-to-rgba';
23 import getIcon from '../../utils/get-icon';
24 import './editor.scss';
25
26 export default function ColorPicker( props ) {
27 const {
28 value,
29 onChange,
30 onOpacityChange,
31 label,
32 alpha = false,
33 valueOpacity = 1,
34 tooltip,
35 } = props;
36
37 const [ valueState, setValueState ] = useState( value || '' );
38 const inputRef = useRef( null );
39
40 const Component = alpha && 1 === valueOpacity
41 ? RgbaStringColorPicker
42 : RgbStringColorPicker;
43
44 const isHex = ( hex ) => {
45 return /^([0-9A-F]{3}){1,2}$/i.test( hex );
46 };
47
48 const getPaletteValue = ( colorValue ) => {
49 if ( String( colorValue ).startsWith( 'var(' ) ) {
50 const variableName = colorValue.match( /\(([^)]+)\)/ );
51
52 if ( variableName ) {
53 const variableValue = getComputedStyle( document.documentElement ).getPropertyValue( variableName[ 1 ] );
54
55 if ( variableValue ) {
56 colorValue = variableValue;
57 }
58 }
59 }
60
61 return colord( colorValue ).toRgbString();
62 };
63
64 const rgbColor = useMemo( () => getPaletteValue( value ), [ value ] );
65 const debouncedSetColor = useDebounce( onChange );
66
67 useEffect( () => {
68 if ( value !== valueState ) {
69 debouncedSetColor( valueState );
70 }
71
72 // Keep the input focused.
73 setTimeout( () => {
74 if ( inputRef.current ) {
75 inputRef.current.focus();
76 }
77 }, 10 );
78 }, [ valueState ] );
79
80 return (
81 <div className="gblocks-color-component">
82 { !! label &&
83 <span className="gblocks-color-component__label">{ label }</span>
84 }
85
86 <Dropdown
87 className="gblocks-color-component__toggle"
88 contentClassName="gblocks-color-component-content"
89 position="top left"
90 renderToggle={ ( { isOpen, onToggle } ) => {
91 const button = <Button
92 className="gblocks-color-component__toggle-button"
93 onClick={ onToggle }
94 aria-expanded={ isOpen }
95 >
96 <span
97 className="gblocks-color-component__toggle-indicator"
98 style={ { background: value ? hexToRGBA( value, valueOpacity ) : null } }
99 />
100 </Button>;
101
102 return (
103 <>
104 { !! tooltip
105 ? <Tooltip text={ tooltip }>{ button }</Tooltip>
106 : button
107 }
108 </>
109 );
110 } }
111 renderContent={ () =>
112 <>
113 <Component
114 color={ rgbColor }
115 onChange={ ( nextColor ) => {
116 if ( colord( nextColor ).isValid() ) {
117 const alphaValue = colord( nextColor ).alpha();
118 nextColor = 1 === alphaValue ? colord( nextColor ).toHex() : nextColor;
119 }
120
121 setValueState( nextColor );
122 } }
123 />
124
125 <div className="gblocks-color-component-content__input-wrapper">
126 <TextControl
127 ref={ inputRef }
128 className="gblocks-color-input"
129 type={ 'text' }
130 value={ valueState }
131 onChange={ ( nextColor ) => {
132 if ( ! nextColor.startsWith( '#' ) && isHex( nextColor ) ) {
133 nextColor = '#' + nextColor;
134 }
135
136 setValueState( nextColor );
137 } }
138 onBlur={ () => {
139 if ( colord( value ).isValid() ) {
140 const alphaValue = colord( value ).alpha();
141
142 if ( 1 === alphaValue ) {
143 setValueState( colord( value ).toHex() );
144 }
145 }
146 } }
147 />
148
149 <Button
150 isSmall
151 isSecondary
152 className="gblocks-color-input-clear"
153 onClick={ () => {
154 setValueState( '' );
155
156 if ( alpha && 1 !== valueOpacity ) {
157 onOpacityChange( 1 );
158 }
159 } }
160 >
161 { __( 'Clear', 'generateblocks' ) }
162 </Button>
163 </div>
164
165 { alpha && 1 !== valueOpacity &&
166 <div className="gblocks-color-component-content__opacity">
167 <Tooltip text={ __( 'Opacity', 'generateblocks' ) }>
168 { getIcon( 'gradient' ) }
169 </Tooltip>
170
171 <RangeControl
172 value={ valueOpacity ? valueOpacity : 0 }
173 onChange={ ( opacityValue ) => onOpacityChange( opacityValue ) }
174 min={ 0 }
175 max={ 1 }
176 step={ 0.01 }
177 initialPosition={ 1 }
178 />
179 </div>
180 }
181
182 <BaseControl
183 className="gblocks-color-component-content__palette"
184 >
185 <ColorPalette
186 value={ value ? value : '' }
187 onChange={ ( color ) => {
188 setValueState( color );
189 } }
190 disableCustomColors={ true }
191 clearable={ false }
192 />
193 </BaseControl>
194 </>
195 }
196 />
197 </div>
198 );
199 }
200