Skip to content

Commit

Permalink
Use the keyboard shortcuts package for the command center (#49680)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Apr 11, 2023
1 parent 32060b8 commit ac871a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/private-apis": "file:../private-apis",
"cmdk": "^0.2.0",
"rememo": "^4.0.0"
Expand Down
37 changes: 26 additions & 11 deletions packages/commands/src/components/command-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { Command } from 'cmdk';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Modal, TextHighlight } from '@wordpress/components';
import {
store as keyboardShortcutsStore,
useShortcut,
} from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
Expand Down Expand Up @@ -116,6 +120,7 @@ export function CommandMenuGroup( { group, search, setLoader, close } ) {
}

export function CommandMenu() {
const { registerShortcut } = useDispatch( keyboardShortcutsStore );
const [ search, setSearch ] = useState( '' );
const [ open, setOpen ] = useState( false );
const { groups } = useSelect( ( select ) => {
Expand All @@ -126,18 +131,28 @@ export function CommandMenu() {
}, [] );
const [ loaders, setLoaders ] = useState( {} );

// Toggle the menu when Meta-K is pressed
useEffect( () => {
const toggleOnMetaK = ( e ) => {
if ( e.key === 'k' && e.metaKey ) {
setOpen( ( prevOpen ) => ! prevOpen );
e.preventDefault();
}
};
registerShortcut( {
name: 'core/commands',
category: 'global',
description: __( 'Open the global command menu' ),
keyCombination: {
modifier: 'primary',
character: 'k',
},
} );
}, [ registerShortcut ] );

document.addEventListener( 'keydown', toggleOnMetaK );
return () => document.removeEventListener( 'keydown', toggleOnMetaK );
}, [] );
useShortcut(
'core/commands',
( event ) => {
event.preventDefault();
setOpen( ( prevOpen ) => ! prevOpen );
},
{
bindGlobal: true,
}
);

const setLoader = useCallback(
( name, value ) =>
Expand Down

1 comment on commit ac871a2

@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 ac871a2.
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/4666268211
📝 Reported issues:

Please sign in to comment.