Skip to content
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

Improve table caption accessibility #41980

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/block-library/src/table/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"caption": {
"type": "string",
"source": "html",
"selector": "figcaption",
"selector": "caption",
"default": ""
},
"head": {
Expand Down
217 changes: 217 additions & 0 deletions packages/block-library/src/table/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
RichText,
getColorClassName,
useBlockProps,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';

const supports = {
Expand Down Expand Up @@ -424,6 +427,220 @@ const deprecated = [
);
},
},
{
attributes: {
hasFixedLayout: {
type: 'boolean',
default: false,
},
caption: {
type: 'string',
source: 'html',
selector: 'figcaption',
default: '',
},
head: {
type: 'array',
default: [],
source: 'query',
selector: 'thead tr',
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'string',
source: 'html',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
},
},
},
},
body: {
type: 'array',
default: [],
source: 'query',
selector: 'tbody tr',
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'string',
source: 'html',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
},
},
},
},
foot: {
type: 'array',
default: [],
source: 'query',
selector: 'tfoot tr',
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'string',
source: 'html',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
},
},
},
},
},
supports: {
anchor: true,
align: true,
__experimentalSelector: '.wp-block-table > table',
},
save: ( { attributes } ) => {
const { hasFixedLayout, head, body, foot, caption } = attributes;
const isEmpty = ! head.length && ! body.length && ! foot.length;

if ( isEmpty ) {
return null;
}

const colorProps = getColorClassesAndStyles( attributes );
const borderProps = getBorderClassesAndStyles( attributes );

const classes = classnames(
colorProps.className,
borderProps.className,
{
'has-fixed-layout': hasFixedLayout,
}
);

const hasCaption = ! RichText.isEmpty( caption );

const Section = ( { type, rows } ) => {
if ( ! rows.length ) {
return null;
}

const Tag = `t${ type }`;

return (
<Tag>
{ rows.map( ( { cells }, rowIndex ) => (
<tr key={ rowIndex }>
{ cells.map(
(
{ content, tag, scope, align },
cellIndex
) => {
const cellClasses = classnames( {
[ `has-text-align-${ align }` ]:
align,
} );

return (
<RichText.Content
className={
cellClasses
? cellClasses
: undefined
}
data-align={ align }
tagName={ tag }
value={ content }
key={ cellIndex }
scope={
tag === 'th'
? scope
: undefined
}
/>
);
}
) }
</tr>
) ) }
</Tag>
);
};

return (
<figure { ...useBlockProps.save() }>
<table
className={ classes === '' ? undefined : classes }
style={ { ...colorProps.style, ...borderProps.style } }
>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
{ hasCaption && (
<RichText.Content
tagName="figcaption"
value={ caption }
className={ __experimentalGetElementClassName(
'caption'
) }
/>
) }
</figure>
);
},
},
];

export default deprecated;
22 changes: 11 additions & 11 deletions packages/block-library/src/table/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ export default function save( { attributes } ) {
};

return (
<figure { ...useBlockProps.save() }>
<table
className={ classes === '' ? undefined : classes }
style={ { ...colorProps.style, ...borderProps.style } }
>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
<table
Copy link
Contributor

@talldan talldan Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <figure> element provided a horizontally scrolling container for wide tables, and without that wide tables now breakout of the layout. I think a div wrapper might be needed to replace that.

The markup in the editor now also doesn't match the frontend.

{ ...useBlockProps.save( {
className: classes === '' ? undefined : classes,
style: { ...colorProps.style, ...borderProps.style },
} ) }
>
{ hasCaption && (
<RichText.Content
tagName="figcaption"
tagName="caption"
value={ caption }
className={ __experimentalGetElementClassName( 'caption' ) }
/>
) }
</figure>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
);
}