Skip to content

Commit

Permalink
File Block: Add a deprecation for the button element class name (#45159)
Browse files Browse the repository at this point in the history
* File Block: Add a deprecation for the button element class name

* switch v2 and v3

* Add fixtures for new deprecation

* Fix deprecation comment

* Remove new fixture for now as failing a php test for some reason

Co-authored-by: Glen Davies <glen.davies@automattic.com>
  • Loading branch information
2 people authored and ockham committed Oct 25, 2022
1 parent 490c14b commit 307bfba
Showing 1 changed file with 130 additions and 2 deletions.
132 changes: 130 additions & 2 deletions packages/block-library/src/file/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { __, sprintf } from '@wordpress/i18n';

// Version of the file block without PR#43050 removing the translated aria-label.
const v2 = {
const v3 = {
attributes: {
id: {
type: 'number',
Expand Down Expand Up @@ -143,6 +143,134 @@ const v2 = {
},
};

// In #41239 the button was made an element button which added a `wp-element-button` classname
// to the download link element.
const v2 = {
attributes: {
id: {
type: 'number',
},
href: {
type: 'string',
},
fileId: {
type: 'string',
source: 'attribute',
selector: 'a:not([download])',
attribute: 'id',
},
fileName: {
type: 'string',
source: 'html',
selector: 'a:not([download])',
},
textLinkHref: {
type: 'string',
source: 'attribute',
selector: 'a:not([download])',
attribute: 'href',
},
textLinkTarget: {
type: 'string',
source: 'attribute',
selector: 'a:not([download])',
attribute: 'target',
},
showDownloadButton: {
type: 'boolean',
default: true,
},
downloadButtonText: {
type: 'string',
source: 'html',
selector: 'a[download]',
},
displayPreview: {
type: 'boolean',
},
previewHeight: {
type: 'number',
default: 600,
},
},
supports: {
anchor: true,
align: true,
},
save( { attributes } ) {
const {
href,
fileId,
fileName,
textLinkHref,
textLinkTarget,
showDownloadButton,
downloadButtonText,
displayPreview,
previewHeight,
} = attributes;

const pdfEmbedLabel = RichText.isEmpty( fileName )
? __( 'PDF embed' )
: sprintf(
/* translators: %s: filename. */
__( 'Embed of %s.' ),
fileName
);

const hasFilename = ! RichText.isEmpty( fileName );

// Only output an `aria-describedby` when the element it's referring to is
// actually rendered.
const describedById = hasFilename ? fileId : undefined;

return (
href && (
<div { ...useBlockProps.save() }>
{ displayPreview && (
<>
<object
className="wp-block-file__embed"
data={ href }
type="application/pdf"
style={ {
width: '100%',
height: `${ previewHeight }px`,
} }
aria-label={ pdfEmbedLabel }
/>
</>
) }
{ hasFilename && (
<a
id={ describedById }
href={ textLinkHref }
target={ textLinkTarget }
rel={
textLinkTarget
? 'noreferrer noopener'
: undefined
}
>
<RichText.Content value={ fileName } />
</a>
) }
{ showDownloadButton && (
<a
href={ href }
className="wp-block-file__button"
download={ true }
aria-describedby={ describedById }
>
<RichText.Content value={ downloadButtonText } />
</a>
) }
</div>
)
);
},
};

// Version of the file block without PR#28062 accessibility fix.
const v1 = {
attributes: {
Expand Down Expand Up @@ -255,6 +383,6 @@ const v1 = {
},
};

const deprecated = [ v2, v1 ];
const deprecated = [ v3, v2, v1 ];

export default deprecated;

0 comments on commit 307bfba

Please sign in to comment.