Add new `alt` attribute to the `Featured Product` media settings (https://github.com/woocommerce/woocommerce-blocks/pull/6308)

* Add new `alt` attribute to Media Settings

Allow to define an alt text for the Feature Product block

* Simplify on change

* Improve alt description and use name instead of short description

Short description can contain html and images, so it's not adequate to use on the alt attribute
This commit is contained in:
Alba Rincón 2022-04-28 16:28:22 +02:00 committed by GitHub
parent ec73743d0f
commit 964f6e5965
4 changed files with 32 additions and 1 deletions

View File

@ -19,12 +19,14 @@ import {
import { withSelect } from '@wordpress/data';
import {
Button,
ExternalLink,
FocalPointPicker,
PanelBody,
Placeholder,
RangeControl,
ResizableBox,
Spinner,
TextareaControl,
ToggleControl,
ToolbarGroup,
withSpokenMessages,
@ -339,6 +341,30 @@ const FeaturedProduct = ( {
} )
}
/>
<TextareaControl
label={ __(
'Alt text (alternative text)',
'woo-gutenberg-products-block'
) }
value={ attributes.alt }
onChange={ ( alt ) => {
setAttributes( { alt } );
} }
help={
<>
<ExternalLink href="https://www.w3.org/WAI/tutorials/images/decision-tree">
{ __(
'Describe the purpose of the image',
'woo-gutenberg-products-block'
) }
</ExternalLink>
{ __(
'Leaving it empty will use the product name.',
'woo-gutenberg-products-block'
) }
</>
}
/>
</PanelBody>
) }
<PanelColorGradientSettings

View File

@ -6,6 +6,7 @@ import { previewProducts } from '@woocommerce/resource-previews';
export const example = {
attributes: {
alt: '',
contentAlign: 'center',
dimRatio: 50,
editMode: false,

View File

@ -66,6 +66,10 @@ registerBlockType( 'woocommerce/featured-product', {
},
example,
attributes: {
alt: {
type: 'string',
default: '',
},
/**
* Alignment of content inside block.
*/

View File

@ -147,7 +147,7 @@ class FeaturedProduct extends AbstractDynamicBlock {
if ( ! empty( $image ) ) {
return sprintf(
'<img alt="%1$s" class="wc-block-featured-product__background-image" src="%2$s" style="%3$s" />',
wp_kses_post( $product->get_short_description() ),
wp_kses_post( $attributes['alt'] ?? $product->get_name() ),
esc_url( $image ),
$style
);