-
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
7f9c713
commit 4965d17
Showing
9 changed files
with
301 additions
and
41 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,48 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find, kebabCase } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
const ALLOWED_COLOR_CONTEXTS = [ | ||
'background', | ||
'text', | ||
]; | ||
|
||
export const getColorFromAttribute = ( colors ) => ( colorAttribute ) => { | ||
if ( ! colorAttribute || colorAttribute[ 0 ] !== '%' ) { | ||
return colorAttribute; | ||
} | ||
const colorObj = find( colors, { name: colorAttribute.slice( 1 ) } ); | ||
if ( ! colorObj ) { | ||
return null; | ||
} | ||
return colorObj.color; | ||
}; | ||
export const getAttributeFromColor = ( colors ) => ( color ) => { | ||
const colorObj = find( colors, { color } ); | ||
if ( colorObj ) { | ||
return '%' + colorObj.name; | ||
} | ||
return color; | ||
}; | ||
|
||
export function getClassFromAttribute( colorContext, colorAttribute ) { | ||
if ( ! colorContext || find( ALLOWED_COLOR_CONTEXTS, colorContext ) ) { | ||
if ( window ) { | ||
window.console.error( 'An invalid color context was passed.' ); | ||
} | ||
return null; | ||
} | ||
|
||
if ( ! colorAttribute || colorAttribute[ 0 ] !== '%' ) { | ||
return null; | ||
} | ||
|
||
return `has-${ kebabCase( colorAttribute.slice( 1 ) ) }-${ colorContext }-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,109 @@ | ||
p.has-pale-pink-background-color, | ||
.wp-block-button.has-pale-pink-background-color .wp-block-button__link { | ||
background-color: #f78da7; | ||
} | ||
|
||
p.has-vivid-red-background-color, | ||
.wp-block-button.has-vivid-red-background-color .wp-block-button__link { | ||
background-color: #cf2e2e; | ||
} | ||
|
||
p.has-luminous-vivid-orange-background-color, | ||
.wp-block-button.has-luminous-vivid-orange-background-color .wp-block-button__link { | ||
background-color: #ff6900; | ||
} | ||
|
||
p.has-luminous-vivid-amber-background-color, | ||
.wp-block-button.has-luminous-vivid-amber-background-color .wp-block-button__link { | ||
background-color: #fcb900; | ||
} | ||
|
||
p.has-light-green-cyan-background-color, | ||
.wp-block-button.has-light-green-cyan-background-color .wp-block-button__link { | ||
background-color: #7bdcb5; | ||
} | ||
|
||
p.has-vivid-green-cyan-background-color, | ||
.wp-block-button.has-vivid-green-cyan-background-color .wp-block-button__link { | ||
background-color: #00d084; | ||
} | ||
|
||
p.has-pale-cyan-blue-background-color, | ||
.wp-block-button.has-pale-cyan-blue-background-color .wp-block-button__link { | ||
background-color: #8ed1fc; | ||
} | ||
|
||
p.has-vivid-cyan-blue-background-color, | ||
.wp-block-button.has-vivid-cyan-blue-background-color .wp-block-button__link { | ||
background-color: #0693e3; | ||
} | ||
|
||
p.has-very-light-gray-background-color, | ||
.wp-block-button.has-very-light-gray-background-color .wp-block-button__link { | ||
background-color: #eeeeee; | ||
} | ||
|
||
p.has-cyan-bluish-gray-background-color, | ||
.wp-block-button.has-cyan-bluish-gray-background-color .wp-block-button__link { | ||
background-color: #abb8c3; | ||
} | ||
|
||
p.has-very-dark-gray-background-color, | ||
.wp-block-button.has-very-dark-gray-background-color .wp-block-button__link { | ||
background-color: #313131; | ||
} | ||
|
||
p.has-pale-pink-text-color, | ||
.wp-block-button.has-pale-pink-text-color .wp-block-button__link { | ||
color: #f78da7; | ||
} | ||
|
||
p.has-vivid-red-text-color, | ||
.wp-block-button.has-vivid-red-text-color .wp-block-button__link { | ||
color: #cf2e2e; | ||
} | ||
|
||
p.has-luminous-vivid-orange-text-color, | ||
.wp-block-button.has-luminous-vivid-orange-text-color .wp-block-button__link { | ||
color: #ff6900; | ||
} | ||
|
||
p.has-luminous-vivid-amber-text-color, | ||
.wp-block-button.has-luminous-vivid-amber-text-color .wp-block-button__link { | ||
color: #fcb900; | ||
} | ||
|
||
p.has-light-green-cyan-text-color, | ||
.wp-block-button.has-light-green-cyan-text-color .wp-block-button__link { | ||
color: #7bdcb5; | ||
} | ||
|
||
p.has-vivid-green-cyan-text-color, | ||
.wp-block-button.has-vivid-green-cyan-text-color .wp-block-button__link { | ||
color: #00d084; | ||
} | ||
|
||
p.has-pale-cyan-blue-text-color, | ||
.wp-block-button.has-pale-cyan-blue-text-color .wp-block-button__link { | ||
color: #8ed1fc; | ||
} | ||
|
||
p.has-vivid-cyan-blue-text-color, | ||
.wp-block-button.has-vivid-cyan-blue-text-color .wp-block-button__link { | ||
color: #0693e3; | ||
} | ||
|
||
p.has-very-light-gray-text-color, | ||
.wp-block-button.has-very-light-gray-text-color .wp-block-button__link { | ||
color: #eeeeee; | ||
} | ||
|
||
p.has-cyan-bluish-gray-text-color, | ||
.wp-block-button.has-cyan-bluish-gray-text-color .wp-block-button__link { | ||
color: #abb8c3; | ||
} | ||
|
||
p.has-very-dark-gray-text-color, | ||
.wp-block-button.has-very-dark-gray-text-color .wp-block-button__link { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getWrapperDisplayName } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { withContext } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getAttributeFromColor, getColorFromAttribute } from '../color-mechanism'; | ||
|
||
/** | ||
* Override the default edit UI to include a new block inspector control for | ||
* assigning the custom class name, if block supports custom class name. | ||
* | ||
* @param {function|Component} BlockEdit Original component. | ||
* | ||
* @return {string} Wrapped component. | ||
*/ | ||
export function withColorMechanism( BlockEdit ) { | ||
const BlockEditWithColorContext = withContext( 'editor' )( | ||
( settings, props ) => ( settings ? { | ||
getAttributeFromColor: getAttributeFromColor( settings.colors ), | ||
getColorFromAttribute: getColorFromAttribute( settings.colors ), | ||
setColorAttributeByColor: ( attribute ) => ( color ) => { | ||
const attributeValue = getAttributeFromColor( settings.colors )( color ); | ||
props.setAttributes( { [ attribute ]: attributeValue || color } ); | ||
}, | ||
} : {} ) | ||
)( BlockEdit ); | ||
|
||
const WrappedBlockEdit = ( props ) => { | ||
return <BlockEditWithColorContext { ...props } />; | ||
}; | ||
WrappedBlockEdit.displayName = getWrapperDisplayName( BlockEdit, 'colorMechanism' ); | ||
|
||
return WrappedBlockEdit; | ||
} | ||
|
||
addFilter( 'blocks.BlockEdit', 'core/color-mechanism', withColorMechanism ); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.editor-block-list__block:not( .is-multi-selected ) .wp-block-paragraph { | ||
.editor-block-list__block:not( .is-multi-selected ) .wp-block-paragraph:not( .has-background ) { | ||
background: white; | ||
} |
Oops, something went wrong.