@wordpress/block-library
Version:
Block library for the WordPress editor.
204 lines (177 loc) • 4.18 kB
JavaScript
/**
* External dependencies
*/
import classnames from 'classnames';
import { omit, pick } from 'lodash';
/**
* WordPress dependencies
*/
import { G, Path, SVG } from '@wordpress/components';
import { __, _x } from '@wordpress/i18n';
import {
RichText,
getColorClassName,
} from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import edit from './edit';
const blockAttributes = {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href',
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title',
},
text: {
type: 'string',
source: 'html',
selector: 'a',
},
backgroundColor: {
type: 'string',
},
textColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
};
export const name = 'core/button';
const colorsMigration = ( attributes ) => {
return omit( {
...attributes,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.color && '#' === attributes.color[ 0 ] ? attributes.color : undefined,
}, [ 'color', 'textColor' ] );
};
export const settings = {
title: __( 'Button' ),
description: __( 'Prompt visitors to take action with a button-style link.' ),
icon: <SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><Path fill="none" d="M0 0h24v24H0V0z" /><G><Path d="M19 6H5c-1.1 0-2 .9-2 2v8c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2zm0 10H5V8h14v8z" /></G></SVG>,
category: 'layout',
keywords: [ __( 'link' ) ],
attributes: blockAttributes,
supports: {
align: true,
alignWide: false,
},
styles: [
{ name: 'default', label: _x( 'Default', 'block style' ), isDefault: true },
{ name: 'outline', label: __( 'Outline' ) },
{ name: 'squared', label: _x( 'Squared', 'block style' ) },
],
edit,
save( { attributes } ) {
const {
url,
text,
title,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
} = attributes;
const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );
const buttonClasses = classnames( 'wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[ textClass ]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );
const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};
return (
<div>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
deprecated: [ {
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},
save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;
const buttonStyle = {
backgroundColor: color,
color: textColor,
};
const linkClass = 'wp-block-button__link';
return (
<div className={ `align${ align }` }>
<RichText.Content
tagName="a"
className={ linkClass }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
{
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
align: {
type: 'string',
default: 'none',
},
},
save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;
return (
<div className={ `align${ align }` } style={ { backgroundColor: color } }>
<RichText.Content
tagName="a"
href={ url }
title={ title }
style={ { color: textColor } }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
],
};