Skip to content
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

Button Block: Use hook based border support #30194

Merged
merged 9 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
"border": {
"customRadius": false
}
},
"core/button": {
"border": {
"customRadius": true
}
}
}
}
10 changes: 4 additions & 6 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
"placeholder": {
"type": "string"
},
"borderRadius": {
"type": "number"
},
"style": {
"type": "object"
},
"backgroundColor": {
"type": "string"
},
Expand All @@ -65,6 +59,10 @@
},
"fontSize": true,
"reusable": false,
"__experimentalBorder": {
"radius": true,
"__experimentalSkipSerialization": true
},
"__experimentalFontFamily": true,
"__experimentalSelector": ".wp-block-button__link"
},
Expand Down
131 changes: 125 additions & 6 deletions packages/block-library/src/button/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@ import {
useBlockProps,
__experimentalGetGradientClass,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import getColorAndStyleProps from './color-props';

const migrateBorderRadius = ( attributes ) => {
const { borderRadius, ...newAttributes } = attributes;

if ( ! borderRadius && borderRadius !== 0 ) {
return newAttributes;
}

return {
...newAttributes,
style: {
...newAttributes.style,
border: { radius: borderRadius },
},
};
};

const migrateCustomColorsAndGradients = ( attributes ) => {
if (
! attributes.customTextColor &&
Expand Down Expand Up @@ -180,6 +197,102 @@ const deprecated = [
</div>
);
},
migrate: migrateBorderRadius,
},
{
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true,
},
reusable: false,
__experimentalSelector: '.wp-block-button__link',
},
attributes: {
...blockAttributes,
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target',
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel',
},
placeholder: {
type: 'string',
},
borderRadius: {
type: 'number',
},
backgroundColor: {
type: 'string',
},
textColor: {
type: 'string',
},
gradient: {
type: 'string',
},
style: {
type: 'object',
},
width: {
type: 'number',
},
},
save( { attributes, className } ) {
const {
borderRadius,
linkTarget,
rel,
text,
title,
url,
width,
} = attributes;
const colorProps = getColorAndStyleProps( attributes );
const buttonClasses = classnames(
'wp-block-button__link',
colorProps.className,
{
'no-border-radius': borderRadius === 0,
}
);
const buttonStyle = {
borderRadius: borderRadius ? borderRadius + 'px' : undefined,
...colorProps.style,
};

// The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.

const wrapperClasses = classnames( className, {
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
} );

return (
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
target={ linkTarget }
rel={ rel }
/>
</div>
);
},
migrate: migrateBorderRadius,
},
{
supports: {
Expand Down Expand Up @@ -249,6 +362,7 @@ const deprecated = [
/>
);
},
migrate: migrateBorderRadius,
},
{
supports: {
Expand Down Expand Up @@ -299,7 +413,10 @@ const deprecated = [
!! attributes.customTextColor ||
!! attributes.customBackgroundColor ||
!! attributes.customGradient,
migrate: migrateCustomColorsAndGradients,
migrate: compose(
migrateBorderRadius,
migrateCustomColorsAndGradients
),
save( { attributes } ) {
const {
backgroundColor,
Expand Down Expand Up @@ -413,11 +530,13 @@ const deprecated = [
.replace( /is-style-squared[\s]?/, '' )
.trim();
}
return migrateCustomColorsAndGradients( {
...attributes,
className: newClassName ? newClassName : undefined,
borderRadius: 0,
} );
return migrateBorderRadius(
migrateCustomColorsAndGradients( {
...attributes,
className: newClassName ? newClassName : undefined,
borderRadius: 0,
} )
);
},
save( { attributes } ) {
const {
Expand Down
38 changes: 2 additions & 36 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ButtonGroup,
KeyboardShortcuts,
PanelBody,
RangeControl,
TextControl,
ToolbarButton,
Popover,
Expand All @@ -37,39 +36,9 @@ import { createBlock } from '@wordpress/blocks';
import getColorAndStyleProps from './color-props';

const NEW_TAB_REL = 'noreferrer noopener';
const MIN_BORDER_RADIUS_VALUE = 0;
const MAX_BORDER_RADIUS_VALUE = 50;
const INITIAL_BORDER_RADIUS_POSITION = 5;

const EMPTY_ARRAY = [];

function BorderPanel( { borderRadius = '', setAttributes } ) {
const initialBorderRadius = borderRadius;
const setBorderRadius = useCallback(
( newBorderRadius ) => {
if ( newBorderRadius === undefined )
setAttributes( {
borderRadius: initialBorderRadius,
} );
else setAttributes( { borderRadius: newBorderRadius } );
},
[ setAttributes ]
);
return (
<PanelBody title={ __( 'Border settings' ) }>
<RangeControl
value={ borderRadius }
label={ __( 'Border radius' ) }
min={ MIN_BORDER_RADIUS_VALUE }
max={ MAX_BORDER_RADIUS_VALUE }
initialPosition={ INITIAL_BORDER_RADIUS_POSITION }
allowReset
onChange={ setBorderRadius }
/>
</PanelBody>
);
}

function WidthPanel( { selectedWidth, setAttributes } ) {
function handleChange( newWidth ) {
// Check if we are toggling the width off
Expand Down Expand Up @@ -191,10 +160,10 @@ function ButtonEdit( props ) {
mergeBlocks,
} = props;
const {
borderRadius,
linkTarget,
placeholder,
rel,
style,
text,
url,
width,
Expand Down Expand Up @@ -231,6 +200,7 @@ function ButtonEdit( props ) {
setAttributes( { text: newText.replace( /<\/?a[^>]*>/g, '' ) } );
};

const borderRadius = style?.border?.radius;
const colorProps = getColorAndStyleProps( attributes, colors, true );
const ref = useRef();
const blockProps = useBlockProps( { ref } );
Expand Down Expand Up @@ -284,10 +254,6 @@ function ButtonEdit( props ) {
anchorRef={ ref }
/>
<InspectorControls>
<BorderPanel
borderRadius={ borderRadius }
setAttributes={ setAttributes }
/>
<WidthPanel
selectedWidth={ width }
setAttributes={ setAttributes }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import getColorAndStyleProps from './color-props';

export default function save( { attributes, className } ) {
const {
borderRadius,
fontSize,
linkTarget,
rel,
Expand All @@ -30,6 +29,7 @@ export default function save( { attributes, className } ) {
return null;
}

const borderRadius = style?.border?.radius;
const colorProps = getColorAndStyleProps( attributes );
const buttonClasses = classnames(
'wp-block-button__link',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:button {"borderRadius":25} -->
<div class="wp-block-button"><a class="wp-block-button__link" style="border-radius:25px">My button</a></div>
<!-- /wp:button --></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"clientId": "_clientId_0",
"name": "core/button",
"isValid": true,
"attributes": {
"text": "My button",
"style": {
"border": {
"radius": 25
}
}
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" style=\"border-radius:25px\">My button</a></div>"
},
{
"clientId": "_clientId_1",
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": ""
},
"innerBlocks": [],
"originalContent": "</div>"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"blockName": "core/button",
"attrs": {
"borderRadius": 25
},
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" style=\"border-radius:25px\">My button</a></div>\n",
"innerContent": [
"\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" style=\"border-radius:25px\">My button</a></div>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "</div>",
"innerContent": [ "</div>" ]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:button {"style":{"border":{"radius":25}}} -->
<div class="wp-block-button"><a class="wp-block-button__link" style="border-radius:25px">My button</a></div>
<!-- /wp:button -->


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"borderRadius":0,"style":{"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<!-- wp:button {"style":{"border":{"radius":0},"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link no-border-radius has-text-color has-background" style="background-color:#aa5a20;color:#1b9b6c">My button</a></div>
<!-- /wp:button -->
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"isValid": true,
"attributes": {
"text": "My button",
"borderRadius": 0,
"style": {
"border": {
"radius": 0
},
"color": {
"text": "#1b9b6c",
"background": "#aa5a20"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
{
"blockName": "core/button",
"attrs": {
"borderRadius": 0,
"style": {
"border": {
"radius": 0
},
"color": {
"text": "#1b9b6c",
"background": "#aa5a20"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"borderRadius":0,"style":{"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<!-- wp:button {"style":{"border":{"radius":0},"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link has-text-color has-background no-border-radius" style="background-color:#aa5a20;color:#1b9b6c">My button</a></div>
<!-- /wp:button -->
Loading