-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ffd9d53
commit 4493aee
Showing
10 changed files
with
259 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
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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find, kebabCase } from 'lodash'; | ||
|
||
export const getColorValue = ( colors, namedColor, customColor ) => { | ||
if ( namedColor ) { | ||
const colorObj = find( colors, { name: namedColor } ); | ||
return colorObj && colorObj.color; | ||
} | ||
if ( customColor ) { | ||
return customColor; | ||
} | ||
}; | ||
|
||
export const setColorValue = ( colors, colorAttributeName, customColorAttributeName, setAttributes ) => | ||
( colorValue ) => { | ||
const colorObj = find( colors, { color: colorValue } ); | ||
setAttributes( { | ||
[ colorAttributeName ]: colorObj ? colorObj.name : undefined, | ||
[ customColorAttributeName ]: colorObj ? 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 }-color`; | ||
} |
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,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getWrapperDisplayName } from '@wordpress/element'; | ||
import { withContext } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getColorValue, getColorClass, setColorValue } from './utils'; | ||
import './style.scss'; | ||
|
||
/** | ||
* 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 function withColors( WrappedComponent ) { | ||
const ComponentWithColorContext = withContext( 'editor' )( | ||
( settings, props ) => { | ||
const colors = get( settings, [ 'colors' ], [] ); | ||
return { | ||
initializeColor: ( 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 EnhancedComponent = ( props ) => { | ||
return <ComponentWithColorContext { ...props } />; | ||
}; | ||
EnhancedComponent.displayName = getWrapperDisplayName( ComponentWithColorContext, 'colorMechanism' ); | ||
|
||
return EnhancedComponent; | ||
} |
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.