I’m experiencing a strange behavior of a block in PluginDocumentSettingPanel. I’ve registered some colors in editor-color-palette. I want these to be selectable in the document setting panel. Here’s the code:
add_action( 'after_setup_theme' , function () {
add_theme_support( 'editor-color-palette' , [
[ 'name' => __( 'White' ) , 'slug' => 'white' , 'color' => '#FFFFFF' ] ,
/* ... */
] );
});
add_action( 'admin_enqueue_scripts' , function () {
wp_enqueue_script( 'colorsPanel' , get_template_uri_by_dir( __DIR__ , 'js/editorPlugin.js' ) , [ 'metaComponents' ] );
} );
;( function ( {
components : { MetaColorPalette } ,
data : { useSelect } ,
editor : { PluginDocumentSettingPanel } ,
element : { createElement : el } ,
i18n : { __ } ,
plugins : { registerPlugin }
} ) {
registerPlugin( 'post-colors' , {
icon : null ,
render : () => {
const colors = useSelect( 'core/block-editor' ).getSettings().colors;
console.log( colors );
return el( PluginDocumentSettingPanel , { name : 'postColors' , title : __( 'Colors' ) , initialOpen : true } ,
el( 'p' , null , __( 'Foreground' ) ) ,
el( MetaColorPalette , { metaKey : 'foregroundColor' , useSlug : true , colors : colors , label : __( 'Foreground' ) } ) ,
el( 'p' , null , __( 'Background' ) ) ,
el( MetaColorPalette , { metaKey : 'backgroundColor' , useSlug : true , colors : colors , label : __( 'Background' ) } ) ,
);
}
} );
} )( window.wp );
So, here’s the thing: Everything is working quite fine, except the colors registered in editor-color-palette only show up when the block is re-rendered, e.g. by clicking somewhere in the editor and then switching back to the document settings.
The colors showing at the beginning are the 12 colors of ‘fresh’, I believe.
MetaColorPalette is one of a bunch of components I’ve added to store post_meta. At the beginning the console.log and the component shows (and saves) the default colors, not mine.
What am I missing?
Additionally, I tried using ‘disable-custom-colors’ in theme support with no result at all. Changing priority of the action containing add_theme_support does not change anything either.