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

Accessibility improvements for Block Inserter #37357

Merged
merged 17 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function InserterLibrary( {
__experimentalFilterValue,
onSelect = noop,
shouldFocusBlock = false,
isOpen = false,
} ) {
const destinationRootClientId = useSelect(
( select ) => {
Expand All @@ -38,6 +39,7 @@ function InserterLibrary( {

return (
<InserterMenu
isOpen={ isOpen }
onSelect={ onSelect }
rootClientId={ destinationRootClientId }
clientId={ clientId }
Expand Down
19 changes: 18 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* WordPress dependencies
*/
import { useState, useCallback, useMemo } from '@wordpress/element';
import {
useState,
useRef,
useEffect,
useCallback,
useMemo,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -29,6 +35,7 @@ function InserterMenu( {
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
isOpen = false,
} ) {
const [ filterValue, setFilterValue ] = useState(
__experimentalFilterValue
Expand Down Expand Up @@ -168,13 +175,23 @@ function InserterMenu( {
[ blocksTab, patternsTab, reusableBlocksTab ]
);

const searchControlRef = useRef();
useEffect( () => {
// Make sure focus only occurs if open
if ( ! isOpen ) {
return;
}
searchControlRef.current.focus();
}, [ isOpen ] );

return (
<div className="block-editor-inserter__menu">
<div className="block-editor-inserter__main-area">
{ /* the following div is necessary to fix the sticky position of the search form */ }
<div className="block-editor-inserter__content">
<SearchControl
className="block-editor-inserter__search"
ref={ searchControlRef }
onChange={ ( value ) => {
if ( hoveredItem ) setHoveredItem( null );
setFilterValue( value );
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function Layout( { styles } ) {

const secondarySidebar = () => {
if ( mode === 'visual' && isInserterOpened ) {
return <InserterSidebar />;
return <InserterSidebar isOpen={ isInserterOpened } />;
}
if ( mode === 'visual' && isListViewOpened ) {
return <ListViewSidebar />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { Button } from '@wordpress/components';
import { Button, VisuallyHidden } from '@wordpress/components';
import { __experimentalLibrary as Library } from '@wordpress/block-editor';
import { close } from '@wordpress/icons';
import {
useViewportMatch,
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';

export default function InserterSidebar() {
export default function InserterSidebar( isOpen = false ) {
const { insertionPoint, showMostUsedBlocks } = useSelect( ( select ) => {
const { isFeatureActive, __experimentalGetInsertionPoint } = select(
editPostStore
Expand All @@ -28,8 +29,10 @@ export default function InserterSidebar() {
const { setIsInserterOpened } = useDispatch( editPostStore );

const isMobileViewport = useViewportMatch( 'medium', '<' );
const TagName = isMobileViewport ? VisuallyHidden : 'div';
const [ inserterDialogRef, inserterDialogProps ] = useDialog( {
onClose: () => setIsInserterOpened( false ),
focusOnMount: null,
Copy link
Contributor Author

@alexstine alexstine Dec 14, 2021

Choose a reason for hiding this comment

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

Had to set this to null to prevent the useFocusOnMount hook from firing. Now that useEffect is being used, it would cause duplicate focus events.

} );

return (
Expand All @@ -38,14 +41,16 @@ export default function InserterSidebar() {
{ ...inserterDialogProps }
className="edit-post-editor__inserter-panel"
>
<div className="edit-post-editor__inserter-panel-header">
<TagName className="edit-post-editor__inserter-panel-header">
<Button
icon={ close }
label={ __( 'Close block inserter' ) }
onClick={ () => setIsInserterOpened( false ) }
/>
</div>
</TagName>
<div className="edit-post-editor__inserter-panel-content">
<Library
isOpen={ isOpen }
showMostUsedBlocks={ showMostUsedBlocks }
showInserterHelpPanel
shouldFocusBlock={ isMobileViewport }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
padding-right: $grid-unit-10;
display: flex;
justify-content: flex-end;

@include break-medium() {
display: none;
}
}

.edit-post-editor__inserter-panel-content,
Expand Down