How to add a class to the element in a custom Gutenberg block

I have InnerBlocks which contain ButtonBlockAppender.

I would like to add any className to the ButtonBlockAppender

ButtonBlockAppender documentation says:

className
    Type: String
    Default: ""
A CSS class to be prepended to the default class of "button-block-appender".

So i tried to do this like this:

<InnerBlocks
    renderAppender={() => (
        <InnerBlocks.ButtonBlockAppender className={'TESTEDCLASSSTRING'} />
    )}
/>

Unfortunately my class is not included. Generally nothing has changed.

The console is clear (no errors).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That documentation is for the base ButtonBlockAppender component in the @wordpress/block-editor package, and that component is exported with the name ButtonBlockerAppender (note the “Blocker” vs “Block”).

But your code is actually using InnerBlocks.ButtonBlockAppender which is an enhanced version of that base ButtonBlockAppender component, and as of writing, that enhanced version does not include the className prop — check the source (from the last commit in April 2020) where you can see BaseButtonBlockAppender is called without the className prop, and that BaseButtonBlockAppender is the one I’ve mentioned above (i.e. ButtonBlockerAppender).

So if you want that className prop, you can try one of these:

  1. Clone the enhanced version (i.e. InnerBlocks.ButtonBlockAppender) and include that className prop, but it’ll be up to you on how to clone it..
  2. Or don’t use that enhanced version, and use the base one instead. E.g.
    const { InnerBlocks, ButtonBlockerAppender } = wp.blockEditor;
    // or if you'd rather import:
    //import { InnerBlocks, ButtonBlockerAppender } from '@wordpress/block-editor';
    
    // Then in the block edit():
    <InnerBlocks
        renderAppender={() => (
            <ButtonBlockerAppender className="your-custom-class" />
        )}
    />
    


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x