admin管理员组文章数量:1318563
I have a custom block with the ColorPalette
component. In this component, I have an array with several colors to choose from.
The ColorPalette
component only passes a color value. I would like to pass the name as well
This is what my block looks like:
registerBlockType('my/block', {
attributes: {
backgroundColor: {
type: 'string',
default: '#ffffff'
},
},
edit({ attributes, setAttributes, className }) {
const { backgroundColor } = attributes;
return (
<Fragment>
<InspectorControls>
<PanelBody title="Container Settings">
<ColorPalette
colors={[
{ name: 'bg-danger', color: '#dc3545' },
{ name: 'bg-dark', color: '#343a40' },
{ name: 'bg-info', color: '#17a2b8' },
{ name: 'bg-light', color: '#f8f9fa' },
{ name: 'bg-secondary', color: '#6c757d' },
{ name: 'bg-warning', color: '#ffc107' },
]}
value={backgroundColor}
onChange={(value) => setAttributes({ backgroundColor: value })}
/>
</PanelBody>
</InspectorControls>
</Fragment>
);
},
save() {
// I use `render_callback` where I manage the data returned by the block
return (null);
},
});
I have a custom block with the ColorPalette
component. In this component, I have an array with several colors to choose from.
The ColorPalette
component only passes a color value. I would like to pass the name as well
This is what my block looks like:
registerBlockType('my/block', {
attributes: {
backgroundColor: {
type: 'string',
default: '#ffffff'
},
},
edit({ attributes, setAttributes, className }) {
const { backgroundColor } = attributes;
return (
<Fragment>
<InspectorControls>
<PanelBody title="Container Settings">
<ColorPalette
colors={[
{ name: 'bg-danger', color: '#dc3545' },
{ name: 'bg-dark', color: '#343a40' },
{ name: 'bg-info', color: '#17a2b8' },
{ name: 'bg-light', color: '#f8f9fa' },
{ name: 'bg-secondary', color: '#6c757d' },
{ name: 'bg-warning', color: '#ffc107' },
]}
value={backgroundColor}
onChange={(value) => setAttributes({ backgroundColor: value })}
/>
</PanelBody>
</InspectorControls>
</Fragment>
);
},
save() {
// I use `render_callback` where I manage the data returned by the block
return (null);
},
});
Share
Improve this question
asked Oct 19, 2020 at 17:13
kanlukaszkanlukasz
5448 silver badges24 bronze badges
1 Answer
Reset to default 1If you just want to get the selected color name, then you can use the getColorObjectByColorValue()
function in the @wordpress/block-editor
package, which is wp.blockEditor.getColorObjectByColorValue
in the browser. The function accepts two parameters: a color list (each is an object with color
and name
as the properties), and the color (a HEX code, e.g. #17a2b8
) that you'd like to find in that color list. So for example:
Import or load the function:
//import { InspectorControls, getColorObjectByColorValue } from '@wordpress/block-editor'; const { InspectorControls, getColorObjectByColorValue } = wp.blockEditor;
Define the color object list:
const colorList = [ { name: 'bg-danger', color: '#dc3545' }, { name: 'bg-dark', color: '#343a40' }, { name: 'bg-info', color: '#17a2b8' }, { name: 'bg-light', color: '#f8f9fa' }, { name: 'bg-secondary', color: '#6c757d' }, { name: 'bg-warning', color: '#ffc107' }, ]; // ... registerBlockType( 'my/block', ... );
Then in the block
edit()
function, you can usegetColorObjectByColorValue( colorList, backgroundColor )
to get the color data/object which contains the selected color.
But actually, you don't necessarily need to use the getColorObjectByColorValue()
function; you could instead just use the find()
prototype/function in JavaScript Array
... Example:
const color = colorList.find( ( obj ) => ( backgroundColor === obj.color ) );
So what I'm saying is, define the color list and use it when retrieving the selected color.
Bonus: Converting the backgroundColor
to an object attribute with color
and name
as the properties
You don't have to convert it to an object — and you could just add another attribute like backgrounColorName
, but I thought this would be useful to you as well as myself. :) And I also included a sample render_callback
function which simply displays the color code and name.
Define the block attributes:
attributes: { backgroundColor: { type: 'object', properties: { color: { type: 'string', default: '#ffffff', format: 'hex-color', }, name: { type: 'string', default: '', }, }, default: { color: '#ffffff', name: '', }, }, },
Display the color palette with the selected color code (
backgroundColor.color
):<ColorPalette colors={ colorList } value={ backgroundColor.color } onChange={ ( value ) => setAttributes({ backgroundColor: { color: value, name: getColorObjectByColorValue( colorList, value ).name, } })} />
Register the block type in PHP:
register_block_type( 'my/block', [ // The schema should match the one used in your JavaScript. 'attributes' => [ 'backgroundColor' => [ 'type' => 'object', 'properties' => [ 'color' => [ 'type' => 'string', 'default' => '#ffffff', 'format' => 'hex-color', ], 'name' => [ 'type' => 'string', 'default' => '', ], ], 'default' => [ 'color' => '#ffffff', 'name' => '', ], ], ], 'render_callback' => 'my_block_render_callback', ] );
Finally, the
render_callback
:function my_block_render_callback( $attrs, $content ) { $background = $attrs['backgroundColor']; echo 'color: ' . $background['color']; echo '; name: ' . $background['name']; }
I hope that helps, and for further details about the attributes, please check:
https://developer.wordpress/rest-api/extending-the-rest-api/schema/#objects
https://developer.wordpress/rest-api/extending-the-rest-api/schema/#format
本文标签: phpPassing the name of selected color from the custom ltColorPalettegt component to rendercallback
版权声明:本文标题:php - Passing the name of selected color from the custom <ColorPalette> component to `render_callback` 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049029a2417963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论