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

Add screen reader instructions for navigating child blocks on block selection #39558

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useScrollIntoView } from './use-scroll-into-view';
import { useBlockRefProvider } from './use-block-refs';
import { useMultiSelection } from './use-multi-selection';
import { useIntersectionObserver } from './use-intersection-observer';
import { useBlockScreenReaderDescription } from './use-block-screen-reader-description';
import { store as blockEditorStore } from '../../../store';

/**
Expand Down Expand Up @@ -148,6 +149,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
tabIndex: 0,
role: 'document',
'aria-label': blockLabel,
'aria-description': useBlockScreenReaderDescription( clientId ),
'data-block': clientId,
'data-type': name,
'data-title': blockTitle,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { sprintf, __ } from '@wordpress/i18n';
import { getBlockType } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';

export function useBlockScreenReaderDescription( clientId ) {
const { hasChildBlocks, blockTitle } = useSelect(
( select ) => {
const { getBlockRootClientId, getBlockName, getBlock } = select(
blockEditorStore
);
const clientIdToUse = getBlockRootClientId( clientId );
const blockName = getBlockName(
clientIdToUse ? clientIdToUse : clientId
);
const blockType = getBlockType( blockName );
return {
hasChildBlocks:
getBlock( clientIdToUse ? clientIdToUse : clientId )
?.innerBlocks?.length > 0,
blockTitle: blockType?.title,
};
},
[ clientId ]
);
let description;
if ( hasChildBlocks ) {
description = sprintf(
// Translators: 1: The block title to lowercase for good sentence structure.
__( 'Child block of %1$s.' ),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@youknowriad @talldan Maybe I should use getBlocks() instead so I can compare the index? The issue is, I cannot tell if I am in a parent block or not. E.g. columns block with equal split column blocks. If I select the columns block, I hear "Child of columns." Can I, in code, detect the difference between parent block and where child blocks actually start?

The other thing to work out is detecting if a block supports child blocks. For example, the paragraph block. In another update, I want to exclude the paragraph block or any block that doesn't have support for child blocks. That way I can add instructions for say the group/columns blocks without having to hear those announced for the paragraph block. Is there some code I can use to see if a block supports child blocks?

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 think I have this fixed up now. Logic may need help.

blockTitle.toLowerCase()
);
} else {
description = __( 'Testing non-child block description.' );
}
return description;
}