Created
June 15, 2021 03:56
-
-
Save glendaviesnz/e2109fc3ed4fc9dc0fc297e49763ad30 to your computer and use it in GitHub Desktop.
Image size control with custom hook for dimension handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* External dependencies | |
*/ | |
import { isEmpty, noop } from 'lodash'; | |
/** | |
* WordPress dependencies | |
*/ | |
import { | |
Button, | |
ButtonGroup, | |
SelectControl, | |
TextControl, | |
} from '@wordpress/components'; | |
import { __ } from '@wordpress/i18n'; | |
/** | |
* Internal dependencies | |
*/ | |
import useDimensionHandler from './useDimensionHandler'; | |
const IMAGE_SIZE_PRESETS = [ 25, 50, 75, 100 ]; | |
export default function ImageSizeControl( { | |
imageWidth, | |
imageHeight, | |
imageSizeOptions = [], | |
isResizable = true, | |
slug, | |
width, | |
height, | |
onChange, | |
onChangeImage = noop, | |
} ) { | |
const { | |
currentWidth, | |
currentHeight, | |
updateDimension, | |
} = useDimensionHandler( imageWidth, width, imageHeight, height, onChange ); | |
function updateDimensions( nextWidth, nextHeight ) { | |
return () => { | |
onChange( { width: nextWidth, height: nextHeight } ); | |
}; | |
} | |
return ( | |
<> | |
{ ! isEmpty( imageSizeOptions ) && ( | |
<SelectControl | |
label={ __( 'Image size' ) } | |
value={ slug } | |
options={ imageSizeOptions } | |
onChange={ onChangeImage } | |
/> | |
) } | |
{ isResizable && ( | |
<div className="block-editor-image-size-control"> | |
<p className="block-editor-image-size-control__row"> | |
{ __( 'Image dimensions' ) } | |
</p> | |
<div className="block-editor-image-size-control__row"> | |
<TextControl | |
type="number" | |
className="block-editor-image-size-control__width" | |
label={ __( 'Width' ) } | |
value={ currentWidth } | |
min={ 1 } | |
onChange={ ( value ) => | |
updateDimension( 'width', value ) | |
} | |
/> | |
<TextControl | |
type="number" | |
className="block-editor-image-size-control__height" | |
label={ __( 'Height' ) } | |
value={ currentHeight } | |
min={ 1 } | |
onChange={ ( value ) => | |
updateDimension( 'height', value ) | |
} | |
/> | |
</div> | |
<div className="block-editor-image-size-control__row"> | |
<ButtonGroup aria-label={ __( 'Image size presets' ) }> | |
{ IMAGE_SIZE_PRESETS.map( ( scale ) => { | |
const scaledWidth = Math.round( | |
imageWidth * ( scale / 100 ) | |
); | |
const scaledHeight = Math.round( | |
imageHeight * ( scale / 100 ) | |
); | |
const isCurrent = | |
width === scaledWidth && | |
height === scaledHeight; | |
return ( | |
<Button | |
key={ scale } | |
isSmall | |
variant={ | |
isCurrent ? 'primary' : undefined | |
} | |
isPressed={ isCurrent } | |
onClick={ updateDimensions( | |
scaledWidth, | |
scaledHeight | |
) } | |
> | |
{ scale }% | |
</Button> | |
); | |
} ) } | |
</ButtonGroup> | |
<Button isSmall onClick={ updateDimensions() }> | |
{ __( 'Reset' ) } | |
</Button> | |
</div> | |
</div> | |
) } | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment