• Resolved Tim Codex

    (@tien-nguyen)


    Dear all,

    I am developing a block that allow to add other blocks inside it using <InnerBlocks/>

    However, there are some unexpected divs that appear inside the block wrapper in the editor screen (back-end) with the names “block-editor-inner-blocks” and “block-editor-block-list__layout” respectively.

    Because those divs are not making the back-end and the front-end mismatch in terms of HTML structure but also bringing difficulty to decorate the back-end in a similar way as the front-end.

    I would like to ask if there is a way to remove “block-editor-inner-blocks” and “block-editor-block-list__layout” inner wrappers.

    Here are the codes that show the difference between the back-end and the front-end HTML structure:

    BACK-END

    <a id="block-d97d1315-fef5-4cc4-872f-7d0b0c4f7b1c" data-type="sneeit-blocks/link" class="block-editor-block-list__block wp-block has-child-selected wp-block-sneeit-blocks-link">
      <div class="block-editor-inner-blocks">
        <div class="block-editor-block-list__layout" data-is-drop-zone="true">
          <span data-type="sneeit-blocks/text">
            <span role="textbox" aria-multiline="true" aria-label="Type a Text" contenteditable="true" class="block-editor-rich-text__editable rich-text">Click Here</span>
          </span>
          <div class="block-list-appender wp-block">
            <div class="components-dropdown block-editor-inserter">
              <button type="button" class="components-button block-editor-button-block-appender">
                <span class="components-visually-hidden ecfd-dceb-a-ae-bfbed-0 e19lxcc00">Add block</span>
                <svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
                  <path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z"></path>
                </svg>
              </button>
            </div>
          </div>
        </div>
      </div>
    </a>

    FRONT-END

    <a class="wp-block-sneeit-blocks-link">
      <span class="wp-block-sneeit-blocks-text">Click Here</span>
    </a>

    These tags make me feel so daunted about the platform and its future. I feel that I am working with the Blogger platform of Google, not the WordPress modern platform.

    Thank you very much.

    Regards,

    Tien Nguyen

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello Tien Nguyen,

    The “block-editor-inner-blocks” and “block-editor-block-list__layout” divs are added by WordPress to provide the necessary structure and functionality for the block editor. It is not recommended to remove them as it may cause issues with the editor’s functionality.

    However, you can modify their appearance using CSS. You can add CSS rules to target these divs and style them as desired. For example, you can use the following CSS to hide the “block-editor-inner-blocks” div:

    .block-editor-inner-blocks {
      display: none;
    }
    

    Similarly, you can target the “block-editor-block-list__layout” div and modify its appearance. However, be careful when modifying these divs as it can affect the functionality of the block editor.

    I hope this helps. Let me know if you have any other questions!

    Thread Starter Tim Codex

    (@tien-nguyen)

    Hello @linardsn

    The .block-editor-inner-blocks contains all inner blocks so we can’t hide it.

    The reason I want to remove them (unwrap them), so the whole structure of blocks in the editor will be similar to the front-end structure.

    This is a must because many layouts are leaning on the Grid and Flex layouts that can be broken if there are any blocks that interfere between the parent wrapper and the children.

    Thanks for the reply.

    While you cannot remove the “block-editor-inner-blocks” and “block-editor-block-list__layout” divs directly, you can use JavaScript to modify the DOM structure to achieve a similar layout in the editor.

    Here’s a possible solution using JavaScript to unwrap the inner elements:

    First, add a custom JavaScript file to your block editor. In your block.json file, add an editorScript property like this:

    {
      "editorScript": "file:./build/index.js",
      ...
    }
    

    In your index.js file, import the required dependencies and register your custom block:

    import { registerBlockType } from '@wordpress/blocks';
    import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
    
    registerBlockType('sneeit-blocks/link', {
      // block configuration here
    });
    

    Add a custom edit function to handle the editor view of the block:

    import { useEffect } from '@wordpress/element';
    
    const Edit = (props) => {
      const blockProps = useBlockProps();
    
      useEffect(() => {
        const blockElement = document.querySelector(
          [data-block="${props.clientId}"]
        );
    
        if (blockElement) {
          const innerBlocksWrapper = blockElement.querySelector(
            '.block-editor-inner-blocks'
          );
          const layoutWrapper = blockElement.querySelector(
            '.block-editor-block-list__layout'
          );
    
          if (innerBlocksWrapper && layoutWrapper) {
            while (layoutWrapper.firstChild) {
              innerBlocksWrapper.parentNode.insertBefore(
                layoutWrapper.firstChild,
                layoutWrapper
              );
            }
            innerBlocksWrapper.removeChild(layoutWrapper);
          }
        }
      }, []);
    
      return (
        <div {...blockProps}>
          <InnerBlocks />
        </div>
      );
    };
    

    set the edit property of your block to use the custom Edit component:

    registerBlockType('sneeit-blocks/link', {
      // other block configuration
      edit: Edit,
    });
    

    This code will unwrap the inner elements of the block in the editor by moving the child elements of the “block-editor-block-list__layout” div to the parent element and removing the “block-editor-block-list__layout” div.

    Please note that this is a workaround and may have unintended side effects on the functionality of the block editor. Always test your code thoroughly and make sure it doesn’t cause any issues.

    I hope this helps. Good luck!

    Thread Starter Tim Codex

    (@tien-nguyen)

    @linardsn Thank you for spending time writing the code. I will definitely test the code.

    However, as I know, WordPress FSE has two back-end parts: the editor and the editor iframe (which actually has the blocks). The editorScript is for working with the editor only, not the blocks inside the editor iframe. We will use editorScript to add/remove features of the editor (like inspectors, and toolbars) but cannot access the editor iframe to modify its structure.

    Thanks again.

    "apiVersion": 2,

    Hello!! This will Help:)

        const blockProps = useBlockProps({});
        const TEMPLATE = [['nametemplate/templatename']];
    
        const innerBlocksProps = useInnerBlocksProps(blockProps, {
            allowedBlocks: TEMPLATE,
            template: TEMPLATE,
            templateLock: false
        });
    return (
    
    
            <div {...innerBlocksProps} ></div>
    
    
        );
    Thread Starter Tim Codex

    (@tien-nguyen)

    Thank you Mohamed. Your code is what I am looking for. Thank you very much.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Remove “block-editor-inner-blocks” and “block-editor-block-list__layout”’ is closed to new replies.