I am trying to extend a gutenberg block. I have a color picker for my tab content background. At the moment, i can pick and change the background color on the backend, and it saves (refresh pa and keeps color).
What i am struggling to do, is to get this tag to show up in the frontend. Currently, this only works in the backend. What am i missing?
import { BlockEdit } from "@wordpress/block-editor";
const { __ } = wp.i18n;
const { createHigherOrderComponent } = wp.compose;
const { InspectorControls, PanelColorSettings } = wp.blockEditor || wp.editor;
const { PanelBody } = wp.components;
const { Fragment } = wp.element;
const enableBackgroundColorOnContent = [
'ub/tabbed-content-block'
];
const addTabContentAttributes = (settings, name) => {
if (!enableBackgroundColorOnContent.includes(name)) {
return settings;
} else {
settings.attributes = Object.assign(settings.attributes, {
contentBackgroundColor: {
type: 'string',
default: '#ffffff' // Set default background color to white
}
});
}
return settings;
};
wp.hooks.addFilter(
'blocks.registerBlockType',
'bt/tab-content-custom-attribute',
addTabContentAttributes
);
const addContentBackgroundColorControl = createHigherOrderComponent((BlockEdit) => {
return (props) => {
const { attributes, setAttributes } = props;
if (!enableBackgroundColorOnContent.includes(props.name)) {
return (
<BlockEdit {...props} />
);
}
return (
<Fragment>
<BlockEdit {...props} />
<InspectorControls>
<PanelBody title={__('Content Background Color')}>
<PanelColorSettings
title={__('Content Background Color')}
colorSettings={[
{
value: attributes.contentBackgroundColor,
onChange: (contentBackgroundColor) => setAttributes({ contentBackgroundColor }),
label: __('Background color')
},
]}
/>
</PanelBody>
</InspectorControls>
<style> {`.wp-block-ub-tabbed-content-tabs-content {background-color: ${attributes.contentBackgroundColor};}`} </style>
</Fragment>
);
};
}, 'addContentBackgroundColorControl');
wp.hooks.addFilter(
'editor.BlockEdit',
'bt/custom-tabbed-content-bg-color',
addContentBackgroundColorControl
);```
settings
part of the blocks definition to enable backgrounds, core itself will provide a background colour control for you and APIs intheme.json
to override it as well as the palette of colours that are available, gradients, control over wether arbitrary colours can be picked, etc etc. Nevermind that this can be done with composition/building blocks using a group block with a background colour and zero block gap/spacing