-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add new HTMLElementControl component #69904
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
Add new HTMLElementControl component #69904
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.
Nice work, @Sukhendu2002!
I left some initial feedback, but the implementation looks promising.
The blocks should also be able to opt out from main
tag warning checks. Why?
- Not every block provides an option to select the
main
tag. Examples: Comments and Spacer blocks. - The calculation inside the
useSelect
would be wasteful for these blocks.
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
Hi @Mamaduka, Thanks for the feedback! I’ve implemented all the updates — looking forward to your thoughts on the latest changes. Let me know if there’s anything else you'd like me to adjust! |
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
@Sukhendu2002, please don't forget to mark PR as "Ready for review". Otherwise contributors might mistake it as "In Progress" work. |
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. |
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.
Again, nice work @Sukhendu2002!
I haven't intensively tested this idea, but I think our useSelect
here can be simplified further. Technically speaking, we only need to know one value - hasMainElementElsewhere
and anything else can be derived.
Additionally, we could use Array.some
instead of Array.filter
, which should be slightly more performant when working with large datasets.
Here's what I'm thinking:
const hasMainElementElsewhere = useSelect(
( select ) => {
if ( ! checkForMainTag ) {
return false;
}
const { getClientIdsWithDescendants, getBlockAttributes } =
select( blockEditorStore );
return getClientIdsWithDescendants().some( ( id ) => {
// Skip the current block.
if ( id === clientId ) {
return false;
}
return getBlockAttributes( id )?.tagName === 'main';
} );
},
[ clientId, checkForMainTag ]
);
cc @carolinan, @luminuu
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-select-control/index.js
Outdated
Show resolved
Hide resolved
Thanks for addressing the feedback, @Sukhendu2002! It seems that failing mobile unit tests is caused by this change. Did you have time to investigate the problem? |
packages/block-editor/src/components/html-element-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-control/README.md
Outdated
Show resolved
Hide resolved
Hi @Mamaduka, I'm not entirely sure, but it looks like HTMLElementControl might not be defined properly in the React Native context. Do you think we should:
What's your thoughts on this? |
packages/block-editor/src/components/html-element-control/index.js
Outdated
Show resolved
Hide resolved
@Sukhendu2002, I think the new private component should also be listed in |
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.
Thank you, @Sukhendu2002!
Everything works as expected ✅
@carolinan, @luminuu, @t-hamano, do you have any notes before merging this enhancement?
packages/block-editor/src/components/html-element-control/index.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/html-element-control/README.md
Outdated
Show resolved
Hide resolved
All the feedback appears to have been addressed. I'm going to merge this. |
@Mamaduka Sorry for the late feedback here. I think it's overall a good implementation, there's just two things I've come across I'd like to mention: The first one is probably a smaller one as it's a design issue, there's not enough space after the warning if there's a following element, same goes if there's no warning and just the select, there's also no space: The second thing is, and I'm not sure if there's a solution for this: When I have a template, where the Content block is already in a Group with the HTML Tag set to One suggestion for making it at least clearer to the user is to communicate that the template may contain the |
Unfortunately, this would be hard to resolve. The editor only has details for the blocks that are rendered on the canvas. When "Show template" is deactivated, we can only check the post content blocks. Maybe this is something we can improve by clear messaging. |
I think this is because the diff --git a/packages/block-editor/src/components/block-inspector/style.scss b/packages/block-editor/src/components/block-inspector/style.scss
index dd9d8f254f..4913b4c5f8 100644
--- a/packages/block-editor/src/components/block-inspector/style.scss
+++ b/packages/block-editor/src/components/block-inspector/style.scss
@@ -11,7 +11,8 @@
}
.components-base-control,
- .components-radio-control {
+ .components-radio-control,
+ .block-editor-html-element-control {
&:where(:not(:last-child)) {
margin-bottom: $grid-unit-20;
}
@@ -20,7 +21,8 @@
// Reset unwanted margin-bottom from being applied to BaseControls within certain components.
.components-focal-point-picker-control,
.components-query-controls,
- .components-range-control {
+ .components-range-control,
+ .block-editor-html-element-control {
.components-base-control {
margin-bottom: 0;
}
diff --git a/packages/block-editor/src/components/html-element-control/index.js b/packages/block-editor/src/components/html-element-control/index.js
index 3024301f19..a3dc68f7ee 100644
--- a/packages/block-editor/src/components/html-element-control/index.js
+++ b/packages/block-editor/src/components/html-element-control/index.js
@@ -86,7 +86,7 @@ export default function HTMLElementControl( {
} );
return (
- <VStack spacing={ 2 }>
+ <VStack spacing={ 2 } className="block-editor-html-element-control">
See #49520 (comment) for why such an approach is necessary. |
Thanks @t-hamano — that makes a lot of sense. I'll start working on the fix! |
Co-authored-by: Sukhendu2002 <sukhendu2002@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: skorasaurus <skorasaurus@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org>
What?
Closes: #69890
This PR introduces a new
HTMLElementSelectControl
component that replaces standard HTML element selection dropdowns across core blocks. The component includes built-in validation to prevent duplicate<main>
elements within a document, improving accessibility and HTML standards compliance.Testing Instructions
Screenshots or screencast
Screen.Recording.2025-04-15.at.3.48.45.PM.mov