Creating a custom Gutenberg block with columns

Question

I would like to create a custom block that contains columns, and starts with various other blocks in each of the columns, however the code I have come up with so far will create a column block in the same way that adding a new column block from the standard block library does, i.e. asking which column variation, and even if I click skip it just gives me empty columns rather than my default content.

The code does the same thing even if I remove the innerBlocks property altogether, and renaming it to content does not help either. There are no console errors in the browser, or in the npm build process.

Adding content into the default columns the code gives me works, and I can save/reload/edit fine, just can’t specify default content.

I am running the latest WordPress 5.5.

import {__} from 'WPBeginner - WordPress Tutorials/i18n';
import {registerBlockType} from 'WPBeginner - WordPress Tutorials/blocks';
import {InnerBlocks} from 'WPBeginner - WordPress Tutorials/block-editor';

const MY_TEMPLATE = [
    ['core/columns', {
        columns: 2,
        innerBlocks: [
            ['core/column', {
                width: 33.33, innerBlocks: [
                    ['core/paragraph', {
                        placeholder: 'LHS Column',
                        content: __(
                            'LHS Lorem ipsum.....'
                        )
                    }]
                ]
            }],
            ['core/column', {
                width: 66.66, innerBlocks: [
                    ['core/paragraph', {
                        placeholder: 'RHS Column',
                        content: __(
                            'RHS Lorem ipsum.....'
                        )
                    }]
                ]
            }]
        ]
    }]
]

registerBlockType('my/block', {
    title: __('my block', 'my-block'),
    icon: 'feedback',
    category: 'layout',
    edit: (props) => {
        return (
            <div>
                <InnerBlocks
                    template={MY_TEMPLATE}
                    templateLock="all"
                    orientation="horizontal"
                />
            </div>
        );
    },
    save: ({attributes}) => {
        return (
            <div>
                <InnerBlocks.Content/>
            </div>
        );
    }

});

0
CodeMonkey 4 months 0 Answers 16 views 0

Leave an answer