Skip to content

Commit

Permalink
Draft adoption of width block support for button block
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Aug 30, 2021
1 parent 3e12547 commit e2ed768
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 65 deletions.
3 changes: 3 additions & 0 deletions lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
"core/button": {
"border": {
"customRadius": true
},
"dimensions": {
"customWidth": true
}
},
"core/pullquote": {
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
"fontSize": true,
"__experimentalFontFamily": true
},
"__experimentalDimensions": {
"width": "segmented",
"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"width": true
}
},
"reusable": false,
"spacing": {
"__experimentalSkipSerialization": true,
Expand Down
68 changes: 17 additions & 51 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { useCallback, useState, useRef } from '@wordpress/element';
import {
Button,
ButtonGroup,
KeyboardShortcuts,
PanelBody,
TextControl,
ToolbarButton,
Popover,
Expand All @@ -31,40 +28,12 @@ import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';

const NEW_TAB_REL = 'noreferrer noopener';

function WidthPanel( { selectedWidth, setAttributes } ) {
function handleChange( newWidth ) {
// Check if we are toggling the width off
const width = selectedWidth === newWidth ? undefined : newWidth;

// Update attributes
setAttributes( { width } );
}
/**
* Internal dependencies
*/
import getWidthClassesAndStyles from './get-width-classes-and-styles';

return (
<PanelBody title={ __( 'Width settings' ) }>
<ButtonGroup aria-label={ __( 'Button width' ) }>
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
return (
<Button
key={ widthValue }
isSmall
variant={
widthValue === selectedWidth
? 'primary'
: undefined
}
onClick={ () => handleChange( widthValue ) }
>
{ widthValue }%
</Button>
);
} ) }
</ButtonGroup>
</PanelBody>
);
}
const NEW_TAB_REL = 'noreferrer noopener';

function URLPicker( {
isSelected,
Expand Down Expand Up @@ -158,14 +127,15 @@ function ButtonEdit( props ) {
mergeBlocks,
} = props;
const {
fontSize,
linkTarget,
placeholder,
rel,
style,
text,
url,
width,
} = attributes;

const onSetLinkRel = useCallback(
( value ) => {
setAttributes( { rel: value } );
Expand Down Expand Up @@ -200,18 +170,20 @@ function ButtonEdit( props ) {
const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const widthProps = getWidthClassesAndStyles( attributes );
const ref = useRef();
const blockProps = useBlockProps( { ref } );

const blockProps = useBlockProps( {
ref,
className: classnames( widthProps.className, {
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} ),
style: widthProps.style,
} );

return (
<>
<div
{ ...blockProps }
className={ classnames( blockProps.className, {
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
[ `has-custom-font-size` ]: blockProps.style.fontSize,
} ) }
>
<div { ...blockProps }>
<RichText
aria-label={ __( 'Button text' ) }
placeholder={ placeholder || __( 'Add text…' ) }
Expand Down Expand Up @@ -253,12 +225,6 @@ function ButtonEdit( props ) {
onToggleOpenInNewTab={ onToggleOpenInNewTab }
anchorRef={ ref }
/>
<InspectorControls>
<WidthPanel
selectedWidth={ width }
setAttributes={ setAttributes }
/>
</InspectorControls>
<InspectorControls __experimentalGroup="advanced">
<TextControl
label={ __( 'Link rel' ) }
Expand Down
22 changes: 22 additions & 0 deletions packages/block-library/src/button/get-width-classes-and-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import classnames from 'classnames';

export default function getWidthProps( { style } ) {
const width = style?.dimensions?.width;
const presetWidth = [ 25, 50, 75, 100 ].find(
( preset ) => `${ preset }%` === width
);
const customWidth = width && ! presetWidth ? width : undefined;

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

return {
className: className || undefined,
style: { width: customWidth },
};
}
27 changes: 14 additions & 13 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ import {
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
} from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import getWidthClassesAndStyles from './get-width-classes-and-styles';

export default function save( { attributes, className } ) {
const {
fontSize,
linkTarget,
rel,
style,
text,
title,
url,
width,
} = attributes;
const { fontSize, linkTarget, rel, style, text, title, url } = attributes;

if ( ! text ) {
return null;
Expand Down Expand Up @@ -53,13 +49,18 @@ export default function save( { attributes, className } ) {
// 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,
const widthProps = getWidthClassesAndStyles( attributes );
const wrapperClasses = classnames( className, widthProps.className, {
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} );

return (
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<div
{ ...useBlockProps.save( {
className: wrapperClasses,
style: widthProps.style,
} ) }
>
<RichText.Content
tagName="a"
className={ buttonClasses }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/button/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ $blocks-block__margin: 0.5em;
.wp-block-buttons {

> .wp-block-button,
&.is-content-justification-right > .wp-block-button, {
&.is-content-justification-right > .wp-block-button {
// Added (duplicate) specificity needed to override the default button margin.
&.wp-block-button {
margin-right: 0;
Expand Down

0 comments on commit e2ed768

Please sign in to comment.