Skip to content

Commit

Permalink
Separate width/height constraints for horizontal or vertical configur…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
ciampo committed Mar 18, 2022
1 parent 0bde2fd commit 15a81af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
24 changes: 20 additions & 4 deletions packages/block-library/src/spacer/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { MIN_SPACER_SIZE, MAX_SPACER_SIZE } from './edit';
import {
MIN_SPACER_WIDTH,
MAX_SPACER_WIDTH,
MIN_SPACER_HEIGHT,
MAX_SPACER_HEIGHT,
} from './edit';

function DimensionInput( { label, onChange, isResizing, value = '' } ) {
function DimensionInput( {
label,
onChange,
isResizing,
value = '',
min,
max,
} ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );

const inputId = useInstanceId( UnitControl, 'block-spacer-height-input' );
Expand Down Expand Up @@ -58,8 +70,8 @@ function DimensionInput( { label, onChange, isResizing, value = '' } ) {
<UnitControl
id={ inputId }
isResetValueOnUnitChange
min={ MIN_SPACER_SIZE }
max={ MAX_SPACER_SIZE }
min={ min }
max={ max }
onBlur={ handleOnBlur }
onChange={ handleOnChange }
style={ { maxWidth: 80 } }
Expand Down Expand Up @@ -90,6 +102,8 @@ export default function SpacerControls( {
setAttributes( { width: nextWidth } )
}
isResizing={ isResizing }
min={ MIN_SPACER_WIDTH }
max={ MAX_SPACER_WIDTH }
/>
) }
{ orientation !== 'horizontal' && (
Expand All @@ -100,6 +114,8 @@ export default function SpacerControls( {
setAttributes( { height: nextHeight } )
}
isResizing={ isResizing }
min={ MIN_SPACER_HEIGHT }
max={ MAX_SPACER_HEIGHT }
/>
) }
</PanelBody>
Expand Down
19 changes: 16 additions & 3 deletions packages/block-library/src/spacer/controls.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { MIN_SPACER_SIZE, MAX_SPACER_SIZE } from './edit';
import {
MIN_SPACER_WIDTH,
MAX_SPACER_WIDTH,
MIN_SPACER_HEIGHT,
MAX_SPACER_HEIGHT,
} from './edit';
import styles from './style.scss';

const DEFAULT_VALUES = { px: 100, em: 10, rem: 10, vw: 10, vh: 25 };
Expand Down Expand Up @@ -67,8 +72,16 @@ function Controls( { attributes, context, setAttributes } ) {
<PanelBody title={ __( 'Dimensions' ) }>
<UnitControl
label={ label }
min={ MIN_SPACER_SIZE }
max={ MAX_SPACER_SIZE }
min={
orientation === 'horizontal'
? MIN_SPACER_WIDTH
: MIN_SPACER_HEIGHT
}
max={
orientation === 'horizontal'
? MAX_SPACER_WIDTH
: MAX_SPACER_HEIGHT
}
value={ value }
onChange={ handleChange }
onUnitChange={ handleUnitChange }
Expand Down
16 changes: 9 additions & 7 deletions packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import { View } from '@wordpress/primitives';
*/
import SpacerControls from './controls';

export const MIN_SPACER_SIZE = 10;
export const MAX_SPACER_SIZE = 500;
export const MIN_SPACER_WIDTH = 1;
export const MAX_SPACER_WIDTH = 500;
export const MIN_SPACER_HEIGHT = 10;
export const MAX_SPACER_HEIGHT = 500;

const ResizableSpacer = ( {
orientation,
Expand Down Expand Up @@ -59,7 +61,9 @@ const ResizableSpacer = ( {
} }
onResizeStop={ ( _event, _direction, elt ) => {
const nextVal = Math.min(
MAX_SPACER_SIZE,
orientation === 'horizontal'
? MAX_SPACER_WIDTH
: MAX_SPACER_HEIGHT,
getCurrentSize( elt )
);
onResizeStop( `${ nextVal }px` );
Expand Down Expand Up @@ -122,8 +126,7 @@ const SpacerEdit = ( {
if ( blockOrientation === 'horizontal' ) {
return (
<ResizableSpacer
minWidth={ MIN_SPACER_SIZE }
maxWidth={ MAX_SPACER_SIZE }
minWidth={ MIN_SPACER_WIDTH }
enable={ {
top: false,
right: true,
Expand All @@ -148,8 +151,7 @@ const SpacerEdit = ( {
return (
<>
<ResizableSpacer
minHeight={ MIN_SPACER_SIZE }
maxHeight={ MAX_SPACER_SIZE }
minHeight={ MIN_SPACER_HEIGHT }
enable={ {
top: false,
right: false,
Expand Down

0 comments on commit 15a81af

Please sign in to comment.