Skip to content

Commit

Permalink
Try outputting has-aspect-ratio classname
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Dec 11, 2023
1 parent 26f2bcb commit bf39d96
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
50 changes: 48 additions & 2 deletions packages/block-editor/src/hooks/dimensions.js
@@ -1,7 +1,12 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useState, useEffect, useCallback } from '@wordpress/element';
import { Platform, useState, useEffect, useCallback } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { getBlockSupport } from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';
Expand All @@ -19,7 +24,6 @@ import { MarginVisualizer } from './margin';
import { PaddingVisualizer } from './padding';
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';

import { cleanEmptyObject } from './utils';

export const DIMENSIONS_SUPPORT_KEY = 'dimensions';
Expand Down Expand Up @@ -131,6 +135,48 @@ function DimensionsPanelPure( { clientId, name, setAttributes, settings } ) {
// and not the whole attributes object.
export const DimensionsPanel = pure( DimensionsPanelPure );

/**
* Determine whether there is block support for dimensions.
*
* @param {string} blockName Block name.
* @param {string} feature Background image feature to check for.
*
* @return {boolean} Whether there is support.
*/
export function hasDimensionsSupport( blockName, feature = 'any' ) {
if ( Platform.OS !== 'web' ) {
return false;
}

const support = getBlockSupport( blockName, DIMENSIONS_SUPPORT_KEY );

if ( support === true ) {
return true;
}

if ( feature === 'any' ) {
return !! support?.dimensions;
}

return !! support?.[ feature ];
}

export default {
useBlockProps,
attributeKeys: [ 'style' ],
hasSupport( name ) {
return hasDimensionsSupport( name, 'aspectRatio' );
},
};

function useBlockProps( { style } ) {
const className = classnames( {
'has-aspect-ratio': !! style?.dimensions?.aspectRatio,
} );

return { className };
}

/**
* @deprecated
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/index.js
Expand Up @@ -12,6 +12,7 @@ import './generated-class-name';
import style from './style';
import './settings';
import color from './color';
import dimensions from './dimensions';
import duotone from './duotone';
import './font-family';
import fontSize from './font-size';
Expand Down Expand Up @@ -42,6 +43,7 @@ createBlockEditFilter(
createBlockListBlockFilter( [
align,
color,
dimensions,
duotone,
fontSize,
border,
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/common.scss
Expand Up @@ -180,3 +180,8 @@ html :where(.is-position-sticky) {
/* stylelint-enable length-zero-no-unit */
}
}

// TODO: Put this rule somewhere else
.has-aspect-ratio {
width: 100%;
}

0 comments on commit bf39d96

Please sign in to comment.