javascript - Wordpress Custom Blocks - editor.css && style.css in Block editor mode

one text

I am building a custom block plugin for Wordpress. It is just supposed to be a Collapsible Block to learn how to build my own.

In the "Outer" block I have a "Root" block and a "Content" block. The Root block is the one you click on and the Content block is the one that unfolds out from the Root.

I have the folding javascript part working. I have been able to enqueue things effectively, but when I am in the editor view for my Content Block, my editor.css is loading (which is good) but my style.css is also loading (which is not good, right?).

Please see the code bellow... It is an amalgamation of about a ton different sources and tutorials that I put together to get where I am now. These files are in my plugin folder as such:

enter image description here

index.php

<?php
/**
 * Plugin Name:       Collapsible
 * Description:       A block that can collapse
 * Version: 1.0
 */

 // Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Directory path of this plugin
 *
 * @var string
 */
define( 'CB_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );


/**
 * Blocks
 */
require_once plugin_dir_path( __FILE__ ) . 'blocks/outer/index.php';
require_once plugin_dir_path( __FILE__ ) . 'blocks/root/index.php';
require_once plugin_dir_path( __FILE__ ) . 'blocks/content/index.php';

blocks/content/index.php

<?php
/**
 * Register the block.
 */
function register_collapsible_content_block() {
    if ( ! function_exists( 'register_block_type' ) ) {
        return;
    }

    register_block_type( __DIR__ );
}

add_action( 'init', 'register_collapsible_content_block' );

blocks/content/block.json

{
    "name": "collapsible/content-block",
    "title": "Collapsible Content",
    "category": "design",
    "icon": "table-row-after",
    "editorScript": "file:./block.js",
    "editorStyle": "file:./editor.css",
    "style": "file:./style.css",
    "description": "The block that is being displayed in a Collapsible Root Block.",
    "version": "1.0.0",
    ...
}

blocks/content/block.asset.php

<?php 
return array( 
    'dependencies' => array(
        'wp-blocks',
        'wp-element',
        'wp-block-editor',
        'wp-polyfill'
    ),
    'version' => '0.1'
);

blocks/content/block.js

( function ( blocks, element, blockEditor) {
    var el = element.createElement;
    var InnerBlocks = blockEditor.InnerBlocks;
    var useBlockProps = blockEditor.useBlockProps;

    blocks.registerBlockType( 'collapsible/content-block', {
        edit: function () {
            var blockProps = useBlockProps();
            return el( 
                'div', 
                blockProps, 
                el( InnerBlocks ) 
            );
        },
        save: function () {
            var blockProps = useBlockProps.save();
            return el( 
                'div', 
                blockProps, 
                el( InnerBlocks.Content ) 
            );
        },
    } );
} )( window.wp.blocks, window.wp.element, window.wp.blockEditor );

blocks/content/editor.css

.wp-block-collapsible-content-block {
    padding: 20px;
}

blocks/content/style.css

.wp-block-collapsible-content-block {
    background-color: #ccc;
    margin: 0px !important;
    max-height: 0px;
    overflow: hidden;
    transition: max-height 1s ease-out;
}

Just want to use vanilla JS and keep it simple.

I think I am missing something in the block.js file or in one or both or all of the index.php files.

Any help would be very much appreciated! Thank you so much!

Source