Skip to content

Commit

Permalink
Add FontSizePicker for button block for custom width size
Browse files Browse the repository at this point in the history
  • Loading branch information
prasadkarmalkar committed Jun 4, 2024
1 parent fd7becb commit db4da66
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
53 changes: 31 additions & 22 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import { unlock } from '../lock-unlock';
import { __ } from '@wordpress/i18n';
import { useEffect, useState, useRef, useMemo } from '@wordpress/element';
import {
Button,
ButtonGroup,
PanelBody,
TextControl,
ToolbarButton,
Popover,
FontSizePicker,
} from '@wordpress/components';
import {
AlignmentControl,
Expand Down Expand Up @@ -125,24 +124,34 @@ function WidthPanel( { selectedWidth, setAttributes } ) {

return (
<PanelBody title={ __( 'Settings' ) }>
<ButtonGroup aria-label={ __( 'Button width' ) }>
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
return (
<Button
key={ widthValue }
size="small"
variant={
widthValue === selectedWidth
? 'primary'
: undefined
}
onClick={ () => handleChange( widthValue ) }
>
{ widthValue }%
</Button>
);
} ) }
</ButtonGroup>
<FontSizePicker
fontSizes={ [
{
name: '25%',
size: '25%',
slug: '25',
},
{
name: '50%',
size: '50%',
slug: '50',
},
{
name: '75%',
size: '75%',
slug: '75',
},
{
name: '100%',
size: '100%',
slug: '100',
},
] }
withSlider
units={ [ '%', 'px', 'rem' ] }
onChange={ ( widthValue ) => handleChange( widthValue ) }
value={ selectedWidth }
/>
</PanelBody>
);
}
Expand Down Expand Up @@ -262,10 +271,10 @@ function ButtonEdit( props ) {
<div
{ ...blockProps }
className={ clsx( blockProps.className, {
[ `has-custom-width wp-block-button__width-${ width }` ]:
width,
[ `has-custom-width` ]: width,
[ `has-custom-font-size` ]: blockProps.style.fontSize,
} ) }
style={ { width } }
>
<RichText
ref={ mergedRef }
Expand Down
9 changes: 7 additions & 2 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ export default function save( { attributes, className } ) {
// A title will no longer be assigned for new or updated button block links.

const wrapperClasses = clsx( className, {
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
[ `has-custom-width` ]: width,
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} );

return (
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<div
{ ...useBlockProps.save( {
className: wrapperClasses,
style: { width },
} ) }
>
<RichText.Content
tagName={ TagName }
type={ isButtonTag ? buttonType : null }
Expand Down
17 changes: 0 additions & 17 deletions packages/block-library/src/button/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,6 @@ $blocks-block__margin: 0.5em;
font-size: inherit;
}
}

&.wp-block-button__width-25 {
width: calc(25% - (var(--wp--style--block-gap, #{$blocks-block__margin}) * 0.75));
}

&.wp-block-button__width-50 {
width: calc(50% - (var(--wp--style--block-gap, #{$blocks-block__margin}) * 0.5));
}

&.wp-block-button__width-75 {
width: calc(75% - (var(--wp--style--block-gap, #{$blocks-block__margin}) * 0.25));
}

&.wp-block-button__width-100 {
width: 100%;
flex-basis: 100%;
}
}

// For vertical buttons, gap is not factored into width calculations.
Expand Down

0 comments on commit db4da66

Please sign in to comment.