Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Accessibility improvements for table of contents block.
  • Loading branch information
alexstine committed Sep 8, 2023
commit a791c306dbbec7d83001b7027169e63fd32d7aab
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ Summarize your post with a list of headings. Add HTML anchors to Heading blocks
- **Name:** core/table-of-contents
- **Experimental:** true
- **Category:** layout
- **Supports:** color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** ariaLabel, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** headings, onlyIncludeCurrentPage

## Tag Cloud
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/table-of-contents/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"ariaLabel": true
},
"example": {}
}
7 changes: 5 additions & 2 deletions packages/block-library/src/table-of-contents/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ export default function TableOfContentsEdit( {
return (
<>
<nav { ...blockProps }>
<ol inert="true">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure why no one caught this regression. This makes the content completely inaccessible to keyboard users. Please don't do this unless you have a really good reason to.

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert

Copy link
Contributor

Choose a reason for hiding this comment

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

inert was added in several places in d50e613; we maybe should take a look at each usage.

<TableOfContentsList nestedHeadingList={ headingTree } />
<ol>
<TableOfContentsList
nestedHeadingList={ headingTree }
disableLinkActivation={ true }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing this prop to disable link activation for the editor side.

/>
</ol>
</nav>
{ toolbarControls }
Expand Down
18 changes: 17 additions & 1 deletion packages/block-library/src/table-of-contents/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,32 @@ const ENTRY_CLASS_NAME = 'wp-block-table-of-contents__entry';

export default function TableOfContentsList( {
nestedHeadingList,
disableLinkActivation,
}: {
nestedHeadingList: NestedHeadingData[];
disableLinkActivation?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a better way to type this?

} ): WPElement {
return (
<>
{ nestedHeadingList.map( ( node, index ) => {
const { content, link } = node.heading;

const entry = link ? (
<a className={ ENTRY_CLASS_NAME } href={ link }>
<a
className={ ENTRY_CLASS_NAME }
href={ link }
aria-disabled={ disableLinkActivation || undefined }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This communicates disabled state to screen readers.

onClick={
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This prevents click.

disableLinkActivation
? ( event ) => event?.preventDefault()
: undefined
}
onContextMenu={
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This prevents context menu open in new tab/window.

disableLinkActivation
? ( event ) => event?.preventDefault()
: undefined
}
>
{ content }
</a>
) : (
Expand Down
8 changes: 7 additions & 1 deletion packages/block-library/src/table-of-contents/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -13,8 +14,13 @@ export default function save( { attributes: { headings = [] } } ) {
if ( headings.length === 0 ) {
return null;
}

const blockProps = useBlockProps.save( {
'aria-label': __( 'Table of Contents' ),
} );

return (
<nav { ...useBlockProps.save() }>
<nav { ...blockProps }>
<ol>
<TableOfContentsList
nestedHeadingList={ linearToNestedHeadingList( headings ) }
Expand Down