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

Table of contents block accessibility improvements #54322

Merged
merged 11 commits into from
Sep 27, 2023
28 changes: 26 additions & 2 deletions packages/block-library/src/table-of-contents/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { useDispatch, useSelect } from '@wordpress/data';
import { renderToString } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useInstanceId } from '@wordpress/compose';
import { store as noticeStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -50,6 +52,24 @@ export default function TableOfContentsEdit( {
useObserveHeadings( clientId );

const blockProps = useBlockProps();
const instanceId = useInstanceId(
TableOfContentsEdit,
'table-of-contents'
);

// If a user clicks to a link prevent redirection and show a warning.
const { createWarningNotice, removeNotice } = useDispatch( noticeStore );
let noticeId;
const showRedirectionPreventedNotice = ( event ) => {
event.preventDefault();
// Remove previous warning if any, to show one at a time per block.
removeNotice( noticeId );
noticeId = `block-library/core/table-of-contents/redirection-prevented/${ instanceId }`;
createWarningNotice( __( 'Links are disabled in the editor.' ), {
id: noticeId,
type: 'snackbar',
} );
};

const canInsertList = useSelect(
( select ) => {
Expand Down Expand Up @@ -137,8 +157,12 @@ 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.

onClick={ showRedirectionPreventedNotice }
/>
</ol>
</nav>
{ toolbarControls }
Expand Down
30 changes: 29 additions & 1 deletion packages/block-library/src/table-of-contents/list.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import type { MouseEvent } from 'react';

/**
* WordPress dependencies
*/
Expand All @@ -12,16 +17,30 @@ const ENTRY_CLASS_NAME = 'wp-block-table-of-contents__entry';

export default function TableOfContentsList( {
nestedHeadingList,
disableLinkActivation,
onClick,
}: {
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?

onClick?: ( event: MouseEvent< HTMLAnchorElement > ) => void;
} ): 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 &&
'function' === typeof onClick
? onClick
: undefined
}
>
{ content }
</a>
) : (
Expand All @@ -35,6 +54,15 @@ export default function TableOfContentsList( {
<ol>
<TableOfContentsList
nestedHeadingList={ node.children }
disableLinkActivation={
disableLinkActivation
}
onClick={
disableLinkActivation &&
'function' === typeof onClick
? onClick
: undefined
}
/>
</ol>
) : null }
Expand Down