-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add height block support to dimension #71914
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
Changes from all commits
ef44476
6496c30
43cd3aa
92934c3
7a6abfb
6f055fb
bb50e57
66c1d6a
095a41f
5e5fdd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
|
|
@@ -50,6 +51,7 @@ export function useHasDimensionsPanel( settings ) { | |
| hasPadding || | ||
| hasMargin || | ||
| hasGap || | ||
| hasHeight || | ||
| hasMinHeight || | ||
| hasWidth || | ||
| hasAspectRatio || | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -211,6 +217,7 @@ const DEFAULT_CONTROLS = { | |
| padding: true, | ||
| margin: true, | ||
| blockGap: true, | ||
| height: true, | ||
| minHeight: true, | ||
| width: true, | ||
| aspectRatio: true, | ||
|
|
@@ -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 ); | ||
|
|
@@ -455,6 +485,7 @@ export default function DimensionsPanel( { | |
| }, | ||
| dimensions: { | ||
| ...previousValue?.dimensions, | ||
| height: undefined, | ||
| minHeight: undefined, | ||
| aspectRatio: undefined, | ||
| width: undefined, | ||
|
|
@@ -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 } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to question above. And if so, does it need handling in |
||
| /> | ||
| </ToolsPanelItem> | ||
| ) } | ||
| { showWidthControl && ( | ||
| <ToolsPanelItem | ||
| hasValue={ hasWidthValue } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,6 +159,7 @@ export function hasDimensionsSupport( blockName, feature = 'any' ) { | |
| if ( feature === 'any' ) { | ||
| return !! ( | ||
| support?.aspectRatio || | ||
| !! support?.height || | ||
| !! support?.minHeight || | ||
| !! support?.width | ||
| ); | ||
|
|
@@ -169,13 +170,13 @@ export function hasDimensionsSupport( blockName, feature = 'any' ) { | |
|
|
||
| export default { | ||
| useBlockProps, | ||
| attributeKeys: [ 'minHeight', 'width', 'style' ], | ||
| attributeKeys: [ 'height', 'minHeight', 'width', 'style' ], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ) | ||
|
|
@@ -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'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
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'; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.