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

Verse Block: Add padding control #27579

Closed
wants to merge 9 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ These are the current color properties supported by blocks:
| --- | --- |
| Cover | Yes |
| Group | Yes |
| Verse | Yes |

#### Typography Properties

Expand Down
17 changes: 16 additions & 1 deletion packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { Platform } from '@wordpress/element';
import { getBlockSupport } from '@wordpress/blocks';
import { getBlockSupport, getBlockType } from '@wordpress/blocks';
import { __experimentalBoxControl as BoxControl } from '@wordpress/components';

/**
Expand Down Expand Up @@ -69,13 +69,28 @@ export function PaddingEdit( props ) {
} );
};

const onReset = ( initialValues ) => {
const blockType = getBlockType( blockName );
let defaultSettings = blockType?.attributes?.style?.default;
if ( ! defaultSettings ) {
defaultSettings = initialValues;
}

setAttributes( {
style: cleanEmptyObject( defaultSettings ),
} );

return true;
};

return Platform.select( {
web: (
<>
<BoxControl
values={ style?.spacing?.padding }
onChange={ onChange }
onChangeShowVisualizer={ onChangeShowVisualizer }
onReset={ onReset }
label={ __( 'Padding' ) }
units={ units }
/>
Expand Down
17 changes: 16 additions & 1 deletion packages/block-library/src/verse/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@
"default": "",
"__unstablePreserveWhiteSpace": true
},
"style": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting defaults in this way will make sure they are included as inline styles. Doing that will make theme.json not work as expected because theme.json does not produce inline styles but targets using a class. Also including inline styles even if the user does not change anything does not seems like a good idea. As all the verse blocks would have this markup instead of just having a single class with the style rule we would repeat it every time.

Allowing blocks to set default style properties may be a good idea. An equivalent to theme.json that blocks can use to target their own selector. In the future, I guess we should probably consider this. But I guess for now these defaults should just be added as CSS to the style.scss file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the documentation refers to something that at least for now should not be used. I will propose a PR to remove that from the documentation. Thank you for bringing our attention to this documentation isse.

"default": {
"spacing": {
"padding": {
"top": "1em",
"right": "1em",
"bottom": "1em",
"left": "1em"
}
}
}
},
"textAlign": {
"type": "string"
}
},
"supports": {
"anchor": true,
"__experimentalFontFamily": true,
"fontSize": true
"fontSize": true,
"spacing": {
"padding": true
}
},
"style": "wp-block-verse",
"editorStyle": "wp-block-verse-editor"
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/verse/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
AlignmentToolbar,
useBlockProps,
} from '@wordpress/block-editor';
import { __experimentalBoxControl as BoxControl } from '@wordpress/components';
const { __Visualizer: BoxControlVisualizer } = BoxControl;

export default function VerseEdit( {
attributes,
Expand All @@ -36,6 +38,10 @@ export default function VerseEdit( {
} }
/>
</BlockControls>
<BoxControlVisualizer
values={ attributes.style?.spacing?.padding }
showValues={ attributes.style?.visualizers?.padding }
/>
<RichText
tagName="pre"
identifier="content"
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/verse/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pre.wp-block-verse {
color: $gray-900;
padding: 1em;
}
7 changes: 5 additions & 2 deletions packages/components/src/box-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function BoxControl( {
inputProps = defaultInputProps,
onChange = noop,
onChangeShowVisualizer = noop,
onReset = noop,
label = __( 'Box Control' ),
values: valuesProp,
units,
Expand Down Expand Up @@ -93,8 +94,10 @@ export default function BoxControl( {

const handleOnReset = () => {
const initialValues = DEFAULT_VALUES;

onChange( initialValues );
const success = onReset( initialValues );
if ( ! success ) {
onChange( initialValues );
}
setValues( initialValues );
setIsDirty( false );
};
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/box-control/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ function mode( arr ) {
* @return {string} A value + unit for the 'all' input.
*/
export function getAllValue( values = {} ) {
const parsedValues = Object.values( values ).map( parseUnit );
const parsedValues = Object.values( values ).map( ( value ) =>
parseUnit( value )
);

const allValues = parsedValues.map( ( value ) => value[ 0 ] );
const allUnits = parsedValues.map( ( value ) => value[ 1 ] );
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e-tests/fixtures/blocks/core__verse.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:core/verse -->
<pre class="wp-block-verse">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:core/verse -->
<!-- wp:verse -->
<pre class="wp-block-verse" style="padding-bottom:20px;padding-left:20px;padding-right:20px;padding-top:20px">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->
14 changes: 12 additions & 2 deletions packages/e2e-tests/fixtures/blocks/core__verse.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
"name": "core/verse",
"isValid": true,
"attributes": {
"content": "A <em>verse</em>…<br>And more!"
"content": "A <em>verse</em>…<br>And more!",
"style": {
"spacing": {
"padding": {
"top": 20,
"right": 20,
"bottom": 20,
"left": 20
}
}
}
},
"innerBlocks": [],
"originalContent": "<pre class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>"
"originalContent": "<pre class=\"wp-block-verse\" style=\"padding-bottom:20px;padding-left:20px;padding-right:20px;padding-top:20px\">A <em>verse</em>…<br>And more!</pre>"
}
]
4 changes: 2 additions & 2 deletions packages/e2e-tests/fixtures/blocks/core__verse.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"blockName": "core/verse",
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<pre class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>\n",
"innerHTML": "\n<pre class=\"wp-block-verse\" style=\"padding-bottom:20px;padding-left:20px;padding-right:20px;padding-top:20px\">A <em>verse</em>…<br>And more!</pre>\n",
"innerContent": [
"\n<pre class=\"wp-block-verse\">A <em>verse</em>…<br>And more!</pre>\n"
"\n<pre class=\"wp-block-verse\" style=\"padding-bottom:20px;padding-left:20px;padding-right:20px;padding-top:20px\">A <em>verse</em>…<br>And more!</pre>\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:verse -->
<pre class="wp-block-verse">A <em>verse</em>…<br>And more!</pre>
<pre class="wp-block-verse" style="padding-bottom:20px;padding-left:20px;padding-right:20px;padding-top:20px">A <em>verse</em>…<br>And more!</pre>
<!-- /wp:verse -->