This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add size settings to the
Product Image
block (#10034)
* Add height & width setting to the image sidebar settings * Extract settings and add scale options * Add width settings * Apply settings on the frontend * Style placeholder image * Replace post featured image with product image * Allow the width to be wider than container * Fix image on top of other elements
- Loading branch information
Showing
9 changed files
with
204 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
127 changes: 127 additions & 0 deletions
127
assets/js/atomic/blocks/product-elements/image/image-size-settings.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { BlockAttributes } from '@wordpress/blocks'; | ||
import { | ||
// @ts-expect-error Using experimental features | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalToggleGroupControl as ToggleGroupControl, | ||
// @ts-expect-error Using experimental features | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalToggleGroupControlOption as ToggleGroupControlOption, | ||
// @ts-expect-error Using experimental features | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalToolsPanel as ToolsPanel, | ||
// @ts-expect-error Using experimental features | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
// @ts-expect-error Using experimental features | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalUnitControl as UnitControl, | ||
} from '@wordpress/components'; | ||
|
||
interface ImageSizeSettingProps { | ||
scale: string; | ||
width: string | undefined; | ||
height: string | undefined; | ||
setAttributes: ( attrs: BlockAttributes ) => void; | ||
} | ||
|
||
const scaleHelp: Record< string, string > = { | ||
cover: __( | ||
'Image is scaled and cropped to fill the entire space without being distorted.', | ||
'woo-gutenberg-products-block' | ||
), | ||
contain: __( | ||
'Image is scaled to fill the space without clipping nor distorting.', | ||
'woo-gutenberg-products-block' | ||
), | ||
fill: __( | ||
'Image will be stretched and distorted to completely fill the space.', | ||
'woo-gutenberg-products-block' | ||
), | ||
}; | ||
|
||
export const ImageSizeSettings = ( { | ||
scale, | ||
width, | ||
height, | ||
setAttributes, | ||
}: ImageSizeSettingProps ) => { | ||
return ( | ||
<ToolsPanel | ||
className="wc-block-product-image__tools-panel" | ||
label={ __( 'Image size', 'woo-gutenberg-products-block' ) } | ||
> | ||
<UnitControl | ||
label={ __( 'Height', 'woo-gutenberg-products-block' ) } | ||
onChange={ ( value: string ) => { | ||
setAttributes( { height: value } ); | ||
} } | ||
value={ height } | ||
units={ [ | ||
{ | ||
value: 'px', | ||
label: 'px', | ||
}, | ||
] } | ||
/> | ||
<UnitControl | ||
label={ __( 'Width', 'woo-gutenberg-products-block' ) } | ||
onChange={ ( value: string ) => { | ||
setAttributes( { width: value } ); | ||
} } | ||
value={ width } | ||
units={ [ | ||
{ | ||
value: 'px', | ||
label: 'px', | ||
}, | ||
] } | ||
/> | ||
{ height && ( | ||
<ToolsPanelItem | ||
hasValue={ () => true } | ||
label={ __( 'Scale', 'woo-gutenberg-products-block' ) } | ||
> | ||
<ToggleGroupControl | ||
label={ __( 'Scale', 'woo-gutenberg-products-block' ) } | ||
value={ scale } | ||
help={ scaleHelp[ scale ] } | ||
onChange={ ( value: string ) => | ||
setAttributes( { | ||
scale: value, | ||
} ) | ||
} | ||
isBlock | ||
> | ||
<> | ||
<ToggleGroupControlOption | ||
value="cover" | ||
label={ __( | ||
'Cover', | ||
'woo-gutenberg-products-block' | ||
) } | ||
/> | ||
<ToggleGroupControlOption | ||
value="contain" | ||
label={ __( | ||
'Contain', | ||
'woo-gutenberg-products-block' | ||
) } | ||
/> | ||
<ToggleGroupControlOption | ||
value="fill" | ||
label={ __( | ||
'Fill', | ||
'woo-gutenberg-products-block' | ||
) } | ||
/> | ||
</> | ||
</ToggleGroupControl> | ||
</ToolsPanelItem> | ||
) } | ||
</ToolsPanel> | ||
); | ||
}; |
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
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
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
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
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