How can I add arbitrary `data-` attributes to a block's `edit()` container?

For example:Before: <div id="block-fc502ca9-0d7b-440c-959c-3da152db0192" class="wp-block editor-block-

For example:

Before: <div id="block-fc502ca9-0d7b-440c-959c-3da152db0192" class="wp-block editor-block-list__block" data-type="wordcamp/schedule">

After: <div id="block-fc502ca9-0d7b-440c-959c-3da152db0192" class="wp-block editor-block-list__block" data-type="wordcamp/schedule" data-foo="bar">

For example:

Before: <div id="block-fc502ca9-0d7b-440c-959c-3da152db0192" class="wp-block editor-block-list__block" data-type="wordcamp/schedule">

After: <div id="block-fc502ca9-0d7b-440c-959c-3da152db0192" class="wp-block editor-block-list__block" data-type="wordcamp/schedule" data-foo="bar">

Share Improve this question asked Apr 22, 2019 at 18:44 Ian DunnIan Dunn 4,8875 gold badges44 silver badges70 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You can define a getEditWrapperProps function when you register the block, and return an object that sets the props for the wrapper.

The function will be passed the props for the block, in case those are needed.

registerBlockType( 'wpse/example-get-edit-wrapper-props', {
    title : 'getEditWrapperProps() Example',
    // ...

    getEditWrapperProps( props ) {
        return {
            'data-foo': 'bar',
            'data-quix': props.quix || false,
        };
    },
} );

That also lets you change any of other other props that get passed to the container, like id, className, onClick, tabIndex, etc. Obviously, you probably shouldn't mess with those unless you really know what you're doing, and understand the consequences.

Declaration in Gutenberg source: 1, 2.

Examples in Core blocks: 1, 2, 3

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745566136a4633405.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信