From a6a28094e5dfa46fb9cae68fbc2b69fdd0b08beb Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 13 Sep 2023 14:52:09 +0300 Subject: [PATCH] Code-quality improvements to the button block * Add isLinkTag const to avoid code duplication * Add `enum` to the tagName definition in `block.json` * Add isButtonTag const to avoid code duplication --- packages/block-library/src/button/block.json | 1 + packages/block-library/src/button/edit.js | 9 +++++---- packages/block-library/src/button/save.js | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/button/block.json b/packages/block-library/src/button/block.json index 28c72d4d22ab0..eec327b4ca48e 100644 --- a/packages/block-library/src/button/block.json +++ b/packages/block-library/src/button/block.json @@ -11,6 +11,7 @@ "attributes": { "tagName": { "type": "string", + "enum": [ "a", "button" ], "default": "a" }, "type": { diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index a6c9c035e5bc1..0c1892c6e4c70 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -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(); @@ -220,7 +221,7 @@ function ButtonEdit( props ) { setAttributes( { textAlign: nextAlign } ); } } /> - { ! isURLSet && 'a' === TagName && ( + { ! isURLSet && isLinkTag && ( ) } - { isURLSet && 'a' === TagName && ( + { isURLSet && isLinkTag && ( ) } - { 'a' === TagName && isSelected && ( isEditingURL || isURLSet ) && ( + { isLinkTag && isSelected && ( isEditingURL || isURLSet ) && ( { @@ -279,7 +280,7 @@ function ButtonEdit( props ) { /> - { 'a' === TagName && ( + { isLinkTag && ( );