From 2692f44b64f4fe9ff966feb4f9d1e0bf4390df29 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:08:45 +1100 Subject: [PATCH] Try setting preventAnnouncement at the parent list view, and pass down to the select button --- .../components/list-view/block-contents.js | 2 ++ .../list-view/block-select-button.js | 26 +++++++++++++++++-- .../src/components/list-view/block.js | 2 ++ .../src/components/list-view/branch.js | 2 ++ .../src/components/list-view/index.js | 10 +++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/list-view/block-contents.js b/packages/block-editor/src/components/list-view/block-contents.js index 2fb787dac3137e..d6fdfcdb0644a8 100644 --- a/packages/block-editor/src/components/list-view/block-contents.js +++ b/packages/block-editor/src/components/list-view/block-contents.js @@ -28,6 +28,7 @@ const ListViewBlockContents = forwardRef( level, isExpanded, selectedClientIds, + preventAnnouncement, ...props }, ref @@ -80,6 +81,7 @@ const ListViewBlockContents = forwardRef( onDragStart={ onDragStart } onDragEnd={ onDragEnd } isExpanded={ isExpanded } + preventAnnouncement={ preventAnnouncement } { ...props } /> ) } diff --git a/packages/block-editor/src/components/list-view/block-select-button.js b/packages/block-editor/src/components/list-view/block-select-button.js index 452f01d30b3bd4..2c44cbbc995c82 100644 --- a/packages/block-editor/src/components/list-view/block-select-button.js +++ b/packages/block-editor/src/components/list-view/block-select-button.js @@ -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'; /** @@ -37,6 +37,7 @@ function ListViewBlockSelectButton( onDragEnd, draggable, isExpanded, + preventAnnouncement, }, ref ) { @@ -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. @@ -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 ( <>