From b7fba0101142c7b0a1d6e7d929368b4031309eec Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 7 Sep 2023 12:47:17 +0300 Subject: [PATCH] Allow using a button element for button blocks (#54206) * Allow using a button element for button blocks * update tests / fixtures * Add "type" attribute for buttons --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/button/block.json | 12 +++++- packages/block-library/src/button/edit.js | 37 +++++++++++++------ packages/block-library/src/button/save.js | 13 +++++-- .../blocks/core__button__squared.json | 2 + .../fixtures/blocks/core__buttons.html | 8 ++++ .../fixtures/blocks/core__buttons.json | 24 ++++++++++++ .../fixtures/blocks/core__buttons.parsed.json | 28 +++++++++++++- .../blocks/core__buttons.serialized.html | 8 ++++ 9 files changed, 115 insertions(+), 19 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 1d83564fe3c17c..5767ce77461931 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -53,7 +53,7 @@ Prompt visitors to take action with a button-style link. ([Source](https://githu - **Category:** design - **Parent:** core/buttons - **Supports:** anchor, color (background, gradients, text), shadow, spacing (padding), typography (fontSize, lineHeight), ~~alignWide~~, ~~align~~, ~~reusable~~ -- **Attributes:** backgroundColor, gradient, linkTarget, placeholder, rel, text, textAlign, textColor, title, url, width +- **Attributes:** backgroundColor, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width ## Buttons diff --git a/packages/block-library/src/button/block.json b/packages/block-library/src/button/block.json index e337aa857fc175..28c72d4d22ab04 100644 --- a/packages/block-library/src/button/block.json +++ b/packages/block-library/src/button/block.json @@ -9,6 +9,14 @@ "keywords": [ "link" ], "textdomain": "default", "attributes": { + "tagName": { + "type": "string", + "default": "a" + }, + "type": { + "type": "string", + "default": "button" + }, "textAlign": { "type": "string" }, @@ -22,14 +30,14 @@ "title": { "type": "string", "source": "attribute", - "selector": "a", + "selector": "a,button", "attribute": "title", "__experimentalRole": "content" }, "text": { "type": "string", "source": "html", - "selector": "a", + "selector": "a,button", "__experimentalRole": "content" }, "linkTarget": { diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index 4684c05a37f7d6..a6c9c035e5bc1c 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -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 && ( ) } - { isURLSet && ( + { isURLSet && 'a' === TagName && ( ) } - { isSelected && ( isEditingURL || isURLSet ) && ( + { 'a' === TagName && isSelected && ( isEditingURL || isURLSet ) && ( { @@ -268,12 +279,16 @@ function ButtonEdit( props ) { /> - setAttributes( { rel: newRel } ) } - /> + { 'a' === TagName && ( + + setAttributes( { rel: newRel } ) + } + /> + ) } ); diff --git a/packages/block-library/src/button/save.js b/packages/block-library/src/button/save.js index a09ae59059f780..6403c0e5800e67 100644 --- a/packages/block-library/src/button/save.js +++ b/packages/block-library/src/button/save.js @@ -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 (
); diff --git a/test/integration/fixtures/blocks/core__button__squared.json b/test/integration/fixtures/blocks/core__button__squared.json index 8cc161cf5c8767..ef629c91d4be93 100644 --- a/test/integration/fixtures/blocks/core__button__squared.json +++ b/test/integration/fixtures/blocks/core__button__squared.json @@ -3,6 +3,8 @@ "name": "core/button", "isValid": true, "attributes": { + "tagName": "a", + "type": "button", "text": "My button", "style": { "border": { diff --git a/test/integration/fixtures/blocks/core__buttons.html b/test/integration/fixtures/blocks/core__buttons.html index 282f38c7ddb11d..e130ce79258c7f 100644 --- a/test/integration/fixtures/blocks/core__buttons.html +++ b/test/integration/fixtures/blocks/core__buttons.html @@ -7,5 +7,13 @@ + + + + + + +
+ diff --git a/test/integration/fixtures/blocks/core__buttons.json b/test/integration/fixtures/blocks/core__buttons.json index 6b9f8ada321e2f..74d0c140360609 100644 --- a/test/integration/fixtures/blocks/core__buttons.json +++ b/test/integration/fixtures/blocks/core__buttons.json @@ -14,6 +14,8 @@ "name": "core/button", "isValid": true, "attributes": { + "tagName": "a", + "type": "button", "text": "My button 1" }, "innerBlocks": [] @@ -22,9 +24,31 @@ "name": "core/button", "isValid": true, "attributes": { + "tagName": "a", + "type": "button", "text": "My button 2" }, "innerBlocks": [] + }, + { + "name": "core/button", + "isValid": true, + "attributes": { + "tagName": "a", + "type": "button", + "text": "My button 3" + }, + "innerBlocks": [] + }, + { + "name": "core/button", + "isValid": true, + "attributes": { + "tagName": "button", + "type": "button", + "text": "My button 4" + }, + "innerBlocks": [] } ] } diff --git a/test/integration/fixtures/blocks/core__buttons.parsed.json b/test/integration/fixtures/blocks/core__buttons.parsed.json index d15ccdcd2012dd..849adbaf491fcb 100644 --- a/test/integration/fixtures/blocks/core__buttons.parsed.json +++ b/test/integration/fixtures/blocks/core__buttons.parsed.json @@ -26,14 +26,40 @@ "innerContent": [ "\n\t\n\t" ] + }, + { + "blockName": "core/button", + "attrs": { + "tagName": "a" + }, + "innerBlocks": [], + "innerHTML": "\n\t\n\t", + "innerContent": [ + "\n\t\n\t" + ] + }, + { + "blockName": "core/button", + "attrs": { + "tagName": "button" + }, + "innerBlocks": [], + "innerHTML": "\n\t
\n\t", + "innerContent": [ + "\n\t
\n\t" + ] } ], - "innerHTML": "\n
\n\t\n\n\t\n
\n", + "innerHTML": "\n
\n\t\n\n\t\n\n\t\n\n\t\n
\n", "innerContent": [ "\n
\n\t", null, "\n\n\t", null, + "\n\n\t", + null, + "\n\n\t", + null, "\n
\n" ] } diff --git a/test/integration/fixtures/blocks/core__buttons.serialized.html b/test/integration/fixtures/blocks/core__buttons.serialized.html index 7da8f97de51eba..452f3e167e78d8 100644 --- a/test/integration/fixtures/blocks/core__buttons.serialized.html +++ b/test/integration/fixtures/blocks/core__buttons.serialized.html @@ -5,5 +5,13 @@ + + + + + + + +