-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Allow using a button element for button blocks #54206
Conversation
Size Change: +83 B (0%) Total Size: 1.52 MB
ℹ️ View Unchanged
|
f2df198
to
a6d4ccd
Compare
Question 🤔 It is often recommended that any button element should have an explicit
I can see how this automatic contextual awareness could also be seen as a feature rather than an issue. But then we run into the issue that we have no way of adding additional buttons inside forms like a reset button or just a regular button. I would therefore say that we should add a <!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"tagName":"button","type":"button"} -->
<div class="wp-block-button"><button class="wp-block-button__link wp-element-button" type="button">Actual button</button></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div> |
Thank you for the feedback @fabiankaegy!
Makes sense... Added, and updated the PR description 👍 |
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.
The code looks good to me and in my testing this works as intended. I didn't run into any deprecation issues with it also.
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.
Sorry, I'm late to the party. This is a nice enhancement.
I left a few minor suggestions for a follow-up.
"tagName": { | ||
"type": "string", | ||
"default": "a" | ||
}, |
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.
This should probably be an enum
as we only want to allow a
and button
elements.
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 don't think we can actually use enum
... At least not without changing the schema for block.json 🤔
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.
What's the exact reason?
I thought we could use something like this:
gutenberg/packages/block-library/src/paragraph/block.json
Lines 29 to 32 in 6e9c1d3
"direction": { | |
"type": "string", | |
"enum": [ "ltr", "rtl" ] | |
} |
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.
Ah OK!! That makes sense now... I thought you were talking about making the type
an enum
😅
@@ -209,7 +220,7 @@ function ButtonEdit( props ) { | |||
setAttributes( { textAlign: nextAlign } ); | |||
} } | |||
/> | |||
{ ! isURLSet && ( | |||
{ ! isURLSet && 'a' === TagName && ( |
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.
Suggestion: The 'a' === TagName
check could be extracted into isLinkTag
and reused in code.
className={ buttonClasses } | ||
href={ url } | ||
href={ 'button' === TagName ? null : url } |
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.
Same as above - 'button' === TagName
could be extracted into isButtonTag
and reused.
Warning: Type of PR label error To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. |
What?
Allows using a
<button>
element for button blocks.Why?
Because it would improve semantics (and therefore also a11y), and allow us to use the block in more scenarios where a link is not the right element to use.
Right now, the
core/button
blocks prints a link. Though visually this is OK, semantically it can be limiting and in some cases incorrect. Currently, we have no way to add an actual<button>
element, and the button block doesn't do what someone with some basic HTML knowledge would expect it to do.Additionally, a real
<button>
element is something we could use in other blocks like the search block, comments block, as well as other future blocks.This came out of #44214
How?
Added a
tagName
attribute to thecore/button
block.<button>
elements don't havehref
, so when using a button HTML element, the URL input is hidden from the UI.UPDATE: After this comment, A
type
attribute was also added to allow defining the button type.Nothing changes for link buttons.
IMPORTANT:
This PR does not add a UI for selecting the element. It just makes it possible for users to manually change it via the Code Editor, without getting an error when they switch back to the visual editor.
Testing Instructions
Testing Instructions for Keyboard
Nothing to test