-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented mechanism to use classes for configured colors. #5273
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { getColorClass } from './utils'; | ||
export { default as withColors } from './with-colors'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find, kebabCase } from 'lodash'; | ||
|
||
/** | ||
* Returns the color value based on an array of named colors and the namedColor or the customColor value. | ||
* | ||
* @param {Array} colors Array of color objects containing the "name" and "color" value as properties. | ||
* @param {?string} namedColor A string containing the color name. | ||
* @param {?string} customColor A string containing the customColor value. | ||
* | ||
* @return {?string} If namedColor is passed and the name is found in colors it returns the color for that name. | ||
* Otherwise, the customColor parameter is returned. | ||
*/ | ||
export const getColorValue = ( colors, namedColor, customColor ) => { | ||
if ( namedColor ) { | ||
const colorObj = find( colors, { name: namedColor } ); | ||
return colorObj && colorObj.color; | ||
} | ||
if ( customColor ) { | ||
return customColor; | ||
} | ||
}; | ||
|
||
/** | ||
* Returns a function that receives the color value and sets it using the attribute for named colors or for custom colors. | ||
* | ||
* @param {Array} colors Array of color objects containing the "name" and "color" value as properties. | ||
* @param {string} colorAttributeName Name of the attribute where named colors are stored. | ||
* @param {string} customColorAttributeName Name of the attribute where custom colors are stored. | ||
* @param {string} setAttributes A function that receives an object with the attributes to set. | ||
* | ||
* @return {function} A function that receives the color value and sets the attributes necessary to correctly store it. | ||
*/ | ||
export const setColorValue = ( colors, colorAttributeName, customColorAttributeName, setAttributes ) => | ||
( colorValue ) => { | ||
const colorObj = find( colors, { color: colorValue } ); | ||
setAttributes( { | ||
[ colorAttributeName ]: colorObj && colorObj.name ? colorObj.name : undefined, | ||
[ customColorAttributeName ]: colorObj && colorObj.name ? undefined : colorValue, | ||
} ); | ||
}; | ||
|
||
/** | ||
* Returns a class based on the context a color is being used and its name. | ||
* | ||
* @param {string} colorContextName Context/place where color is being used e.g: background, text etc... | ||
* @param {string} colorName Name of the color. | ||
* | ||
* @return {string} String with the class corresponding to the color in the provided context. | ||
*/ | ||
export function getColorClass( colorContextName, colorName ) { | ||
if ( ! colorContextName || ! colorName ) { | ||
return; | ||
} | ||
|
||
return `has-${ kebabCase( colorName ) }-${ colorContextName }`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createHigherOrderComponent } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getColorValue, getColorClass, setColorValue } from './utils'; | ||
import { withEditorSettings } from '../editor-settings'; | ||
|
||
/** | ||
* Higher-order component, which handles color logic for class generation | ||
* color value, retrieval and color attribute setting. | ||
* | ||
* @param {WPElement} WrappedComponent The wrapped component. | ||
* | ||
* @return {Component} Component with a new colors prop. | ||
*/ | ||
export default createHigherOrderComponent( | ||
withEditorSettings( | ||
( settings, props ) => { | ||
const colors = get( settings, [ 'colors' ], [] ); | ||
return { | ||
initializeColor: ( { colorContext, colorAttribute, customColorAttribute } ) => ( { | ||
value: getColorValue( | ||
colors, | ||
props.attributes[ colorAttribute ], | ||
props.attributes[ customColorAttribute ] | ||
), | ||
class: getColorClass( colorContext, props.attributes[ colorAttribute ] ), | ||
set: setColorValue( colors, colorAttribute, customColorAttribute, props.setAttributes ), | ||
} ), | ||
}; | ||
} ), | ||
'withColors' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my console:
Edit: Actually, this is because I had #5570 (review) in place. Still an issue, though not certain it's introduced here. No warning when many color options exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not able to replicate, I think it was not introduced here as the color value used in the key should be unique.