Skip to content

Commit

Permalink
Announce (via speak) when block inserted in Navigation list view (#47034
Browse files Browse the repository at this point in the history
)

* Announce when block inserted in list view

* Announce insert via callback
  • Loading branch information
getdave authored Jan 25, 2023
1 parent 1cb9ec5 commit 6517008
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
import { useSelect } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';
import { forwardRef, useState, useEffect } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

/**
Expand All @@ -15,6 +16,8 @@ import Inserter from '../inserter';

export const Appender = forwardRef(
( { nestingLevel, blockCount, ...props }, ref ) => {
const [ insertedBlock, setInsertedBlock ] = useState( null );

const instanceId = useInstanceId( Appender );
const { hideInserter, clientId } = useSelect( ( select ) => {
const {
Expand All @@ -38,6 +41,26 @@ export const Appender = forwardRef(
context: 'list-view',
} );

const insertedBlockTitle = useBlockDisplayTitle( {
clientId: insertedBlock?.clientId,
context: 'list-view',
} );

useEffect( () => {
if ( ! insertedBlockTitle?.length ) {
return;
}

speak(
sprintf(
// translators: %s: name of block being inserted (i.e. Paragraph, Image, Group etc)
__( '%s block inserted' ),
insertedBlockTitle
),
'assertive'
);
}, [ insertedBlockTitle ] );

if ( hideInserter ) {
return null;
}
Expand All @@ -63,6 +86,11 @@ export const Appender = forwardRef(
__experimentalIsQuick
{ ...props }
toggleProps={ { 'aria-describedby': descriptionId } }
onSelectOrClose={ ( maybeInsertedBlock ) => {
if ( maybeInsertedBlock?.clientId ) {
setInsertedBlock( maybeInsertedBlock );
}
} }
/>
<div
className="offcanvas-editor-appender__description"
Expand Down

0 comments on commit 6517008

Please sign in to comment.