Repository navigation
Allow using a button element for button blocks #54206
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,8 +78,19 @@ function ButtonEdit( props ) { | |
| onReplace, | ||
| mergeBlocks, | ||
| } = props; | ||
| const { textAlign, linkTarget, placeholder, rel, style, text, url, width } = | ||
| attributes; | ||
| const { | ||
| tagName, | ||
| textAlign, | ||
| linkTarget, | ||
| placeholder, | ||
| rel, | ||
| style, | ||
| text, | ||
| url, | ||
| width, | ||
| } = attributes; | ||
|
|
||
| const TagName = tagName || 'a'; | ||
|
|
||
| function onToggleOpenInNewTab( value ) { | ||
| const newLinkTarget = value ? '_blank' : undefined; | ||
|
|
@@ -209,7 +220,7 @@ function ButtonEdit( props ) { | |
| setAttributes( { textAlign: nextAlign } ); | ||
| } } | ||
| /> | ||
| { ! isURLSet && ( | ||
| { ! isURLSet && 'a' === TagName && ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The |
||
| <ToolbarButton | ||
| name="link" | ||
| icon={ link } | ||
|
|
@@ -218,7 +229,7 @@ function ButtonEdit( props ) { | |
| onClick={ startEditing } | ||
| /> | ||
| ) } | ||
| { isURLSet && ( | ||
| { isURLSet && 'a' === TagName && ( | ||
| <ToolbarButton | ||
| name="link" | ||
| icon={ linkOff } | ||
|
|
@@ -229,7 +240,7 @@ function ButtonEdit( props ) { | |
| /> | ||
| ) } | ||
| </BlockControls> | ||
| { isSelected && ( isEditingURL || isURLSet ) && ( | ||
| { 'a' === TagName && isSelected && ( isEditingURL || isURLSet ) && ( | ||
| <Popover | ||
| placement="bottom" | ||
| onClose={ () => { | ||
|
|
@@ -268,12 +279,16 @@ function ButtonEdit( props ) { | |
| /> | ||
| </InspectorControls> | ||
| <InspectorControls group="advanced"> | ||
| <TextControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Link rel' ) } | ||
| value={ rel || '' } | ||
| onChange={ ( newRel ) => setAttributes( { rel: newRel } ) } | ||
| /> | ||
| { 'a' === TagName && ( | ||
| <TextControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Link rel' ) } | ||
| value={ rel || '' } | ||
| onChange={ ( newRel ) => | ||
| setAttributes( { rel: newRel } ) | ||
| } | ||
| /> | ||
| ) } | ||
| </InspectorControls> | ||
| </> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import { | |
|
|
||
| export default function save( { attributes, className } ) { | ||
| const { | ||
| tagName, | ||
| type, | ||
| textAlign, | ||
| fontSize, | ||
| linkTarget, | ||
|
|
@@ -32,6 +34,8 @@ export default function save( { attributes, className } ) { | |
| return null; | ||
| } | ||
|
|
||
| const TagName = tagName || 'a'; | ||
| const buttonType = type || 'button'; | ||
| const borderProps = getBorderClassesAndStyles( attributes ); | ||
| const colorProps = getColorClassesAndStyles( attributes ); | ||
| const spacingProps = getSpacingClassesAndStyles( attributes ); | ||
|
|
@@ -65,14 +69,15 @@ export default function save( { attributes, className } ) { | |
| return ( | ||
| <div { ...useBlockProps.save( { className: wrapperClasses } ) }> | ||
| <RichText.Content | ||
| tagName="a" | ||
| tagName={ TagName } | ||
| type={ 'button' === TagName ? buttonType : null } | ||
| className={ buttonClasses } | ||
| href={ url } | ||
| href={ 'button' === TagName ? null : url } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above - |
||
| title={ title } | ||
| style={ buttonStyle } | ||
| value={ text } | ||
| target={ linkTarget } | ||
| rel={ rel } | ||
| target={ 'button' === TagName ? null : linkTarget } | ||
| rel={ 'button' === TagName ? null : rel } | ||
| /> | ||
| </div> | ||
| ); | ||
|
|
||
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
enumas we only want to allowaandbuttonelements.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
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
typeanenum😅