-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Refactor block-inspector: improve maintainability and readability #71608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the block-inspector/index.js component to improve maintainability and code readability by eliminating duplication and simplifying the component structure. The refactoring consolidates repeated inspector control slot patterns and optimizes performance.
Key changes include:
- Extracted duplicated
InspectorControls.Slotpatterns into a reusableStyleInspectorSlotscomponent - Created a
ContentPanelcomponent to encapsulate content panel logic - Eliminated duplicate
useInspectorControlsTabshook calls by passing the result down as props
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Size Change: +42 B (0%) Total Size: 1.95 MB
ℹ️ View Unchanged
|
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in 6e368dd. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17822246291
|
jeryj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it needs cleanup, but, overall, I like combining the Slots into one component rather than having the same thing unnecessarily duplicated.
| const shouldShowWarning = | ||
| ! blockType || ! selectedBlockClientId || isSelectedBlockUnregistered; | ||
|
|
||
| if ( shouldShowWarning ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine moving this to a named const to define what we're checking, but shouldShowWarning doesn't feel appropriate when the message is "No block selected." Maybe, noBlockSelection? But, oddly, isSelectedBlockUnregistered could show a "No block selected" message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither seem to be a great fit at the moment so perhaps we can leave as is for now?
|
I think we could extend this PR to fix #71626 |
aaronrobertshaw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the work in refactoring these slots @getdave and @jeryj 👍
It might help to get additional eyes on this PR as it impacts several use cases. The test also instructions need to help reviewers as well (I'll update them soon).
To ensure existing functionality I compared this branch against trunk for:
- a section block, template parts, unsynced patterns, and synced patterns
- a regular individual block selection
- a mutli-selection with only non-section blocks
- a multi-selection including a section block
From testing it looks like there was a bug introduced for mutli-selections but it's an easy fix.
Overall, I think the changes are heading in the right direction. So to help move this along and progress #71626, I'll push a few tweaks addressing current feedback.
|
I've pushed a fix and addressed the current feedback on this PR. I think it is ready for final review and test before merging. I'll also be putting up a PR based on this one to introduce content tabs for #71626 shortly. |
…ContentPanel components - Extract duplicated InspectorControls.Slot patterns into StyleInspectorSlots component - Extract content panel logic into ContentPanel component - Inline both components into main index.js file to reduce file complexity - Eliminate borderPanelLabel prop drilling by calculating it internally - Remove separate component files (style-inspector-slots.js, content-panel.js) - Improve code cohesion and maintainability
The onSelect prop was never passed to ContentPanel and was always undefined. This cleans up the API by removing the unused parameter.
The ContentPanel component was only used in one place, so inline its simple logic directly to reduce component complexity.
0b18c3b to
6e368dd
Compare
|
I've rebased this to resolve the conflicts against trunk after #71708. I believe it is back to working, ready for testing, and review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the work in refactoring these slots @getdave and @jeryj 👍
+1, and thanks for the updates @aaronrobertshaw, overall I like the idea of this refactor, too 👍
To ensure existing functionality I compared this branch against trunk for:
a section block, template parts, unsynced patterns, and synced patterns
a regular individual block selection
a mutli-selection with only non-section blocks
a multi-selection including a section block
Cheers for this, these are all testing well for me, along with a block set up with block bindings, too (shows in single selection, not when part of a multiselection).
LGTM!
tellthemachines
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find any changes in behaviour between this and trunk, but was curious about some of the logic so left questions below. Not blockers for merging though!
tellthemachines
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
Thanks for taking this over and merging (FYI I was AFK sick) 🙇 |
…rdPress#71608) Co-authored-by: getdave <[email protected]> Co-authored-by: aaronrobertshaw <[email protected]> Co-authored-by: jeryj <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: tellthemachines <[email protected]>
…rdPress#71608) Co-authored-by: getdave <[email protected]> Co-authored-by: aaronrobertshaw <[email protected]> Co-authored-by: jeryj <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: tellthemachines <[email protected]>
What
This PR refactors the
block-inspector/index.jscomponent to eliminate duplication, improve code readability, and reduce maintenance burden while preserving all existing functionality.Key Changes:
InspectorControls.Slotpatterns intoStyleInspectorSlotscomponentContentPanelcomponentborderPanelLabelprop drilling by calculating internallyuseInspectorControlsTabscalls for better performancecount→selectedBlockCount)hasMultipleTabsprop)Why
The
block-inspector/index.jscomponent had several maintainability issues that made it difficult to work with:1. Significant Code Duplication
InspectorControls.Slotpatterns were duplicated in two places (multi-selection and single block rendering)useInspectorControlsTabscalls with identical parameters2. Poor Code Readability
count(count of what?)3. Performance Inefficiencies
useInspectorControlsTabscalled twice with same parameters4. Maintenance Burden
How
Eliminated Duplication
StyleInspectorSlotscomponent to consolidate all style-related inspector control slotsContentPanelcomponent to encapsulate content panel logicImproved Readability
count > 1 && ! isSectionBlock→isMultiSelection! blockType || ! selectedBlockClientId || isSelectedBlockUnregistered→shouldShowWarningcount→selectedBlockCountOptimized Performance
useInspectorControlsTabscalls - now called once in parent and passed downSimplified APIs
hasMultipleTabsprop - child component calculates this locallyborderPanelLabelprop drilling - component calculates its own labelTesting
To confirm existing functionality is preserved ensure the displayed tabs and controls for this branch match trunk using:
Additionally,
Rationale for Value
This refactoring provides genuine value by solving real problems developers face:
Preparing for Simplified Editing Experience (WP 6.9)
This refactoring is particularly important as we prepare for the Simplified Editing Experience work planned for WordPress 6.9. The current monolithic structure of the block inspector would make it difficult to:
By breaking the component into smaller, focused pieces with clear responsibilities, we create a foundation that can more easily accommodate the architectural changes needed for the simplified editing experience.
This is not refactoring for its own sake - each change addresses a specific maintainability or performance issue that developers encounter when working with this component, while also preparing the codebase for future enhancements.
Screenshots
No visual changes - this is a pure refactoring that preserves all existing functionality.