-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented mechanism to use classes for configured colors instead of…
… inline styles.
- Loading branch information
1 parent
f4cc3bf
commit f95623c
Showing
7 changed files
with
257 additions
and
31 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find, get, kebabCase } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getWrapperDisplayName } from '@wordpress/element'; | ||
import { withContext } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
const getColorValue = ( colors, namedColor, customColor ) => { | ||
if ( namedColor ) { | ||
const colorObj = find( colors, { name: namedColor } ); | ||
return colorObj && colorObj.color; | ||
} | ||
if ( customColor ) { | ||
return customColor; | ||
} | ||
}; | ||
|
||
const setColorValue = ( colors, colorAttributeName, customColorAttributeName, setAttributes ) => | ||
( colorValue ) => { | ||
const colorObj = find( colors, { color: colorValue } ); | ||
if ( colorObj ) { | ||
setAttributes( { | ||
[ colorAttributeName ]: colorObj.name, | ||
[ customColorAttributeName ]: undefined, | ||
} ); | ||
return; | ||
} | ||
setAttributes( { | ||
[ colorAttributeName ]: undefined, | ||
[ customColorAttributeName ]: 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 undefined; | ||
} | ||
|
||
return `has-${ kebabCase( colorName ) }-${ colorContextName }-color`; | ||
} | ||
|
||
/** | ||
* 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 function withColors( WrappedComponent ) { | ||
const ComponentWithColorContext = withContext( 'editor' )( | ||
( settings, props ) => { | ||
const colors = get( settings, 'colors', [] ); | ||
return { | ||
colors: ( colorContextName, colorAttributeName, customColorAttributeName ) => ( { | ||
value: getColorValue( | ||
colors, | ||
props.attributes[ colorAttributeName ], | ||
props.attributes[ customColorAttributeName ] | ||
), | ||
class: getColorClass( colorContextName, props.attributes[ colorAttributeName ] ), | ||
set: setColorValue( colors, colorAttributeName, customColorAttributeName, props.setAttributes ), | ||
} ), | ||
}; | ||
} )( WrappedComponent ); | ||
|
||
const NewComponent = ( props ) => { | ||
return <ComponentWithColorContext { ...props } />; | ||
}; | ||
NewComponent.displayName = getWrapperDisplayName( WrappedComponent, 'colorMechanism' ); | ||
|
||
return NewComponent; | ||
} |
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,87 @@ | ||
p.has-pale-pink-background-color { | ||
background-color: #f78da7; | ||
} | ||
|
||
p.has-vivid-red-background-color { | ||
background-color: #cf2e2e; | ||
} | ||
|
||
p.has-luminous-vivid-orange-background-color { | ||
background-color: #ff6900; | ||
} | ||
|
||
p.has-luminous-vivid-amber-background-color { | ||
background-color: #fcb900; | ||
} | ||
|
||
p.has-light-green-cyan-background-color { | ||
background-color: #7bdcb5; | ||
} | ||
|
||
p.has-vivid-green-cyan-background-color { | ||
background-color: #00d084; | ||
} | ||
|
||
p.has-pale-cyan-blue-background-color { | ||
background-color: #8ed1fc; | ||
} | ||
|
||
p.has-vivid-cyan-blue-background-color { | ||
background-color: #0693e3; | ||
} | ||
|
||
p.has-very-light-gray-background-color { | ||
background-color: #eeeeee; | ||
} | ||
|
||
p.has-cyan-bluish-gray-background-color { | ||
background-color: #abb8c3; | ||
} | ||
|
||
p.has-very-dark-gray-background-color { | ||
background-color: #313131; | ||
} | ||
|
||
p.has-pale-pink-text-color { | ||
color: #f78da7; | ||
} | ||
|
||
p.has-vivid-red-text-color { | ||
color: #cf2e2e; | ||
} | ||
|
||
p.has-luminous-vivid-orange-text-color { | ||
color: #ff6900; | ||
} | ||
|
||
p.has-luminous-vivid-amber-text-color { | ||
color: #fcb900; | ||
} | ||
|
||
p.has-light-green-cyan-text-color { | ||
color: #7bdcb5; | ||
} | ||
|
||
p.has-vivid-green-cyan-text-color { | ||
color: #00d084; | ||
} | ||
|
||
p.has-pale-cyan-blue-text-color { | ||
color: #8ed1fc; | ||
} | ||
|
||
p.has-vivid-cyan-blue-text-color { | ||
color: #0693e3; | ||
} | ||
|
||
p.has-very-light-gray-text-color { | ||
color: #eeeeee; | ||
} | ||
|
||
p.has-cyan-bluish-gray-text-color { | ||
color: #abb8c3; | ||
} | ||
|
||
p.has-very-dark-gray-text-color { | ||
color: #313131; | ||
} |
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.