Skip to content

Commit

Permalink
[Button Block]: Add padding block support (#31774)
Browse files Browse the repository at this point in the history
* Add a utility for getting the spacing inline styles
* Add padding support to the Button block
  • Loading branch information
stacimc authored Jul 29, 2021
1 parent 1c733c0 commit 4c59c1b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import './layout';
export { useCustomSides } from './spacing';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
export { getSpacingClassesAndStyles } from './use-spacing-props';
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ import './font-size';

export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
export { getSpacingClassesAndStyles } from './use-spacing-props';
28 changes: 28 additions & 0 deletions packages/block-editor/src/hooks/use-spacing-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import { getInlineStyles } from './style';

// This utility is intended to assist where the serialization of the spacing
// block support is being skipped for a block but the spacing related CSS
// styles still need to be generated so they can be applied to inner elements.

/**
* Provides the CSS class names and inline styles for a block's spacing support
* attributes.
*
* @param {Object} attributes Block attributes.
*
* @return {Object} Spacing block support derived CSS classes & styles.
*/
export function getSpacingClassesAndStyles( attributes ) {
const { style } = attributes;

// Collect inline styles for spacing.
const spacingStyles = style?.spacing || {};
const styleProp = getInlineStyles( { spacing: spacingStyles } );

return {
style: styleProp,
};
}
1 change: 1 addition & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
getColorClassesAndStyles as __experimentalGetColorClassesAndStyles,
useColorProps as __experimentalUseColorProps,
useCustomSides as __experimentalUseCustomSides,
getSpacingClassesAndStyles as __experimentalGetSpacingClassesAndStyles,
} from './hooks';
export * from './components';
export * from './utils';
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"__experimentalFontFamily": true
},
"reusable": false,
"spacing": {
"__experimentalSkipSerialization": true,
"padding": true
},
"__experimentalBorder": {
"radius": true,
"__experimentalSkipSerialization": true
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
Expand Down Expand Up @@ -199,6 +200,7 @@ function ButtonEdit( props ) {

const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const ref = useRef();
const blockProps = useBlockProps( { ref } );

Expand Down Expand Up @@ -231,6 +233,7 @@ function ButtonEdit( props ) {
style={ {
...borderProps.style,
...colorProps.style,
...spacingProps.style,
} }
onSplit={ ( value ) =>
createBlock( 'core/button', {
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useBlockProps,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
} from '@wordpress/block-editor';

export default function save( { attributes, className } ) {
Expand All @@ -31,6 +32,7 @@ export default function save( { attributes, className } ) {

const borderProps = getBorderClassesAndStyles( attributes );
const colorProps = getColorClassesAndStyles( attributes );
const spacingProps = getSpacingClassesAndStyles( attributes );
const buttonClasses = classnames(
'wp-block-button__link',
colorProps.className,
Expand All @@ -44,6 +46,7 @@ export default function save( { attributes, className } ) {
const buttonStyle = {
...borderProps.style,
...colorProps.style,
...spacingProps.style,
};

// The use of a `title` attribute here is soft-deprecated, but still applied
Expand Down

0 comments on commit 4c59c1b

Please sign in to comment.