-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ImageSizeControls: Replace ButtonGroup with ToggleGroupControl #65386
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 1 commit
30dbcb1
05ebec6
1196a22
e740c51
5ee7630
12532be
20d8650
393bb5d
c84ff9f
08f19cd
4979ddd
558cd41
4bc666b
a3d6c29
4fd8d34
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 | ||
|---|---|---|---|---|
|
|
@@ -2,11 +2,11 @@ | |||
| * WordPress dependencies | ||||
| */ | ||||
| import { | ||||
| Button, | ||||
| ButtonGroup, | ||||
| SelectControl, | ||||
| __experimentalNumberControl as NumberControl, | ||||
| __experimentalHStack as HStack, | ||||
| __experimentalToggleGroupControl as ToggleGroupControl, | ||||
| __experimentalToggleGroupControlOption as ToggleGroupControlOption, | ||||
| } from '@wordpress/components'; | ||||
| import { __ } from '@wordpress/i18n'; | ||||
|
|
||||
|
|
@@ -33,6 +33,47 @@ export default function ImageSizeControl( { | |||
| const { currentHeight, currentWidth, updateDimension, updateDimensions } = | ||||
| useDimensionHandler( height, width, imageHeight, imageWidth, onChange ); | ||||
|
|
||||
| /** | ||||
| * Updates the dimensions for the given scale. | ||||
| * Handler for toggle group control change. | ||||
| * | ||||
| * @param {number} scale The scale to update the dimensions for. | ||||
| */ | ||||
| const handleUpdateDimensions = ( scale ) => { | ||||
| /** | ||||
| * Check if scale is deselected from toggle group control option. | ||||
| */ | ||||
| if ( undefined === scale ) { | ||||
| /** | ||||
| * Update dimensions to default. | ||||
| */ | ||||
| updateDimensions(); | ||||
| return; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Calculate scaled width and height. | ||||
| */ | ||||
| const scaledWidth = Math.round( imageWidth * ( scale / 100 ) ); | ||||
| const scaledHeight = Math.round( imageHeight * ( scale / 100 ) ); | ||||
|
|
||||
| /** | ||||
| * Update dimensions. | ||||
| */ | ||||
hbhalodia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| updateDimensions( scaledHeight, scaledWidth ); | ||||
| }; | ||||
|
|
||||
| /** | ||||
| * Handler for adding the value to toggle group control. | ||||
| */ | ||||
| const addSelectedScaleValue = () => { | ||||
| /** | ||||
| * Calculate scale size based on current width and image width. | ||||
| */ | ||||
| const scaleSize = Math.round( currentWidth * ( 100 / imageWidth ) ); | ||||
| return scaleSize; | ||||
| }; | ||||
|
|
||||
| return ( | ||||
| <> | ||||
| { imageSizeOptions && imageSizeOptions.length > 0 && ( | ||||
|
|
@@ -70,47 +111,28 @@ export default function ImageSizeControl( { | |||
| size="__unstable-large" | ||||
| /> | ||||
| </HStack> | ||||
| <HStack> | ||||
| <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 = | ||||
| currentWidth === scaledWidth && | ||||
| currentHeight === scaledHeight; | ||||
|
|
||||
| return ( | ||||
| <Button | ||||
| key={ scale } | ||||
| size="small" | ||||
| variant={ | ||||
| isCurrent ? 'primary' : undefined | ||||
| } | ||||
| isPressed={ isCurrent } | ||||
| onClick={ () => | ||||
| updateDimensions( | ||||
| scaledHeight, | ||||
| scaledWidth | ||||
| ) | ||||
| } | ||||
| > | ||||
| { scale }% | ||||
| </Button> | ||||
| ); | ||||
| } ) } | ||||
| </ButtonGroup> | ||||
| <Button | ||||
| size="small" | ||||
| onClick={ () => updateDimensions() } | ||||
| > | ||||
| { __( 'Reset' ) } | ||||
| </Button> | ||||
| </HStack> | ||||
| <ToggleGroupControl | ||||
| label={ __( 'Image size presets' ) } | ||||
| hideLabelFromVision | ||||
| onChange={ ( scale ) => | ||||
| handleUpdateDimensions( scale ) | ||||
| } | ||||
hbhalodia marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| value={ addSelectedScaleValue() } | ||||
| isBlock | ||||
| isDeselectable | ||||
|
||||
| __next40pxDefaultSize | ||||
| __nextHasNoMarginBottom | ||||
| > | ||||
| { IMAGE_SIZE_PRESETS.map( ( scale ) => { | ||||
| return ( | ||||
| <ToggleGroupControlOption | ||||
| key={ scale } | ||||
| value={ scale } | ||||
| label={ `${ scale }%` } | ||||
|
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. It might make sense for the percentage string to be localized. In some languages, it might actually be displayed differently:
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. Do we localize other percentages across Gutenberg? If not, it may be better to split it to a separate PR to localize all percentages at the same time
Contributor
Author
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 do not think, we are localizing other percentage implementations, here is the example for column block -
I agree with @ciampo, to add all at once, so it does not block the scope of this PR. Thank You,
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 filed issue #66298 regarding localizing percentage values. |
||||
| /> | ||||
| ); | ||||
| } ) } | ||||
| </ToggleGroupControl> | ||||
| </div> | ||||
| ) } | ||||
| </> | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.