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:
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!
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.