Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/7.0/10474.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/10474

* https://github.com/WordPress/gutenberg/pull/71914
4 changes: 3 additions & 1 deletion docs/how-to-guides/themes/global-settings-and-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ The settings section has the following structure:
"custom": {},
"dimensions": {
"aspectRatio": false,
"height": false,
"minHeight": false,
"width": false,
},
Expand Down Expand Up @@ -302,7 +303,7 @@ There's one special setting property, `appearanceTools`, which is a boolean and
- background: backgroundImage, backgroundSize
- border: color, radius, style, width
- color: link
- dimensions: aspectRatio, minHeight, width
- dimensions: aspectRatio, height, minHeight, width
- position: sticky
- spacing: blockGap, margin, padding
- typography: lineHeight
Expand Down Expand Up @@ -772,6 +773,7 @@ Each block declares which style properties it exposes via the [block supports me
},
"dimensions": {
"aspectRatio": "value",
"height": "value"
"minHeight": "value"
"width": "value"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/how-to-guides/themes/theme-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ Use this setting to enable the following Global Styles settings:
- color: link
- spacing: blockGap, margin, padding
- typography: lineHeight
- dimensions: aspectRatio, minHeight, width
- dimensions: aspectRatio, height, minHeight, width
- position: sticky

```php
Expand Down
5 changes: 4 additions & 1 deletion docs/reference-guides/block-api/block-supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ _**Note:** Since WordPress 6.2._
- Type: `Object`
- Default value: null
- Subproperties:
- `height`: type `boolean`, default value `false`
- `minHeight`: type `boolean`, default value `false`
- `width`: type `boolean`, default value `false`

Expand All @@ -638,6 +639,7 @@ This value signals that a block supports some of the CSS style properties relate
supports: {
dimensions: {
aspectRatio: true // Enable aspect ratio control.
height: true // Enable height control.
minHeight: true // Enable min height control.
width: true // Enable width control.
}
Expand All @@ -646,13 +648,14 @@ supports: {

When a block declares support for a specific dimensions property, its attributes definition is extended to include the `style` attribute.

- `style`: an attribute of `object` type with no default assigned. This is added when `aspectRatio`, `minHeight`, or `width` support is declared. It stores the custom values set by the user. For example:
- `style`: an attribute of `object` type with no default assigned. This is added when `aspectRatio`, `height`, `minHeight`, or `width` support is declared. It stores the custom values set by the user. For example:

```js
attributes: {
style: {
dimensions: {
aspectRatio: "16/9",
height: "40vh",
minHeight: "50vh",
width: "400px",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ New styles options are integrated into theme.json on a regular basis. Knowing th
| `spacing.margin.left` | 5.8 | 5.9 |
| `spacing.margin.bottom` | 5.8 | 5.9 |
| `spacing.blockGap` | 5.9 | 5.9 |
| `dimensions.height` | 7.0 | N/A |
| `dimensions.minHeight` | 6.2 | N/A |
| `dimensions.width` | 7.0 | N/A |
| `outline.color` | 6.2 | N/A |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Setting that enables the following UI tools:
- background: backgroundImage, backgroundSize
- border: color, radius, style, width
- color: link, heading, button, caption
- dimensions: aspectRatio, minHeight, width
- dimensions: aspectRatio, height, minHeight, width
- position: sticky
- spacing: blockGap, margin, padding
- typography: lineHeight
Expand Down Expand Up @@ -111,6 +111,7 @@ Settings related to dimensions.
| aspectRatio | Allow users to set an aspect ratio. | `boolean` | `false` |
| defaultAspectRatios | Allow users to choose aspect ratios from the default set of aspect ratios. | `boolean` | `true` |
| aspectRatios | Allow users to define aspect ratios for some blocks. | `[ { name, slug, ratio } ]` | |
| height | Allow users to set custom height. | `boolean` | `false` |
| minHeight | Allow users to set custom minimum height. | `boolean` | `false` |
| width | Allow users to set custom width. | `boolean` | `false` |
| dimensionSizes | Dimension size presets for dimension block supports. | `[ { name, slug, size } ]` | |
Expand Down Expand Up @@ -269,6 +270,7 @@ Dimensions styles.
| Property | Description | Type |
| -------- | ----------- | ---- |
| aspectRatio | Sets the `aspect-ratio` CSS property. | `string`, `{ ref }` |
| height | Sets the `height` CSS property. | `string`, `{ ref }` |
| minHeight | Sets the `min-height` CSS property. | `string`, `{ ref }` |
| width | Sets the `width` CSS property. | `string`, `{ ref }` |

Expand Down
9 changes: 6 additions & 3 deletions lib/block-supports/dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) {
}

$dimensions_block_styles = array();
$supported_features = array( 'minHeight', 'width' );
$supported_features = array( 'minHeight', 'height', 'width' );

foreach ( $supported_features as $feature ) {
$has_support = block_has_support( $block_type, array( 'dimensions', $feature ), false );
Expand Down Expand Up @@ -103,14 +103,17 @@ function gutenberg_render_dimensions_support( $block_content, $block ) {
$dimensions_block_styles = array();
$dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;

// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
// To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule.
if (
isset( $dimensions_block_styles['aspectRatio'] )
) {
$dimensions_block_styles['minHeight'] = 'unset';
$dimensions_block_styles['height'] = 'unset';
} elseif (
isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
isset( $block_attributes['minHeight'] )
isset( $block_attributes['minHeight'] ) ||
isset( $block_attributes['style']['dimensions']['height'] ) ||
isset( $block_attributes['height'] )
) {
$dimensions_block_styles['aspectRatio'] = 'unset';
}
Expand Down
13 changes: 9 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class WP_Theme_JSON_Gutenberg {
'value_key' => 'size',
'css_vars' => '--wp--preset--dimension--$slug',
'classes' => array(),
'properties' => array( 'width', 'min-height' ),
'properties' => array( 'width', 'height', 'min-height' ),
),
);

Expand All @@ -241,7 +241,7 @@ class WP_Theme_JSON_Gutenberg {
* @since 6.2.0 Added `outline-*`, and `min-height` properties.
* @since 6.3.0 Added `writing-mode` property.
* @since 6.6.0 Added `background-[image|position|repeat|size]` properties.
* @since 7.0.0 Added `dimensions.width`.
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
*
* @var array
*/
Expand Down Expand Up @@ -307,6 +307,7 @@ class WP_Theme_JSON_Gutenberg {
'text-transform' => array( 'typography', 'textTransform' ),
'filter' => array( 'filter', 'duotone' ),
'box-shadow' => array( 'shadow' ),
'height' => array( 'dimensions', 'height' ),
'width' => array( 'dimensions', 'width' ),
'writing-mode' => array( 'typography', 'writingMode' ),
);
Expand Down Expand Up @@ -397,7 +398,7 @@ class WP_Theme_JSON_Gutenberg {
* @since 6.4.0 Added `layout.allowEditing`.
* @since 6.4.0 Added `lightbox`.
* @since 7.0.0 Added type markers to the schema for boolean values.
* @since 7.0.0 Added `dimensions.width`.
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
* @var array
*/
const VALID_SETTINGS = array(
Expand Down Expand Up @@ -437,6 +438,7 @@ class WP_Theme_JSON_Gutenberg {
'aspectRatios' => null,
'defaultAspectRatios' => null,
'dimensionSizes' => null,
'height' => null,
'minHeight' => null,
'width' => null,
),
Expand Down Expand Up @@ -525,6 +527,7 @@ class WP_Theme_JSON_Gutenberg {
* @since 6.2.0 Added `outline`, and `minHeight` properties.
* @since 6.6.0 Added `background` sub properties to top-level only.
* @since 6.6.0 Added `dimensions.aspectRatio`.
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
* @var array
*/
const VALID_STYLES = array(
Expand Down Expand Up @@ -552,6 +555,7 @@ class WP_Theme_JSON_Gutenberg {
),
'dimensions' => array(
'aspectRatio' => null,
'height' => null,
'minHeight' => null,
'width' => null,
),
Expand Down Expand Up @@ -757,7 +761,7 @@ public static function get_element_class_name( $element ) {
*
* @since 6.0.0
* @since 6.2.0 Added `dimensions.minHeight` and `position.sticky`.
* @since 7.0.0 Added `dimensions.width`.
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
* @var array
*/
const APPEARANCE_TOOLS_OPT_INS = array(
Expand All @@ -772,6 +776,7 @@ public static function get_element_class_name( $element ) {
array( 'color', 'button' ),
array( 'color', 'caption' ),
array( 'dimensions', 'aspectRatio' ),
array( 'dimensions', 'height' ),
array( 'dimensions', 'minHeight' ),
array( 'dimensions', 'width' ),
// BEGIN EXPERIMENTAL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function useHasDimensionsPanel( settings ) {
const hasPadding = useHasPadding( settings );
const hasMargin = useHasMargin( settings );
const hasGap = useHasGap( settings );
const hasHeight = useHasHeight( settings );
const hasMinHeight = useHasMinHeight( settings );
const hasWidth = useHasWidth( settings );
const hasAspectRatio = useHasAspectRatio( settings );
Expand All @@ -50,6 +51,7 @@ export function useHasDimensionsPanel( settings ) {
hasPadding ||
hasMargin ||
hasGap ||
hasHeight ||
hasMinHeight ||
hasWidth ||
hasAspectRatio ||
Expand Down Expand Up @@ -77,6 +79,10 @@ function useHasGap( settings ) {
return settings?.spacing?.blockGap;
}

function useHasHeight( settings ) {
return settings?.dimensions?.height;
}

function useHasMinHeight( settings ) {
return settings?.dimensions?.minHeight;
}
Expand Down Expand Up @@ -211,6 +217,7 @@ const DEFAULT_CONTROLS = {
padding: true,
margin: true,
blockGap: true,
height: true,
minHeight: true,
width: true,
aspectRatio: true,
Expand Down Expand Up @@ -391,6 +398,29 @@ export default function DimensionsPanel( {
};
const hasMinHeightValue = () => !! value?.dimensions?.minHeight;

// Height
const showHeightControl = useHasHeight( settings );
const heightValue = decodeValue( inheritedValue?.dimensions?.height );
const setHeightValue = ( newValue ) => {
const tempValue = setImmutably(
value,
[ 'dimensions', 'height' ],
newValue
);
// Apply height, while removing any applied aspect ratio.
onChange(
setImmutably(
tempValue,
[ 'dimensions', 'aspectRatio' ],
undefined
)
);
};
const resetHeightValue = () => {
setHeightValue( undefined );
};
const hasHeightValue = () => !! value?.dimensions?.height;

// Width
const showWidthControl = useHasWidth( settings );
const widthValue = decodeValue( inheritedValue?.dimensions?.width );
Expand Down Expand Up @@ -455,6 +485,7 @@ export default function DimensionsPanel( {
},
dimensions: {
...previousValue?.dimensions,
height: undefined,
minHeight: undefined,
aspectRatio: undefined,
width: undefined,
Expand Down Expand Up @@ -707,6 +738,23 @@ export default function DimensionsPanel( {
/>
</ToolsPanelItem>
) }
{ showHeightControl && (
<ToolsPanelItem
hasValue={ hasHeightValue }
label={ __( 'Height' ) }
onDeselect={ resetHeightValue }
isShownByDefault={
defaultControls.height ?? DEFAULT_CONTROLS.height
}
panelId={ panelId }
>
<DimensionControl
label={ __( 'Height' ) }
value={ heightValue }
onChange={ setHeightValue }
Copy link
Member

Choose a reason for hiding this comment

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

Similar to question above. setMinHeightValue removes any applied aspect ratio. Is that required here too?

And if so, does it need handling in gutenberg_render_dimensions_support?

/>
</ToolsPanelItem>
) }
{ showWidthControl && (
<ToolsPanelItem
hasValue={ hasWidthValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function useSettingsForBlockElement(
}
} );

[ 'aspectRatio', 'minHeight', 'width' ].forEach( ( key ) => {
[ 'aspectRatio', 'height', 'minHeight', 'width' ].forEach( ( key ) => {
if ( ! supportedStyles.includes( key ) ) {
updatedSettings.dimensions = {
...updatedSettings.dimensions,
Expand Down
25 changes: 16 additions & 9 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export function hasDimensionsSupport( blockName, feature = 'any' ) {
if ( feature === 'any' ) {
return !! (
support?.aspectRatio ||
!! support?.height ||
!! support?.minHeight ||
!! support?.width
);
Expand All @@ -169,13 +170,13 @@ export function hasDimensionsSupport( blockName, feature = 'any' ) {

export default {
useBlockProps,
attributeKeys: [ 'minHeight', 'width', 'style' ],
attributeKeys: [ 'height', 'minHeight', 'width', 'style' ],
Copy link
Member

Choose a reason for hiding this comment

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

Just checking: do we need to add logic similar to what's below (around line 201) for minHeight to handle height + aspectRatio conflicts?

E.g., unsetting height when aspectRatio is set, or vice versa?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, quite possibly. I'll work on updating that soon.

hasSupport( name ) {
return hasDimensionsSupport( name );
},
};

function useBlockProps( { name, minHeight, style } ) {
function useBlockProps( { name, height, minHeight, style } ) {
if (
! hasDimensionsSupport( name, 'aspectRatio' ) ||
shouldSkipSerialization( name, DIMENSIONS_SUPPORT_KEY, 'aspectRatio' )
Expand All @@ -192,16 +193,22 @@ function useBlockProps( { name, minHeight, style } ) {
const inlineStyleOverrides = {};

// Apply rules to unset incompatible styles.
// Note that a set `aspectRatio` will win out if both an aspect ratio and a minHeight are set.
// Note that a set `aspectRatio` will win out if both an aspect ratio and height-related properties are set.
// This is because the aspect ratio is a newer block support, so (in theory) any aspect ratio
// that is set should be intentional and should override any existing minHeight. The Cover block
// and dimensions controls have logic that will manually clear the aspect ratio if a minHeight
// is set.
// that is set should be intentional and should override any existing height properties. The Cover block
// and dimensions controls have logic that will manually clear the aspect ratio if height properties
// are set.
if ( style?.dimensions?.aspectRatio ) {
// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
// To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule.
inlineStyleOverrides.minHeight = 'unset';
} else if ( minHeight || style?.dimensions?.minHeight ) {
// To ensure the minHeight does not get overridden by `aspectRatio` unset any existing rule.
inlineStyleOverrides.height = 'unset';
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why we're doing these things at the rendering level, can't we "unset" in the UI? Or do we even need to be this smart? (Maybe we do, just asking)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have full context on the original decision to handle this in this manner but @andrewserong might be able to shed further light.

The PR introducing aspect ratio support was #56897 and details the following for the "why" behind it.

This PR tries to let the CSS aspect-ratio work the way that it works, with minimal interference. To achieve this, and to avoid overflow issues, when an aspect ratio is set, we unset any value for min-height. Similarly, if a min-height is set at the block level, we output an aspect-ratio: unset rule to override any aspect ratio set in global styles. These unset rules should only be output for blocks that opt-in to aspect ratio, and are not saved to post content.

Before seeing that, my understanding was that the chosen approach was based on the block inspector not having access to the full set of global styles or third-party stylesheets. Now that global styles data is available in the post editor, most use cases should be addressable directly through the UI.

This also relates to how style inheritance is handled in the inspector. With those values exposed, the UI could more reliably determine things like aspect ratio versus min-height versus height, even if theme-enqueued stylesheets remain outside that scope.

Overall, the current approach seems slightly more robust.

} else if (
minHeight ||
style?.dimensions?.minHeight ||
height ||
style?.dimensions?.height
) {
// To ensure height properties do not get overridden by `aspectRatio` unset any existing rule.
inlineStyleOverrides.aspectRatio = 'unset';
}

Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export function useBlockSettings( name, parentLayout ) {
themeSpacingSizes,
units,
aspectRatio,
height,
minHeight,
width,
dimensionSizes,
Expand Down Expand Up @@ -323,6 +324,7 @@ export function useBlockSettings( name, parentLayout ) {
'spacing.spacingSizes.theme',
'spacing.units',
'dimensions.aspectRatio',
'dimensions.height',
'dimensions.minHeight',
'dimensions.width',
'dimensions.dimensionSizes',
Expand Down Expand Up @@ -434,6 +436,7 @@ export function useBlockSettings( name, parentLayout ) {
},
dimensions: {
aspectRatio,
height,
minHeight,
width,
dimensionSizes,
Expand Down Expand Up @@ -472,6 +475,7 @@ export function useBlockSettings( name, parentLayout ) {
themeSpacingSizes,
units,
aspectRatio,
height,
minHeight,
width,
dimensionSizes,
Expand Down
Loading
Loading