Skip to content

Commit

Permalink
Try possibly better method for Block Inserter Search focus (#37793)
Browse files Browse the repository at this point in the history
* Try useImperativeHandle.

* Pass the prop to focus.

* Implement reviewer feedback.

* More reviewer feedback. Apply to edit-site/edit-widgets.
  • Loading branch information
alexstine authored Jan 11, 2022
1 parent 8debd02 commit 906f86e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 49 deletions.
29 changes: 17 additions & 12 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import { noop } from 'lodash';
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import InserterMenu from './menu';
import { store as blockEditorStore } from '../../store';

function InserterLibrary( {
rootClientId,
clientId,
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalInsertionIndex,
__experimentalFilterValue,
onSelect = noop,
shouldFocusBlock = false,
} ) {
function InserterLibrary(
{
rootClientId,
clientId,
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalInsertionIndex,
__experimentalFilterValue,
onSelect = noop,
shouldFocusBlock = false,
},
ref
) {
const destinationRootClientId = useSelect(
( select ) => {
const { getBlockRootClientId } = select( blockEditorStore );
Expand All @@ -47,8 +51,9 @@ function InserterLibrary( {
__experimentalInsertionIndex={ __experimentalInsertionIndex }
__experimentalFilterValue={ __experimentalFilterValue }
shouldFocusBlock={ shouldFocusBlock }
ref={ ref }
/>
);
}

export default InserterLibrary;
export default forwardRef( InserterLibrary );
44 changes: 31 additions & 13 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* WordPress dependencies
*/
import { useState, useCallback, useMemo } from '@wordpress/element';
import {
forwardRef,
useState,
useCallback,
useMemo,
useImperativeHandle,
useRef,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
Expand All @@ -19,17 +26,20 @@ import useInsertionPoint from './hooks/use-insertion-point';
import InserterTabs from './tabs';
import { store as blockEditorStore } from '../../store';

function InserterMenu( {
rootClientId,
clientId,
isAppender,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
} ) {
function InserterMenu(
{
rootClientId,
clientId,
isAppender,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
},
ref
) {
const [ filterValue, setFilterValue ] = useState(
__experimentalFilterValue
);
Expand Down Expand Up @@ -168,6 +178,13 @@ function InserterMenu( {
[ blocksTab, patternsTab, reusableBlocksTab ]
);

const searchRef = useRef();
useImperativeHandle( ref, () => ( {
focusSearch: () => {
searchRef.current.focus();
},
} ) );

return (
<div className="block-editor-inserter__menu">
<div className="block-editor-inserter__main-area">
Expand All @@ -182,6 +199,7 @@ function InserterMenu( {
value={ filterValue }
label={ __( 'Search for blocks and patterns' ) }
placeholder={ __( 'Search' ) }
ref={ searchRef }
/>
{ !! filterValue && (
<InserterSearchResults
Expand Down Expand Up @@ -219,4 +237,4 @@ function InserterMenu( {
);
}

export default InserterMenu;
export default forwardRef( InserterMenu );
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const inserterContentRef = useRef();
const libraryRef = useRef();
useEffect( () => {
inserterContentRef.current
.querySelector( '.block-editor-inserter__search input' )
.focus();
libraryRef.current.focusSearch();
}, [] );

return (
Expand All @@ -56,10 +54,7 @@ export default function InserterSidebar() {
onClick={ () => setIsInserterOpened( false ) }
/>
</TagName>
<div
className="edit-post-editor__inserter-panel-content"
ref={ inserterContentRef }
>
<div className="edit-post-editor__inserter-panel-content">
<Library
showMostUsedBlocks={ showMostUsedBlocks }
showInserterHelpPanel
Expand All @@ -69,6 +64,7 @@ export default function InserterSidebar() {
insertionPoint.insertionIndex
}
__experimentalFilterValue={ insertionPoint.filterValue }
ref={ libraryRef }
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const inserterContentRef = useRef();
const libraryRef = useRef();
useEffect( () => {
inserterContentRef.current
.querySelector( '.block-editor-inserter__search input' )
.focus();
libraryRef.current.focusSearch();
}, [] );

return (
Expand All @@ -51,10 +49,7 @@ export default function InserterSidebar() {
onClick={ () => setIsInserterOpened( false ) }
/>
</TagName>
<div
className="edit-post-editor__inserter-panel-content"
ref={ inserterContentRef }
>
<div className="edit-post-editor__inserter-panel-content">
<Library
showInserterHelpPanel
shouldFocusBlock={ isMobile }
Expand All @@ -63,6 +58,7 @@ export default function InserterSidebar() {
insertionPoint.insertionIndex
}
__experimentalFilterValue={ insertionPoint.filterValue }
ref={ libraryRef }
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const inserterContentRef = useRef();
const libraryRef = useRef();
useEffect( () => {
inserterContentRef.current
.querySelector( '.block-editor-inserter__search input' )
.focus();
libraryRef.current.focusSearch();
}, [] );

return (
Expand All @@ -54,15 +52,13 @@ export default function InserterSidebar() {
label={ __( 'Close block inserter' ) }
/>
</TagName>
<div
className="edit-widgets-layout__inserter-panel-content"
ref={ inserterContentRef }
>
<div className="edit-widgets-layout__inserter-panel-content">
<Library
showInserterHelpPanel
shouldFocusBlock={ isMobileViewport }
rootClientId={ rootClientId }
__experimentalInsertionIndex={ insertionIndex }
ref={ libraryRef }
/>
</div>
</div>
Expand Down

0 comments on commit 906f86e

Please sign in to comment.