Skip to content

Commit

Permalink
Try setting preventAnnouncement at the parent list view, and pass dow…
Browse files Browse the repository at this point in the history
…n to the select button
  • Loading branch information
andrewserong committed Mar 18, 2022
1 parent 058b8a6 commit 2692f44
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ListViewBlockContents = forwardRef(
level,
isExpanded,
selectedClientIds,
preventAnnouncement,
...props
},
ref
Expand Down Expand Up @@ -80,6 +81,7 @@ const ListViewBlockContents = forwardRef(
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
isExpanded={ isExpanded }
preventAnnouncement={ preventAnnouncement }
{ ...props }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Button, VisuallyHidden } from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { forwardRef } from '@wordpress/element';
import { useDebounce, useInstanceId, usePrevious } from '@wordpress/compose';
import { forwardRef, useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -37,6 +37,7 @@ function ListViewBlockSelectButton(
onDragEnd,
draggable,
isExpanded,
preventAnnouncement,
},
ref
) {
Expand All @@ -48,6 +49,11 @@ function ListViewBlockSelectButton(
siblingBlockCount,
level
);
const [ ariaHidden, setAriaHidden ] = useState( undefined );

// This debounced version is used so that while moving out of focus,
// the block isn't updated and then re-announced.
const delaySetAriaHidden = useDebounce( setAriaHidden, 200 );

// The `href` attribute triggers the browser's native HTML drag operations.
// When the link is dragged, the element's outerHTML is set in DataTransfer object as text/html.
Expand All @@ -64,6 +70,21 @@ function ListViewBlockSelectButton(
}
}

const previousPreventAnnouncement = usePrevious( preventAnnouncement );

useEffect( () => {
// If we prevent screen readers from announcing the block,
// we should apply this immediately.
if ( preventAnnouncement ) {
setAriaHidden( true );
}
// Delay re-enabling so that if focus is being moved between
// buttons, we don't accidentally re-announce a focused button.
if ( ! preventAnnouncement && previousPreventAnnouncement ) {
delaySetAriaHidden( undefined );
}
}, [ preventAnnouncement ] );

return (
<>
<Button
Expand All @@ -82,6 +103,7 @@ function ListViewBlockSelectButton(
draggable={ draggable }
href={ `#block-${ clientId }` }
aria-expanded={ isExpanded }
aria-hidden={ ariaHidden }
>
<ListViewExpander onClick={ onToggleExpanded } />
<BlockIcon icon={ blockInformation?.icon } showColors />
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function ListViewBlock( {
path,
isExpanded,
selectedClientIds,
preventAnnouncement,
} ) {
const cellRef = useRef( null );
const [ isHovered, setIsHovered ] = useState( false );
Expand Down Expand Up @@ -207,6 +208,7 @@ function ListViewBlock( {
onFocus={ onFocus }
isExpanded={ isExpanded }
selectedClientIds={ selectedClientIds }
preventAnnouncement={ preventAnnouncement }
/>
</div>
) }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function ListViewBranch( props ) {
listPosition = 0,
fixedListWindow,
expandNested = true,
preventAnnouncement,
} = props;

const {
Expand Down Expand Up @@ -167,6 +168,7 @@ function ListViewBranch( props ) {
isExpanded={ isExpanded }
listPosition={ nextPosition }
selectedClientIds={ selectedClientIds }
preventAnnouncement={ preventAnnouncement }
/>
) }
{ ! showBlock && (
Expand Down
10 changes: 10 additions & 0 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
useMemo,
useRef,
useReducer,
useState,
forwardRef,
} from '@wordpress/element';
import { HOME, END } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -102,6 +104,7 @@ function ListView(
const { updateBlockSelection } = useBlockSelection();

const [ expandedState, setExpandedState ] = useReducer( expanded, {} );
const [ preventAnnouncement, setPreventAnnouncement ] = useState( false );

const { ref: dropZoneRef, target: blockDropTarget } = useListViewDropZone();
const elementRef = useRef();
Expand All @@ -116,6 +119,7 @@ function ListView(
( event, clientId ) => {
updateBlockSelection( event, clientId );
setSelectedTreeId( clientId );
setPreventAnnouncement( false );
},
[ setSelectedTreeId, updateBlockSelection ]
);
Expand Down Expand Up @@ -174,6 +178,11 @@ function ListView(
startRow?.dataset?.block,
endRow?.dataset?.block
);
if ( event.keyCode === HOME || event.keyCode === END ) {
setPreventAnnouncement( true );
}
} else {
setPreventAnnouncement( false );
}
},
[ updateBlockSelection ]
Expand Down Expand Up @@ -226,6 +235,7 @@ function ListView(
fixedListWindow={ fixedListWindow }
selectedClientIds={ selectedClientIds }
expandNested={ expandNested }
preventAnnouncement={ preventAnnouncement }
{ ...props }
/>
</ListViewContext.Provider>
Expand Down

0 comments on commit 2692f44

Please sign in to comment.