Skip to content

Commit

Permalink
Change how we focus search
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Oct 26, 2022
1 parent 82a536b commit d5182ed
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 64 deletions.
29 changes: 12 additions & 17 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -12,20 +11,17 @@ import { store as blockEditorStore } from '../../store';

const noop = () => {};

function InserterLibrary(
{
rootClientId,
clientId,
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalInsertionIndex,
__experimentalFilterValue,
onSelect = noop,
shouldFocusBlock = false,
},
ref
) {
function InserterLibrary( {
rootClientId,
clientId,
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalInsertionIndex,
__experimentalFilterValue,
onSelect = noop,
shouldFocusBlock = false,
} ) {
const { destinationRootClientId, prioritizePatterns } = useSelect(
( select ) => {
const { getBlockRootClientId, getSettings } =
Expand Down Expand Up @@ -54,9 +50,8 @@ function InserterLibrary(
__experimentalFilterValue={ __experimentalFilterValue }
shouldFocusBlock={ shouldFocusBlock }
prioritizePatterns={ prioritizePatterns }
ref={ ref }
/>
);
}

export default forwardRef( InserterLibrary );
export default InserterLibrary;
46 changes: 20 additions & 26 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import {
forwardRef,
useState,
useCallback,
useMemo,
useImperativeHandle,
useRef,
useEffect,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -33,21 +32,18 @@ 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,
prioritizePatterns,
},
ref
) {
function InserterMenu( {
rootClientId,
clientId,
isAppender,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
prioritizePatterns,
} ) {
const [ filterValue, setFilterValue ] = useState(
__experimentalFilterValue
);
Expand Down Expand Up @@ -171,12 +167,9 @@ function InserterMenu(
);

const searchRef = useRef();
// TODO: this doesn't work now..
useImperativeHandle( ref, () => ( {
focusSearch: () => {
searchRef.current?.focus();
},
} ) );
useEffect( () => {
searchRef.current?.focus();
}, [ selectedTab ] );

const getCurrentTab = useCallback(
( tab ) => {
Expand Down Expand Up @@ -239,6 +232,7 @@ function InserterMenu(
</>
);
},
// TODO: check/add deps..
[ blocksTab, patternsTab, reusableBlocksTab, filterValue ]
);

Expand All @@ -263,9 +257,9 @@ function InserterMenu(
{ getCurrentTab }
</InserterTabs>
) }
{ ! filterValue && ! showAsTabs && (
{ ! showAsTabs && (
<div className="block-editor-inserter__no-tab-container">
{ blocksTab }
{ getCurrentTab( { name: 'blocks' } ) }
</div>
) }
</div>
Expand All @@ -283,4 +277,4 @@ function InserterMenu(
);
}

export default forwardRef( InserterMenu );
export default InserterMenu;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -35,11 +34,6 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const libraryRef = useRef();
useEffect( () => {
libraryRef.current.focusSearch();
}, [] );

return (
<div
ref={ inserterDialogRef }
Expand All @@ -63,7 +57,6 @@ 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 @@ -10,7 +10,6 @@ import {
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -31,11 +30,6 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const libraryRef = useRef();
useEffect( () => {
libraryRef.current.focusSearch();
}, [] );

return (
<div
ref={ inserterDialogRef }
Expand All @@ -58,7 +52,6 @@ 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 @@ -8,7 +8,7 @@ import {
useViewportMatch,
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { useCallback, useEffect, useRef } from '@wordpress/element';
import { useCallback } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

Expand All @@ -34,11 +34,6 @@ export default function InserterSidebar() {
focusOnMount: null,
} );

const libraryRef = useRef();
useEffect( () => {
libraryRef.current.focusSearch();
}, [] );

return (
<div
ref={ inserterDialogRef }
Expand All @@ -58,7 +53,6 @@ export default function InserterSidebar() {
shouldFocusBlock={ isMobileViewport }
rootClientId={ rootClientId }
__experimentalInsertionIndex={ insertionIndex }
ref={ libraryRef }
/>
</div>
</div>
Expand Down

0 comments on commit d5182ed

Please sign in to comment.