Skip to content

Commit

Permalink
Focus pattern inserter search when activating zoom out inserter (#64396)
Browse files Browse the repository at this point in the history
* Add new search focus option for inserter panel

* Try focusing when inserter clicked in Zoom Out

* Store search input ref in block editor settings

* Remove legacy selector

* Remove more legacy code

* Move all ref selectors to block editor

* Update packages/editor/src/components/inserter-sidebar/index.js

---------

Co-authored-by: Ben Dwyer <ben@scruffian.com>
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
  • Loading branch information
4 people committed Aug 14, 2024
1 parent e2502a3 commit 00df3eb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function ZoomOutModeInserters() {
sectionRootClientId,
selectedBlockClientId,
hoveredBlockClientId,
inserterSearchInputRef,
} = useSelect( ( select ) => {
const {
getSettings,
Expand All @@ -32,8 +33,11 @@ function ZoomOutModeInserters() {
getSelectedBlockClientId,
getHoveredBlockClientId,
isBlockInsertionPointVisible,
} = select( blockEditorStore );
getInserterSearchInputRef,
} = unlock( select( blockEditorStore ) );

const { sectionRootClientId: root } = unlock( getSettings() );

return {
hasSelection: !! getSelectionStart().clientId,
blockInsertionPoint: getBlockInsertionPoint(),
Expand All @@ -44,6 +48,7 @@ function ZoomOutModeInserters() {
getSettings().__experimentalSetIsInserterOpened,
selectedBlockClientId: getSelectedBlockClientId(),
hoveredBlockClientId: getHoveredBlockClientId(),
inserterSearchInputRef: getInserterSearchInputRef(),
};
}, [] );

Expand Down Expand Up @@ -98,6 +103,7 @@ function ZoomOutModeInserters() {
showInsertionPoint( sectionRootClientId, index, {
operation: 'insert',
} );
inserterSearchInputRef?.current?.focus();
} }
/>
) }
Expand Down
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 @@ -27,6 +27,7 @@ function InserterLibrary(
onSelect = noop,
shouldFocusBlock = false,
onClose,
__experimentalSearchInputRef,
},
ref
) {
Expand Down Expand Up @@ -58,6 +59,7 @@ function InserterLibrary(
shouldFocusBlock={ shouldFocusBlock }
ref={ ref }
onClose={ onClose }
__experimentalSearchInputRef={ __experimentalSearchInputRef }
/>
);
}
Expand Down
34 changes: 21 additions & 13 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import useInsertionPoint from './hooks/use-insertion-point';
import { store as blockEditorStore } from '../../store';
import TabbedSidebar from '../tabbed-sidebar';
import { useZoomOut } from '../../hooks/use-zoom-out';
import { unlock } from '../../lock-unlock';

const NOOP = () => {};
function InserterMenu(
Expand All @@ -53,11 +54,16 @@ function InserterMenu(
},
ref
) {
const isZoomOutMode = useSelect(
( select ) =>
select( blockEditorStore ).__unstableGetEditorMode() === 'zoom-out',
[]
);
const { isZoomOutMode, inserterSearchInputRef } = useSelect( ( select ) => {
const { __unstableGetEditorMode, getInserterSearchInputRef } = unlock(
select( blockEditorStore )
);
return {
isZoomOutMode: __unstableGetEditorMode() === 'zoom-out',
inserterSearchInputRef: getInserterSearchInputRef(),
};
}, [] );

const [ filterValue, setFilterValue, delayedFilterValue ] =
useDebouncedInput( __experimentalFilterValue );
const [ hoveredItem, setHoveredItem ] = useState( null );
Expand Down Expand Up @@ -104,7 +110,7 @@ function InserterMenu(
}
} );
},
[ onInsertBlocks, onSelect, shouldFocusBlock ]
[ onInsertBlocks, onSelect, ref, shouldFocusBlock ]
);

const onInsertPattern = useCallback(
Expand All @@ -113,7 +119,7 @@ function InserterMenu(
onInsertBlocks( blocks, { patternName } );
onSelect();
},
[ onInsertBlocks, onSelect ]
[ onInsertBlocks, onSelect, onToggleInsertionPoint ]
);

const onHover = useCallback(
Expand Down Expand Up @@ -164,7 +170,9 @@ function InserterMenu(
value={ filterValue }
label={ __( 'Search for blocks and patterns' ) }
placeholder={ __( 'Search' ) }
ref={ inserterSearchInputRef }
/>

{ !! delayedFilterValue && (
<InserterSearchResults
filterValue={ delayedFilterValue }
Expand All @@ -185,18 +193,18 @@ function InserterMenu(
);
}, [
selectedTab,
hoveredItem,
setHoveredItem,
setFilterValue,
filterValue,
inserterSearchInputRef,
delayedFilterValue,
onSelect,
onHover,
shouldFocusBlock,
clientId,
rootClientId,
__experimentalInsertionIndex,
clientId,
isAppender,
__experimentalInsertionIndex,
shouldFocusBlock,
hoveredItem,
setFilterValue,
] );

const blocksTab = useMemo( () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,7 @@ export function getTemporarilyEditingAsBlocks( state ) {
export function getTemporarilyEditingFocusModeToRevert( state ) {
return state.temporarilyEditingFocusModeRevert;
}

export function getInserterSearchInputRef( state ) {
return state.inserterSearchInputRef;
}
5 changes: 5 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,10 @@ export function hoveredBlockClientId( state = false, action ) {
return state;
}

export function inserterSearchInputRef( state = { current: null } ) {
return state;
}

const combinedReducers = combineReducers( {
blocks,
isDragging,
Expand Down Expand Up @@ -2120,6 +2124,7 @@ const combinedReducers = combineReducers( {
openedBlockSettingsMenu,
registeredInserterMediaCategories,
hoveredBlockClientId,
inserterSearchInputRef,
} );

function withAutomaticChangeReset( reducer ) {
Expand Down

0 comments on commit 00df3eb

Please sign in to comment.