Skip to content

Commit

Permalink
[Commands]: Add aria-activedescendant attribute to suggestions (#52930
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ntsekouras authored Jul 25, 2023
1 parent 2892f3c commit d1c1553
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions packages/commands/src/components/command-menu.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/**
* External dependencies
*/
import { Command } from 'cmdk';
import { Command, useCommandState } from 'cmdk';

/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import {
useState,
useEffect,
useRef,
useCallback,
useMemo,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Modal,
Expand Down Expand Up @@ -43,6 +49,7 @@ function CommandMenuLoader( { name, search, hook, setLoader, close } ) {
key={ command.name }
value={ command.searchLabel ?? command.label }
onSelect={ () => command.callback( { close } ) }
id={ command.name }
>
<HStack
alignment="left"
Expand Down Expand Up @@ -112,6 +119,7 @@ export function CommandMenuGroup( { isContextual, search, setLoader, close } ) {
key={ command.name }
value={ command.searchLabel ?? command.label }
onSelect={ () => command.callback( { close } ) }
id={ command.name }
>
<HStack
alignment="left"
Expand Down Expand Up @@ -140,6 +148,32 @@ export function CommandMenuGroup( { isContextual, search, setLoader, close } ) {
);
}

function CommandInput( { isOpen, search, setSearch } ) {
const commandMenuInput = useRef();
const _value = useCommandState( ( state ) => state.value );
const selectedItemId = useMemo( () => {
const item = document.querySelector(
`[cmdk-item=""][data-value="${ _value }"]`
);
return item?.getAttribute( 'id' );
}, [ _value ] );
useEffect( () => {
// Focus the command palette input when mounting the modal.
if ( isOpen ) {
commandMenuInput.current.focus();
}
}, [ isOpen ] );
return (
<Command.Input
ref={ commandMenuInput }
value={ search }
onValueChange={ setSearch }
placeholder={ __( 'Type a command or search' ) }
aria-activedescendant={ selectedItemId }
/>
);
}

export function CommandMenu() {
const { registerShortcut } = useDispatch( keyboardShortcutsStore );
const [ search, setSearch ] = useState( '' );
Expand All @@ -149,7 +183,6 @@ export function CommandMenu() {
);
const { open, close } = useDispatch( commandsStore );
const [ loaders, setLoaders ] = useState( {} );
const commandMenuInput = useRef();

useEffect( () => {
registerShortcut( {
Expand Down Expand Up @@ -191,13 +224,6 @@ export function CommandMenu() {
close();
};

useEffect( () => {
// Focus the command palette input when mounting the modal.
if ( isOpen ) {
commandMenuInput.current.focus();
}
}, [ isOpen ] );

if ( ! isOpen ) {
return false;
}
Expand Down Expand Up @@ -230,11 +256,10 @@ export function CommandMenu() {
onKeyDown={ onKeyDown }
>
<div className="commands-command-menu__header">
<Command.Input
ref={ commandMenuInput }
value={ search }
onValueChange={ setSearch }
placeholder={ __( 'Type a command or search' ) }
<CommandInput
search={ search }
setSearch={ setSearch }
isOpen={ isOpen }
/>
</div>
<Command.List>
Expand Down

1 comment on commit d1c1553

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in d1c1553.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5659309574
📝 Reported issues:

Please sign in to comment.