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

Focus pattern inserter search when activating zoom out inserter #64396

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -110,6 +115,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 @@ -2085,6 +2085,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 @@ -2118,6 +2122,7 @@ const combinedReducers = combineReducers( {
openedBlockSettingsMenu,
registeredInserterMediaCategories,
hoveredBlockClientId,
inserterSearchInputRef,
} );

function withAutomaticChangeReset( reducer ) {
Expand Down
Loading