Master Gutenberg block controls by using InspectorControls, custom React inputs, and precise attribute synchronization to build professional editor experiences.
Previously in this course, we covered the fundamentals of Block API v2 Essentials and advanced layout patterns like InnerBlocks and Nested Structures. While those lessons focused on the editor canvas, this lesson shifts our focus to the sidebar—the "command center" of your block—where complex configurations live.
In Gutenberg, InspectorControls is a specialized component that renders content into the block settings sidebar. It is the designated space for configuration that doesn't belong directly on the canvas, such as toggle switches, color pickers, or API-driven data selectors.
Because our Knowledge Base plugin requires users to configure specific settings (like category filtering or display limits), we cannot rely solely on the main editor area. We must synchronize our React state with the block's attributes defined in block.json.
To implement this, you'll need the @wordpress/block-editor and @wordpress/components packages. The core pattern involves wrapping your inputs in an InspectorControls component and ensuring your setAttributes function triggers a re-render.
Let’s add a sidebar control to our kb-post-list block that allows the user to toggle whether or not to show the post excerpt.
JSXimport { InspectorControls } from CE9178">'@wordpress/block-editor'; import { PanelBody, ToggleControl } from CE9178">'@wordpress/components'; import { __ } from CE9178">'@wordpress/i18n'; export default function Edit({ attributes, setAttributes }) { const { showExcerpt } = attributes; return ( <> <InspectorControls> <PanelBody title={__(CE9178">'Knowledge Base Settings', CE9178">'kb-plugin')}> <ToggleControl label={__(CE9178">'Show Post Excerpt', CE9178">'kb-plugin')} checked={showExcerpt} onChange={(value) => setAttributes({ showExcerpt: value })} /> </PanelBody> </InspectorControls> <div className="kb-block-preview"> {/* Block content logic here */} </div> </> ); }
Simple toggles are easy, but real-world requirements often involve fetching remote data—like a list of categories—to populate a dropdown. You’ll want to combine InspectorControls with the techniques we discussed in Custom REST API Integration: Fetching Data in React.
When dealing with complex objects or arrays (e.g., a selection of multiple categories), avoid storing the entire object in your attributes. Store only the ID, and use a selector to hydrate the data in the UI. This keeps your block.json attributes clean and prevents unnecessary bloat in the database.
kb-post-list block.postsPerPage (integer) to your block.json.Edit.js, add a RangeControl within an InspectorControls panel.RangeControl to postsPerPage using setAttributes.InspectorControls must be a sibling to your block's main edit components inside a fragment (<>...</>) or a wrapper div. If you place it inside the block's primary markup, it will render literally on the page.attributes directly (e.g., attributes.showExcerpt = true). Always use the setAttributes function provided by the block's edit method to ensure the editor registers the change and triggers a save.We've successfully extended the block editor UI by:
InspectorControls to create a dedicated settings panel.ToggleControl for consistent UX.setAttributes.These controls are essential for turning a basic block into a production-ready configuration tool for your users.
Up next: We will dive into Block Transforms and Deprecation to ensure your block remains resilient as your plugin evolves.
Master the @wordpress/components library to build consistent, accessible, and native-feeling WordPress admin interfaces for your plugin's React dashboard.
Read moreLearn to persist Gutenberg state using Redux middleware. We’ll show you how to sync editor data to localStorage for a seamless, high-performance experience.
Custom Gutenberg Block Controls