JavaScript for WordPress › Forums › Gutenberg Development › How to get a color class name
- This topic has 11 replies, 5 voices, and was last updated 5 years, 7 months ago by alchemyunited.
-
AuthorPosts
-
May 25, 2018 at 2:34 pm #47916ericvaloisParticipant
Hi, Gutenberg 2.8 introduced a new way to handle colors. In order to reduce inline styles. Which is a good thing!
However, I’m having a hard time figure out how to get the color name instead of the color value.
// give has-ffffff-background-color instead of has-white-background-color const backgroundClass = getColorClass( 'background-color', blockBackgroundColor );
I took a look at the paragraph block source code for inspiration but it was too complicated for me.
Any ideas?
May 25, 2018 at 3:01 pm #47927Zac GordonKeymasterFew things:
1. Make sure you’ve added custom colors to your theme or plugin
2. Make sure to import getColorClass from editor
3. Setup your attributes all the same way as before (I believe)
4. Save getColorClass( ‘color’, colorName ) to a variable
5. Wherever you want to use that custom CSS name, call that variableHope this helps!
I don’t have any examples of this at the moment, but I will make a note for the next course update 🙂
May 25, 2018 at 3:23 pm #47935ericvaloisParticipantThanks for the fast answer, but it doesn’t work.
My block is already set like you explained. My question is how to get the color name? Because the color attribute returns the color value, not the color’s name.
https://github.com/time-to-first-byte/ttfb-blocks/blob/dev/blocks/section/index.js#L120May 25, 2018 at 4:17 pm #47952Zac GordonKeymasterHmmm, gotcha.
I haven’t played with this enough, but maybe some of there other functions can help?
Wonder if it’s setting in a color picker to return name if available instead of hex but you may still be able to get yourself.
July 10, 2018 at 12:22 pm #51740menglandParticipantIronically I’m working on the same thing and having some struggles getting the color classes to work. Were you able to get a working example?
July 10, 2018 at 1:11 pm #51745ericvaloisParticipantNo sorry, I have never been able to make it work.
July 10, 2018 at 2:13 pm #51754Zac GordonKeymaster@mengland this is probably a good question for the core editor Slack channel 👍
July 10, 2018 at 2:59 pm #51758menglandParticipantI hopped on there and presented the question. I’ll post back if they share anything helpful. However, in the meantime I believe I stumbled upon something that provides a solution and I hope it helps someone else.
Florian created a remake of the paragraph block which has a working Color Palette with color class names.
Create different color palettes with GutenbergJuly 10, 2018 at 3:13 pm #51761ericvaloisParticipantThanks for the link!
July 10, 2018 at 6:40 pm #51769menglandParticipantI have one final follow up and I wanted to share this because it was a lot of work to get here. I have a working example of a block that pulls in custom colors that have been defined in theme support. The original link I shared showed how to utilize your own custom color sets defined at the block level.
/** * Based on a block by Florian Brinkmann */ import classnames from 'classnames'; const { registerBlockType, } = wp.blocks; const { InspectorControls, InnerBlocks, PanelColor, withColors, getColorClass } = wp.editor; const { PanelBody, withFallbackStyles, ColorPalette, } = wp.components; const { compose, Component, } = wp.element; const {getComputedStyle} = window; const FallbackStyles = withFallbackStyles((node, ownProps) => { const {textColor, backgroundColor} = ownProps.attributes; const editableNode = node.querySelector('[contenteditable="true"]'); //verify if editableNode is available, before using getComputedStyle. const computedStyles = editableNode ? getComputedStyle(editableNode) : null; return { fallbackBackgroundColor: backgroundColor || !computedStyles ? undefined : computedStyles.backgroundColor, fallbackTextColor: textColor || !computedStyles ? undefined : computedStyles.color, }; }); class OneColumnBlock extends Component { constructor() { super(...arguments); } render() { const { attributes, setAttributes, mergeBlocks, onReplace, className, backgroundColor, textColor, setBackgroundColor, setTextColor, fallbackBackgroundColor, fallbackTextColor, fallbackFontSize, } = this.props; const { align, content, dropCap, placeholder, } = attributes; return ( <div className={classnames(className, { 'has-background': backgroundColor.value, [backgroundColor.class]: backgroundColor.class, [textColor.class]: textColor.class, })} style={{ backgroundColor: backgroundColor.value, color: textColor.value, }} > <InnerBlocks layouts={ [ { name: 'inner-content', label: 'Story Container', icon: 'columns' }, ] } template={ [ [ 'core/heading', { layout:'inner-content', placeholder:'Example Title...' } ], [ 'core/paragraph', { layout:'inner-content', placeholder:'Example Heading...' } ], ] } /> <InspectorControls> <PanelColor {...{ title: 'Background Color', colorName: backgroundColor.name, colorValue: backgroundColor.value, initialOpen: false, onChange: setBackgroundColor } } /> <PanelColor {...{ title: 'Text Color', colorName: textColor.name, colorValue: textColor.value, initialOpen: false, onChange: setTextColor } } /> </InspectorControls> </div> ); } } export default registerBlockType('slug/one-column', { title: 'ColorPanel Example Column', icon: 'admin-appearance', category: 'common', edit: compose( [ withColors('backgroundColor', {textColor: 'color'}), FallbackStyles, ] )(OneColumnBlock), save: props => { const { backgroundColor, textColor, customBackgroundColor, customTextColor, } = props.attributes; const textClass = getColorClass( 'color', textColor ); const backgroundClass = getColorClass( 'background-color', backgroundColor ); const className = classnames( { 'has-background': backgroundColor || customBackgroundColor, [ textClass ]: textClass, [ backgroundClass ]: backgroundClass, } ); return ( <div className={className}> <InnerBlocks.Content/> </div> ); }, });
January 19, 2019 at 11:25 pm #84683Tomas MulderParticipantIf you make
select
available to dig into WordPress’s data and a few color helper functionsconst { select } = wp.data; const { getColorClassName, getColorObjectByColorValue, } = wp.editor;
then you can grab the class name or style (in case they select a custom color)
if( color ) { const settings = select( 'core/editor' ).getEditorSettings(); const colorObject = getColorObjectByColorValue( settings.colors, color ); if( colorObject ) { console.log( 'add a class of', getColorClassName( 'background-color', colorObject.slug ) ); } else { console.log( 'add a style of', { backgroundColor: color } ); } }
pretty easily.
May 5, 2019 at 8:15 pm #106793alchemyunitedParticipantDid anyone ever find a final / current answer on this? My JS is basic so this might not be the best way to do this, but it’s working now (which is better than inlining the style colors.
function colorObject(colors, color, prop){ var colorObj = getColorObjectByColorValue(colors, color) if (typeof colorObj === 'undefined'){ return color.slice(1); } switch(prop) { case 'color': if ( colorObj.color ){ return colorObj.color } return ''; case 'name': if ( colorObj.name ){ return colorObj.name } return ''; case 'slug': if ( colorObj.slug ){ return colorObj.slug } return ''; default: return '' } }
Then for classnames I have something like this:
const classes = classnames( className, { [ <code>block-color-${colorObject(myObj.blockColors, blockColor, "slug") }</code>]: blockColor, });
Where myObj is an object of settings / values that I pass in via the WP PHP function localize script. This is allowing me to set the colors at the color pallet level, instead of a global one size fits all list of colors (via theme suppprts()). That same obj + property is also used then I configure the various pallets in the inspector.
Maybe there’s a better way to do this? If so, some one please let me know. TIA
-
AuthorPosts
- You must be logged in to reply to this topic.