{"id":275,"date":"2024-11-14T15:21:00","date_gmt":"2024-11-14T20:21:00","guid":{"rendered":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/?page_id=275"},"modified":"2025-08-15T10:32:46","modified_gmt":"2025-08-15T14:32:46","slug":"inspector-controls","status":"publish","type":"page","link":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/getting-started\/inspector-controls\/","title":{"rendered":"Adding Inspector Controls"},"content":{"rendered":"\n<p>Let&#8217;s go step by step through the Caterpillar Block. You might not need every technique used in this example, but it covers a lot of common features you may need in developing your own blocks.<\/p>\n\n\n\n<h2>Setup\/Assumptions<\/h2>\n\n\n\n<ol><li>You are working in a repo cloned from <a href=\"https:\/\/github.com\/bu-ist\/responsive-child-starter-3x-block-editor\">responsive-child-starter-3x-block-editor<\/a> and are using npm 20<\/li><li>You are familiar with the command line<\/li><li>You&#8217;ve read and understand <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/block-architecture\/\">Block Architecture<\/a> and <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/dynamic-vs-static\/\">Dynamic vs Static<\/a><\/li><li>Screenshots will depict VS Code<\/li><li>This is what the final code will look like: <a href=\"https:\/\/github.com\/bu-ist\/block-sandbox\/tree\/sandbox\/src\/blocks\/caterpillar\">https:\/\/github.com\/bu-ist\/block-sandbox\/tree\/sandbox\/src\/blocks\/caterpillar<\/a><\/li><\/ol>\n\n\n\n<h2>Step 1: Create a New Block Using Create-Block Template<\/h2>\n\n\n\n<p>In the command terminal, from the root of your project run <code>npm run new-id-block<\/code>.<\/p>\n\n\n\n<p>This will start a series of prompts. Here&#8217;s what was used to make this block:<\/p>\n\n\n\n<ul><li>The template variant to use for this block: <strong>theme<\/strong><\/li><li>The block slug used for identification (also the output folder name): <strong>demo<\/strong><\/li><li>The internal namespace for the block name (something unique for your products): <strong>block-sandbox<\/strong><\/li><li>The display title for your block: <strong>demo<\/strong><\/li><li>The short description for your block (optional): <strong>demo<\/strong><\/li><li>The dashicon to make it easier to identify your block (optional): <strong>ellipsis<\/strong><\/li><li>The category name to help users browse and discover your block: <strong>text<\/strong><\/li><li>The text domain used to make strings translatable in the block (optional): <strong>block-sandbox<\/strong><\/li><\/ul>\n\n\n\n<p>When this is done there will be a new block in the folder <code>\/src\/blocks\/caterpillar<\/code>.<\/p>\n\n\n\n<p>You can <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/hybrid-themes\/adding-a-custom-theme-block\/using-create-block-template\/\">learn more about what is happening in this step here<\/a>.<\/p>\n\n\n\n<h2>Step 2: Update Bundled Styles<\/h2>\n\n\n\n<ul><li>In your IDE, navigate to <code>\/src\/blocks<\/code><\/li><li>Open <code style=\"font-size: 16px;\">blocks-bundled.scss<\/code><\/li><li>Scroll down to the section <code style=\"font-size: 16px;\">\/\/ Import Block Styles<\/code><\/li><li>Add a new line: <code style=\"font-size: 16px;\">@import 'caterpillar\/block-base';<\/code><\/li><\/ul>\n\n\n\n<p>This ensures the styles for your new block will be bundled with the other block styles when the theme serves the bundled style file.<\/p>\n\n\n\n<p>Now it&#8217;s time to start work on the block itself.<\/p>\n\n\n\n<h2>Step 3: Updating the Files<\/h2>\n\n\n\n<p>Check out <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/block-architecture\/\">Block Architecture page<\/a> for an overview of all the files in this directory. Otherwise, get ready to get your feet wet!<\/p>\n\n\n\n<h3><code>block.json<\/code> <\/h3>\n\n\n\n<p>Simplifies the process of defining and registering a block by using the same block\u2019s definition in JSON format to register the block on both the server and the client. <\/p>\n\n\n\n<p>We defined most things here through the command line create-block command, but there are a few things we need to add.<\/p>\n\n\n\n<h4>example<\/h4>\n\n\n\n<p>Below the description key, let&#8217;s add example data for the block. This data is used to construct a preview for the block to be shown in the Inspector Help Panel when the user mouses over the block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"example\": {\n        \"attributes\": {\n            \"message\": \"hungry hungry\"\n        }\n    }<\/code><\/pre>\n\n\n\n<h4>supports<\/h4>\n\n\n\n<p>Block Supports is the API that allows a block to declare support for certain features.<\/p>\n\n\n\n<p>link to docs&#8230;<\/p>\n\n\n\n<p>Right below the example we just added, let&#8217;s add support for HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"supports\": {\n\t\t\"html\": true\n\t},<\/code><\/pre>\n\n\n\n<h4>attributes<\/h4>\n\n\n\n<p>Block attributes provide information about the data stored by a block. For example, rich content, a list of image URLs, a background color, or a button title.<\/p>\n\n\n\n<p>link to docs&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"attributes\": {\n\t\t\"textField\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t},<\/code><\/pre>\n\n\n\n<h4>viewScript<\/h4>\n\n\n\n<p>We want to run some JS in the frontend, so we will need to add that here<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"viewScript\": \"file:.\/view.js\"<\/code><\/pre>\n\n\n\n<ul><li>See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/block-js\/\">block.json<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>Styles<\/h2>\n\n\n\n<p>These files are all optional and will not be used in this tutorial. Feel free to play around with them though!<\/p>\n\n\n\n<p><em>You can put whatever you want here. I&#8217;ve added a few just to visually confirm that the file is loading as intended.<\/em><\/p>\n\n\n\n<h3><code>block-base.scss<\/code><\/h3>\n\n\n\n<p>SCSS partial that contains all of the base (structural) styles for this block. These are compiled both individually and in a bundle.<\/p>\n\n\n\n<p><em>@todo more about this&#8230;<\/em><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/bu-ist\/block-sandbox\/blob\/sandbox\/src\/blocks\/caterpillar\/block-base.scss\">https:\/\/github.com\/bu-ist\/block-sandbox\/blob\/sandbox\/src\/blocks\/caterpillar\/block-base.scss<\/a><\/p>\n\n\n\n<h3><code>editor.scss<\/code> (block.json: editorStyle)<\/h3>\n\n\n\n<p>Styles that are applied inside the editor only.<\/p>\n\n\n\n<p>If you are not using this, remove the <code>editorStyle<\/code> entry from <code>block.json<\/code>.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/bu-ist\/block-sandbox\/blob\/sandbox\/src\/blocks\/caterpillar\/editor.scss\">https:\/\/github.com\/bu-ist\/block-sandbox\/blob\/sandbox\/src\/blocks\/caterpillar\/editor.scss<\/a><\/p>\n\n\n\n<ul><li>See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/editor-styling\/\">Editor Styling<\/a><\/li><\/ul>\n\n\n\n<h3><code>modules\/inspector-editor.scss<\/code><\/h3>\n\n\n\n<p>Basically the same as the <code>editor.scss<\/code> above, but specific to the editor-partials code.<\/p>\n\n\n\n<p>It is included @todo<\/p>\n\n\n\n<h3><code>style.scss<\/code> (block.json: style)<\/h3>\n\n\n\n<p>Styles that are applied both on the front of your site and in the editor. Our block template @imports some common tools and the <code>block-base.scss<\/code> in order to stay DRY.<\/p>\n\n\n\n<p>If you are not using this, remove the <code>style<\/code> entry from <code>block.json<\/code>.<\/p>\n\n\n\n<ul><li>See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/frontend-styling\/\">Frontend Styling<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>Scripts<\/h2>\n\n\n\n<h3><code>edit.js<\/code> (block.json: editorScript)<\/h3>\n\n\n\n<p>Contains the React component responsible for rendering the block\u2019s editing user interface, allowing users to interact with and customize the block\u2019s content and settings in the Block Editor. This component gets passed to the edit property of the registerBlockType function in the index.js file.<\/p>\n\n\n\n<p>If you are not using this, remove the <code>???<\/code> entry from <code>block.json<\/code>.<\/p>\n\n\n\n<ul><li>See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/frontend-styling\/\">@todo<\/a><\/li><\/ul>\n\n\n\n<h3><code>index.js<\/code> <\/h3>\n\n\n\n<p>The entry point file for JavaScript that should only get loaded in the Block Editor. It\u2019s responsible for calling the registerBlockType function to register the block on the client and typically imports the edit.js and save.js files to get the functions required for block registration.<\/p>\n\n\n\n<h3><code><code>modules<\/code>\/inspector.js<\/code><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<h3><code>view.js<\/code> (block.json: viewScript)<\/h3>\n\n\n\n<p>Use this file for JavaScript code that you want to run in the front-end on posts\/pages that contain this block. <\/p>\n\n\n\n<p>If you are not using this, remove the <code>viewScript<\/code> entry from <code>block.json<\/code>.<\/p>\n\n\n\n<ul><li>See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/frontend-js\/\">Frontend JS<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2>PHP<\/h2>\n\n\n\n<h3><code>render.php<\/code> (block.json: render)<\/h3>\n\n\n\n<p>PHP file to use when rendering the block type on the server to show on the front end. See <a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/render-template\/\">Render Template<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><\/p>\n\n\n\n<ol><li>update each file&#8230;<ol><li>modify components<\/li><li><a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/importing-components\/\">Importing Components<\/a><\/li><li><a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/useblockprops\/\">useBlockProps<\/a><\/li><li><a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/inner-blocks\/\">Inner Blocks<\/a><\/li><\/ol><\/li><li>linting<ol><li><a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/testing-the-block\/\">Testing the Block<\/a><\/li><\/ol><\/li><li>building<\/li><li><a href=\"https:\/\/developer.bu.edu\/gutenberg\/getting-started\/building-a-block\/deprecating-a-block\/\">Deprecating a Block<\/a><\/li><\/ol>\n\n\n\n<p>if we make the repo public we can embed gists using <a href=\"https:\/\/emgithub.com\/\">https:\/\/emgithub.com\/<\/a><\/p>\n\n\n\n<p>todo: find a good tutorial style and mimic it&#8230; maybe <a href=\"https:\/\/web.dev\/articles\/building\/a-dialog-component\">https:\/\/web.dev\/articles\/building\/a-dialog-component<\/a> or <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/dialog\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/dialog<\/a><\/p>\n\n\n\n<script src=\"https:\/\/emgithub.com\/embed-v2.js?target=https%3A%2F%2Fgithub.com%2Fbu-ist%2Fgists%2Fblob%2Fmain%2Fblocks%2Fexample-caterpillar%2Fedit.js&amp;style=default&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on\"><\/script>\n\n\n\n<p>include: wp support; php\/css support; accessibility; known issues; author (so they can be contacted with questions or bugs)<\/p>\n\n\n\n<p>see also: code embedding tests <a href=\"https:\/\/id-timk-upgrade-58.cms-devl.bu.edu\/wp-admin\/post.php?post=381265&amp;action=edit&amp;classic-editor__forget\">https:\/\/id-timk-upgrade-58.cms-devl.bu.edu\/wp-admin\/post.php?post=381265&amp;action=edit&amp;classic-editor__forget<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s go step by step through the Caterpillar Block. You might not need every technique used in this example, but it covers a lot of common features you may need in developing your own blocks. Setup\/Assumptions You are working in a repo cloned from responsive-child-starter-3x-block-editor and are using npm 20You are familiar with the command [&hellip;]<\/p>\n","protected":false},"author":9175,"featured_media":0,"parent":16,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"status-is-a-reserved-term":[5],"assignee":[4],"_links":{"self":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/275"}],"collection":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/users\/9175"}],"replies":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":32,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/275\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/275\/revisions\/712"}],"up":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/16"}],"wp:attachment":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"status-is-a-reserved-term","embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/status-is-a-reserved-term?post=275"},{"taxonomy":"assignee","embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/assignee?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}