Skip to content

Commit

Permalink
Focus the search text input when click on a command or click on back …
Browse files Browse the repository at this point in the history
…button (#85363)
  • Loading branch information
sejas authored Dec 18, 2023
1 parent fe8d181 commit dce0889
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions client/components/command-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,28 @@ interface CommandInputProps {
isOpen: boolean;
search: string;
setSearch: ( search: string ) => void;
selectedCommandName: string;
placeholder?: string;
}

function CommandInput( { isOpen, search, setSearch, placeholder }: CommandInputProps ) {
function CommandInput( {
isOpen,
search,
setSearch,
placeholder,
selectedCommandName,
}: CommandInputProps ) {
const commandMenuInput = useRef< HTMLInputElement >( null );
const itemValue = useCommandState( ( state ) => state.value );
const itemId = useMemo( () => cleanForSlug( itemValue ), [ itemValue ] );

useEffect( () => {
// Focus the command palette input when mounting the modal.
if ( isOpen ) {
// Focus the command palette input when mounting the modal,
// or when a command is selected.
if ( isOpen || selectedCommandName ) {
commandMenuInput.current?.focus();
}
}, [ isOpen ] );
}, [ isOpen, selectedCommandName ] );

return (
<Command.Input
Expand Down Expand Up @@ -236,6 +244,7 @@ const CommandPalette = () => {
<Icon icon={ inputIcon } />
) }
<CommandInput
selectedCommandName={ selectedCommandName }
search={ search }
setSearch={ setSearch }
isOpen={ isOpen }
Expand Down

0 comments on commit dce0889

Please sign in to comment.