forked from dimadinev/control-color-alpha
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWPTRTColorAlphaForm.js
168 lines (150 loc) · 4.68 KB
/
WPTRTColorAlphaForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* globals _, wp, React */
import ChromePicker from 'react-color';
import reactCSS from 'reactcss';
const WPTRTColorAlphaForm = ( props ) => {
/**
* Get the CSS value of the selected color.
*
* @param {Object} color - The color object from react-color.
* @return {string}
*/
const getCSSColor = ( color ) => {
if ( 'string' === typeof color ) {
return color;
}
if ( color.css ) {
return color.css;
}
if ( color.rgb && color.rgb.a && 1 > color.rgb.a ) {
return 'rgba(' + color.rgb.r + ',' + color.rgb.g + ',' + color.rgb.b + ',' + color.rgb.a + ')';
}
if ( color.hex ) {
return color.hex;
}
return color;
}
/**
* Properly format the value to be saved depending on our options.
*
* @param {string} value
* @return {string|Object}
*/
const formatValue = ( value ) => {
const saveArray = ( 'array' === props.choices.save_as );
let color, white, black, finalValue;
if ( saveArray ) {
color = Color( value );
white = Color( '#ffffff' );
black = Color( '#000000' );
// Get the basics for this color.
finalValue = {
r: color.r(),
g: color.g(),
b: color.b(),
h: color.h(),
s: color.s(),
l: color.l(),
a: color.a(),
v: color.toHsl().v
};
finalValue.hex = 1 === finalValue.a ? color.toCSS() : color.clone().a( 1 ).toCSS();
finalValue.css = 1 === finalValue.a ? finalValue.hex : 'rgba(' + finalValue.r + ',' + finalValue.g + ',' + finalValue.b + ',' + finalValue.a + ')';
// A11y properties.
finalValue.a11y = {
luminance: color.toLuminosity(),
distanceFromWhite: color.getDistanceLuminosityFrom( white ),
distanceFromBlack: color.getDistanceLuminosityFrom( black ),
maxContrastColor: color.clone().a( 1 ).getMaxContrastColor().toCSS(),
readableContrastingColorFromWhite: [
color.clone().a( 1 ).getReadableContrastingColor( white, 7 ).toCSS(),
color.clone().a( 1 ).getReadableContrastingColor( white, 4.5 ).toCSS()
],
readableContrastingColorFromBlack: [
color.clone().a( 1 ).getReadableContrastingColor( black, 7 ).toCSS(),
color.clone().a( 1 ).getReadableContrastingColor( black, 4.5 ).toCSS()
]
};
finalValue.a11y.isDark = finalValue.a11y.distanceFromWhite > finalValue.a11y.distanceFromBlack;
return finalValue;
}
return value;
};
/**
* Save the value when changing the colorpicker.
*
* @param {Object} color - The color object from react-color.
* @return {void}
*/
const handleChangeComplete = ( color ) => {
wp.customize.control( props.customizerSetting.id ).setting.set( formatValue( getCSSColor( color ) ) );
};
/**
* Save the value when changing the text input.
*
* @param {Object} e - The change event.
* @return {void}
*/
const handleInputChange = ( e ) => {
wp.customize.control( props.customizerSetting.id ).setting.set( formatValue( e.target.value ) );
};
/**
* Reset value to its default.
*
* @param {Object} e - The click event.
* @return {void}
*/
const resetValue = ( e ) => {
e.preventDefault();
let defaultValue = ( props.defaultValue ) ? props.defaultValue : '';
if ( 'string' === typeof defaultValue ) {
defaultValue = formatValue( defaultValue );
}
wp.customize.control( props.customizerSetting.id ).setting.set( defaultValue );
}
const styles = reactCSS({
'default': {
textInput: {
fontFamily: 'Menlo, Consolas, monaco, monospace',
borderRadius: '0 4px 4px 0',
width: '100%'
},
colorIndicator: {
background: props.value.css ? props.value.css : props.value,
display: 'block',
width: '40px',
borderRadius: '4px 0 0 4px'
},
inputWrapper: {
display: 'flex',
marginBottom: '12px',
},
resetButton: {
}
}
});
return (
<div>
<label className="customize-control-title" for={ props.control.id + '-input' }>{ props.label }</label>
<span className="description customize-control-description" dangerouslySetInnerHTML={{ __html: props.description }}></span>
<div className="customize-control-notifications-container" ref={ props.setNotificationContainer }></div>
<div style={ styles.inputWrapper }>
<span style={ styles.colorIndicator }></span>
<input
type="text"
style={ styles.textInput }
value={ 'array' === props.choices.save_as ? props.value.css : props.value }
onChange={ handleInputChange }
id={ props.control.id + '-input' }
/>
<button className="button" onClick={ resetValue } style={ styles.resetButton }>{ window.wp.i18n.__( 'Default' ) }</button>
</div>
<ChromePicker
width="300"
{ ...props.choices }
color={ 'array' === props.choices.save_as ? props.value.css : props.value }
onChangeComplete={ handleChangeComplete }
/>
</div>
);
};
export default WPTRTColorAlphaForm;