-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
with-colors.js
272 lines (255 loc) · 7.55 KB
/
with-colors.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* External dependencies
*/
import { isString, kebabCase, reduce, upperFirst } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { compose, createHigherOrderComponent } from '@wordpress/compose';
/**
* Internal dependencies
*/
import {
getColorClassName,
getColorObjectByColorValue,
getColorObjectByAttributeValues,
getMostReadableColor,
} from './utils';
import useEditorFeature from '../use-editor-feature';
const DEFAULT_COLORS = [];
/**
* Higher order component factory for injecting the `colorsArray` argument as
* the colors prop in the `withCustomColors` HOC.
*
* @param {Array} colorsArray An array of color objects.
*
* @return {Function} The higher order component.
*/
const withCustomColorPalette = ( colorsArray ) =>
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => (
<WrappedComponent { ...props } colors={ colorsArray } />
),
'withCustomColorPalette'
);
/**
* Higher order component factory for injecting the editor colors as the
* `colors` prop in the `withColors` HOC.
*
* @return {Function} The higher order component.
*/
const withEditorColorPalette = () =>
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const colors =
useEditorFeature( 'color.palette' ) || DEFAULT_COLORS;
return <WrappedComponent { ...props } colors={ colors } />;
},
'withEditorColorPalette'
);
/**
* Helper function used with `createHigherOrderComponent` to create
* higher order components for managing color logic.
*
* @param {Array} colorTypes An array of color types (e.g. 'backgroundColor, borderColor).
* @param {Function} withColorPalette A HOC for injecting the 'colors' prop into the WrappedComponent.
*
* @return {WPComponent} The component that can be used as a HOC.
*/
function createColorHOC( colorTypes, withColorPalette ) {
const colorMap = reduce(
colorTypes,
( colorObject, colorType ) => {
return {
...colorObject,
...( isString( colorType )
? { [ colorType ]: kebabCase( colorType ) }
: colorType ),
};
},
{}
);
return compose( [
withColorPalette,
( WrappedComponent ) => {
return class extends Component {
constructor( props ) {
super( props );
this.setters = this.createSetters();
this.colorUtils = {
getMostReadableColor: this.getMostReadableColor.bind(
this
),
};
this.state = {};
}
getMostReadableColor( colorValue ) {
const { colors } = this.props;
return getMostReadableColor( colors, colorValue );
}
createSetters() {
return reduce(
colorMap,
(
settersAccumulator,
colorContext,
colorAttributeName
) => {
const upperFirstColorAttributeName = upperFirst(
colorAttributeName
);
const customColorAttributeName = `custom${ upperFirstColorAttributeName }`;
settersAccumulator[
`set${ upperFirstColorAttributeName }`
] = this.createSetColor(
colorAttributeName,
customColorAttributeName
);
return settersAccumulator;
},
{}
);
}
createSetColor( colorAttributeName, customColorAttributeName ) {
return ( colorValue ) => {
const colorObject = getColorObjectByColorValue(
this.props.colors,
colorValue
);
this.props.setAttributes( {
[ colorAttributeName ]:
colorObject && colorObject.slug
? colorObject.slug
: undefined,
[ customColorAttributeName ]:
colorObject && colorObject.slug
? undefined
: colorValue,
} );
};
}
static getDerivedStateFromProps(
{ attributes, colors },
previousState
) {
return reduce(
colorMap,
( newState, colorContext, colorAttributeName ) => {
const colorObject = getColorObjectByAttributeValues(
colors,
attributes[ colorAttributeName ],
attributes[
`custom${ upperFirst(
colorAttributeName
) }`
]
);
const previousColorObject =
previousState[ colorAttributeName ];
const previousColor = previousColorObject?.color;
/**
* The "and previousColorObject" condition checks that a previous color object was already computed.
* At the start previousColorObject and colorValue are both equal to undefined
* bus as previousColorObject does not exist we should compute the object.
*/
if (
previousColor === colorObject.color &&
previousColorObject
) {
newState[
colorAttributeName
] = previousColorObject;
} else {
newState[ colorAttributeName ] = {
...colorObject,
class: getColorClassName(
colorContext,
colorObject.slug
),
};
}
return newState;
},
{}
);
}
render() {
return (
<WrappedComponent
{ ...{
...this.props,
colors: undefined,
...this.state,
...this.setters,
colorUtils: this.colorUtils,
} }
/>
);
}
};
},
] );
}
/**
* A higher-order component factory for creating a 'withCustomColors' HOC, which handles color logic
* for class generation color value, retrieval and color attribute setting.
*
* Use this higher-order component to work with a custom set of colors.
*
* @example
*
* ```jsx
* const CUSTOM_COLORS = [ { name: 'Red', slug: 'red', color: '#ff0000' }, { name: 'Blue', slug: 'blue', color: '#0000ff' } ];
* const withCustomColors = createCustomColorsHOC( CUSTOM_COLORS );
* // ...
* export default compose(
* withCustomColors( 'backgroundColor', 'borderColor' ),
* MyColorfulComponent,
* );
* ```
*
* @param {Array} colorsArray The array of color objects (name, slug, color, etc... ).
*
* @return {Function} Higher-order component.
*/
export function createCustomColorsHOC( colorsArray ) {
return ( ...colorTypes ) => {
const withColorPalette = withCustomColorPalette( colorsArray );
return createHigherOrderComponent(
createColorHOC( colorTypes, withColorPalette ),
'withCustomColors'
);
};
}
/**
* A higher-order component, which handles color logic for class generation color value, retrieval and color attribute setting.
*
* For use with the default editor/theme color palette.
*
* @example
*
* ```jsx
* export default compose(
* withColors( 'backgroundColor', { textColor: 'color' } ),
* MyColorfulComponent,
* );
* ```
*
* @param {...(Object|string)} colorTypes The arguments can be strings or objects. If the argument is an object,
* it should contain the color attribute name as key and the color context as value.
* If the argument is a string the value should be the color attribute name,
* the color context is computed by applying a kebab case transform to the value.
* Color context represents the context/place where the color is going to be used.
* The class name of the color is generated using 'has' followed by the color name
* and ending with the color context all in kebab case e.g: has-green-background-color.
*
* @return {Function} Higher-order component.
*/
export default function withColors( ...colorTypes ) {
const withColorPalette = withEditorColorPalette();
return createHigherOrderComponent(
createColorHOC( colorTypes, withColorPalette ),
'withColors'
);
}