Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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 @@ -181,7 +181,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
ref: mergedRefs,
id: `block-${ clientId }${ htmlSuffix }`,
role: 'document',
'aria-label': blockLabel,
'aria-label': wrapperProps[ 'aria-label' ] || blockLabel,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should be careful not to introduce a new API here. If it wasn't possible before for extenders to set an aria label, then I'm not sure if we should add that. Perhaps we can check the store if revisions mode is on, or add some React context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @ellatrix, thanks for the feedback!

What do you think about using a private React context (e.g., BlockAriaLabelOverrideContext exported via lock-unlock)? We could provide the diff label from revisions-canvas.js and consume it directly in useBlockProps.

This approach lets us completely revert the changes to the paragraph block and avoids passing anything through wrapperProps.

'data-block': clientId,
'data-type': name,
'data-title': blockTitle,
Expand Down
21 changes: 13 additions & 8 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ function ParagraphBlock( {
style: { direction },
} );
const blockEditingMode = useBlockEditingMode();
const { 'aria-label': injectedAriaLabel, ...restBlockProps } = blockProps;
const defaultParagraphLabel = __( 'Block: Paragraph' );

let ariaLabel = defaultParagraphLabel;
if ( injectedAriaLabel !== defaultParagraphLabel ) {
ariaLabel = injectedAriaLabel;
} else if ( RichText.isEmpty( content ) ) {
ariaLabel = __(
'Empty block; start writing or type forward slash to choose a block'
);
}

return (
<>
Expand All @@ -143,21 +154,15 @@ function ParagraphBlock( {
<RichText
identifier="content"
tagName="p"
{ ...blockProps }
{ ...restBlockProps }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onRemove }
aria-label={
RichText.isEmpty( content )
? __(
'Empty block; start writing or type forward slash to choose a block'
)
: __( 'Block: Paragraph' )
}
aria-label={ ariaLabel }
data-empty={ RichText.isEmpty( content ) }
placeholder={ placeholder || __( 'Type / to choose a block' ) }
data-custom-placeholder={ placeholder ? true : undefined }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { getBlockType } from '@wordpress/blocks';
import { sprintf, __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -92,6 +94,28 @@ const REVISION_DIFF_STYLES = `
}
`;

/**
* Returns an accessible label for a block based on its revision diff status.
*
* @param {string} status The diff status: 'added', 'removed', or 'modified'.
* @param {string} blockTitle The human-readable block type name.
* @return {string|undefined} The aria-label string, or undefined if not applicable.
*/
function getDiffStatusLabel( status, blockTitle ) {
Comment thread
himanshupathak95 marked this conversation as resolved.
if ( status === 'added' ) {
// translators: %s: block type name e.g. "Paragraph"
return sprintf( __( 'Added Block: %s' ), blockTitle );
}
if ( status === 'removed' ) {
// translators: %s: block type name e.g. "Paragraph"
return sprintf( __( 'Removed Block: %s' ), blockTitle );
}
if ( status === 'modified' ) {
// translators: %s: block type name e.g. "Paragraph"
return sprintf( __( 'Modified Block: %s' ), blockTitle );
}
}

/**
* Filter to add diff status CSS classes to blocks.
*
Expand All @@ -100,16 +124,31 @@ const REVISION_DIFF_STYLES = `
*/
function withRevisionDiffClasses( BlockListBlock ) {
return ( props ) => {
const { block, className } = props;
const { block, className, wrapperProps } = props;
const diffStatus = block?.__revisionDiffStatus?.status;

const blockTitle = getBlockType( block.name )?.title;
const diffLabel =
diffStatus && blockTitle
? getDiffStatusLabel( diffStatus, blockTitle )
: undefined;

const enhancedClassName = clsx( className, {
'is-revision-added': diffStatus === 'added',
'is-revision-removed': diffStatus === 'removed',
'is-revision-modified': diffStatus === 'modified',
} );

return <BlockListBlock { ...props } className={ enhancedClassName } />;
return (
<BlockListBlock
{ ...props }
className={ enhancedClassName }
wrapperProps={ {
...wrapperProps,
...( diffLabel && { 'aria-label': diffLabel } ),
} }
/>
);
};
}

Expand Down
Loading