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

Fix labeling of the command palette. #56718

Merged
merged 2 commits into from Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 39 additions & 33 deletions packages/commands/src/components/command-menu.js
Expand Up @@ -32,6 +32,8 @@ import { Icon, search as inputIcon } from '@wordpress/icons';
*/
import { store as commandsStore } from '../store';

const inputLabel = __( 'Search for commands' );

function CommandMenuLoader( { name, search, hook, setLoader, close } ) {
const { isLoading, commands = [] } = hook( { search } ) ?? {};
useEffect( () => {
Expand All @@ -44,34 +46,29 @@ function CommandMenuLoader( { name, search, hook, setLoader, close } ) {

return (
<>
<Command.List>
{ commands.map( ( command ) => (
<Command.Item
key={ command.name }
value={ command.searchLabel ?? command.label }
onSelect={ () => command.callback( { close } ) }
id={ command.name }
{ commands.map( ( command ) => (
<Command.Item
key={ command.name }
value={ command.searchLabel ?? command.label }
onSelect={ () => command.callback( { close } ) }
id={ command.name }
>
<HStack
alignment="left"
className={ classnames( 'commands-command-menu__item', {
'has-icon': command.icon,
} ) }
>
<HStack
alignment="left"
className={ classnames(
'commands-command-menu__item',
{
'has-icon': command.icon,
}
) }
>
{ command.icon && <Icon icon={ command.icon } /> }
<span>
<TextHighlight
text={ command.label }
highlight={ search }
/>
</span>
</HStack>
</Command.Item>
) ) }
</Command.List>
{ command.icon && <Icon icon={ command.icon } /> }
<span>
<TextHighlight
text={ command.label }
highlight={ search }
/>
</span>
</HStack>
</Command.Item>
) ) }
</>
);
}
Expand Down Expand Up @@ -176,7 +173,7 @@ function CommandInput( { isOpen, search, setSearch } ) {
ref={ commandMenuInput }
value={ search }
onValueChange={ setSearch }
placeholder={ __( 'Search for commands' ) }
placeholder={ inputLabel }
aria-activedescendant={ selectedItemId }
icon={ search }
/>
Expand All @@ -195,6 +192,7 @@ export function CommandMenu() {
);
const { open, close } = useDispatch( commandsStore );
const [ loaders, setLoaders ] = useState( {} );
const commandListRef = useRef();

useEffect( () => {
registerShortcut( {
Expand All @@ -208,6 +206,16 @@ export function CommandMenu() {
} );
}, [ registerShortcut ] );

// Temporary fix for the suggestions Listbox labeling.
// See https://github.com/pacocoursey/cmdk/issues/196
useEffect( () => {
commandListRef.current?.removeAttribute( 'aria-labelledby' );
commandListRef.current?.setAttribute(
'aria-label',
__( 'Command suggestions' )
);
}, [ commandListRef.current ] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really necessary, I'd prefer if we avoid this hack personally. We're updating a DOM node controller by React using an imperative API, this can break in different ways. (For example, if the component is re-rendered, these things could get reset).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@youknowriad Yes I know it's hacky 😞 I reported the root problem upstream at pacocoursey/cmdk#196. If you could maybe help getting the cmdk team release a fix soon we could entirely avoid this hack


useShortcut(
'core/commands',
/** @type {import('react').KeyboardEventHandler} */
Expand Down Expand Up @@ -265,12 +273,10 @@ export function CommandMenu() {
overlayClassName="commands-command-menu__overlay"
onRequestClose={ closeAndReset }
__experimentalHideHeader
contentLabel={ __( 'Command palette' ) }
>
<div className="commands-command-menu__container">
<Command
label={ __( 'Command palette' ) }
onKeyDown={ onKeyDown }
>
<Command label={ inputLabel } onKeyDown={ onKeyDown }>
<div className="commands-command-menu__header">
<Icon icon={ inputIcon } />
<CommandInput
Expand All @@ -279,7 +285,7 @@ export function CommandMenu() {
isOpen={ isOpen }
/>
</div>
<Command.List>
<Command.List ref={ commandListRef }>
{ search && ! isLoading && (
<Command.Empty>
{ __( 'No results found.' ) }
Expand Down