{"id":5327,"date":"2018-07-08T18:57:32","date_gmt":"2018-07-08T16:57:32","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=5327"},"modified":"2020-02-09T11:21:29","modified_gmt":"2020-02-09T10:21:29","slug":"different-color-palettes-gutenberg","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/different-color-palettes-gutenberg-5327\/","title":{"rendered":"Create different color palettes with Gutenberg"},"content":{"rendered":"<p>In this post I show how to create different color palettes in Gutenberg, to \u2013 for example \u2013 allow other colors for the text than for the background.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"update-box\">\n<p><strong>Update from November 18, 2018:<\/strong> One of the latest Gutenberg updates removed the <code class=\"lang-js\">PanelColor<\/code> component, so I updated the code with the instead-to-use <code class=\"lang-js\">PanelColorSettings<\/code> component. That makes it easier, because it directly allows us to specify custom colors.<\/p>\n<p><strong>Update from July 11, 2018:<\/strong> Both color attributes need to be set as <code class=\"lang-js\">attributes<\/code> in the last code block. Otherwise, the selected colors are not recognized from the color pickers after reloading the browser.<\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Goal<\/h2>\n\n\n\n<p>The goal was to recreate the color pickers that are available in the paragraph block:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><noscript><img decoding=\"async\" width=\"280\" height=\"367\" src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker.png\" alt=\"A screenshot of the color palettes present in the sidebar when a paragraph block is selected in Gutenberg. Both palettes \u2013 for text and background color \u2013 have the same colors.\" class=\"wp-image-5329\" srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker.png 280w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker-153x200.png 153w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker-229x300.png 229w\" sizes=\"(max-width: 280px) 100vw, 280px\"><\/noscript><img decoding=\"async\" width=\"280\" height=\"367\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20280%20367%22%3E%3C%2Fsvg%3E\" alt=\"A screenshot of the color palettes present in the sidebar when a paragraph block is selected in Gutenberg. Both palettes \u2013 for text and background color \u2013 have the same colors.\" class=\"wp-image-5329 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20280%20367%22%3E%3C%2Fsvg%3E 280w\" sizes=\"(max-width: 280px) 100vw, 280px\" data-srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker.png 280w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker-153x200.png 153w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker-229x300.png 229w\" data-src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/paragraph-color-picker.png\"><\/figure>\n\n\n\n<p>The colors are inside an area that can be toggled, and the selected color is shown next to the title of the area. When a color is selected, the paragraph element gets a class and an inline style.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>First, my solution was to use the <code class=\"lang-js\">PanelColor<\/code> component, which does not allow custom colors easily, so that was a little trickier than the solution via <code class=\"lang-js\">PanelColorSettings<\/code>. That component allows us to set a custom color set \u2013 but you need to define all used colors via <code class=\"lang-php\">add_theme_support( 'editor-color-palette' )<\/code>, because that is what Gutenberg uses for generating the color class names.<\/p>\n\n\n\n<p>With that, the solution is made of two parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Tell Gutenberg about the wanted colors with <code class=\"lang-php\">add_theme_support( 'editor-color-palette' )<\/code>.<\/li><li>Recreate the color part from the <a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/master\/packages\/block-library\/src\/paragraph\/edit.js\">paragraph block<\/a>.<\/li><\/ul>\n\n\n\n<p>I assume that you have installed Gutenberg and included a JavaScript file in the editor and are already using a <code class=\"lang-markup\">package.json<\/code>. I am using modern JavaScript syntax, which is converted to ES5 with Babel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Define the color palette<\/h3>\n\n\n\n<p>At first, we need to define our colors, which we want to use later:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">add_action( <span class=\"hljs-string\">'after_setup_theme'<\/span>, <span class=\"hljs-string\">'setup_theme_supported_features'<\/span> );\n\n<span class=\"hljs-comment\">\/**\n * Remove unused Gutenberg features.\n *\/<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">setup_theme_supported_features<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\tadd_theme_support( <span class=\"hljs-string\">'editor-color-palette'<\/span>, \n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'Puerto Rico'<\/span>,\n\t\t\t<span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'puerto-rico'<\/span>,\n\t\t\t<span class=\"hljs-string\">'color'<\/span> =&gt; <span class=\"hljs-string\">'#53c4ab'<\/span>,\n\t\t],\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'Tundora'<\/span>,\n\t\t\t<span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'tundora'<\/span>,\n\t\t\t<span class=\"hljs-string\">'color'<\/span> =&gt; <span class=\"hljs-string\">'#454545'<\/span>,\n\t\t],\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'Butterfly Bush'<\/span>,\n\t\t\t<span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'butterfly-bush'<\/span>,\n\t\t\t<span class=\"hljs-string\">'color'<\/span> =&gt; <span class=\"hljs-string\">'#5151a0'<\/span>,\n\t\t],\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'name'<\/span> =&gt; <span class=\"hljs-string\">'White'<\/span>,\n\t\t\t<span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'white'<\/span>,\n\t\t\t<span class=\"hljs-string\">'color'<\/span> =&gt; <span class=\"hljs-string\">'#ffffff'<\/span>,\n\t\t],\n\t);\n\n\tadd_theme_support( <span class=\"hljs-string\">'disable-custom-colors'<\/span> );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">The JavaScript part<\/h3>\n\n\n\n<p>Besides Gutenberg components, we need the <code class=\"lang-bash\">classnames<\/code> library. To install it, we run the following command in our command line:<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">npm install classnames<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now let us begin with the external things, that we need to import into the JavaScript file:<\/p>\n\n\n<pre class=\"wp-block-code lang-js\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> classnames <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'classnames'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> {\n\tregisterBlockType,\n} = wp.blocks;\n<span class=\"hljs-keyword\">const<\/span> {\n\tInspectorControls,\n\tInnerBlocks,\n\twithColors,\n\tgetColorClass\n} = wp.editor;\n<span class=\"hljs-keyword\">const<\/span> {\n\tPanelBody,\n\twithFallbackStyles,\n\tPanelColor,\n\tColorPalette,\n} = wp.components;\n\n<span class=\"hljs-keyword\">const<\/span> {\n\tcompose,\n\tComponent,\n} = wp.element;\n\n<span class=\"hljs-keyword\">const<\/span> {getComputedStyle} = <span class=\"hljs-built_in\">window<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> FallbackStyles = withFallbackStyles(<span class=\"hljs-function\">(<span class=\"hljs-params\">node, ownProps<\/span>) =&gt;<\/span> {\n\t<span class=\"hljs-keyword\">const<\/span> {textColor, backgroundColor} = ownProps.attributes;\n\t<span class=\"hljs-keyword\">const<\/span> editableNode = node.querySelector(<span class=\"hljs-string\">'&#91;contenteditable=\"true\"]'<\/span>);\n\t<span class=\"hljs-comment\">\/\/verify if editableNode is available, before using getComputedStyle.<\/span>\n\t<span class=\"hljs-keyword\">const<\/span> computedStyles = editableNode ? getComputedStyle(editableNode) : <span class=\"hljs-literal\">null<\/span>;\n\t<span class=\"hljs-keyword\">return<\/span> {\n\t\t<span class=\"hljs-attr\">fallbackBackgroundColor<\/span>: backgroundColor || !computedStyles ? <span class=\"hljs-literal\">undefined<\/span> : computedStyles.backgroundColor,\n\t\t<span class=\"hljs-attr\">fallbackTextColor<\/span>: textColor || !computedStyles ? <span class=\"hljs-literal\">undefined<\/span> : computedStyles.color,\n\t};\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Like already written, the solution is composed of existing Gutenberg components \u2013 that said, those definitions and the <code class=\"lang-js\">withFallbackStyles()<\/code> function in the <a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/master\/core-blocks\/paragraph\/index.js\">paragraph block<\/a> and the <a href=\"https:\/\/github.com\/WordPress\/gutenberg\/blob\/master\/editor\/components\/panel-color\/index.js\"><code class=\"lang-js\">PanelColor<\/code> component<\/a>.<\/p>\n\n\n\n<p>Like it is made in the paragraph block, we will define the edit part of our block as a subclass of <code class=\"lang-js\">Component<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code lang-js\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">OneColumnBlock<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Component<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">constructor<\/span>() {\n\t\t<span class=\"hljs-keyword\">super<\/span>(...arguments);\n\t}\n\n\trender() {\n\t\t<span class=\"hljs-keyword\">const<\/span> {\n\t\t\tattributes,\n\t\t\tsetAttributes,\n\t\t\tmergeBlocks,\n\t\t\tonReplace,\n\t\t\tclassName,\n\t\t\tbackgroundColor,\n\t\t\ttextColor,\n\t\t\tsetBackgroundColor,\n\t\t\tsetTextColor,\n\t\t\tfallbackBackgroundColor,\n\t\t\tfallbackTextColor,\n\t\t\tfallbackFontSize,\n\t\t} = <span class=\"hljs-keyword\">this<\/span>.props;\n\n\t\t<span class=\"hljs-keyword\">const<\/span> textColors = &#91;\n\t\t\t{\n\t\t\t\t<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Tundora'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">slug<\/span>: <span class=\"hljs-string\">'tundora'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">'#454545'<\/span>\n\t\t\t},\n\t\t];\n\t\t<span class=\"hljs-keyword\">const<\/span> backgroundColors = &#91;\n\t\t\t{\n\t\t\t\t<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Puerto Rico'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">slug<\/span>: <span class=\"hljs-string\">'puerto-rico'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">'#53c4ab'<\/span>\n\t\t\t},\n\t\t\t{\n\t\t\t\t<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'Butterfly Bush'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">slug<\/span>: <span class=\"hljs-string\">'butterfly-bush'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">'#5151a0'<\/span>\n\t\t\t},\n\t\t\t{\n\t\t\t\t<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">'White'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">slug<\/span>: <span class=\"hljs-string\">'white'<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">'#ffffff'<\/span>\n\t\t\t}\n\t\t];\n\n\t\t<span class=\"hljs-keyword\">return<\/span> (\n\t\t\t<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>\n\t\t\t\t<span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">{classnames(className,<\/span> {\n\t\t\t\t\t'<span class=\"hljs-attr\">has-background<\/span>'<span class=\"hljs-attr\">:<\/span> <span class=\"hljs-attr\">backgroundColor.value<\/span>,\n\t\t\t\t\t&#91;<span class=\"hljs-attr\">backgroundColor.class<\/span>]<span class=\"hljs-attr\">:<\/span> <span class=\"hljs-attr\">backgroundColor.class<\/span>,\n\t\t\t\t\t&#91;<span class=\"hljs-attr\">textColor.class<\/span>]<span class=\"hljs-attr\">:<\/span> <span class=\"hljs-attr\">textColor.class<\/span>,\n\t\t\t\t})}\n\t\t\t\t\n\t\t\t\t<span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">{{<\/span>\n\t\t\t\t\t<span class=\"hljs-attr\">backgroundColor:<\/span> <span class=\"hljs-attr\">backgroundColor.value<\/span>,\n\t\t\t\t\t<span class=\"hljs-attr\">color:<\/span> <span class=\"hljs-attr\">textColor.value<\/span>,\n\t\t\t\t}}\n\t\t\t&gt;<\/span>\n\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">InnerBlocks<\/span>\/&gt;<\/span>\n\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">InspectorControls<\/span>&gt;<\/span>\n\t\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">PanelColorSettings<\/span>\n\t\t\t\t\t\t<span class=\"hljs-attr\">title<\/span>=<span class=\"hljs-string\">\"'Farbschema\"<\/span>\n\t\t\t\t\t\t<span class=\"hljs-attr\">colorSettings<\/span>=<span class=\"hljs-string\">{&#91;<\/span>\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">colors:<\/span> <span class=\"hljs-attr\">textColors<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">label:<\/span> '<span class=\"hljs-attr\">Textfarbe<\/span>',\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">onChange:<\/span> <span class=\"hljs-attr\">setTextColor<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">value:<\/span> <span class=\"hljs-attr\">textColor.color<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">disableCustomColors:<\/span> <span class=\"hljs-attr\">true<\/span>,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">colors:<\/span> <span class=\"hljs-attr\">backgroundColors<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">label:<\/span> '<span class=\"hljs-attr\">Hintergrundfarbe<\/span>',\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">onChange:<\/span> <span class=\"hljs-attr\">setBackgroundColor<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">value:<\/span> <span class=\"hljs-attr\">backgroundColor.color<\/span>,\n\t\t\t\t\t\t\t\t<span class=\"hljs-attr\">disableCustomColors:<\/span> <span class=\"hljs-attr\">true<\/span>,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t]}\n\t\t\t\t\t\/&gt;<\/span>\n\t\t\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">InspectorControls<\/span>&gt;<\/span>\n\t\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n\t\t);\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Interesting is the <code class=\"lang-js\">render<\/code> part. The constants at the beginning are from the paragraph block \u2013 <code class=\"lang-js\">textColors<\/code> and <code class=\"lang-js\">backgroundColors<\/code> are our subsets of the colors defined in <code class=\"lang-php\">add_theme_support( 'editor-color-palette' )<\/code>.<\/p>\n\n\n\n<p>In the <code class=\"lang-js\">return<\/code> part, we open a <code class=\"lang-js\">div<\/code>, in which the class names and the inline styles are outputted. This is also taken from the paragraph component. <code class=\"lang-js\">InnerBlocks<\/code> is a component, that enables it to insert other blocks into a block, and after that inside <code class=\"lang-js\">InspectorControls<\/code> we create our color pickers with the <code class=\"lang-js\">PanelColorSettings<\/code> component.<\/p>\n\n\n\n<p>First, we define the title of the panel and pass an array of objects via <code class=\"lang-js\">colorSettings<\/code>, where each object stands for one color picker. <code class=\"lang-js\">colors<\/code> allows us to set a custom set of colors (the default colors would be the ones from the&nbsp; <code class=\"lang-php\">add_theme_support( 'editor-color-palette' )<\/code> part) \u2013 the first one gets our text color, and the second one our background colors.<\/p>\n\n\n\n<p><code class=\"lang-js\">label<\/code> is the \u2013 well \u2013 label of the individual color picker, and <code class=\"lang-js\">onChange<\/code> gets the functions that save the colors (taken from the paragraph block). The value is the hex value and we want to disable that the user can select a custom color.<\/p>\n\n\n\n<p>One last piece of code remains \u2013 also inspired by the paragraph component:<\/p>\n\n\n<pre class=\"wp-block-code lang-js\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> registerBlockType(<span class=\"hljs-string\">'slug\/one-column'<\/span>, {\n\t<span class=\"hljs-attr\">title<\/span>: <span class=\"hljs-string\">'Eine Spalte'<\/span>,\n\t<span class=\"hljs-attr\">icon<\/span>: <span class=\"hljs-string\">'admin-post'<\/span>,\n\t<span class=\"hljs-attr\">category<\/span>: <span class=\"hljs-string\">'layout'<\/span>,\n\t<span class=\"hljs-attr\">edit<\/span>: compose(&#91;\n\t\twithColors(<span class=\"hljs-string\">'backgroundColor'<\/span>, {<span class=\"hljs-attr\">textColor<\/span>: <span class=\"hljs-string\">'color'<\/span>}),\n\t\tFallbackStyles,\n\t])(OneColumnBlock),\n\t<span class=\"hljs-attr\">save<\/span>: <span class=\"hljs-function\"><span class=\"hljs-params\">props<\/span> =&gt;<\/span> {\n\t\t<span class=\"hljs-keyword\">const<\/span> {\n\t\t\tbackgroundColor,\n\t\t\ttextColor,\n\t\t\tcustomBackgroundColor,\n\t\t\tcustomTextColor,\n\t\t} = props.attributes;\n\n\t\t<span class=\"hljs-keyword\">const<\/span> textClass = getColorClass( <span class=\"hljs-string\">'color'<\/span>, textColor );\n\t\t<span class=\"hljs-keyword\">const<\/span> backgroundClass = getColorClass( <span class=\"hljs-string\">'background-color'<\/span>, backgroundColor );\n\t\t\n\t\t<span class=\"hljs-keyword\">const<\/span> className = classnames( {\n\t\t\t<span class=\"hljs-string\">'has-background'<\/span>: backgroundColor || customBackgroundColor,\n\t\t\t&#91; textClass ]: textClass,\n\t\t\t&#91; backgroundClass ]: backgroundClass,\n\t\t} );\n\t\t<span class=\"hljs-keyword\">return<\/span> (\n\t\t\t<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">{className}<\/span>&gt;<\/span>\n\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">InnerBlocks.Content<\/span>\/&gt;<\/span>\n\t\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n\t\t);\n\t},\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here we register our block <code class=\"lang-js\">slug\/one-column<\/code> and set a few meta data. <code class=\"lang-js\">edit<\/code> is our <code class=\"lang-js\">OneColumnBlock<\/code> class and in <code class=\"lang-js\">save<\/code> we build the color classes and set them for a <code class=\"lang-js\">div<\/code> container, that wraps the content of the <code class=\"lang-js\">InnerBlocks<\/code>.<\/p>\n\n\n\n<p>And that is the result (I have a few more colors in the project that used in the example):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><noscript><img decoding=\"async\" width=\"274\" height=\"305\" src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes.png\" alt=\"Screenshot of our result. Two color picker controls with different color palettes.\" class=\"wp-image-5328\" srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes.png 274w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes-180x200.png 180w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes-270x300.png 270w\" sizes=\"(max-width: 274px) 100vw, 274px\"><\/noscript><img decoding=\"async\" width=\"274\" height=\"305\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20274%20305%22%3E%3C%2Fsvg%3E\" alt=\"Screenshot of our result. Two color picker controls with different color palettes.\" class=\"wp-image-5328 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20274%20305%22%3E%3C%2Fsvg%3E 274w\" sizes=\"(max-width: 274px) 100vw, 274px\" data-srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes.png 274w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes-180x200.png 180w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes-270x300.png 270w\" data-src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2018\/07\/different-color-palettes.png\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/florianbrinkmann\/e070d731ad11177e34969cd6fbb1a83e\">The complete code can be found as a Gist on GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I show how to create different color palettes in Gutenberg, to \u2013 for example \u2013 allow other colors for the text than for the background.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-5327","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":"f862dffb8b3742109b54e2743fc2afed","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/f862dffb8b3742109b54e2743fc2afed"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=5327"}],"version-history":[{"count":9,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5327\/revisions"}],"predecessor-version":[{"id":5956,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5327\/revisions\/5956"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=5327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=5327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=5327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}