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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,6 +33,7 @@ import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useBlockRefProvider } from './use-block-refs';
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(
tabIndex: 0,
role: 'document',
'aria-label': blockLabel,
'aria-description': useBlockScreenReaderDescription( clientId ),
'data-block': clientId,
'data-type': name,
'data-title': blockTitle,
Expand Down
@@ -0,0 +1,57 @@
/**
* 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, canSupportChildBlocks } = useSelect(
( select ) => {
const {
getBlockRootClientId,
getBlockName,
getBlock,
getBlockListSettings,
} = 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,
canSupportChildBlocks: getBlockListSettings(
clientIdToUse ? clientIdToUse : clientId
),
};
},
[ clientId ]
);
let description;
if ( canSupportChildBlocks ) {
if ( hasChildBlocks ) {
description = sprintf(
// Translators: 1: The block title to lowercase for good sentence structure.
__( 'Press Escape key to navigate child blocks of %1$s.' ),
blockTitle.toLowerCase()
);
} else {
description = sprintf(
// Translators: 1: The block title to lowercase for good sentence structure.
__(
'Press Tab followed by Enter keys to add a child block to %s.'
),
blockTitle.toLowerCase()
);
}
}
return description;
}