diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 2692b316d4db17..f96567dad0b12e 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -26,7 +26,6 @@ function gutenberg_register_dimensions_support( $block_type ) { } $has_dimensions_support = gutenberg_block_has_support( $block_type, array( '__experimentalDimensions' ), false ); - // Future block supports such as height & width will be added here. if ( $has_dimensions_support ) { $block_type->attributes['style'] = array( @@ -44,14 +43,24 @@ function gutenberg_register_dimensions_support( $block_type ) { * * @return array Block dimensions CSS classes and inline styles. */ -function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable +function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { if ( gutenberg_skip_dimensions_serialization( $block_type ) ) { return array(); } $styles = array(); - // Height support to be added in near future. + // Height. + $has_height_support = gutenberg_block_has_support( $block_type, array( '__experimentalDimensions', 'height' ), false ); + + if ( $has_height_support ) { + $height_value = _wp_array_get( $block_attributes, array( 'style', 'dimensions', 'height' ), null ); + + if ( null !== $height_value ) { + $styles[] = sprintf( 'height: %s;', $height_value ); + } + } + // Width support to be added in near future. return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); @@ -63,10 +72,11 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { * * @param WP_Block_type $block_type Block type. * - * @return boolean Whether to serialize spacing support styles & classes. + * @return boolean Whether to serialize dimensions support styles & classes. */ function gutenberg_skip_dimensions_serialization( $block_type ) { $dimensions_support = _wp_array_get( $block_type->supports, array( '__experimentalDimensions' ), false ); + return is_array( $dimensions_support ) && array_key_exists( '__experimentalSkipSerialization', $dimensions_support ) && $dimensions_support['__experimentalSkipSerialization']; diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 4ae644e25b15bd..bd97330e070c80 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -60,6 +60,9 @@ class WP_Theme_JSON_Gutenberg { 'gradient' => null, 'text' => null, ), + 'dimensions' => array( + 'height' => null, + ), 'filter' => array( 'duotone' => null, ), @@ -99,6 +102,9 @@ class WP_Theme_JSON_Gutenberg { 'text' => null, ), 'custom' => null, + 'dimensions' => array( + 'height' => null, + ), 'layout' => array( 'contentSize' => null, 'wideSize' => null, @@ -232,6 +238,7 @@ class WP_Theme_JSON_Gutenberg { 'font-size' => array( 'typography', 'fontSize' ), 'font-style' => array( 'typography', 'fontStyle' ), 'font-weight' => array( 'typography', 'fontWeight' ), + 'height' => array( 'dimensions', 'height' ), 'letter-spacing' => array( 'typography', 'letterSpacing' ), 'line-height' => array( 'typography', 'lineHeight' ), 'margin' => array( 'spacing', 'margin' ), diff --git a/lib/theme.json b/lib/theme.json index 514af202649c7b..2d0ba992c8bc8c 100644 --- a/lib/theme.json +++ b/lib/theme.json @@ -212,6 +212,9 @@ } ] }, + "dimensions": { + "height": false + }, "spacing": { "blockGap": null, "customMargin": false, diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index ecfb0fa164c345..bcf738d19975cf 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -17,6 +17,13 @@ import { resetGap, useIsGapDisabled, } from './gap'; +import { + HeightEdit, + hasHeightSupport, + hasHeightValue, + resetHeight, + useIsHeightDisabled, +} from './height'; import { MarginEdit, hasMarginSupport, @@ -32,6 +39,7 @@ import { useIsPaddingDisabled, } from './padding'; +export const DIMENSIONS_SUPPORT_KEY = '__experimentalDimensions'; export const SPACING_SUPPORT_KEY = 'spacing'; export const ALL_SIDES = [ 'top', 'right', 'bottom', 'left' ]; export const AXIAL_SIDES = [ 'vertical', 'horizontal' ]; @@ -40,13 +48,13 @@ export const AXIAL_SIDES = [ 'vertical', 'horizontal' ]; * Inspector controls for dimensions support. * * @param {Object} props Block props. - * - * @return {WPElement} Inspector controls for spacing support features. + * @return {WPElement} Inspector controls for dimensions support features. */ export function DimensionsPanel( props ) { const isGapDisabled = useIsGapDisabled( props ); const isPaddingDisabled = useIsPaddingDisabled( props ); const isMarginDisabled = useIsMarginDisabled( props ); + const isHeightDisabled = useIsHeightDisabled( props ); const isDisabled = useIsDimensionsDisabled( props ); const isSupported = hasDimensionsSupport( props.name ); @@ -54,17 +62,24 @@ export function DimensionsPanel( props ) { return null; } + const defaultDimensionsControls = getBlockSupport( props.name, [ + DIMENSIONS_SUPPORT_KEY, + '__experimentalDefaultControls', + ] ); + const defaultSpacingControls = getBlockSupport( props.name, [ SPACING_SUPPORT_KEY, '__experimentalDefaultControls', ] ); - const createResetAllFilter = ( attribute ) => ( newAttributes ) => ( { + const createResetAllFilter = ( attribute, featureSet ) => ( + newAttributes + ) => ( { ...newAttributes, style: { ...newAttributes.style, - spacing: { - ...newAttributes.style?.spacing, + [ featureSet ]: { + ...newAttributes.style?.[ featureSet ], [ attribute ]: undefined, }, }, @@ -72,12 +87,31 @@ export function DimensionsPanel( props ) { return ( + { ! isHeightDisabled && ( + hasHeightValue( props ) } + label={ __( 'Height' ) } + onDeselect={ () => resetHeight( props ) } + resetAllFilter={ createResetAllFilter( + 'height', + 'dimensions' + ) } + isShownByDefault={ defaultDimensionsControls?.height } + panelId={ props.clientId } + > + + + ) } { ! isPaddingDisabled && ( hasPaddingValue( props ) } label={ __( 'Padding' ) } onDeselect={ () => resetPadding( props ) } - resetAllFilter={ createResetAllFilter( 'padding' ) } + resetAllFilter={ createResetAllFilter( + 'padding', + 'spacing' + ) } isShownByDefault={ defaultSpacingControls?.padding } panelId={ props.clientId } > @@ -89,7 +123,10 @@ export function DimensionsPanel( props ) { hasValue={ () => hasMarginValue( props ) } label={ __( 'Margin' ) } onDeselect={ () => resetMargin( props ) } - resetAllFilter={ createResetAllFilter( 'margin' ) } + resetAllFilter={ createResetAllFilter( + 'margin', + 'spacing' + ) } isShownByDefault={ defaultSpacingControls?.margin } panelId={ props.clientId } > @@ -101,7 +138,10 @@ export function DimensionsPanel( props ) { hasValue={ () => hasGapValue( props ) } label={ __( 'Block spacing' ) } onDeselect={ () => resetGap( props ) } - resetAllFilter={ createResetAllFilter( 'blockGap' ) } + resetAllFilter={ createResetAllFilter( + 'blockGap', + 'spacing' + ) } isShownByDefault={ defaultSpacingControls?.blockGap } panelId={ props.clientId } > @@ -126,6 +166,7 @@ export function hasDimensionsSupport( blockName ) { return ( hasGapSupport( blockName ) || + hasHeightSupport( blockName ) || hasPaddingSupport( blockName ) || hasMarginSupport( blockName ) ); @@ -135,15 +176,15 @@ export function hasDimensionsSupport( blockName ) { * Determines whether dimensions support has been disabled. * * @param {Object} props Block properties. - * - * @return {boolean} If spacing support is completely disabled. + * @return {boolean} If dimensions support is completely disabled. */ const useIsDimensionsDisabled = ( props = {} ) => { const gapDisabled = useIsGapDisabled( props ); + const heightDisabled = useIsHeightDisabled( props ); const paddingDisabled = useIsPaddingDisabled( props ); const marginDisabled = useIsMarginDisabled( props ); - return gapDisabled && paddingDisabled && marginDisabled; + return gapDisabled && heightDisabled && paddingDisabled && marginDisabled; }; /** diff --git a/packages/block-editor/src/hooks/dimensions.scss b/packages/block-editor/src/hooks/dimensions.scss new file mode 100644 index 00000000000000..e67e16ad92295c --- /dev/null +++ b/packages/block-editor/src/hooks/dimensions.scss @@ -0,0 +1,5 @@ +.dimensions-block-support-panel { + .single-column { + grid-column: span 1; + } +} diff --git a/packages/block-editor/src/hooks/height.js b/packages/block-editor/src/hooks/height.js new file mode 100644 index 00000000000000..a2c80bc4eea9b9 --- /dev/null +++ b/packages/block-editor/src/hooks/height.js @@ -0,0 +1,121 @@ +/** + * WordPress dependencies + */ +import { getBlockSupport } from '@wordpress/blocks'; +import { + __experimentalUseCustomUnits as useCustomUnits, + __experimentalUnitControl as UnitControl, +} from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import useSetting from '../components/use-setting'; +import { DIMENSIONS_SUPPORT_KEY } from './dimensions'; +import { cleanEmptyObject } from './utils'; + +/** + * Determines if there is height support. + * + * @param {string|Object} blockType Block name or Block Type object. + * @return {boolean} Whether there is support. + */ +export function hasHeightSupport( blockType ) { + const support = getBlockSupport( blockType, DIMENSIONS_SUPPORT_KEY ); + return !! ( true === support || support?.height ); +} + +/** + * Checks if there is a current value in the height block support attributes. + * + * @param {Object} props Block props. + * @return {boolean} Whether or not the block has a height value set. + */ +export function hasHeightValue( props ) { + return props.attributes.style?.dimensions?.height !== undefined; +} + +/** + * Resets the height block support attributes. This can be used when + * disabling the height support controls for a block via a progressive + * discovery panel. + * + * @param {Object} props Block props. + * @param {Object} props.attributes Block's attributes. + * @param {Object} props.setAttributes Function to set block's attributes. + */ +export function resetHeight( { attributes = {}, setAttributes } ) { + const { style } = attributes; + + setAttributes( { + style: cleanEmptyObject( { + ...style, + dimensions: { + ...style?.dimensions, + height: undefined, + }, + } ), + } ); +} + +/** + * Custom hook that checks if height controls have been disabled. + * + * @param {string} name The name of the block. + * @return {boolean} Whether height control is disabled. + */ +export function useIsHeightDisabled( { name: blockName } = {} ) { + const isDisabled = ! useSetting( 'dimensions.height' ); + return ! hasHeightSupport( blockName ) || isDisabled; +} + +/** + * Inspector control panel containing the height related configuration. + * + * @param {Object} props Block props. + * @return {WPElement} Edit component for height. + */ +export function HeightEdit( props ) { + const { + attributes: { style }, + setAttributes, + } = props; + + const units = useCustomUnits( { + availableUnits: useSetting( 'dimensions.units' ) || [ + '%', + 'px', + 'em', + 'rem', + 'vh', + 'vw', + ], + } ); + + if ( useIsHeightDisabled( props ) ) { + return null; + } + + const onChange = ( next ) => { + const newStyle = { + ...style, + dimensions: { + ...style?.dimensions, + height: next, + }, + }; + + setAttributes( { style: cleanEmptyObject( newStyle ) } ); + }; + + return ( + + ); +} diff --git a/packages/block-editor/src/hooks/style.js b/packages/block-editor/src/hooks/style.js index 8b0fbd6820e27b..e11348e69524e5 100644 --- a/packages/block-editor/src/hooks/style.js +++ b/packages/block-editor/src/hooks/style.js @@ -39,13 +39,18 @@ import { TYPOGRAPHY_SUPPORT_KEY, TYPOGRAPHY_SUPPORT_KEYS, } from './typography'; -import { SPACING_SUPPORT_KEY, DimensionsPanel } from './dimensions'; +import { + DIMENSIONS_SUPPORT_KEY, + SPACING_SUPPORT_KEY, + DimensionsPanel, +} from './dimensions'; import useDisplayBlockControls from '../components/use-display-block-controls'; const styleSupportKeys = [ ...TYPOGRAPHY_SUPPORT_KEYS, BORDER_SUPPORT_KEY, COLOR_SUPPORT_KEY, + DIMENSIONS_SUPPORT_KEY, SPACING_SUPPORT_KEY, ]; @@ -159,8 +164,11 @@ const skipSerializationPathsEdit = { [ `${ TYPOGRAPHY_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ TYPOGRAPHY_SUPPORT_KEY, ], + [ `${ DIMENSIONS_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ + DIMENSIONS_SUPPORT_KEY, + ], [ `${ SPACING_SUPPORT_KEY }.__experimentalSkipSerialization` ]: [ - 'spacing', + SPACING_SUPPORT_KEY, ], }; diff --git a/packages/block-editor/src/hooks/test/style.js b/packages/block-editor/src/hooks/test/style.js index e8c3264eeba6b1..aa177149e2625a 100644 --- a/packages/block-editor/src/hooks/test/style.js +++ b/packages/block-editor/src/hooks/test/style.js @@ -23,6 +23,9 @@ describe( 'getInlineStyles', () => { style: 'dotted', color: '#21759b', }, + dimensions: { + height: '500px', + }, spacing: { blockGap: '1em', padding: { top: '10px' }, @@ -39,6 +42,7 @@ describe( 'getInlineStyles', () => { color: 'red', lineHeight: 1.5, fontSize: 10, + height: '500px', marginBottom: '15px', paddingTop: '10px', } ); diff --git a/packages/block-editor/src/style.scss b/packages/block-editor/src/style.scss index de72069bcde141..826444fe652e0e 100644 --- a/packages/block-editor/src/style.scss +++ b/packages/block-editor/src/style.scss @@ -56,6 +56,7 @@ @import "./hooks/layout.scss"; @import "./hooks/border.scss"; @import "./hooks/typography.scss"; +@import "./hooks/dimensions.scss"; @import "./components/block-toolbar/style.scss"; @import "./components/inserter/style.scss"; diff --git a/packages/blocks/src/api/constants.js b/packages/blocks/src/api/constants.js index 674caea6af4c99..170d3a7e7cdf04 100644 --- a/packages/blocks/src/api/constants.js +++ b/packages/blocks/src/api/constants.js @@ -76,6 +76,10 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = { value: [ 'typography', 'fontWeight' ], support: [ 'typography', '__experimentalFontWeight' ], }, + height: { + value: [ 'dimensions', 'height' ], + support: [ '__experimentalDimensions', 'height' ], + }, lineHeight: { value: [ 'typography', 'lineHeight' ], support: [ 'typography', 'lineHeight' ], diff --git a/packages/edit-site/src/components/global-styles/dimensions-panel.js b/packages/edit-site/src/components/global-styles/dimensions-panel.js index 6a17ef692c80ae..fdc6bd0be7835d 100644 --- a/packages/edit-site/src/components/global-styles/dimensions-panel.js +++ b/packages/edit-site/src/components/global-styles/dimensions-panel.js @@ -6,8 +6,8 @@ import { __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalBoxControl as BoxControl, - __experimentalUnitControl as UnitControl, __experimentalUseCustomUnits as useCustomUnits, + __experimentalUnitControl as UnitControl, } from '@wordpress/components'; import { __experimentalUseCustomSides as useCustomSides } from '@wordpress/block-editor'; @@ -22,8 +22,9 @@ export function useHasDimensionsPanel( name ) { const hasPadding = useHasPadding( name ); const hasMargin = useHasMargin( name ); const hasGap = useHasGap( name ); + const hasHeight = useHasHeight( name ); - return hasPadding || hasMargin || hasGap; + return hasPadding || hasMargin || hasGap || hasHeight; } function useHasPadding( name ) { @@ -47,6 +48,13 @@ function useHasGap( name ) { return settings && supports.includes( '--wp--style--block-gap' ); } +function useHasHeight( name ) { + const supports = getSupportedGlobalStylesPanels( name ); + const [ settings ] = useSetting( 'dimensions.height', name ); + + return settings && supports.includes( 'height' ); +} + function filterValuesBySides( values, sides ) { if ( ! sides ) { // If no custom side configuration all sides are opted into by default. @@ -89,6 +97,7 @@ export default function DimensionsPanel( { name } ) { const showPaddingControl = useHasPadding( name ); const showMarginControl = useHasMargin( name ); const showGapControl = useHasGap( name ); + const showHeightControl = useHasHeight( name ); const units = useCustomUnits( { availableUnits: useSetting( 'spacing.units', name )[ 0 ] || [ '%', @@ -99,6 +108,7 @@ export default function DimensionsPanel( { name } ) { ], } ); + // Padding. const [ rawPadding, setRawPadding ] = useStyle( 'spacing.padding', name ); const paddingValues = splitStyleValue( rawPadding ); const paddingSides = useCustomSides( name, 'padding' ); @@ -114,6 +124,7 @@ export default function DimensionsPanel( { name } ) { const hasPaddingValue = () => !! paddingValues && Object.keys( paddingValues ).length; + // Margin. const [ rawMargin, setRawMargin ] = useStyle( 'spacing.margin', name ); const marginValues = splitStyleValue( rawMargin ); const marginSides = useCustomSides( name, 'margin' ); @@ -129,11 +140,21 @@ export default function DimensionsPanel( { name } ) { const hasMarginValue = () => !! marginValues && Object.keys( marginValues ).length; + // Gap. const [ gapValue, setGapValue ] = useStyle( 'spacing.blockGap', name ); const resetGapValue = () => setGapValue( undefined ); const hasGapValue = () => !! gapValue; + // Height. + const [ heightValue, setHeightValue ] = useStyle( + 'dimensions.height', + name + ); + const resetHeightValue = () => setHeightValue( undefined ); + const hasHeightValue = () => !! heightValue; + const resetAll = () => { + resetHeightValue(); resetPaddingValue(); resetMarginValue(); resetGapValue(); @@ -141,6 +162,23 @@ export default function DimensionsPanel( { name } ) { return ( + { showHeightControl && ( + + + + ) } { showPaddingControl && ( { const pickStyleKeys = ( treeToPickFrom ) => pickBy( treeToPickFrom, ( value, key ) => - [ 'border', 'color', 'spacing', 'typography' ].includes( key ) + [ + 'border', + 'color', + 'dimensions', + 'spacing', + 'typography', + ].includes( key ) ); // Top-level. diff --git a/packages/edit-site/src/components/sidebar/style.scss b/packages/edit-site/src/components/sidebar/style.scss index b74ba0b9370276..b9b65db7548267 100644 --- a/packages/edit-site/src/components/sidebar/style.scss +++ b/packages/edit-site/src/components/sidebar/style.scss @@ -53,6 +53,10 @@ border: 0; } +.edit-site-global-styles-sidebar .components-tools-panel-item.single-column { + grid-column: span 1; +} + .edit-site-global-styles-sidebar__blocks-group { padding-top: $grid-unit-30; border-top: $border-width solid $gray-200; diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 5b1acab34059b9..c9bac6154cc66d 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -345,17 +345,20 @@ function test_get_stylesheet() { ), 'blocks' => array( 'core/group' => array( - 'border' => array( + 'border' => array( 'radius' => '10px', ), - 'elements' => array( + 'elements' => array( 'link' => array( 'color' => array( 'text' => '#111', ), ), ), - 'spacing' => array( + 'dimensions' => array( + 'height' => '200px', + ), + 'spacing' => array( 'padding' => '24px', ), ), @@ -408,7 +411,7 @@ function test_get_stylesheet() { ); $variables = 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}'; - $styles = 'body{color: var(--wp--preset--color--grey);}body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}'; + $styles = 'body{color: var(--wp--preset--color--grey);}body { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); }a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;height: 200px;padding: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;margin-bottom: 30px;}'; $presets = '.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-small-font-family{font-family: var(--wp--preset--font-family--small) !important;}.has-big-font-family{font-family: var(--wp--preset--font-family--big) !important;}'; $all = $variables . $styles . $presets; $this->assertEquals( $all, $theme_json->get_stylesheet() );