Skip to content

Commit

Permalink
Code-quality improvements to the button block
Browse files Browse the repository at this point in the history
* Add isLinkTag const to avoid code duplication

* Add `enum` to the tagName definition in `block.json`

* Add isButtonTag const to avoid code duplication
  • Loading branch information
aristath authored Sep 13, 2023
1 parent 3e497b3 commit a6a2809
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"attributes": {
"tagName": {
"type": "string",
"enum": [ "a", "button" ],
"default": "a"
},
"type": {
Expand Down
9 changes: 5 additions & 4 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function ButtonEdit( props ) {
const [ isEditingURL, setIsEditingURL ] = useState( false );
const isURLSet = !! url;
const opensInNewTab = linkTarget === '_blank';
const isLinkTag = 'a' === TagName;

function startEditing( event ) {
event.preventDefault();
Expand Down Expand Up @@ -220,7 +221,7 @@ function ButtonEdit( props ) {
setAttributes( { textAlign: nextAlign } );
} }
/>
{ ! isURLSet && 'a' === TagName && (
{ ! isURLSet && isLinkTag && (
<ToolbarButton
name="link"
icon={ link }
Expand All @@ -229,7 +230,7 @@ function ButtonEdit( props ) {
onClick={ startEditing }
/>
) }
{ isURLSet && 'a' === TagName && (
{ isURLSet && isLinkTag && (
<ToolbarButton
name="link"
icon={ linkOff }
Expand All @@ -240,7 +241,7 @@ function ButtonEdit( props ) {
/>
) }
</BlockControls>
{ 'a' === TagName && isSelected && ( isEditingURL || isURLSet ) && (
{ isLinkTag && isSelected && ( isEditingURL || isURLSet ) && (
<Popover
placement="bottom"
onClose={ () => {
Expand Down Expand Up @@ -279,7 +280,7 @@ function ButtonEdit( props ) {
/>
</InspectorControls>
<InspectorControls group="advanced">
{ 'a' === TagName && (
{ isLinkTag && (
<TextControl
__nextHasNoMarginBottom
label={ __( 'Link rel' ) }
Expand Down
9 changes: 5 additions & 4 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function save( { attributes, className } ) {
}

const TagName = tagName || 'a';
const isButtonTag = 'button' === TagName;
const buttonType = type || 'button';
const borderProps = getBorderClassesAndStyles( attributes );
const colorProps = getColorClassesAndStyles( attributes );
Expand Down Expand Up @@ -70,14 +71,14 @@ export default function save( { attributes, className } ) {
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<RichText.Content
tagName={ TagName }
type={ 'button' === TagName ? buttonType : null }
type={ isButtonTag ? buttonType : null }
className={ buttonClasses }
href={ 'button' === TagName ? null : url }
href={ isButtonTag ? null : url }
title={ title }
style={ buttonStyle }
value={ text }
target={ 'button' === TagName ? null : linkTarget }
rel={ 'button' === TagName ? null : rel }
target={ isButtonTag ? null : linkTarget }
rel={ isButtonTag ? null : rel }
/>
</div>
);
Expand Down

0 comments on commit a6a2809

Please sign in to comment.