Skip to content

Commit

Permalink
Cover: Add aria-label to fixed and repeated image backgrounds (#50990)
Browse files Browse the repository at this point in the history
* Cover: Add aria-label to fixed and repeated backgrounds

* Update deprecated.js

* Update duotone and fixed background fixture

* Add a new fixture for a cover block with fixed background and alternative text as aria-label.

* Update packages/block-library/src/cover/deprecated.js

* Oops: Hide the alt text field for video cover blocks

* Try to fix merge conflicts

* Try to fix merge conflicts

* Use constants for attributes and supports
  • Loading branch information
carolinan authored Oct 18, 2023
1 parent 418a3fc commit af34a42
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 35 deletions.
3 changes: 0 additions & 3 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
},
"alt": {
"type": "string",
"source": "attribute",
"selector": "img",
"attribute": "alt",
"default": ""
},
"hasParallax": {
Expand Down
152 changes: 151 additions & 1 deletion packages/block-library/src/cover/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,156 @@ const v12BlockSupports = {
},
};

// Deprecation for blocks that does not have the aria-label when the image background is fixed or repeated.
const v13 = {
attributes: v12BlockAttributes,
supports: v12BlockSupports,
save( { attributes } ) {
const {
backgroundType,
gradient,
contentPosition,
customGradient,
customOverlayColor,
dimRatio,
focalPoint,
useFeaturedImage,
hasParallax,
isDark,
isRepeated,
overlayColor,
url,
alt,
id,
minHeight: minHeightProp,
minHeightUnit,
tagName: Tag,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
overlayColor
);
const gradientClass = __experimentalGetGradientClass( gradient );
const minHeight =
minHeightProp && minHeightUnit
? `${ minHeightProp }${ minHeightUnit }`
: minHeightProp;

const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType;
const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType;

const isImgElement = ! ( hasParallax || isRepeated );

const style = {
minHeight: minHeight || undefined,
};

const bgStyle = {
backgroundColor: ! overlayColorClass
? customOverlayColor
: undefined,
background: customGradient ? customGradient : undefined,
};

const objectPosition =
// prettier-ignore
focalPoint && isImgElement
? mediaPosition(focalPoint)
: undefined;

const backgroundImage = url ? `url(${ url })` : undefined;

const backgroundPosition = mediaPosition( focalPoint );

const classes = classnames(
{
'is-light': ! isDark,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
'has-custom-content-position':
! isContentPositionCenter( contentPosition ),
},
getPositionClassName( contentPosition )
);

const imgClasses = classnames(
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null,
{
'has-parallax': hasParallax,
'is-repeated': isRepeated,
}
);

const gradientValue = gradient || customGradient;

return (
<Tag { ...useBlockProps.save( { className: classes, style } ) }>
<span
aria-hidden="true"
className={ classnames(
'wp-block-cover__background',
overlayColorClass,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ bgStyle }
/>

{ ! useFeaturedImage &&
isImageBackground &&
url &&
( isImgElement ? (
<img
className={ imgClasses }
alt={ alt }
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) : (
<div
role="img"
className={ imgClasses }
style={ { backgroundPosition, backgroundImage } }
/>
) ) }
{ isVideoBackground && url && (
<video
className={ classnames(
'wp-block-cover__video-background',
'intrinsic-ignore'
) }
autoPlay
muted
loop
playsInline
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) }
<div
{ ...useInnerBlocksProps.save( {
className: 'wp-block-cover__inner-container',
} ) }
/>
</Tag>
);
},
};

// Deprecation for blocks to prevent auto overlay color from overriding previously set values.
const v12 = {
attributes: v12BlockAttributes,
Expand Down Expand Up @@ -1673,4 +1823,4 @@ const v1 = {
},
};

export default [ v12, v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
export default [ v13, v12, v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
3 changes: 2 additions & 1 deletion packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ function CoverEdit( {
) : (
<div
ref={ mediaElement }
role="img"
role={ alt ? 'img' : undefined }
aria-label={ alt ? alt : undefined }
className={ classnames(
classes,
'wp-block-cover__image-background'
Expand Down
44 changes: 19 additions & 25 deletions packages/block-library/src/cover/edit/inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export default function CoverInspectorControls( {
isImageBackground,
mediaElement,
url,
isImgElement,
overlayColor,
} = currentSettings;

Expand Down Expand Up @@ -202,32 +201,27 @@ export default function CoverInspectorControls( {
}
/>
) }
{ ! useFeaturedImage &&
url &&
isImageBackground &&
isImgElement && (
<TextareaControl
__nextHasNoMarginBottom
label={ __( 'Alternative text' ) }
value={ alt }
onChange={ ( newAlt ) =>
setAttributes( { alt: newAlt } )
}
help={
<>
<ExternalLink href="https://www.w3.org/WAI/tutorials/images/decision-tree">
{ __(
'Describe the purpose of the image.'
) }
</ExternalLink>
<br />
{ ! useFeaturedImage && url && ! isVideoBackground && (
<TextareaControl
__nextHasNoMarginBottom
label={ __( 'Alternative text' ) }
value={ alt }
onChange={ ( newAlt ) =>
setAttributes( { alt: newAlt } )
}
help={
<>
<ExternalLink href="https://www.w3.org/WAI/tutorials/images/decision-tree">
{ __(
'Leave empty if decorative.'
'Describe the purpose of the image.'
) }
</>
}
/>
) }
</ExternalLink>
<br />
{ __( 'Leave empty if decorative.' ) }
</>
}
/>
) }
<PanelRow>
<Button
variant="secondary"
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export default function save( { attributes } ) {
/>
) : (
<div
role="img"
role={ alt ? 'img' : undefined }
aria-label={ alt ? alt : undefined }
className={ imgClasses }
style={ { backgroundPosition, backgroundImage } }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59So","id":612,"alt":"Custom alt text","hasParallax":true,"dimRatio":50,"isDark":false,"layout":{"type":"constrained"}} -->
<div class="wp-block-cover is-light has-parallax"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><div role="img" aria-label="Custom alt text" class="wp-block-cover__image-background wp-image-612 has-parallax" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"name": "core/cover",
"isValid": true,
"attributes": {
"url": "https://w.wiki/59So",
"useFeaturedImage": false,
"id": 612,
"alt": "Custom alt text",
"hasParallax": true,
"isRepeated": false,
"dimRatio": 50,
"backgroundType": "image",
"isDark": false,
"tagName": "div",
"layout": {
"type": "constrained"
}
},
"innerBlocks": [
{
"name": "core/paragraph",
"isValid": true,
"attributes": {
"align": "center",
"content": "",
"dropCap": false,
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"blockName": "core/cover",
"attrs": {
"url": "https://w.wiki/59So",
"id": 612,
"alt": "Custom alt text",
"hasParallax": true,
"dimRatio": 50,
"isDark": false,
"layout": {
"type": "constrained"
}
},
"innerBlocks": [
{
"blockName": "core/paragraph",
"attrs": {
"align": "center",
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": [],
"innerHTML": "\n<p class=\"has-text-align-center has-large-font-size\"></p>\n",
"innerContent": [
"\n<p class=\"has-text-align-center has-large-font-size\"></p>\n"
]
}
],
"innerHTML": "\n<div class=\"wp-block-cover is-light has-parallax\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"></span><div role=\"img\" aria-label=\"Custom alt text\" class=\"wp-block-cover__image-background wp-image-612 has-parallax\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\"></div></div>\n",
"innerContent": [
"\n<div class=\"wp-block-cover is-light has-parallax\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"></span><div role=\"img\" aria-label=\"Custom alt text\" class=\"wp-block-cover__image-background wp-image-612 has-parallax\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\">",
null,
"</div></div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59So","id":612,"alt":"Custom alt text","hasParallax":true,"dimRatio":50,"isDark":false,"layout":{"type":"constrained"}} -->
<div class="wp-block-cover is-light has-parallax"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><div role="img" aria-label="Custom alt text" class="wp-block-cover__image-background wp-image-612 has-parallax" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59So","id":612,"hasParallax":true,"isRepeated":true,"dimRatio":10,"isDark":false,"style":{"color":{"duotone":["#8c00b7","#fcff41"]}}} -->
<div class="wp-block-cover is-light has-parallax is-repeated"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-10 has-background-dim"></span><div role="img" class="wp-block-cover__image-background wp-image-612 has-parallax is-repeated" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<div class="wp-block-cover is-light has-parallax is-repeated"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-10 has-background-dim"></span><div class="wp-block-cover__image-background wp-image-612 has-parallax is-repeated" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
]
}
],
"innerHTML": "\n<div class=\"wp-block-cover is-light has-parallax is-repeated\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-10 has-background-dim\"></span><div role=\"img\" class=\"wp-block-cover__image-background wp-image-612 has-parallax is-repeated\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\"></div></div>\n ",
"innerHTML": "\n<div class=\"wp-block-cover is-light has-parallax is-repeated\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-10 has-background-dim\"></span><div class=\"wp-block-cover__image-background wp-image-612 has-parallax is-repeated\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\"></div></div>\n ",
"innerContent": [
"\n<div class=\"wp-block-cover is-light has-parallax is-repeated\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-10 has-background-dim\"></span><div role=\"img\" class=\"wp-block-cover__image-background wp-image-612 has-parallax is-repeated\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\">",
"\n<div class=\"wp-block-cover is-light has-parallax is-repeated\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim-10 has-background-dim\"></span><div class=\"wp-block-cover__image-background wp-image-612 has-parallax is-repeated\" style=\"background-position:50% 50%;background-image:url(https://w.wiki/59So)\"></div><div class=\"wp-block-cover__inner-container\">",
null,
"</div></div>\n "
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59So","id":612,"hasParallax":true,"isRepeated":true,"dimRatio":10,"isDark":false,"style":{"color":{"duotone":["#8c00b7","#fcff41"]}}} -->
<div class="wp-block-cover is-light has-parallax is-repeated"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-10 has-background-dim"></span><div role="img" class="wp-block-cover__image-background wp-image-612 has-parallax is-repeated" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<div class="wp-block-cover is-light has-parallax is-repeated"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-10 has-background-dim"></span><div class="wp-block-cover__image-background wp-image-612 has-parallax is-repeated" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

0 comments on commit af34a42

Please sign in to comment.