From 0aee46e515a8ed0bea50d72a7390590e8068f034 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 25 Jul 2023 14:29:03 +0300 Subject: [PATCH] [Commands]: Add `aria-activedescendant` attribute to suggestions --- .../commands/src/components/command-menu.js | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/commands/src/components/command-menu.js b/packages/commands/src/components/command-menu.js index aa77925763007..1bd3e38675861 100644 --- a/packages/commands/src/components/command-menu.js +++ b/packages/commands/src/components/command-menu.js @@ -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, @@ -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 } > command.callback( { close } ) } + id={ command.name } > 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 ( + + ); +} + export function CommandMenu() { const { registerShortcut } = useDispatch( keyboardShortcutsStore ); const [ search, setSearch ] = useState( '' ); @@ -149,7 +183,6 @@ export function CommandMenu() { ); const { open, close } = useDispatch( commandsStore ); const [ loaders, setLoaders ] = useState( {} ); - const commandMenuInput = useRef(); useEffect( () => { registerShortcut( { @@ -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; } @@ -230,11 +256,10 @@ export function CommandMenu() { onKeyDown={ onKeyDown } >
-